some cleanup
This commit is contained in:
1
.flake8
1
.flake8
@ -2,4 +2,5 @@
|
|||||||
ignore = ANN101, ANN102, W503, S101
|
ignore = ANN101, ANN102, W503, S101
|
||||||
max-complexity = 8
|
max-complexity = 8
|
||||||
max-line-length = 88
|
max-line-length = 88
|
||||||
|
import-order-style = pycharm
|
||||||
exclude = p38venv,.pytest_cache
|
exclude = p38venv,.pytest_cache
|
||||||
|
|||||||
@ -33,4 +33,5 @@ __all__ = [
|
|||||||
"ModelNotSet",
|
"ModelNotSet",
|
||||||
"MultipleMatches",
|
"MultipleMatches",
|
||||||
"NoMatch",
|
"NoMatch",
|
||||||
|
"ForeignKey",
|
||||||
]
|
]
|
||||||
|
|||||||
@ -1,18 +1,18 @@
|
|||||||
|
from orm.fields.base import BaseField
|
||||||
|
from orm.fields.foreign_key import ForeignKey
|
||||||
from orm.fields.model_fields import (
|
from orm.fields.model_fields import (
|
||||||
BigInteger,
|
BigInteger,
|
||||||
Boolean,
|
Boolean,
|
||||||
Date,
|
Date,
|
||||||
DateTime,
|
DateTime,
|
||||||
Decimal,
|
Decimal,
|
||||||
String,
|
|
||||||
Integer,
|
|
||||||
Text,
|
|
||||||
Float,
|
Float,
|
||||||
Time,
|
Integer,
|
||||||
JSON,
|
JSON,
|
||||||
|
String,
|
||||||
|
Text,
|
||||||
|
Time,
|
||||||
)
|
)
|
||||||
from orm.fields.foreign_key import ForeignKey
|
|
||||||
from orm.fields.base import BaseField
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"Decimal",
|
"Decimal",
|
||||||
|
|||||||
@ -1,8 +1,11 @@
|
|||||||
from typing import Type, Any, Dict, Optional, List
|
from typing import Any, Dict, List, Optional, TYPE_CHECKING, Type
|
||||||
|
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
|
|
||||||
from orm import ModelDefinitionError
|
from orm import ModelDefinitionError # noqa I101
|
||||||
|
|
||||||
|
if TYPE_CHECKING: # pragma no cover
|
||||||
|
from orm.models import Model
|
||||||
|
|
||||||
|
|
||||||
class RequiredParams:
|
class RequiredParams:
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
from typing import Type, List, Any, Union, TYPE_CHECKING, Optional
|
from typing import Any, List, Optional, TYPE_CHECKING, Type, Union
|
||||||
|
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
import orm
|
import orm # noqa I101
|
||||||
from orm.exceptions import RelationshipInstanceError
|
from orm.exceptions import RelationshipInstanceError
|
||||||
from orm.fields.base import BaseField
|
from orm.fields.base import BaseField
|
||||||
|
|
||||||
@ -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,7 +50,7 @@ class ForeignKey(BaseField):
|
|||||||
return to_column.get_column_type()
|
return to_column.get_column_type()
|
||||||
|
|
||||||
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:
|
||||||
@ -80,9 +80,7 @@ class ForeignKey(BaseField):
|
|||||||
model = create_dummy_instance(fk=self.to, pk=value)
|
model = create_dummy_instance(fk=self.to, pk=value)
|
||||||
|
|
||||||
model._orm_relationship_manager.add_relation(
|
model._orm_relationship_manager.add_relation(
|
||||||
model,
|
model, child, virtual=self.virtual,
|
||||||
child,
|
|
||||||
virtual=self.virtual,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return model
|
return model
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import decimal
|
|||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
from pydantic import Json
|
from pydantic import Json
|
||||||
|
|
||||||
from orm.fields.base import BaseField, RequiredParams
|
from orm.fields.base import BaseField, RequiredParams # noqa I101
|
||||||
|
|
||||||
|
|
||||||
@RequiredParams("length")
|
@RequiredParams("length")
|
||||||
|
|||||||
@ -6,18 +6,16 @@ from typing import Any, List, Optional, TYPE_CHECKING, Tuple, Type, TypeVar
|
|||||||
from typing import Callable, Dict, Set
|
from typing import Callable, Dict, Set
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
|
|
||||||
import orm.queryset as qry
|
|
||||||
from orm.exceptions import ModelDefinitionError
|
|
||||||
from orm import ForeignKey
|
|
||||||
from orm.fields.base import BaseField
|
|
||||||
from orm.relations import RelationshipManager
|
|
||||||
|
|
||||||
import pydantic
|
import pydantic
|
||||||
|
import sqlalchemy
|
||||||
from pydantic import BaseConfig, BaseModel, create_model
|
from pydantic import BaseConfig, BaseModel, create_model
|
||||||
from pydantic.fields import ModelField
|
from pydantic.fields import ModelField
|
||||||
|
|
||||||
import sqlalchemy
|
import orm.queryset as qry # noqa I100
|
||||||
|
from orm import ForeignKey
|
||||||
|
from orm.exceptions import ModelDefinitionError
|
||||||
|
from orm.fields.base import BaseField
|
||||||
|
from orm.relations import RelationshipManager
|
||||||
|
|
||||||
relationship_manager = RelationshipManager()
|
relationship_manager = RelationshipManager()
|
||||||
|
|
||||||
|
|||||||
@ -11,16 +11,15 @@ from typing import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
|
import sqlalchemy
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
import orm
|
import orm # noqa I100
|
||||||
import orm.fields.foreign_key
|
import orm.fields.foreign_key
|
||||||
from orm import ForeignKey
|
from orm import ForeignKey
|
||||||
from orm.exceptions import MultipleMatches, NoMatch, QueryDefinitionError
|
from orm.exceptions import MultipleMatches, NoMatch, QueryDefinitionError
|
||||||
from orm.fields.base import BaseField
|
from orm.fields.base import BaseField
|
||||||
|
|
||||||
import sqlalchemy
|
|
||||||
from sqlalchemy import text
|
|
||||||
|
|
||||||
if TYPE_CHECKING: # pragma no cover
|
if TYPE_CHECKING: # pragma no cover
|
||||||
from orm.models import Model
|
from orm.models import Model
|
||||||
|
|
||||||
|
|||||||
@ -16,7 +16,7 @@ def get_table_alias() -> str:
|
|||||||
|
|
||||||
|
|
||||||
def get_relation_config(
|
def get_relation_config(
|
||||||
relation_type: str, table_name: str, field: ForeignKey
|
relation_type: str, table_name: str, field: ForeignKey
|
||||||
) -> Dict[str, str]:
|
) -> Dict[str, str]:
|
||||||
alias = get_table_alias()
|
alias = get_table_alias()
|
||||||
config = {
|
config = {
|
||||||
@ -37,7 +37,7 @@ class RelationshipManager:
|
|||||||
self._relations = dict()
|
self._relations = dict()
|
||||||
|
|
||||||
def add_relation_type(
|
def add_relation_type(
|
||||||
self, relations_key: str, reverse_key: str, field: ForeignKey, table_name: str
|
self, relations_key: str, reverse_key: str, field: ForeignKey, table_name: str
|
||||||
) -> None:
|
) -> None:
|
||||||
if relations_key not in self._relations:
|
if relations_key not in self._relations:
|
||||||
self._relations[relations_key] = get_relation_config(
|
self._relations[relations_key] = get_relation_config(
|
||||||
@ -56,10 +56,7 @@ class RelationshipManager:
|
|||||||
del self._relations[rel_type][model._orm_id]
|
del self._relations[rel_type][model._orm_id]
|
||||||
|
|
||||||
def add_relation(
|
def add_relation(
|
||||||
self,
|
self, parent: "FakePydantic", child: "FakePydantic", virtual: bool = False,
|
||||||
parent: "FakePydantic",
|
|
||||||
child: "FakePydantic",
|
|
||||||
virtual: bool = False,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
parent_id = parent._orm_id
|
parent_id = parent._orm_id
|
||||||
child_id = child._orm_id
|
child_id = child._orm_id
|
||||||
@ -97,7 +94,7 @@ class RelationshipManager:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def get(
|
def get(
|
||||||
self, relations_key: str, instance: "FakePydantic"
|
self, relations_key: str, instance: "FakePydantic"
|
||||||
) -> Union["Model", List["Model"]]:
|
) -> Union["Model", List["Model"]]:
|
||||||
if relations_key in self._relations:
|
if relations_key in self._relations:
|
||||||
if instance._orm_id in self._relations[relations_key]:
|
if instance._orm_id in self._relations[relations_key]:
|
||||||
@ -108,8 +105,8 @@ class RelationshipManager:
|
|||||||
def resolve_relation_join(self, from_table: str, to_table: str) -> str:
|
def resolve_relation_join(self, from_table: str, to_table: str) -> str:
|
||||||
for relation_name, relation in self._relations.items():
|
for relation_name, relation in self._relations.items():
|
||||||
if (
|
if (
|
||||||
relation["source_table"] == from_table
|
relation["source_table"] == from_table
|
||||||
and relation["target_table"] == to_table
|
and relation["target_table"] == to_table
|
||||||
):
|
):
|
||||||
return self._relations[relation_name]["table_alias"]
|
return self._relations[relation_name]["table_alias"]
|
||||||
return ""
|
return ""
|
||||||
|
|||||||
Reference in New Issue
Block a user