def mark_removed(self) -> None: '''Mark document for removal.''' if getattr(self, '_id', None) is not None: logger.debug('Marking object %s for deletion', self) uow = context.get() if uow is not None: uow.register_removed(self)
def mark_new(self) -> None: '''Mark object as new.''' uow = context.get() if uow is not None: logger.debug('Mark object %s as new', self) uow.register_new(self) else: logger.warning( 'Creating object without db context.', stack_info=True)
def mark_dirty(self, keys: Set[str]) -> None: '''Mark object as dirty.''' if getattr(self, '_id', None) is not None: uow = context.get() if uow is not None: uow.register_dirty(self, keys) else: logger.warning( 'Changing object without database context.', stack_info=True)
def __post_init__(self, id_: Optional[ObjectId] = None) -> None: logger.debug('Stored.__post_init__ (%s) start', self) if id_ is None: id_ = ObjectId() self._id: ObjectId = id_ self.mark_new() self.__unit_of_work__ = context.get() super().__post_init__() logger.debug('Stored.__post_init__ (%s) end', self)
def compile_object(document: DatabaseDict) -> DocumentOrObject: '''Превращает документ, полученный из базы в объект python.''' if document is not None and '_class' in document: splitted = document['_class'].split('.') class_name = splitted.pop() module_name = '.'.join(splitted) try: module = modules[module_name] except KeyError: module = import_module(module_name) cls = getattr(module, class_name) obj = cls.__new__(cls) obj.__setstate__(document) obj.__unit_of_work__ = context.get() document = obj return document
async def test_context(self): '''Изменение переменной в контексте.''' assert context.get() is None async with UnitOfWork() as uow: assert context.get() is uow