From bc890cc49b72b68c8e1ab03927c922499a939c0e Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 11 Mar 2025 14:53:19 +0100 Subject: [PATCH] change flow for subscription --- dashboard/templates/unassigned_devices.html | 4 - lot/forms.py | 21 ++++-- .../0009_donor_lotsubscription_and_more.py | 9 +-- lot/models.py | 15 +++- lot/templates/subscription.html | 73 ++++++++++++++----- lot/urls.py | 2 +- lot/views.py | 44 ++++++----- 7 files changed, 115 insertions(+), 53 deletions(-) diff --git a/dashboard/templates/unassigned_devices.html b/dashboard/templates/unassigned_devices.html index b98f787..8336c4d 100644 --- a/dashboard/templates/unassigned_devices.html +++ b/dashboard/templates/unassigned_devices.html @@ -24,10 +24,6 @@ {% trans 'Subscription' %} {% endif %} - - - {% trans 'Unsubscription' %} - {% if donor %} diff --git a/lot/forms.py b/lot/forms.py index c662ae8..33e7047 100644 --- a/lot/forms.py +++ b/lot/forms.py @@ -54,6 +54,14 @@ class LotSubscriptionForm(forms.Form): if self._user and self._user.institution != self.institution: txt = _("This user is from another institution") raise ValidationError(txt) + slot = LotSubscription.objects.filter( + user=self._user, + lot_id=self.lot_pk, + ) + if slot: + txt = _("This user is already subscripted") + raise ValidationError(txt) + return def save(self, commit=True): @@ -72,7 +80,8 @@ class LotSubscriptionForm(forms.Form): slot = LotSubscription.objects.filter( user=self._user, lot_id=self.lot_pk, - is_circuit_manager=True + type=LotSubscription.Type.CIRCUIT_MANAGER + ) if slot: return @@ -80,14 +89,14 @@ class LotSubscriptionForm(forms.Form): LotSubscription.objects.create( user=self._user, lot_id=self.lot_pk, - is_circuit_manager=True + type=LotSubscription.Type.CIRCUIT_MANAGER ) if self._type == "shop": slot = LotSubscription.objects.filter( user=self._user, lot_id=self.lot_pk, - is_shop=True + type=LotSubscription.Type.SHOP ) if slot: return @@ -95,7 +104,7 @@ class LotSubscriptionForm(forms.Form): LotSubscription.objects.create( user=self._user, lot_id=self.lot_pk, - is_shop=True + type=LotSubscription.Type.SHOP ) def remove(self): @@ -106,14 +115,14 @@ class LotSubscriptionForm(forms.Form): lot_subscription = LotSubscription.objects.filter( user=self._user, lot_id=self.lot_pk, - is_circuit_manager=True + type=LotSubscription.Type.CIRCUIT_MANAGER ) elif self._type == "shop": lot_subscription = LotSubscription.objects.filter( user=self._user, lot_id=self.lot_pk, - is_circuit_manager=True + type=LotSubscription.Type.SHOP ) else: diff --git a/lot/migrations/0009_donor_lotsubscription_and_more.py b/lot/migrations/0009_donor_lotsubscription_and_more.py index 3a98ce9..c23aedf 100644 --- a/lot/migrations/0009_donor_lotsubscription_and_more.py +++ b/lot/migrations/0009_donor_lotsubscription_and_more.py @@ -1,4 +1,4 @@ -# Generated by Django 5.0.6 on 2025-03-10 16:29 +# Generated by Django 5.0.6 on 2025-03-11 10:55 import django.db.models.deletion import uuid @@ -55,12 +55,11 @@ class Migration(migrations.Migration): ), ), ( - "is_circuit_manager", - models.BooleanField( - default=False, verbose_name="is circuit manager" + "type", + models.SmallIntegerField( + choices=[(0, "Circuit Manager"), (1, "Shop")] ), ), - ("is_shop", models.BooleanField(default=False, verbose_name="is shop")), ( "lot", models.ForeignKey( diff --git a/lot/models.py b/lot/models.py index 6aaaaba..4b951ca 100644 --- a/lot/models.py +++ b/lot/models.py @@ -64,10 +64,13 @@ class LotProperty(Property): class LotSubscription(models.Model): + class Type(models.IntegerChoices): + CIRCUIT_MANAGER = 0, _("Circuit Manager") + SHOP = 1, _("Shop") + lot = models.ForeignKey(Lot, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE, null=False, blank=False) - is_circuit_manager = models.BooleanField(_("is circuit manager"), default=False) - is_shop = models.BooleanField(_("is shop"), default=False) + type = models.SmallIntegerField(choices=Type.choices) class Meta: constraints = [ @@ -75,6 +78,14 @@ class LotSubscription(models.Model): fields=["lot", "user"], name="unique_lot_user") ] + @property + def is_circuit_manager(self): + return self.type == self.Type.CIRCUIT_MANAGER + + @property + def is_shop(self): + return self.type == self.Type.SHOP + class Donor(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) diff --git a/lot/templates/subscription.html b/lot/templates/subscription.html index 7bf070c..2a0db29 100644 --- a/lot/templates/subscription.html +++ b/lot/templates/subscription.html @@ -8,25 +8,64 @@ -{% load django_bootstrap5 %} -
-{% csrf_token %} -{% if form.errors %} -