wip pc problems backup
This commit is contained in:
@ -22,15 +22,15 @@ And what's a better name for python ORM than snakes cabinet :)
|
||||
from ormar.protocols import QuerySetProtocol, RelationProtocol # noqa: I100
|
||||
from ormar.decorators import ( # noqa: I100
|
||||
post_delete,
|
||||
post_save,
|
||||
post_update,
|
||||
post_relation_add,
|
||||
post_relation_remove,
|
||||
post_save,
|
||||
post_update,
|
||||
pre_delete,
|
||||
pre_save,
|
||||
pre_update,
|
||||
pre_relation_add,
|
||||
pre_relation_remove,
|
||||
pre_save,
|
||||
pre_update,
|
||||
property_field,
|
||||
)
|
||||
from ormar.exceptions import ( # noqa: I100
|
||||
|
||||
@ -10,15 +10,15 @@ Currently only:
|
||||
from ormar.decorators.property_field import property_field
|
||||
from ormar.decorators.signals import (
|
||||
post_delete,
|
||||
post_save,
|
||||
post_update,
|
||||
post_relation_add,
|
||||
post_relation_remove,
|
||||
post_save,
|
||||
post_update,
|
||||
pre_delete,
|
||||
pre_save,
|
||||
pre_update,
|
||||
pre_relation_add,
|
||||
pre_relation_remove,
|
||||
pre_save,
|
||||
pre_update,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
import uuid
|
||||
from typing import Dict, Optional, Set, TYPE_CHECKING
|
||||
|
||||
import pydantic
|
||||
|
||||
import ormar
|
||||
from ormar.exceptions import ModelPersistenceError
|
||||
from ormar.models.helpers.validation import validate_choices
|
||||
@ -50,11 +53,30 @@ class SavePrepareMixin(RelationMixin, AliasMixin):
|
||||
pkname = cls.Meta.pkname
|
||||
pk = cls.Meta.model_fields[pkname]
|
||||
if new_kwargs.get(pkname, ormar.Undefined) is None and (
|
||||
pk.nullable or pk.autoincrement
|
||||
pk.nullable or pk.autoincrement
|
||||
):
|
||||
del new_kwargs[pkname]
|
||||
return new_kwargs
|
||||
|
||||
@classmethod
|
||||
def parse_non_db_fields(cls, model_dict: Dict) -> Dict:
|
||||
"""
|
||||
Receives dictionary of model that is about to be saved and changes uuid fields
|
||||
to strings in bulk_update.
|
||||
|
||||
:param model_dict: dictionary of model that is about to be saved
|
||||
:type model_dict: Dict
|
||||
:return: dictionary of model that is about to be saved
|
||||
:rtype: Dict
|
||||
"""
|
||||
for name, field in cls.Meta.model_fields.items():
|
||||
if field.__type__ == uuid.UUID and name in model_dict:
|
||||
if field.column_type.uuid_format == "string":
|
||||
model_dict[name] = str(model_dict[name])
|
||||
else:
|
||||
model_dict[name] = "%.32x" % model_dict[name].int
|
||||
return model_dict
|
||||
|
||||
@classmethod
|
||||
def substitute_models_with_pks(cls, model_dict: Dict) -> Dict: # noqa CCR001
|
||||
"""
|
||||
@ -104,9 +126,9 @@ class SavePrepareMixin(RelationMixin, AliasMixin):
|
||||
"""
|
||||
for field_name, field in cls.Meta.model_fields.items():
|
||||
if (
|
||||
field_name not in new_kwargs
|
||||
and field.has_default(use_server=False)
|
||||
and not field.pydantic_only
|
||||
field_name not in new_kwargs
|
||||
and field.has_default(use_server=False)
|
||||
and not field.pydantic_only
|
||||
):
|
||||
new_kwargs[field_name] = field.get_default()
|
||||
# clear fields with server_default set as None
|
||||
|
||||
@ -69,6 +69,7 @@ class Model(ModelRow):
|
||||
:return: saved Model
|
||||
:rtype: Model
|
||||
"""
|
||||
await self.signals.pre_save.send(sender=self.__class__, instance=self)
|
||||
self_fields = self._extract_model_db_fields()
|
||||
|
||||
if not self.pk and self.Meta.model_fields[self.Meta.pkname].autoincrement:
|
||||
@ -82,8 +83,6 @@ class Model(ModelRow):
|
||||
}
|
||||
)
|
||||
|
||||
await self.signals.pre_save.send(sender=self.__class__, instance=self)
|
||||
|
||||
self_fields = self.translate_columns_to_aliases(self_fields)
|
||||
expr = self.Meta.table.insert()
|
||||
expr = expr.values(**self_fields)
|
||||
@ -216,7 +215,9 @@ class Model(ModelRow):
|
||||
"You cannot update not saved model! Use save or upsert method."
|
||||
)
|
||||
|
||||
await self.signals.pre_update.send(sender=self.__class__, instance=self)
|
||||
await self.signals.pre_update.send(
|
||||
sender=self.__class__, instance=self, passed_args=kwargs
|
||||
)
|
||||
self_fields = self._extract_model_db_fields()
|
||||
self_fields.pop(self.get_column_name_from_alias(self.Meta.pkname))
|
||||
self_fields = self.translate_columns_to_aliases(self_fields)
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
from typing import Callable, TYPE_CHECKING, Type
|
||||
import decimal
|
||||
from typing import Any, Callable, TYPE_CHECKING, Type
|
||||
|
||||
import sqlalchemy
|
||||
|
||||
from ormar.queryset.actions.query_action import QueryAction
|
||||
from ormar.queryset.actions.query_action import QueryAction # noqa: I202
|
||||
|
||||
if TYPE_CHECKING:
|
||||
if TYPE_CHECKING: # pragma: no cover
|
||||
from ormar import Model
|
||||
|
||||
|
||||
@ -22,7 +23,7 @@ class SelectAction(QueryAction):
|
||||
self, select_str: str, model_cls: Type["Model"], alias: str = None
|
||||
) -> None:
|
||||
super().__init__(query_str=select_str, model_cls=model_cls)
|
||||
if alias:
|
||||
if alias: # pragma: no cover
|
||||
self.table_prefix = alias
|
||||
|
||||
def _split_value_into_parts(self, order_str: str) -> None:
|
||||
@ -30,6 +31,13 @@ class SelectAction(QueryAction):
|
||||
self.field_name = parts[-1]
|
||||
self.related_parts = parts[:-1]
|
||||
|
||||
@property
|
||||
def is_numeric(self) -> bool:
|
||||
return self.get_target_field_type() in [int, float, decimal.Decimal]
|
||||
|
||||
def get_target_field_type(self) -> Any:
|
||||
return self.target_model.Meta.model_fields[self.field_name].__type__
|
||||
|
||||
def get_text_clause(self) -> sqlalchemy.sql.expression.TextClause:
|
||||
alias = f"{self.table_prefix}_" if self.table_prefix else ""
|
||||
return sqlalchemy.text(f"{alias}{self.field_name}")
|
||||
|
||||
@ -320,6 +320,48 @@ class SqlJoin:
|
||||
)
|
||||
self.sorted_orders[clause] = clause.get_text_clause()
|
||||
|
||||
def _verify_allowed_order_field(self, order_by: str) -> None:
|
||||
"""
|
||||
Verifies if proper field string is used.
|
||||
:param order_by: string with order by definition
|
||||
:type order_by: str
|
||||
"""
|
||||
parts = order_by.split("__")
|
||||
if len(parts) > 2 or parts[0] != self.target_field.through.get_name():
|
||||
raise ModelDefinitionError(
|
||||
"You can order the relation only " "by related or link table columns!"
|
||||
)
|
||||
|
||||
def _get_alias_and_model(self, order_by: str) -> Tuple[str, Type["Model"]]:
|
||||
"""
|
||||
Returns proper model and alias to be applied in the clause.
|
||||
|
||||
:param order_by: string with order by definition
|
||||
:type order_by: str
|
||||
:return: alias and model to be used in clause
|
||||
:rtype: Tuple[str, Type["Model"]]
|
||||
"""
|
||||
if self.target_field.is_multi and "__" in order_by:
|
||||
self._verify_allowed_order_field(order_by=order_by)
|
||||
alias = self.next_alias
|
||||
model = self.target_field.owner
|
||||
elif self.target_field.is_multi:
|
||||
alias = self.alias_manager.resolve_relation_alias(
|
||||
from_model=self.target_field.through,
|
||||
relation_name=cast(
|
||||
"ManyToManyField", self.target_field
|
||||
).default_target_field_name(),
|
||||
)
|
||||
model = self.target_field.to
|
||||
else:
|
||||
alias = self.alias_manager.resolve_relation_alias(
|
||||
from_model=self.target_field.owner,
|
||||
relation_name=self.target_field.name,
|
||||
)
|
||||
model = self.target_field.to
|
||||
|
||||
return alias, model
|
||||
|
||||
def _get_order_bys(self) -> None: # noqa: CCR001
|
||||
"""
|
||||
Triggers construction of order bys if they are given.
|
||||
@ -339,44 +381,13 @@ class SqlJoin:
|
||||
self.already_sorted[
|
||||
f"{self.next_alias}_{self.next_model.get_name()}"
|
||||
] = condition
|
||||
# TODO: refactor into smaller helper functions
|
||||
if self.target_field.orders_by and not current_table_sorted:
|
||||
current_table_sorted = True
|
||||
for order_by in self.target_field.orders_by:
|
||||
if self.target_field.is_multi and "__" in order_by:
|
||||
parts = order_by.split("__")
|
||||
if (
|
||||
len(parts) > 2
|
||||
or parts[0] != self.target_field.through.get_name()
|
||||
):
|
||||
raise ModelDefinitionError(
|
||||
"You can order the relation only"
|
||||
"by related or link table columns!"
|
||||
)
|
||||
model = self.target_field.owner
|
||||
clause = ormar.OrderAction(
|
||||
order_str=order_by, model_cls=model, alias=alias,
|
||||
)
|
||||
elif self.target_field.is_multi:
|
||||
alias = self.alias_manager.resolve_relation_alias(
|
||||
from_model=self.target_field.through,
|
||||
relation_name=cast(
|
||||
"ManyToManyField", self.target_field
|
||||
).default_target_field_name(),
|
||||
)
|
||||
model = self.target_field.to
|
||||
clause = ormar.OrderAction(
|
||||
order_str=order_by, model_cls=model, alias=alias
|
||||
)
|
||||
else:
|
||||
alias = self.alias_manager.resolve_relation_alias(
|
||||
from_model=self.target_field.owner,
|
||||
relation_name=self.target_field.name,
|
||||
)
|
||||
model = self.target_field.to
|
||||
clause = ormar.OrderAction(
|
||||
order_str=order_by, model_cls=model, alias=alias
|
||||
)
|
||||
alias, model = self._get_alias_and_model(order_by=order_by)
|
||||
clause = ormar.OrderAction(
|
||||
order_str=order_by, model_cls=model, alias=alias
|
||||
)
|
||||
self.sorted_orders[clause] = clause.get_text_clause()
|
||||
self.already_sorted[f"{alias}_{model.get_name()}"] = clause
|
||||
|
||||
|
||||
@ -14,7 +14,6 @@ from typing import (
|
||||
import databases
|
||||
import sqlalchemy
|
||||
from sqlalchemy import bindparam
|
||||
from sqlalchemy.engine import ResultProxy
|
||||
|
||||
import ormar # noqa I100
|
||||
from ormar import MultipleMatches, NoMatch
|
||||
@ -558,22 +557,24 @@ class QuerySet:
|
||||
expr = sqlalchemy.func.count().select().select_from(expr)
|
||||
return await self.database.fetch_val(expr)
|
||||
|
||||
async def _query_aggr_function(self, func_name: str, columns: List):
|
||||
async def _query_aggr_function(self, func_name: str, columns: List) -> Any:
|
||||
func = getattr(sqlalchemy.func, func_name)
|
||||
select_actions = [
|
||||
SelectAction(select_str=column, model_cls=self.model)
|
||||
for column in columns
|
||||
SelectAction(select_str=column, model_cls=self.model) for column in columns
|
||||
]
|
||||
if func_name in ["sum", "avg"]:
|
||||
if any(not x.is_numeric for x in select_actions):
|
||||
raise QueryDefinitionError(
|
||||
"You can use sum and svg only with" "numeric types of columns"
|
||||
)
|
||||
select_columns = [x.apply_func(func, use_label=True) for x in select_actions]
|
||||
expr = self.build_select_expression().alias(f"subquery_for_{func_name}")
|
||||
expr = sqlalchemy.select(select_columns).select_from(expr)
|
||||
# print("\n", expr.compile(compile_kwargs={"literal_binds": True}))
|
||||
result = await self.database.fetch_one(expr)
|
||||
return result if len(result) > 1 else result[0] # type: ignore
|
||||
return dict(result) if len(result) > 1 else result[0] # type: ignore
|
||||
|
||||
async def max( # noqa: A003
|
||||
self, columns: Union[str, List[str]]
|
||||
) -> Union[Any, ResultProxy]:
|
||||
async def max(self, columns: Union[str, List[str]]) -> Any: # noqa: A003
|
||||
"""
|
||||
Returns max value of columns for rows matching the given criteria
|
||||
(applied with `filter` and `exclude` if set before).
|
||||
@ -585,9 +586,7 @@ class QuerySet:
|
||||
columns = [columns]
|
||||
return await self._query_aggr_function(func_name="max", columns=columns)
|
||||
|
||||
async def min( # noqa: A003
|
||||
self, columns: Union[str, List[str]]
|
||||
) -> Union[Any, ResultProxy]:
|
||||
async def min(self, columns: Union[str, List[str]]) -> Any: # noqa: A003
|
||||
"""
|
||||
Returns min value of columns for rows matching the given criteria
|
||||
(applied with `filter` and `exclude` if set before).
|
||||
@ -599,9 +598,7 @@ class QuerySet:
|
||||
columns = [columns]
|
||||
return await self._query_aggr_function(func_name="min", columns=columns)
|
||||
|
||||
async def sum( # noqa: A003
|
||||
self, columns: Union[str, List[str]]
|
||||
) -> Union[Any, ResultProxy]:
|
||||
async def sum(self, columns: Union[str, List[str]]) -> Any: # noqa: A003
|
||||
"""
|
||||
Returns sum value of columns for rows matching the given criteria
|
||||
(applied with `filter` and `exclude` if set before).
|
||||
@ -613,7 +610,7 @@ class QuerySet:
|
||||
columns = [columns]
|
||||
return await self._query_aggr_function(func_name="sum", columns=columns)
|
||||
|
||||
async def avg(self, columns: Union[str, List[str]]) -> Union[Any, ResultProxy]:
|
||||
async def avg(self, columns: Union[str, List[str]]) -> Any:
|
||||
"""
|
||||
Returns avg value of columns for rows matching the given criteria
|
||||
(applied with `filter` and `exclude` if set before).
|
||||
@ -974,6 +971,7 @@ class QuerySet:
|
||||
"You cannot update unsaved objects. "
|
||||
f"{self.model.__name__} has to have {pk_name} filled."
|
||||
)
|
||||
new_kwargs = self.model.parse_non_db_fields(new_kwargs)
|
||||
new_kwargs = self.model.substitute_models_with_pks(new_kwargs)
|
||||
new_kwargs = self.model.translate_columns_to_aliases(new_kwargs)
|
||||
new_kwargs = {"new_" + k: v for k, v in new_kwargs.items() if k in columns}
|
||||
|
||||
@ -12,9 +12,8 @@ from typing import ( # noqa: I100, I201
|
||||
cast,
|
||||
)
|
||||
|
||||
from sqlalchemy.engine import ResultProxy
|
||||
|
||||
import ormar
|
||||
import ormar # noqa: I100, I202
|
||||
from ormar.exceptions import ModelPersistenceError, QueryDefinitionError
|
||||
|
||||
if TYPE_CHECKING: # pragma no cover
|
||||
@ -118,7 +117,6 @@ class QuerysetProxy:
|
||||
:type child: Model
|
||||
"""
|
||||
model_cls = self.relation.through
|
||||
# TODO: Add support for pk with default not only autoincrement id
|
||||
owner_column = self.related_field.default_target_field_name() # type: ignore
|
||||
child_column = self.related_field.default_source_field_name() # type: ignore
|
||||
rel_kwargs = {owner_column: self._owner.pk, child_column: child.pk}
|
||||
@ -129,10 +127,8 @@ class QuerysetProxy:
|
||||
f"model without primary key set! \n"
|
||||
f"Save the child model first."
|
||||
)
|
||||
expr = model_cls.Meta.table.insert()
|
||||
expr = expr.values(**final_kwargs)
|
||||
# print("\n", expr.compile(compile_kwargs={"literal_binds": True}))
|
||||
await model_cls.Meta.database.execute(expr)
|
||||
print('final kwargs', final_kwargs)
|
||||
await model_cls(**final_kwargs).save()
|
||||
|
||||
async def update_through_instance(self, child: "Model", **kwargs: Any) -> None:
|
||||
"""
|
||||
@ -148,6 +144,7 @@ class QuerysetProxy:
|
||||
child_column = self.related_field.default_source_field_name() # type: ignore
|
||||
rel_kwargs = {owner_column: self._owner.pk, child_column: child.pk}
|
||||
through_model = await model_cls.objects.get(**rel_kwargs)
|
||||
print('update kwargs', kwargs)
|
||||
await through_model.update(**kwargs)
|
||||
|
||||
async def delete_through_instance(self, child: "Model") -> None:
|
||||
@ -188,9 +185,7 @@ class QuerysetProxy:
|
||||
"""
|
||||
return await self.queryset.count()
|
||||
|
||||
async def max( # noqa: A003
|
||||
self, columns: Union[str, List[str]]
|
||||
) -> Union[Any, ResultProxy]:
|
||||
async def max(self, columns: Union[str, List[str]]) -> Any: # noqa: A003
|
||||
"""
|
||||
Returns max value of columns for rows matching the given criteria
|
||||
(applied with `filter` and `exclude` if set before).
|
||||
@ -200,9 +195,7 @@ class QuerysetProxy:
|
||||
"""
|
||||
return await self.queryset.max(columns=columns)
|
||||
|
||||
async def min( # noqa: A003
|
||||
self, columns: Union[str, List[str]]
|
||||
) -> Union[Any, ResultProxy]:
|
||||
async def min(self, columns: Union[str, List[str]]) -> Any: # noqa: A003
|
||||
"""
|
||||
Returns min value of columns for rows matching the given criteria
|
||||
(applied with `filter` and `exclude` if set before).
|
||||
@ -212,9 +205,7 @@ class QuerysetProxy:
|
||||
"""
|
||||
return await self.queryset.min(columns=columns)
|
||||
|
||||
async def sum( # noqa: A003
|
||||
self, columns: Union[str, List[str]]
|
||||
) -> Union[Any, ResultProxy]:
|
||||
async def sum(self, columns: Union[str, List[str]]) -> Any: # noqa: A003
|
||||
"""
|
||||
Returns sum value of columns for rows matching the given criteria
|
||||
(applied with `filter` and `exclude` if set before).
|
||||
@ -224,7 +215,7 @@ class QuerysetProxy:
|
||||
"""
|
||||
return await self.queryset.sum(columns=columns)
|
||||
|
||||
async def avg(self, columns: Union[str, List[str]]) -> Union[Any, ResultProxy]:
|
||||
async def avg(self, columns: Union[str, List[str]]) -> Any:
|
||||
"""
|
||||
Returns avg value of columns for rows matching the given criteria
|
||||
(applied with `filter` and `exclude` if set before).
|
||||
|
||||
Reference in New Issue
Block a user