sloppy work on passing all of the test and reimplementing most of the features from encode

This commit is contained in:
collerek
2020-08-04 18:44:17 +02:00
parent a6f8fc6d7e
commit 345fd227d1
9 changed files with 648 additions and 24 deletions

View File

@ -6,6 +6,7 @@ import pydantic
import sqlalchemy
from orm.exceptions import ModelDefinitionError
from orm.relations import Relationship
class BaseField:
@ -24,7 +25,7 @@ class BaseField:
self.name = name
self.primary_key = kwargs.pop('primary_key', False)
self.autoincrement = kwargs.pop('autoincrement', self.primary_key)
self.autoincrement = kwargs.pop('autoincrement', self.primary_key and self.__type__ == int)
self.nullable = kwargs.pop('nullable', not self.primary_key)
self.default = kwargs.pop('default', None)
@ -37,11 +38,30 @@ class BaseField:
if self.pydantic_only and self.primary_key:
raise ModelDefinitionError('Primary key column cannot be pydantic only.')
@property
def is_required(self):
return not self.nullable and not self.has_default and not self.is_auto_primary_key
@property
def default_value(self):
default = self.default if self.default is not None else self.server_default
return default() if callable(default) else default
@property
def has_default(self):
return self.default is not None or self.server_default is not None
@property
def is_auto_primary_key(self):
if self.primary_key:
return self.autoincrement
return False
def get_column(self, name: str = None) -> sqlalchemy.Column:
name = self.name or name
self.name = self.name or name
constraints = self.get_constraints()
return sqlalchemy.Column(
name,
self.name,
self.get_column_type(),
*constraints,
primary_key=self.primary_key,
@ -59,6 +79,9 @@ class BaseField:
def get_constraints(self) -> Optional[List]:
return []
def expand_relationship(self, value, parent):
return value
class String(BaseField):
__type__ = str
@ -147,3 +170,37 @@ class Decimal(BaseField):
def get_column_type(self):
return sqlalchemy.DECIMAL(self.length, self.precision)
class ForeignKey(BaseField):
def __init__(self, to, related_name: str = None, nullable: bool = False):
super().__init__(nullable=nullable)
self.related_name = related_name
self.to = to
@property
def __type__(self):
return self.to.__pydantic_model__
def get_constraints(self):
fk_string = self.to.__tablename__ + "." + self.to.__pkname__
return [sqlalchemy.schema.ForeignKey(fk_string)]
def get_column_type(self):
to_column = self.to.__model_fields__[self.to.__pkname__]
return to_column.get_column_type()
def expand_relationship(self, value, child):
if isinstance(value, self.to):
model = value
else:
model = self.to(**{self.to.__pkname__: value})
child_model_name = self.related_name or child.__class__.__name__.lower() + 's'
model._orm_relationship_manager.add(
Relationship(name=child_model_name, child=child, parent=model, fk_side='child'))
model.__fields__[child_model_name] = pydantic.fields.ModelField(name=child_model_name,
type_=child.__pydantic_model__,
model_config=child.__pydantic_model__.__config__,
class_validators=child.__pydantic_model__.__validators__)
return model