Skip to content

Commit

Permalink
Fix linting for queryset
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Jan 10, 2024
1 parent 882b388 commit 91ecbac
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 120 deletions.
4 changes: 2 additions & 2 deletions edgy/contrib/multi_tenancy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ async def get_active_user_tenant(cls, user: Type["Model"]) -> Union[Type["Model"
Obtains the active user tenant.
"""
try:
tenant = await get_model(
tenant = await get_model( # type: ignore
registry=cls.meta.registry, model_name=cls.__name__
).query.get(user=user, is_active=True)
await tenant.tenant.load()
Expand All @@ -183,7 +183,7 @@ def __str__(self) -> str:
async def save(self, *args: Any, **kwargs: Any) -> Type["TenantUserMixin"]:
await super().save(*args, **kwargs)
if self.is_active:
await get_model(
await get_model( # type: ignore
registry=self.meta.registry, model_name=self.__class__.__name__
).query.filter(is_active=True, user=self.user).exclude(pk=self.pk).update(
is_active=False
Expand Down
5 changes: 4 additions & 1 deletion edgy/core/db/models/managers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Type, cast

from edgy.core.db.context_vars import get_tenant, set_tenant
from edgy.core.db.querysets.base import QuerySet
Expand Down Expand Up @@ -38,6 +38,9 @@ class MyModel(saffier.Model):
def __init__(self, model_class: Any = None):
self.model_class = model_class

def __get__(self, _: Any, owner: Any) -> Type["QuerySet"]:
return cast("Type[QuerySet]", self.__class__(model_class=owner))

def get_queryset(self) -> "QuerySet":
"""
Returns the queryset object.
Expand Down
8 changes: 5 additions & 3 deletions edgy/core/db/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def update(self, **kwargs: Any) -> Any:
"""
await self.signals.pre_update.send(sender=self.__class__, instance=self)

kwargs = self.update_auto_now_fields(kwargs, self.fields)
kwargs = self._update_auto_now_fields(kwargs, self.fields)
pk_column = getattr(self.table.c, self.pkname)
expression = self.table.update().values(**kwargs).where(pk_column == self.pk)
await self.database.execute(expression)
Expand Down Expand Up @@ -175,10 +175,10 @@ async def save(

self.update_from_dict(dict_values=dict(extracted_fields.items()))

validated_values = values or self.extract_values_from_field(
validated_values = values or self._extract_values_from_field(
extracted_values=extracted_fields
)
kwargs = self.update_auto_now_fields(values=validated_values, fields=self.fields)
kwargs = self._update_auto_now_fields(values=validated_values, fields=self.fields)
kwargs, model_references = self.update_model_references(**kwargs)

# Performs the update or the create based on a possible existing primary key
Expand Down Expand Up @@ -208,6 +208,8 @@ async def save(
await self.signals.post_save.send(sender=self.__class__, instance=self)
return self

# def __get__


class ReflectModel(Model, EdgyBaseReflectModel):
"""
Expand Down
23 changes: 13 additions & 10 deletions edgy/core/db/models/row.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ def from_sqla_row(
# Making sure if the model being queried is not inside a select related
# This way it is not overritten by any value
for related, foreign_key in cls.meta.foreign_key_fields.items():
ignore_related: bool = cls.should_ignore_related_name(related, select_related)
ignore_related: bool = cls.__should_ignore_related_name(related, select_related)
if ignore_related:
continue

model_related = foreign_key.target

# Apply the schema to the model
model_related = cls.apply_schema(model_related, using_schema)
model_related = cls.__apply_schema(model_related, using_schema)

child_item = {}
for column in model_related.table.columns:
Expand Down Expand Up @@ -121,9 +121,9 @@ def from_sqla_row(
model = cast("Type[Model]", cls.proxy_model(**item))

# Apply the schema to the model
model = cls.apply_schema(model, using_schema)
model = cls.__apply_schema(model, using_schema)

model = cls.handle_prefetch_related(
model = cls.__handle_prefetch_related(
row=row, model=model, prefetch_related=prefetch_related
)
return model
Expand All @@ -143,25 +143,28 @@ def from_sqla_row(
if not exclude_secrets
else cast("Type[Model]", cls.proxy_model(**item))
)

# Apply the schema to the model
model = cls.apply_schema(model, using_schema)
model = cls.__apply_schema(model, using_schema)

# Handle prefetch related fields.
model = cls.handle_prefetch_related(
model = cls.__handle_prefetch_related(
row=row, model=model, prefetch_related=prefetch_related
)
return model

@classmethod
def apply_schema(cls, model: Type["Model"], schema: Optional[str] = None) -> Type["Model"]:
def __apply_schema(cls, model: Type["Model"], schema: Optional[str] = None) -> Type["Model"]:
# Apply the schema to the model
if schema is not None:
model.table = model.build(schema) # type: ignore
model.proxy_model.table = model.proxy_model.build(schema) # type: ignore
return model

@classmethod
def should_ignore_related_name(cls, related_name: str, select_related: Sequence[str]) -> bool:
def __should_ignore_related_name(
cls, related_name: str, select_related: Sequence[str]
) -> bool:
"""
Validates if it should populate the related field if select related is not considered.
"""
Expand All @@ -172,7 +175,7 @@ def should_ignore_related_name(cls, related_name: str, select_related: Sequence[
return False

@classmethod
def handle_prefetch_related(
def __handle_prefetch_related(
cls,
row: Row,
model: Type["Model"],
Expand Down Expand Up @@ -216,7 +219,7 @@ def handle_prefetch_related(

# Recursively continue the process of handling the
# new prefetch
model_cls.handle_prefetch_related(
model_cls.__handle_prefetch_related(
row,
model,
prefetch_related=[remainder_prefetch],
Expand Down
Loading

0 comments on commit 91ecbac

Please sign in to comment.