From efbf2a80f5dc6f8bb8c421bac9dae0e069c6d577 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Wed, 9 Jul 2025 12:36:48 +0530 Subject: [PATCH] notifications: Fix setting last_update_time The field last_update_time is set to auto_now in the Django model, but it is not being updated when using update_or_create() since Django 4.2. This is because it sends update_fields= argument to save(). Say, a user installed an app a few hours ago and uninstalls it now, the notification should be updated to show the uninstallation status but it shows the timestamp of the installation instead. Explicitly setting the updated timestamp is one way of fixing this issue. Signed-off-by: Joseph Nuthalapati [sunil: Use django_db mark in test case] Signed-off-by: Sunil Mohan Adapa Reviewed-by: Sunil Mohan Adapa --- plinth/notification.py | 5 ++++- plinth/tests/test_notification.py | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) 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