add setting to supress room change messages
continuous-integration/drone/push Build is passing Details

This commit is contained in:
xAndy 2025-05-17 22:50:33 +02:00 committed by Luca
parent 04d85811fe
commit d034e39cb8
3 changed files with 93 additions and 3 deletions

View File

@ -94,3 +94,11 @@ class EventEndAt(types.DatePreference):
name = "event_end_at"
default = datetime.date(2025, 5, 28)
help_text = "The end date and time of the event. Date navigation will only show days between start and end time."
@global_preferences_registry.register
class SupressRoomChangeNotifications(types.BooleanPreference):
section = helper
name = "supress_room_change_notifications"
default = False
help_text = "If true, notifications about room changes will be suppressed."

View File

@ -2,11 +2,14 @@ from django.db.models.signals import pre_save
from django.dispatch import receiver
from django.shortcuts import reverse
from django.template import Context, Template
from dynamic_preferences.registries import global_preferences_registry
from shiftregister.core.signals import populate_nav
from .models import Message, Shift
global_preferences = global_preferences_registry.manager()
@receiver(pre_save, dispatch_uid="notify_shift_changed")
def notify_shift_changed(sender, **kwargs):
@ -16,9 +19,11 @@ def notify_shift_changed(sender, **kwargs):
prev = Shift.all_objects.get(pk=instance.id)
except Shift.DoesNotExist:
return
room_unchanged = (prev.room == instance.room) or global_preferences[
"helper__supress_room_change_notifications"
]
if (
prev.room == instance.room
room_unchanged
and prev.start_at == instance.start_at
and prev.duration == instance.duration
and prev.deleted == instance.deleted

View File

@ -2,8 +2,9 @@ from datetime import timedelta
from django.test import TestCase
from django.utils import timezone
from dynamic_preferences.registries import global_preferences_registry
from .models import Helper, Room, Shift, ShiftRegistration
from .models import Helper, Message, Room, Shift, ShiftRegistration
class ShiftOverlapTests(TestCase):
@ -162,3 +163,79 @@ class ShiftOverlapTests(TestCase):
required_helpers=1,
)
self.assertIsNone(self.helper.has_overlapping_shift(shift_same_time))
class ShiftSignalTests(TestCase):
def setUp(self):
# Create rooms
self.room1 = Room.objects.create(
name="Test Room 1", required_helpers=1, meeting_location="Test Location 1"
)
self.room2 = Room.objects.create(
name="Test Room 2", required_helpers=1, meeting_location="Test Location 2"
)
# Create a helper
self.helper = Helper.objects.create(
phone="+491234567890", name="Test Helper", number_validated=True
)
# Create a shift
self.shift = Shift.objects.create(
room=self.room1,
start_at=timezone.now() + timedelta(hours=1),
duration=timedelta(hours=2),
required_helpers=1,
)
# Register the helper for the shift
self.registration = ShiftRegistration.objects.create(
shift=self.shift,
helper=self.helper,
state=ShiftRegistration.RegState.REGISTERED,
)
# Get global preferences manager
self.global_preferences = global_preferences_registry.manager()
def test_room_change_notification_default(self):
"""Test that room changes create notifications by default"""
# Ensure the preference is False (default)
self.global_preferences["helper__supress_room_change_notifications"] = False
# Change the room
self.shift.room = self.room2
self.shift.save()
# Check that a notification was created
self.assertEqual(Message.objects.count(), 1)
message = Message.objects.first()
self.assertEqual(message.to, self.helper)
self.assertTrue("geändert" in message.text)
def test_room_change_notification_suppressed(self):
"""Test that room changes don't create notifications when suppressed"""
# Enable notification suppression
self.global_preferences["helper__supress_room_change_notifications"] = True
# Change the room
self.shift.room = self.room2
self.shift.save()
# Check that no notification was created
self.assertEqual(Message.objects.count(), 0)
def test_other_changes_still_notify_when_suppressed(self):
"""Test that other changes still create notifications when room notifications are suppressed"""
# Enable notification suppression
self.global_preferences["helper__supress_room_change_notifications"] = True
# Change something other than the room
self.shift.start_at = timezone.now() + timedelta(hours=2)
self.shift.save()
# Check that a notification was created
self.assertEqual(Message.objects.count(), 1)
message = Message.objects.first()
self.assertEqual(message.to, self.helper)
self.assertTrue("geändert" in message.text)