diff --git a/src/backend/InvenTree/InvenTree/test_tasks.py b/src/backend/InvenTree/InvenTree/test_tasks.py index 9224b784cf..098b1cc9d0 100644 --- a/src/backend/InvenTree/InvenTree/test_tasks.py +++ b/src/backend/InvenTree/InvenTree/test_tasks.py @@ -211,7 +211,7 @@ class InvenTreeTaskTests(PluginRegistryMixin, TestCase): entry = NotificationEntry.objects.get() self.assertEqual(message.link, release_url) - self.assertEqual(entry.uid, 0) + self.assertEqual(entry.uid, '0') serialized = NotificationMessageSerializer(message).data self.assertEqual(serialized['target']['link'], release_url) diff --git a/src/backend/InvenTree/common/migrations/0049_notificationentry_charfield_uid.py b/src/backend/InvenTree/common/migrations/0049_notificationentry_charfield_uid.py new file mode 100644 index 0000000000..889597ed2e --- /dev/null +++ b/src/backend/InvenTree/common/migrations/0049_notificationentry_charfield_uid.py @@ -0,0 +1,18 @@ +"""Allow notification entries to reference UUID primary keys.""" + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('common', '0048_notificationmessage_link'), + ] + + operations = [ + migrations.AlterField( + model_name='notificationentry', + name='uid', + field=models.CharField(max_length=255), + ), + ] diff --git a/src/backend/InvenTree/common/models.py b/src/backend/InvenTree/common/models.py index 931630922c..321b45a3c3 100644 --- a/src/backend/InvenTree/common/models.py +++ b/src/backend/InvenTree/common/models.py @@ -1656,10 +1656,12 @@ class NotificationEntry(MetaMixin): key = models.CharField(max_length=250, blank=False) - uid = models.IntegerField() + # Notification references may point to models with UUID primary keys. + # Store the value as text so both integer and non-integer identifiers work. + uid = models.CharField(max_length=255) @classmethod - def check_recent(cls, key: str, uid: int, delta: timedelta): + def check_recent(cls, key: str, uid: str | int | uuid.UUID, delta: timedelta): """Test if a particular notification has been sent in the specified time period.""" since = InvenTree.helpers.current_date() - delta @@ -1668,7 +1670,7 @@ class NotificationEntry(MetaMixin): return entries.exists() @classmethod - def notify(cls, key: str, uid: int): + def notify(cls, key: str, uid: str | int | uuid.UUID): """Notify the database that a particular notification has been sent out.""" entry, _ = cls.objects.get_or_create(key=key, uid=uid) diff --git a/src/backend/InvenTree/common/tests.py b/src/backend/InvenTree/common/tests.py index 5589fb8eb6..49463dfc45 100644 --- a/src/backend/InvenTree/common/tests.py +++ b/src/backend/InvenTree/common/tests.py @@ -4,6 +4,7 @@ import io import json import os import time +import uuid from datetime import timedelta from http import HTTPStatus from unittest import mock @@ -1333,6 +1334,18 @@ class NotificationTest(InvenTreeAPITestCase): self.assertTrue(NotificationEntry.check_recent('test.notification', 1, delta)) + def test_uuid_notification_entry(self): + """Notification entries support objects with UUID primary keys.""" + notification_uid = uuid.uuid4() + + NotificationEntry.notify('test.uuid_notification', notification_uid) + + self.assertTrue( + NotificationEntry.check_recent( + 'test.uuid_notification', notification_uid, timedelta(days=1) + ) + ) + def test_api_list(self): """Test list URL.""" url = reverse('api-notifications-list')