mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
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 <njoseph@riseup.net> [sunil: Use django_db mark in test case] Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
parent
33dfc2cd41
commit
efbf2a80f5
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user