refactors in fk

This commit is contained in:
collerek
2020-08-11 18:32:48 +02:00
parent d82340bcb1
commit 3e04646fd4
2 changed files with 23 additions and 22 deletions

BIN
.coverage

Binary file not shown.

View File

@ -25,12 +25,12 @@ def create_dummy_instance(fk: Type["Model"], pk: int = None) -> "Model":
class ForeignKey(BaseField): class ForeignKey(BaseField):
def __init__( def __init__(
self, self,
to: Type["Model"], to: Type["Model"],
name: str = None, name: str = None,
related_name: str = None, related_name: str = None,
nullable: bool = True, nullable: bool = True,
virtual: bool = False, virtual: bool = False,
) -> None: ) -> None:
super().__init__(nullable=nullable, name=name) super().__init__(nullable=nullable, name=name)
self.virtual = virtual self.virtual = virtual
@ -50,36 +50,42 @@ class ForeignKey(BaseField):
return to_column.get_column_type() return to_column.get_column_type()
def extract_model_from_sequence( def extract_model_from_sequence(
self, value: Any, child: "Model" self, value: Any, child: "Model"
) -> Tuple[Union["Model", List["Model"]], bool]: ) -> Union["Model", List["Model"]]:
if isinstance(value, list) and not isinstance(value, self.to): if isinstance(value, list) and not isinstance(value, self.to):
model = [self.expand_relationship(val, child) for val in value] model = [self.expand_relationship(val, child) for val in value]
return model, True return model
if isinstance(value, self.to): if isinstance(value, self.to):
model = value model = value
else: else:
model = self.to(**value) model = self.to(**value)
return model, False self.register_relation(model, child)
return model
def construct_model_from_pk(self, value: Any) -> "Model": def construct_model_from_pk(self, value: Any, child: "Model") -> "Model":
if not isinstance(value, self.to.pk_type()): if not isinstance(value, self.to.pk_type()):
raise RelationshipInstanceError( raise RelationshipInstanceError(
f"Relationship error - ForeignKey {self.to.__name__} " f"Relationship error - ForeignKey {self.to.__name__} "
f"is of type {self.to.pk_type()} " f"is of type {self.to.pk_type()} "
f"while {type(value)} passed as a parameter." f"while {type(value)} passed as a parameter."
) )
return create_dummy_instance(fk=self.to, pk=value) model = create_dummy_instance(fk=self.to, pk=value)
self.register_relation(model, child)
return model
def register_relation(self, model, child):
model._orm_relationship_manager.add_relation(
model, child, virtual=self.virtual
)
def expand_relationship( def expand_relationship(
self, value: Any, child: "Model" self, value: Any, child: "Model"
) -> Optional[Union["Model", List["Model"]]]: ) -> Optional[Union["Model", List["Model"]]]:
if value is None: if value is None:
return None return None
is_sequence = False
if isinstance(value, orm.models.Model) and not isinstance(value, self.to): if isinstance(value, orm.models.Model) and not isinstance(value, self.to):
raise RelationshipInstanceError( raise RelationshipInstanceError(
f"Relationship error - expecting: {self.to.__name__}, " f"Relationship error - expecting: {self.to.__name__}, "
@ -87,13 +93,8 @@ class ForeignKey(BaseField):
) )
if isinstance(value, (dict, list, self.to)): if isinstance(value, (dict, list, self.to)):
model, is_sequence = self.extract_model_from_sequence(value, child) model = self.extract_model_from_sequence(value, child)
else: else:
model = self.construct_model_from_pk(value) model = self.construct_model_from_pk(value, child)
if not is_sequence:
model._orm_relationship_manager.add_relation(
model, child, virtual=self.virtual
)
return model return model