예제 #1
0
 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)
예제 #2
0
 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)
예제 #3
0
 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)
예제 #4
0
    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)
예제 #5
0
파일: database.py 프로젝트: bigur/store
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
예제 #6
0
 async def test_context(self):
     '''Изменение переменной в контексте.'''
     assert context.get() is None
     async with UnitOfWork() as uow:
         assert context.get() is uow