Compare commits

...

2 Commits

Author SHA1 Message Date
xAndy d034e39cb8 add setting to supress room change messages
continuous-integration/drone/push Build is passing Details
2025-05-18 01:29:10 +02:00
xAndy 04d85811fe change default required helpers to 1, even in import, so locations can be changed 2025-05-18 01:29:10 +02:00
5 changed files with 95 additions and 7 deletions

View File

@ -20,7 +20,7 @@ class ShiftInline(admin.TabularInline):
@admin.register(Room)
class RoomAdmin(admin.ModelAdmin):
list_display = ("name", "description_length", "shift_count")
list_display = ("name", "description_length", "shift_count", "required_helpers")
inlines = [ShiftInline]
def description_length(self, object):

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)

View File

@ -89,9 +89,7 @@ def import_calendar(calendar):
rooms[r.name] = r
for room, r in rooms.items():
if r == None:
rooms[room] = Room(
name=room, required_helpers=0
) # required_helpers=0 ensures a shift in a new room is not displayed unless the correct number of required helpers is set or the shift itself specifies it
rooms[room] = Room(name=room)
rooms[room].save()
for e in Event.objects.filter(calendar=calendar, uuid__in=events):