mirror of
https://github.com/inventree/InvenTree.git
synced 2026-08-13 00:46:15 +00:00
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:
@@ -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)
|
||||
|
||||
@@ -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),
|
||||
),
|
||||
]
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user