From c44c185e0c968556e8688e42f6bbde3ecaed7441 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 7 Feb 2025 14:38:17 +0100 Subject: [PATCH] base of subscription & unsubscription --- dashboard/templates/unassigned_devices.html | 8 +++ ...ription_lotsubscription_unique_lot_user.py | 28 +++++++++ lot/models.py | 11 ++++ lot/templates/subscription.html | 32 ++++++++++ lot/urls.py | 2 + lot/views.py | 59 ++++++++++++++++++- ...02_user_is_circuit_manager_user_is_shop.py | 23 ++++++++ user/models.py | 31 +++++++++- 8 files changed, 190 insertions(+), 4 deletions(-) create mode 100644 lot/migrations/0003_lotsubscription_lotsubscription_unique_lot_user.py create mode 100644 lot/templates/subscription.html create mode 100644 user/migrations/0002_user_is_circuit_manager_user_is_shop.py diff --git a/dashboard/templates/unassigned_devices.html b/dashboard/templates/unassigned_devices.html index 25c1bec..bdeaba0 100644 --- a/dashboard/templates/unassigned_devices.html +++ b/dashboard/templates/unassigned_devices.html @@ -18,6 +18,14 @@ {% trans 'properties' %} + + + {% trans 'Subscription' %} + + + + {% trans 'Unsubscription' %} + {% endif %} diff --git a/lot/migrations/0003_lotsubscription_lotsubscription_unique_lot_user.py b/lot/migrations/0003_lotsubscription_lotsubscription_unique_lot_user.py new file mode 100644 index 0000000..65ac77e --- /dev/null +++ b/lot/migrations/0003_lotsubscription_lotsubscription_unique_lot_user.py @@ -0,0 +1,28 @@ +# Generated by Django 5.0.6 on 2025-02-07 13:41 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('lot', '0002_alter_lot_closed'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='LotSubscription', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('lot', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='lot.lot')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.AddConstraint( + model_name='lotsubscription', + constraint=models.UniqueConstraint(fields=('lot', 'user'), name='unique_lot_user'), + ), + ] diff --git a/lot/models.py b/lot/models.py index d196ae7..15055a6 100644 --- a/lot/models.py +++ b/lot/models.py @@ -59,4 +59,15 @@ class LotProperty(Property): constraints = [ models.UniqueConstraint( fields=["key", "lot"], name="property_unique_type_key_lot") + ] + + +class LotSubscription(models.Model): + lot = models.ForeignKey(Lot, on_delete=models.CASCADE) + user = models.ForeignKey(User, on_delete=models.CASCADE, null=False, blank=False) + + class Meta: + constraints = [ + models.UniqueConstraint( + fields=["lot", "user"], name="unique_lot_user") ] diff --git a/lot/templates/subscription.html b/lot/templates/subscription.html new file mode 100644 index 0000000..8c6faf7 --- /dev/null +++ b/lot/templates/subscription.html @@ -0,0 +1,32 @@ +{% extends "base.html" %} +{% load i18n %} + +{% block content %} +
+
+

{{ subtitle }}

+
+
+ +{% load django_bootstrap5 %} +
+{% csrf_token %} +{% if form.errors %} + +{% endif %} +{% bootstrap_form form %} + + +
+{% endblock %} diff --git a/lot/urls.py b/lot/urls.py index 4ddd452..018950f 100644 --- a/lot/urls.py +++ b/lot/urls.py @@ -14,4 +14,6 @@ urlpatterns = [ path("/property/add", views.AddLotPropertyView.as_view(), name="add_property"), path("/property/update", views.UpdateLotPropertyView.as_view(), name="update_property"), path("/property/delete", views.DeleteLotPropertyView.as_view(), name="delete_property"), + path("/subscription/", views.SubscriptLotView.as_view(), name="subscription"), + path("/unsubscription/", views.UnsubscriptLotView.as_view(), name="unsubscription"), ] diff --git a/lot/views.py b/lot/views.py index 8049935..d2f2552 100644 --- a/lot/views.py +++ b/lot/views.py @@ -11,7 +11,7 @@ from django.views.generic.edit import ( FormView, ) from dashboard.mixins import DashboardView -from lot.models import Lot, LotTag, LotProperty +from lot.models import Lot, LotTag, LotProperty, LotSubscription from lot.forms import LotsForm class NewLotView(DashboardView, CreateView): @@ -83,7 +83,7 @@ class EditLotView(DashboardView, UpdateView): owner=self.request.user.institution, pk=pk, ) - # self.success_url = reverse_lazy('dashbiard:lot', args=[pk]) + # self.success_url = reverse_lazy('dashboard:lot', args=[pk]) kwargs = super().get_form_kwargs() return kwargs @@ -274,3 +274,58 @@ class DeleteLotPropertyView(DashboardView, DeleteView): # Redirect back to the original URL return redirect(self.success_url) + +class SubscriptLotView(DashboardView, CreateView): + template_name = "subscription.html" + title = _("Subscription") + breadcrumb = "Lot / Subscription" + model = LotSubscription + fields = ("user",) + + def get_context_data(self, **kwargs): + self.pk = self.kwargs.get('pk') + context = super().get_context_data(**kwargs) + self.lot = get_object_or_404( + Lot, owner=self.request.user.institution, + id=self.pk + ) + context.update({ + 'lot': self.lot, + }) + return context + + def form_valid(self, form): + # form.instance.user = self.request.user + form.instance.lot = self.lot + form.instance.type = LotAnnotation.Type.USER + response = super().form_valid(form) + return response + + def get_form_kwargs(self): + self.pk = self.kwargs.get('pk') + self.success_url = reverse_lazy('dashboard:lot', args=[self.pk]) + kwargs = super().get_form_kwargs() + return kwargs + + +class UnsubscriptLotView(DashboardView, CreateView): + template_name = "subscription.html" + title = _("Subscription") + breadcrumb = "Lot / Subscription" + model = LotSubscription + fields = ("key", "value") + + def form_valid(self, form): + form.instance.user = self.request.user + form.instance.lot = self.lot + self.success_url = reverse_lazy('lot', args=[self.lot.pk]) + response = super().form_valid(form) + return response + + def get_form_kwargs(self): + pk = self.kwargs.get('pk') + self.subscription = get_object_or_404(self.model, pk=pk, user=self.request.user) + self.lot = self.subscription.lot + self.success_url = reverse_lazy('lot:suscription', args=[pk]) + kwargs = super().get_form_kwargs() + return kwargs diff --git a/user/migrations/0002_user_is_circuit_manager_user_is_shop.py b/user/migrations/0002_user_is_circuit_manager_user_is_shop.py new file mode 100644 index 0000000..7d7861b --- /dev/null +++ b/user/migrations/0002_user_is_circuit_manager_user_is_shop.py @@ -0,0 +1,23 @@ +# Generated by Django 5.0.6 on 2025-02-07 13:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('user', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='is_circuit_manager', + field=models.BooleanField(default=False, verbose_name='is circuitmanager'), + ), + migrations.AddField( + model_name='user', + name='is_shop', + field=models.BooleanField(default=False, verbose_name='is shop'), + ), + ] diff --git a/user/models.py b/user/models.py index a289270..fc7b3e8 100644 --- a/user/models.py +++ b/user/models.py @@ -52,8 +52,7 @@ class UserManager(BaseUserManager): def create_superuser(self, email, institution, password=None): """ - Creates and saves a superuser with the given email, date of - birth and password. + Creates and saves a superuser with the given email and password. """ user = self.create_user( email, @@ -64,6 +63,32 @@ class UserManager(BaseUserManager): user.save(using=self._db) return user + def create_circuit_manager(self, email, institution, password=None): + """ + Creates and saves a circuit manager with the given email and password. + """ + user = self.create_user( + email, + institution=institution, + password=password, + ) + user.is_circuit_manager = True + user.save(using=self._db) + return user + + def create_shop(self, email, institution, password=None): + """ + Creates and saves a shop with the given email and password. + """ + user = self.create_user( + email, + institution=institution, + password=password, + ) + user.is_shop = True + user.save(using=self._db) + return user + class User(AbstractBaseUser): email = models.EmailField( @@ -73,6 +98,8 @@ class User(AbstractBaseUser): ) is_active = models.BooleanField(_("is active"), default=True) is_admin = models.BooleanField(_("is admin"), default=False) + is_circuit_manager = models.BooleanField(_("is circuitmanager"), default=False) + is_shop = models.BooleanField(_("is shop"), default=False) first_name = models.CharField(_("First name"), max_length=255, blank=True, null=True) last_name = models.CharField(_("Last name"), max_length=255, blank=True, null=True) accept_gdpr = models.BooleanField(default=False)