From 450556592b687936e4675225ec1abde041945e79 Mon Sep 17 00:00:00 2001 From: "Andreas (@xAndy) Zimmermann" Date: Fri, 19 May 2023 20:00:05 +0200 Subject: [PATCH] deactivate shift backup assignments if full --- .../0006_fallbackassignment_was_full.py | 18 +++++++++ shiftregister/fallback/models.py | 1 + shiftregister/fallback/tasks.py | 37 +++++++++++++++++++ .../templates/my_fallback_shifts.html | 8 ++-- shiftregister/fallback/views.py | 5 ++- shiftregister/settings.py | 4 ++ 6 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 shiftregister/fallback/migrations/0006_fallbackassignment_was_full.py create mode 100644 shiftregister/fallback/tasks.py diff --git a/shiftregister/fallback/migrations/0006_fallbackassignment_was_full.py b/shiftregister/fallback/migrations/0006_fallbackassignment_was_full.py new file mode 100644 index 0000000..2be44e4 --- /dev/null +++ b/shiftregister/fallback/migrations/0006_fallbackassignment_was_full.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.4 on 2023-05-19 16:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("fallback", "0005_alter_teammember_id"), + ] + + operations = [ + migrations.AddField( + model_name="fallbackassignment", + name="was_full", + field=models.BooleanField(default=False), + ), + ] diff --git a/shiftregister/fallback/models.py b/shiftregister/fallback/models.py index a1939f7..286a1ba 100644 --- a/shiftregister/fallback/models.py +++ b/shiftregister/fallback/models.py @@ -170,6 +170,7 @@ class TeamMember(models.Model): class FallbackAssignment(models.Model): shift = models.ForeignKey(Shift, on_delete=models.CASCADE) team_member = models.ForeignKey(TeamMember, on_delete=models.CASCADE) + was_full = models.BooleanField(default=False) def __str__(self): return f"{self.shift} {self.team_member.name}" diff --git a/shiftregister/fallback/tasks.py b/shiftregister/fallback/tasks.py new file mode 100644 index 0000000..bf91f09 --- /dev/null +++ b/shiftregister/fallback/tasks.py @@ -0,0 +1,37 @@ +from celery import shared_task +from .models import FallbackAssignment +from shiftregister.app.models import ShiftRegistration +from django.conf import settings +from django.db import transaction +from django.utils import timezone +from dynamic_preferences.registries import global_preferences_registry +from django.db.models import F, Count, Q, ExpressionWrapper, Case, When +from datetime import datetime +from django.utils import timezone + + +global_preferences = global_preferences_registry.manager() + + +# cron task to send normal messages(reminders,changes) in batches +@shared_task +def deactivate_fallbacks(): + for f in FallbackAssignment.objects.annotate( + reg_count=Count( + "shift__shiftregistration", + distinct=True, + filter=Q( + shift__shiftregistration__state__in=( + ShiftRegistration.RegState.REGISTERED, + ShiftRegistration.RegState.CHECKED_IN, + ) + ), + ), + ).filter( + was_full=False, + shift__start_at__lte=timezone.now() + + global_preferences["helper__min_cancel_time"], + shift__required_helpers__lte=F("reg_count"), + ): + f.was_full = True + f.save() diff --git a/shiftregister/fallback/templates/my_fallback_shifts.html b/shiftregister/fallback/templates/my_fallback_shifts.html index 134037e..608a4c4 100644 --- a/shiftregister/fallback/templates/my_fallback_shifts.html +++ b/shiftregister/fallback/templates/my_fallback_shifts.html @@ -9,7 +9,7 @@
Häufig gestellte Fragen zu Teamschichten
-{% if shifts %} +{% if assignments %} {% if is_draw %}
Hallo {{ team_member.name }}, hier deine Teamschichten für das Festival:
 
@@ -35,8 +35,9 @@ Diese Schichtzuteilung wurde maschinell erstellt und ist auch ohne Unterschrift
         
     
     
-{% for shift in shifts %}
-        
+{% for assignment in assignments %}
+{% with assignment.shift as shift %}
+        
             {{ shift.start_at }}
             {{ shift.duration|duration }}
             {{ shift.room.name }} 
@@ -52,6 +53,7 @@ Diese Schichtzuteilung wurde maschinell erstellt und ist auch ohne Unterschrift
 {% endif %}
             
         
+{% endwith %}
 {% endfor %}
     
 
diff --git a/shiftregister/fallback/views.py b/shiftregister/fallback/views.py
index a85d653..5c04ccc 100644
--- a/shiftregister/fallback/views.py
+++ b/shiftregister/fallback/views.py
@@ -25,7 +25,10 @@ def my_fallback_shifts(request, team_member_id):
 
     context = {
         "team_member": team_member,
-        "shifts": team_member.fallback_shifts.order_by("start_at").all(),
+        "assignments": team_member.fallbackassignment_set.order_by(
+            "shift__start_at"
+        ).all(),
+        # "shifts": team_member.fallback_shifts.order_by("start_at").all(),
         "is_draw": is_draw,
     }
     return render(request, "my_fallback_shifts.html", context)
diff --git a/shiftregister/settings.py b/shiftregister/settings.py
index 20523aa..73d177b 100644
--- a/shiftregister/settings.py
+++ b/shiftregister/settings.py
@@ -175,6 +175,10 @@ CELERY_BEAT_SCHEDULE = {
         "task": "shiftregister.team.tasks.receive_messages",
         "schedule": float(getenv("MESSAGE_RECEIVE_INTERVAL", 300.0)),  # seconds
     },
+    "deactivate-fallbacks-every-300-seconds": {
+        "task": "shiftregister.fallback.tasks.deactivate_fallbacks",
+        "schedule": float(getenv("FALLBACK_DEACTIVAtE_INTERVAL", 300.0)),  # seconds
+    },
 }
 
 CELERY_BEAT_SCHEDULE_FILENAME = str(BASE_DIR / "storage" / "celerybeat-schedule")