wip pc problems backup

This commit is contained in:
collerek
2021-03-14 19:09:34 +01:00
parent 1c63b1c80f
commit 6d0a5477cd
12 changed files with 268 additions and 184 deletions

View File

@ -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

View File

@ -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)