fix: support UUID notification references (#12616)

* fix: support UUID notification references

* test: update notification uid expectation

---------

Co-authored-by: FurinaDog <FurinaDog@users.noreply.github.com>
This commit is contained in:
FurinaDog
2026-08-12 07:47:25 +10:00
committed by GitHub
co-authored by FurinaDog
parent 1c86102d82
commit 55a193e5d7
4 changed files with 37 additions and 4 deletions
@@ -211,7 +211,7 @@ class InvenTreeTaskTests(PluginRegistryMixin, TestCase):
entry = NotificationEntry.objects.get() entry = NotificationEntry.objects.get()
self.assertEqual(message.link, release_url) self.assertEqual(message.link, release_url)
self.assertEqual(entry.uid, 0) self.assertEqual(entry.uid, '0')
serialized = NotificationMessageSerializer(message).data serialized = NotificationMessageSerializer(message).data
self.assertEqual(serialized['target']['link'], release_url) self.assertEqual(serialized['target']['link'], release_url)
@@ -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),
),
]
+5 -3
View File
@@ -1656,10 +1656,12 @@ class NotificationEntry(MetaMixin):
key = models.CharField(max_length=250, blank=False) 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 @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.""" """Test if a particular notification has been sent in the specified time period."""
since = InvenTree.helpers.current_date() - delta since = InvenTree.helpers.current_date() - delta
@@ -1668,7 +1670,7 @@ class NotificationEntry(MetaMixin):
return entries.exists() return entries.exists()
@classmethod @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.""" """Notify the database that a particular notification has been sent out."""
entry, _ = cls.objects.get_or_create(key=key, uid=uid) entry, _ = cls.objects.get_or_create(key=key, uid=uid)
+13
View File
@@ -4,6 +4,7 @@ import io
import json import json
import os import os
import time import time
import uuid
from datetime import timedelta from datetime import timedelta
from http import HTTPStatus from http import HTTPStatus
from unittest import mock from unittest import mock
@@ -1333,6 +1334,18 @@ class NotificationTest(InvenTreeAPITestCase):
self.assertTrue(NotificationEntry.check_recent('test.notification', 1, delta)) 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): def test_api_list(self):
"""Test list URL.""" """Test list URL."""
url = reverse('api-notifications-list') url = reverse('api-notifications-list')