Skip to content
Snippets Groups Projects
Commit 19a41aee authored by Martin Burchell's avatar Martin Burchell
Browse files

Fixed multiple updates of item last_modified field

parent 135d705d
No related branches found
No related tags found
1 merge request!7Data Layer updates a "last edit date" field on the Item
...@@ -13,7 +13,7 @@ class DataLayerModel(models.Model): ...@@ -13,7 +13,7 @@ class DataLayerModel(models.Model):
abstract = True abstract = True
def note_external_modification(self): def note_external_modification(self):
self.last_modified = timezone.now() # This will set the last_modified field
self.save() self.save()
...@@ -55,5 +55,6 @@ Item = Message ...@@ -55,5 +55,6 @@ Item = Message
@receiver(models.signals.m2m_changed, sender=Item.terms.through, @receiver(models.signals.m2m_changed, sender=Item.terms.through,
dispatch_uid="data_layer.models.terms_signal_handler") dispatch_uid="data_layer.models.terms_signal_handler")
def terms_signal_handler(sender, **kwargs): def terms_signal_handler(sender, **kwargs):
instance = kwargs.get('instance') if kwargs.get('action') == 'post_add':
instance.note_external_modification() instance = kwargs.get('instance')
instance.note_external_modification()
...@@ -17,13 +17,23 @@ def last_modified(item): ...@@ -17,13 +17,23 @@ def last_modified(item):
# Ensure value of "now" always increases by amount sufficient # Ensure value of "now" always increases by amount sufficient
# to show up as a change, even if db resolution for datetime # to show up as a change, even if db resolution for datetime
# is one second. # is one second.
def time_granularity():
return datetime.timedelta(hours=1)
def now_iter(start): def now_iter(start):
t = start t = start
while True: while True:
t += datetime.timedelta(hours=1) t += time_granularity()
yield t yield t
def num_updates(old_time, new_time):
elapsed_time = new_time - old_time
return elapsed_time.seconds / time_granularity().seconds
@pytest.mark.django_db @pytest.mark.django_db
def test_last_modified_date_updates_on_body_change(): def test_last_modified_date_updates_on_body_change():
item = ItemFactory() item = ItemFactory()
...@@ -35,7 +45,7 @@ def test_last_modified_date_updates_on_body_change(): ...@@ -35,7 +45,7 @@ def test_last_modified_date_updates_on_body_change():
item.body = 'replacement text' item.body = 'replacement text'
item.save() item.save()
assert orig_last_modified < last_modified(item) assert num_updates(orig_last_modified, last_modified(item)) == 1
@pytest.mark.django_db @pytest.mark.django_db
...@@ -49,4 +59,4 @@ def test_last_modified_date_updates_on_category_change(): ...@@ -49,4 +59,4 @@ def test_last_modified_date_updates_on_category_change():
term = TermFactory() term = TermFactory()
item.terms.add(term) item.terms.add(term)
assert orig_last_modified < last_modified(item) assert num_updates(orig_last_modified, last_modified(item)) == 1
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment