mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add functionality and unit testing for new model
This commit is contained in:
parent
1f7676ee65
commit
bebf368d06
@ -9,6 +9,7 @@ from __future__ import unicode_literals
|
||||
import os
|
||||
import decimal
|
||||
import math
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from django.db import models, transaction
|
||||
from django.contrib.auth.models import User, Group
|
||||
@ -1257,3 +1258,32 @@ class NotificationEntry(models.Model):
|
||||
auto_now=True,
|
||||
null=False,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def check_recent(cls, key: str, uid: int, delta: timedelta):
|
||||
"""
|
||||
Test if a particular notification has been sent in the specified time period
|
||||
"""
|
||||
|
||||
since = datetime.now().date() - delta
|
||||
|
||||
entries = cls.objects.filter(
|
||||
key=key,
|
||||
uid=uid,
|
||||
updated__gte=since
|
||||
)
|
||||
|
||||
return entries.exists()
|
||||
|
||||
@classmethod
|
||||
def notify(cls, key: str, uid: int):
|
||||
"""
|
||||
Notify the database that a particular notification has been sent out
|
||||
"""
|
||||
|
||||
entry, created = cls.objects.get_or_create(
|
||||
key=key,
|
||||
uid=uid
|
||||
)
|
||||
|
||||
entry.save()
|
||||
|
@ -1,10 +1,13 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from django.test import TestCase
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from .models import InvenTreeSetting
|
||||
from .models import NotificationEntry
|
||||
|
||||
|
||||
class SettingsTest(TestCase):
|
||||
@ -85,3 +88,25 @@ class SettingsTest(TestCase):
|
||||
|
||||
if setting.default_value not in [True, False]:
|
||||
raise ValueError(f'Non-boolean default value specified for {key}')
|
||||
|
||||
|
||||
class NotificationTest(TestCase):
|
||||
|
||||
def test_check_notification_entries(self):
|
||||
|
||||
# Create some notification entries
|
||||
|
||||
self.assertEqual(NotificationEntry.objects.count(), 0)
|
||||
|
||||
NotificationEntry.notify('test.notification', 1)
|
||||
|
||||
self.assertEqual(NotificationEntry.objects.count(), 1)
|
||||
|
||||
delta = timedelta(days=1)
|
||||
|
||||
self.assertFalse(NotificationEntry.check_recent('test.notification', 2, delta))
|
||||
self.assertFalse(NotificationEntry.check_recent('test.notification2', 1, delta))
|
||||
|
||||
self.assertTrue(NotificationEntry.check_recent('test.notification', 1, delta))
|
||||
|
||||
|
||||
|
@ -151,6 +151,7 @@ class RuleSet(models.Model):
|
||||
'common_colortheme',
|
||||
'common_inventreesetting',
|
||||
'common_inventreeusersetting',
|
||||
'common_notificationentry',
|
||||
'company_contact',
|
||||
'users_owner',
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user