From 54aaa97de2053594d94d415577b7078914a4d865 Mon Sep 17 00:00:00 2001 From: collerek Date: Tue, 15 Sep 2020 14:01:36 +0200 Subject: [PATCH] more refactors in relations' --- .coverage | Bin 53248 -> 53248 bytes ormar/relations/__init__.py | 13 ++++++- ormar/relations/relation_manager.py | 55 ++++------------------------ ormar/relations/utils.py | 44 ++++++++++++++++++++++ 4 files changed, 64 insertions(+), 48 deletions(-) create mode 100644 ormar/relations/utils.py diff --git a/.coverage b/.coverage index 428308a7ce8aaf4d69734fa00147102962e93359..e405d35945826d3b6dc1f2aa95ec42c6f14b3a53 100644 GIT binary patch delta 385 zcmZozz}&Ead4rxlyNQ*7ft9iGW>fw10?gvv_LJEiv{;vL=X2X{78Ky*X4MmCW=Nhq z(MM)-pC3C{ZhlH?j$T2fvDoDEzVaZU$)~(|nG8iI=ljV|mhx;i+Q!8_xj0&% zMcjlrcXDBz3dm#@F(c;8$&9h;JTP-bZJFyq0+XwKI5%_0{8VNZ=Tn=^-XJHqnQuAY zG`@De624@#IOClopb;B*{Iw9OaErqzkmB%ObjG0_Dyy2 z_xu0;y{)glpDh3D?e{otQJ}bl=(k7q|KH2Sfw10?cCEMU&Ybw3ruh7i|_4u;XUZ6PxVltm2AHB_7KEIfy#JGzY_^%+S!MBhvkB^zVn0xVLxd1Vs zetYi84$=CIVv`G^O;|*2nd>Jr#;Q-Q_Tk*j9rII}S&UDAGJAs@{}#R#eAD?l_)7WW z`P}*Rff|4Fx`>IfFmmdNUHf@E=l=7vQMdn>{>`p`|Ms_-C`erN+tQQ2^XvbA+rRhy zM*hFws`W)gfZ|#r-@5fen#_IxBofCzdD8hj0R3-ZcK`qY diff --git a/ormar/relations/__init__.py b/ormar/relations/__init__.py index 52ed667..12aa418 100644 --- a/ormar/relations/__init__.py +++ b/ormar/relations/__init__.py @@ -1,5 +1,16 @@ from ormar.relations.alias_manager import AliasManager from ormar.relations.relation import Relation, RelationType from ormar.relations.relation_manager import RelationsManager +from ormar.relations.utils import ( + get_relations_sides_and_names, + register_missing_relation, +) -__all__ = ["AliasManager", "Relation", "RelationsManager", "RelationType"] +__all__ = [ + "AliasManager", + "Relation", + "RelationsManager", + "RelationType", + "register_missing_relation", + "get_relations_sides_and_names", +] diff --git a/ormar/relations/relation_manager.py b/ormar/relations/relation_manager.py index 9574374..eb0d7fe 100644 --- a/ormar/relations/relation_manager.py +++ b/ormar/relations/relation_manager.py @@ -1,11 +1,13 @@ -from typing import List, Optional, TYPE_CHECKING, Tuple, Type, Union +from typing import List, Optional, TYPE_CHECKING, Type, Union from weakref import proxy -import ormar from ormar.fields.foreign_key import ForeignKeyField from ormar.fields.many_to_many import ManyToManyField -from ormar.relations import Relation -from ormar.relations.relation import RelationType +from ormar.relations.relation import Relation, RelationType +from ormar.relations.utils import ( + get_relations_sides_and_names, + register_missing_relation, +) if TYPE_CHECKING: # pragma no cover from ormar import Model @@ -48,58 +50,17 @@ class RelationsManager: if relation is not None: return relation - @staticmethod - def register_missing_relation( - parent: "Model", child: "Model", child_name: str - ) -> Relation: - ormar.models.expand_reverse_relationships(child.__class__) - name = parent.resolve_relation_name(parent, child) - field = parent.Meta.model_fields[name] - parent._orm._add_relation(field) - parent_relation = parent._orm._get(child_name) - return parent_relation - - @staticmethod - def get_relations_sides_and_names( - to_field: Type[ForeignKeyField], - parent: "Model", - child: "Model", - child_name: str, - virtual: bool, - ) -> Tuple["Model", "Model", str, str]: - to_name = to_field.name - if issubclass(to_field, ManyToManyField): - child_name, to_name = ( - child.resolve_relation_name(parent, child), - child.resolve_relation_name(child, parent), - ) - child = proxy(child) - elif virtual: - child_name, to_name = to_name, child_name or child.get_name() - child, parent = parent, proxy(child) - else: - child_name = child_name or child.get_name() + "s" - child = proxy(child) - return parent, child, child_name, to_name - @staticmethod def add(parent: "Model", child: "Model", child_name: str, virtual: bool) -> None: to_field = child.resolve_relation_field(child, parent) - ( - parent, - child, - child_name, - to_name, - ) = RelationsManager.get_relations_sides_and_names( + (parent, child, child_name, to_name,) = get_relations_sides_and_names( to_field, parent, child, child_name, virtual ) parent_relation = parent._orm._get(child_name) if not parent_relation: - parent_relation = RelationsManager.register_missing_relation( - parent, child, child_name - ) + parent_relation = register_missing_relation(parent, child, child_name) parent_relation.add(child) child._orm._get(to_name).add(parent) diff --git a/ormar/relations/utils.py b/ormar/relations/utils.py new file mode 100644 index 0000000..a182fec --- /dev/null +++ b/ormar/relations/utils.py @@ -0,0 +1,44 @@ +from typing import TYPE_CHECKING, Tuple, Type +from weakref import proxy + +import ormar +from ormar.fields.foreign_key import ForeignKeyField +from ormar.fields.many_to_many import ManyToManyField +from ormar.relations import Relation + +if TYPE_CHECKING: # pragma no cover + from ormar import Model + + +def register_missing_relation( + parent: "Model", child: "Model", child_name: str +) -> Relation: + ormar.models.expand_reverse_relationships(child.__class__) + name = parent.resolve_relation_name(parent, child) + field = parent.Meta.model_fields[name] + parent._orm._add_relation(field) + parent_relation = parent._orm._get(child_name) + return parent_relation + + +def get_relations_sides_and_names( + to_field: Type[ForeignKeyField], + parent: "Model", + child: "Model", + child_name: str, + virtual: bool, +) -> Tuple["Model", "Model", str, str]: + to_name = to_field.name + if issubclass(to_field, ManyToManyField): + child_name, to_name = ( + child.resolve_relation_name(parent, child), + child.resolve_relation_name(child, parent), + ) + child = proxy(child) + elif virtual: + child_name, to_name = to_name, child_name or child.get_name() + child, parent = parent, proxy(child) + else: + child_name = child_name or child.get_name() + "s" + child = proxy(child) + return parent, child, child_name, to_name