diff --git a/plinth/notification.py b/plinth/notification.py index c1be960e0..2711df750 100644 --- a/plinth/notification.py +++ b/plinth/notification.py @@ -8,6 +8,7 @@ from django.core.exceptions import ValidationError from django.db.models import Q from django.template.exceptions import TemplateDoesNotExist from django.template.response import SimpleTemplateResponse +from django.utils import timezone from django.utils.translation import gettext from plinth import cfg @@ -229,9 +230,11 @@ class Notification(models.StoredNotification): """ id = kwargs.pop('id') + fields_to_update = kwargs.copy() + fields_to_update['last_update_time'] = timezone.now() with db.lock: return Notification.objects.update_or_create( - defaults=kwargs, id=id)[0] + defaults=fields_to_update, id=id)[0] @staticmethod def get(key): # pylint: disable=redefined-builtin diff --git a/plinth/tests/test_notification.py b/plinth/tests/test_notification.py index cd72755c0..5e1c3f6d7 100644 --- a/plinth/tests/test_notification.py +++ b/plinth/tests/test_notification.py @@ -413,3 +413,28 @@ def test_display_context_body_template(note, user, load_cfg, rf): context_note = context['notifications'][0] assert context_note['body'].content == \ b'Test notification body /plinth/help/about/\n' + + +@pytest.mark.django_db +def test_last_update_time_updates_on_modify(): + """Test that last_update_time is updated on modify via update_or_create.""" + # Given a notification is created at time t1 + t1 = datetime.datetime(2025, 1, 1, tzinfo=datetime.timezone.utc) + with patch('django.utils.timezone.now', return_value=t1): + Notification.update_or_create(id='timestamp-test', app_id='app', + severity='info', title='Title') + + first_note = Notification.get('timestamp-test') + assert first_note.last_update_time == t1 + + # When the same notification is updated at later time t2 + t2 = t1 + datetime.timedelta(minutes=5) + with patch('django.utils.timezone.now', return_value=t2): + Notification.update_or_create(id='timestamp-test', app_id='app', + severity='warning', title='Title2') + + second_note = Notification.get('timestamp-test') + + # Then the last_update_time should be t2 + assert second_note.last_update_time == t2 + assert second_note.last_update_time > first_note.last_update_time