Ejemplo n.º 1
0
 def create_clock(self, entity, period=effective_now()):
     """creates a new clock for a temporal model.
     """
     clock = entity._clock()
     clock.effective = period
     clock.entity = entity
     clock.ticks = 0
     #  self.add(clock)
     return clock
Ejemplo n.º 2
0
 def create_clock_bulk(self, entity, period=effective_now()):
     """creates a new clock for a temporal model.
     """
     clock = entity._clock()
     clock.rid = uuid4()
     clock.effective = period
     clock.entity_id = entity.rid
     clock.ticks = 0
     return clock, True
Ejemplo n.º 3
0
    def get_or_create_clock(self, entity, period=effective_now()):
        """gets an existing or creates a new entity clock
           for a temporal model.
        """
        if entity.rid:
            clock = self.query(entity.__class__).clock(entity, period)

            if clock:
                return clock, False

        return self.create_clock(entity, period), True
Ejemplo n.º 4
0
def get_or_create_clock_entity(entity, period=effective_now()):
    """Tries to retrieve an entity clock for the given period
       from the database. If it does not exist, a new clock is
       created.
    """
    clock_cls = entity._clock

    clock_entity = session.query(clock_cls)\
        .filter(
            clock_cls.effective.contains(period),
            clock_cls.entity_id == entity.rid
        ).one_or_none()

    if not clock_entity:
        clock_entity = clock_cls()
        clock_entity.effective = period
        clock_entity.entity = entity
        clock_entity.ticks = 0
        session.add(clock_entity)

    return clock_entity
Ejemplo n.º 5
0
    def clock(self, entity, period=effective_now()):
        cls = entity._clock

        return self.session.query(cls).filter(
            cls.effective.contains(period),
            cls.entity_id == entity.rid).one_or_none()