base of subscription & unsubscription

This commit is contained in:
Cayo Puigdefabregas 2025-02-07 14:38:17 +01:00
parent 1e388b2c26
commit c44c185e0c
8 changed files with 190 additions and 4 deletions

View file

@ -18,6 +18,14 @@
<i class="bi bi-tag"></i> <i class="bi bi-tag"></i>
{% trans 'properties' %} {% trans 'properties' %}
</a> </a>
<a href="{% url 'lot:subscription' object.id %}" type="button" class="btn btn-green-admin">
<i class="bi bi-tag"></i>
{% trans 'Subscription' %}
</a>
<a href="{% url 'lot:unsubscription' object.id %}" type="button" class="btn btn-green-admin">
<i class="bi bi-tag"></i>
{% trans 'Unsubscription' %}
</a>
{% endif %} {% endif %}
</div> </div>
</div> </div>

View file

@ -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'),
),
]

View file

@ -60,3 +60,14 @@ class LotProperty(Property):
models.UniqueConstraint( models.UniqueConstraint(
fields=["key", "lot"], name="property_unique_type_key_lot") 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")
]

View file

@ -0,0 +1,32 @@
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<div class="row">
<div class="col">
<h3>{{ subtitle }}</h3>
</div>
</div>
{% load django_bootstrap5 %}
<form role="form" method="post">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger alert-icon alert-icon-border alert-dismissible" role="alert">
<div class="icon"><span class="mdi mdi-close-circle-o"></span></div>
<div class="message">
{% for field, error in form.errors.items %}
{{ error }}<br />
{% endfor %}
<button class="btn-close" type="button" data-dismiss="alert" aria-label="Close"></button>
</div>
</div>
{% endif %}
{% bootstrap_form form %}
<div class="form-actions-no-box">
<a class="btn btn-grey" href="{% url 'dashboard:lot' lot.id %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>
</form>
{% endblock %}

View file

@ -14,4 +14,6 @@ urlpatterns = [
path("<int:pk>/property/add", views.AddLotPropertyView.as_view(), name="add_property"), path("<int:pk>/property/add", views.AddLotPropertyView.as_view(), name="add_property"),
path("<int:pk>/property/update", views.UpdateLotPropertyView.as_view(), name="update_property"), path("<int:pk>/property/update", views.UpdateLotPropertyView.as_view(), name="update_property"),
path("<int:pk>/property/delete", views.DeleteLotPropertyView.as_view(), name="delete_property"), path("<int:pk>/property/delete", views.DeleteLotPropertyView.as_view(), name="delete_property"),
path("<int:pk>/subscription/", views.SubscriptLotView.as_view(), name="subscription"),
path("<int:pk>/unsubscription/", views.UnsubscriptLotView.as_view(), name="unsubscription"),
] ]

View file

@ -11,7 +11,7 @@ from django.views.generic.edit import (
FormView, FormView,
) )
from dashboard.mixins import DashboardView 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 from lot.forms import LotsForm
class NewLotView(DashboardView, CreateView): class NewLotView(DashboardView, CreateView):
@ -83,7 +83,7 @@ class EditLotView(DashboardView, UpdateView):
owner=self.request.user.institution, owner=self.request.user.institution,
pk=pk, 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() kwargs = super().get_form_kwargs()
return kwargs return kwargs
@ -274,3 +274,58 @@ class DeleteLotPropertyView(DashboardView, DeleteView):
# Redirect back to the original URL # Redirect back to the original URL
return redirect(self.success_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

View file

@ -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'),
),
]

View file

@ -52,8 +52,7 @@ class UserManager(BaseUserManager):
def create_superuser(self, email, institution, password=None): def create_superuser(self, email, institution, password=None):
""" """
Creates and saves a superuser with the given email, date of Creates and saves a superuser with the given email and password.
birth and password.
""" """
user = self.create_user( user = self.create_user(
email, email,
@ -64,6 +63,32 @@ class UserManager(BaseUserManager):
user.save(using=self._db) user.save(using=self._db)
return user 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): class User(AbstractBaseUser):
email = models.EmailField( email = models.EmailField(
@ -73,6 +98,8 @@ class User(AbstractBaseUser):
) )
is_active = models.BooleanField(_("is active"), default=True) is_active = models.BooleanField(_("is active"), default=True)
is_admin = models.BooleanField(_("is admin"), default=False) 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) 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) last_name = models.CharField(_("Last name"), max_length=255, blank=True, null=True)
accept_gdpr = models.BooleanField(default=False) accept_gdpr = models.BooleanField(default=False)