From 5fdba3492dccba0e70960c78edba12cdda5a525d Mon Sep 17 00:00:00 2001 From: "Andreas (@xAndy) Zimmermann" Date: Tue, 10 May 2022 23:04:47 +0200 Subject: [PATCH] convert reg states to choices class --- shiftregister/app/models.py | 39 +++++++++++++++++++------------------ shiftregister/app/views.py | 5 ++++- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/shiftregister/app/models.py b/shiftregister/app/models.py index f4315fc..49a4383 100644 --- a/shiftregister/app/models.py +++ b/shiftregister/app/models.py @@ -34,8 +34,8 @@ class Shift(models.Model): Case( When( shiftregistration__state__in=[ - ShiftRegistration.REGISTERED, - ShiftRegistration.CHECKED_IN, + ShiftRegistration.RegState.REGISTERED, + ShiftRegistration.RegState.CHECKED_IN, ], then=1, ), @@ -55,7 +55,10 @@ class Shift(models.Model): def registration_count(self): return self.shiftregistration_set.filter( - state__in=[ShiftRegistration.REGISTERED, ShiftRegistration.CHECKED_IN] + state__in=[ + ShiftRegistration.RegState.REGISTERED, + ShiftRegistration.RegState.CHECKED_IN, + ] ).count() @@ -86,7 +89,10 @@ class Helper(models.Model): helper=self, shift_end__gte=timezone.now(), shift__deleted=False, - state__in=[ShiftRegistration.REGISTERED, ShiftRegistration.CHECKED_IN], + state__in=[ + ShiftRegistration.RegState.REGISTERED, + ShiftRegistration.RegState.CHECKED_IN, + ], ) .order_by("shift__start_at") .first() @@ -104,24 +110,19 @@ class ShiftRegistration(models.Model): helper = models.ForeignKey(Helper, on_delete=models.CASCADE) reminder_sent = models.BooleanField(default=False) - # default is registered - REGISTERED = "REG" - CHECKED_IN = "CHECK" - # cancel via infopoint - CANCELED = "CANCEL" - # did not attend shift - FAILED = "FAIL" + class RegState(models.TextChoices): + # default is registered + REGISTERED = "REG", "registriert" + CHECKED_IN = "CHECK", "checkin" + # cancel via infopoint + CANCELED = "CANCEL", "abgemeldet" + # did not attend shift + FAILED = "FAIL", "nicht angetreten" - STATE_CHOICES = [ - (REGISTERED, "registriert"), - (CHECKED_IN, "checkin"), - (CANCELED, "abgemeldet"), - (FAILED, "nicht angetreten"), - ] state = models.CharField( max_length=7, - choices=STATE_CHOICES, - default=REGISTERED, + choices=RegState.choices, + default=RegState.REGISTERED, ) def can_cancel(self): diff --git a/shiftregister/app/views.py b/shiftregister/app/views.py index 9f12119..b02d95b 100644 --- a/shiftregister/app/views.py +++ b/shiftregister/app/views.py @@ -28,7 +28,10 @@ def index(request): reg.shift for reg in request.helper.shiftregistration_set.filter( shift__start_at__gt=timezone.now(), - state__in=[ShiftRegistration.REGISTERED, ShiftRegistration.CHECKED_IN], + state__in=[ + ShiftRegistration.RegState.REGISTERED, + ShiftRegistration.RegState.CHECKED_IN, + ], ).order_by("shift__start_at") )