def sorted_events(self, reverse=False): for event in sorted([ comparable_event.get(self.log.__class__)(x) for x in self.log.get_events() ], reverse=reverse): yield event
def sorted_events(self, start=None, end=None, users=None, reverse=False): comparable_event_class = comparable_event.get(self.log.__class__) events = sorted( (comparable_event_class(x) for x in self.log.get_events(start=start, end=end, users=users)), reverse=reverse) for event in events: yield event
def get_events(self, start=None, n=None): sortable = comparable_event.get(self.log.__class__) start = start or (timezone.now() - timedelta(days=30)) events = sorted( sortable(ev) for ev in self.log.get_events(start=start)) if n is not None: events = events[-n:] return reversed(events)
def test_comparable_unit_timelime_log(member, store0): assert comparable_event.get(UnitTimelineLog) == ComparableLogEvent start = timezone.now().replace(microsecond=0) unit = store0.units.filter(state=TRANSLATED).first() unit.target += 'UPDATED IN TEST' unit.save(user=member) unit = store0.units.filter(state=TRANSLATED).first() unit.target += 'UPDATED IN TEST AGAIN' unit.save(user=member) unit_log = UnitTimelineLog(unit) event1, event2 = [ComparableLogEvent(x) for x in unit_log.get_events(users=[member.id], start=start)] assert (event1 < event2) == (event1.revision < event2.revision) assert (event2 < event1) == (event2.revision < event1.revision) unit = store0.units.filter(state=UNTRANSLATED).first() sugg1, created_ = review.get(Suggestion)().add( unit, unit.source_f + 'SUGGESTION', user=member) sugg2, created_ = review.get(Suggestion)().add( unit, unit.source_f + 'SUGGESTION AGAIN', user=member) unit_log = UnitTimelineLog(unit) Suggestion.objects.filter(id=sugg2.id).update( creation_time=sugg1.creation_time + timedelta(seconds=1)) event1, event2 = [ComparableLogEvent(x) for x in unit_log.get_events(users=[member.id], start=start)] assert (event1 < event2) == (event1.timestamp < event2.timestamp) assert (event2 < event1) == (event2.timestamp < event1.timestamp) Suggestion.objects.filter(id=sugg2.id).update( creation_time=sugg1.creation_time) event1, event2 = [ComparableLogEvent(x) for x in unit_log.get_events(users=[member.id], start=start)] assert (event1 < event2) == (event1.value.pk < event2.value.pk) assert (event2 < event1) == (event2.value.pk < event1.value.pk) Suggestion.objects.filter(id=sugg2.id).update(creation_time=None) sugg2 = Suggestion.objects.get(id=sugg2.id) event1 = [ComparableLogEvent(x) for x in unit_log.get_events(users=[member.id], start=start)][0] event2 = ComparableLogEvent(unit_log.event(sugg2.unit, sugg2.user, sugg2.creation_time, "suggestion_created", sugg2)) assert event2 < event1 assert not (event1 < event2) unit = store0.units.filter(state=UNTRANSLATED)[0] unit.target = 'Unit Target' unit.save() unit_log = UnitTimelineLog(unit) event1, event2 = [ComparableLogEvent(x) for x in unit_log.get_submission_events()] assert (event1 < event2) == ( ACTION_ORDER[event1.action] < ACTION_ORDER[event2.action]) assert (event2 < event1) == ( ACTION_ORDER[event2.action] < ACTION_ORDER[event1.action]) assert not (event1 < event1) and not (event1 > event1)
def test_comparable_log(member, store0, store_po): assert comparable_event.get(Log) == ComparableLogEvent start = timezone.now().replace(microsecond=0) unit = store0.units.filter(state=TRANSLATED).first() unit.target += 'UPDATED IN TEST' unit.save(user=member) unit = store0.units.filter(state=TRANSLATED).first() unit.target += 'UPDATED IN TEST AGAIN' unit.save(user=member) unit_log = log.get(unit.__class__)(unit) event1, event2 = [ComparableLogEvent(x) for x in unit_log.get_events(users=[member.id], start=start)] assert (event1 < event2) == (event1.revision < event2.revision) assert (event2 < event1) == (event2.revision < event1.revision) unit = store0.units.filter(state=UNTRANSLATED).first() sugg1, created_ = review.get(Suggestion)().add( unit, unit.source_f + 'SUGGESTION', user=member) sugg2, created_ = review.get(Suggestion)().add( unit, unit.source_f + 'SUGGESTION AGAIN', user=member) Suggestion.objects.filter(id=sugg2.id).update(creation_time=sugg1.creation_time) unit_log = log.get(unit.__class__)(unit) event1, event2 = [ComparableLogEvent(x) for x in unit_log.get_events(users=[member.id], start=start)] assert (event1 < event2) == (event1.value.pk < event2.value.pk) assert (event2 < event1) == (event2.value.pk < event1.value.pk) Suggestion.objects.filter(id=sugg2.id).update(creation_time=None) sugg2 = Suggestion.objects.get(id=sugg2.id) event1 = [ComparableLogEvent(x) for x in unit_log.get_events(users=[member.id], start=start)][0] event2 = ComparableLogEvent(unit_log.event(sugg2.unit, sugg2.user, sugg2.creation_time, "suggestion_created", sugg2)) assert event2 < event1 assert not (event1 < event2) units = [ ('Unit 0 Source', 'Unit 0 Target', False), ('Unit 1 Source', '', False), ] store_po.update(create_store(units=units)) unit1, unit2 = store_po.units unit2.__class__.objects.filter(id=unit2.id).update( creation_time=unit1.creation_time) store_log = log.get(store_po.__class__)(store_po) event1, event2 = [ComparableLogEvent(x) for x in store_log.get_events()] assert (event1 < event2) == (event1.unit.id < event2.unit.id) assert (event2 < event1) == (event2.unit.id < event1.unit.id) creation_time = unit1.creation_time + timedelta(seconds=1) unit2.__class__.objects.filter(id=unit2.id).update(creation_time=creation_time) event1, event2 = [ComparableLogEvent(x) for x in store_log.get_events()] assert (event1 < event2) == (event1.timestamp < event2.timestamp) assert (event2 < event1) == (event2.timestamp < event1.timestamp) unit = store_po.units.filter(state=UNTRANSLATED)[0] unit.target = 'Unit 1 Target' unit.save() unit_log = log.get(unit.__class__)(unit) event1, event2 = [ComparableLogEvent(x) for x in unit_log.get_submission_events()] assert not (event1 < event2) and not (event2 < event1)
def test_comparable_log(member, store0, store_po): assert comparable_event.get(Log) == ComparableLogEvent start = timezone.now().replace(microsecond=0) unit = store0.units.filter(state=TRANSLATED).first() unit.target += 'UPDATED IN TEST' unit.save(user=member) unit = store0.units.filter(state=TRANSLATED).first() unit.target += 'UPDATED IN TEST AGAIN' unit.save(user=member) unit_log = log.get(unit.__class__)(unit) event1, event2 = [ ComparableLogEvent(x) for x in unit_log.get_events(users=[member.id], start=start) ] assert (event1 < event2) == (event1.revision < event2.revision) assert (event2 < event1) == (event2.revision < event1.revision) unit = store0.units.filter(state=UNTRANSLATED).first() sugg1, created_ = review.get(Suggestion)().add(unit, unit.source_f + 'SUGGESTION', user=member) sugg2, created_ = review.get(Suggestion)().add(unit, unit.source_f + 'SUGGESTION AGAIN', user=member) Suggestion.objects.filter(id=sugg2.id).update( creation_time=sugg1.creation_time) unit_log = log.get(unit.__class__)(unit) event1, event2 = [ ComparableLogEvent(x) for x in unit_log.get_events(users=[member.id], start=start) ] assert (event1 < event2) == (event1.value.pk < event2.value.pk) assert (event2 < event1) == (event2.value.pk < event1.value.pk) Suggestion.objects.filter(id=sugg2.id).update(creation_time=None) sugg2 = Suggestion.objects.get(id=sugg2.id) event1 = [ ComparableLogEvent(x) for x in unit_log.get_events(users=[member.id], start=start) ][0] event2 = ComparableLogEvent( unit_log.event(sugg2.unit, sugg2.user, sugg2.creation_time, "suggestion_created", sugg2)) assert event2 < event1 assert not (event1 < event2) units = [ ('Unit 0 Source', 'Unit 0 Target', False), ('Unit 1 Source', '', False), ] store_po.update(create_store(units=units)) unit1, unit2 = store_po.units unit2.__class__.objects.filter(id=unit2.id).update( creation_time=unit1.creation_time) store_log = log.get(store_po.__class__)(store_po) event1, event2 = [ComparableLogEvent(x) for x in store_log.get_events()] assert (event1 < event2) == (event1.unit.id < event2.unit.id) assert (event2 < event1) == (event2.unit.id < event1.unit.id) creation_time = unit1.creation_time + timedelta(seconds=1) unit2.__class__.objects.filter(id=unit2.id).update( creation_time=creation_time) event1, event2 = [ComparableLogEvent(x) for x in store_log.get_events()] assert (event1 < event2) == (event1.timestamp < event2.timestamp) assert (event2 < event1) == (event2.timestamp < event1.timestamp) unit = store_po.units.filter(state=UNTRANSLATED)[0] unit.target = 'Unit 1 Target' unit.save() unit_log = log.get(unit.__class__)(unit) event1, event2 = [ ComparableLogEvent(x) for x in unit_log.get_submission_events() ] assert not (event1 < event2) and not (event2 < event1)
def sorted_events(self, reverse=False): for event in sorted([comparable_event.get(self.log.__class__)(x) for x in self.log.get_events()], reverse=reverse): yield event