add subscription and donor correctly
This commit is contained in:
parent
9826d8cd2d
commit
a6f0bcc35a
|
@ -5,7 +5,7 @@
|
|||
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<div class="col-3">
|
||||
<h3>{{ subtitle }}</h3>
|
||||
</div>
|
||||
<div class="col text-center">
|
||||
|
@ -18,14 +18,27 @@
|
|||
<i class="bi bi-tag"></i>
|
||||
{% trans 'properties' %}
|
||||
</a>
|
||||
{% if user %}
|
||||
<a href="{% url 'lot:subscription' object.id %}" type="button" class="btn btn-green-admin">
|
||||
<i class="bi bi-tag"></i>
|
||||
{% trans 'Subscription' %}
|
||||
</a>
|
||||
{% endif %}
|
||||
<a href="{% url 'lot:unsubscription' object.id %}" type="button" class="btn btn-green-admin">
|
||||
<i class="bi bi-tag"></i>
|
||||
{% trans 'Unsubscription' %}
|
||||
</a>
|
||||
{% if lot.donor %}
|
||||
<a href="{% url 'lot:del_donor' object.id %}" type="button" class="btn btn-green-admin">
|
||||
<i class="bi bi-tag"></i>
|
||||
{% trans 'Donor' %}
|
||||
</a>
|
||||
{% else %}
|
||||
<a href="{% url 'lot:add_donor' object.id %}" type="button" class="btn btn-green-admin">
|
||||
<i class="bi bi-tag"></i>
|
||||
{% trans 'Donor' %}
|
||||
</a>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
|
96
lot/forms.py
96
lot/forms.py
|
@ -2,7 +2,7 @@ from django.utils.translation import gettext_lazy as _
|
|||
from django.core.exceptions import ValidationError
|
||||
from django import forms
|
||||
from user.models import User
|
||||
from lot.models import Lot
|
||||
from lot.models import Lot, LotSubscription
|
||||
|
||||
|
||||
class LotsForm(forms.Form):
|
||||
|
@ -43,14 +43,15 @@ class LotSubscriptionForm(forms.Form):
|
|||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.institution = kwargs.pop("institution")
|
||||
self.lot_pk = kwargs.pop("lot_pk")
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def clean(self):
|
||||
self._user = self.cleaned_data.get("user")
|
||||
self.form_user = self.cleaned_data.get("user")
|
||||
self._type = self.cleaned_data.get("type")
|
||||
|
||||
self.user = User.objects.filter(email=self._user).first()
|
||||
if self.user and self.user.institution != self.institution:
|
||||
self._user = User.objects.filter(email=self.form_user).first()
|
||||
if self._user and self._user.institution != self.institution:
|
||||
txt = _("This user is from another institution")
|
||||
raise ValidationError(txt)
|
||||
return
|
||||
|
@ -59,33 +60,96 @@ class LotSubscriptionForm(forms.Form):
|
|||
if not commit:
|
||||
return
|
||||
|
||||
if not self.user:
|
||||
self.user = User.objects.create_user(
|
||||
self._user,
|
||||
if not self._user:
|
||||
self._user = User.objects.create_user(
|
||||
self.form_user,
|
||||
self.institution,
|
||||
commit=False
|
||||
)
|
||||
# TODO
|
||||
# self.send_email()
|
||||
|
||||
if self._type == "circuit_manager":
|
||||
self.user.is_circuit_manager = True
|
||||
slot = LotSubscription.objects.filter(
|
||||
user=self._user,
|
||||
lot_id=self.lot_pk,
|
||||
is_circuit_manager=True
|
||||
)
|
||||
if slot:
|
||||
return
|
||||
|
||||
LotSubscription.objects.create(
|
||||
user=self._user,
|
||||
lot_id=self.lot_pk,
|
||||
is_circuit_manager=True
|
||||
)
|
||||
|
||||
if self._type == "shop":
|
||||
self.user.is_shop = True
|
||||
slot = LotSubscription.objects.filter(
|
||||
user=self._user,
|
||||
lot_id=self.lot_pk,
|
||||
is_shop=True
|
||||
)
|
||||
if slot:
|
||||
return
|
||||
|
||||
self.user.save()
|
||||
LotSubscription.objects.create(
|
||||
user=self._user,
|
||||
lot_id=self.lot_pk,
|
||||
is_shop=True
|
||||
)
|
||||
|
||||
def remove(self):
|
||||
if not self.user:
|
||||
if not self._user:
|
||||
return
|
||||
|
||||
if self._type == "circuit_manager":
|
||||
self.user.is_circuit_manager = False
|
||||
lot_subscription = LotSubscription.objects.filter(
|
||||
user=self._user,
|
||||
lot_id=self.lot_pk,
|
||||
is_circuit_manager=True
|
||||
)
|
||||
|
||||
if self._type == "shop":
|
||||
self.user.is_shop = False
|
||||
elif self._type == "shop":
|
||||
lot_subscription = LotSubscription.objects.filter(
|
||||
user=self._user,
|
||||
lot_id=self.lot_pk,
|
||||
is_circuit_manager=True
|
||||
)
|
||||
|
||||
self.user.save()
|
||||
else:
|
||||
lot_subscription = None
|
||||
|
||||
if lot_subscription:
|
||||
for l in lot_subscription:
|
||||
l.delete()
|
||||
|
||||
return
|
||||
|
||||
|
||||
class AddDonorForm(forms.Form):
|
||||
user = forms.CharField()
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.institution = kwargs.pop("institution")
|
||||
self.lot = kwargs.pop("lot")
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def clean(self):
|
||||
self._user = self.cleaned_data.get("user")
|
||||
return
|
||||
|
||||
def save(self, commit=True):
|
||||
if not commit:
|
||||
return
|
||||
|
||||
self.lot.donor = self._user
|
||||
self.lot.save()
|
||||
# TODO
|
||||
# if self._user:
|
||||
# self.send_email()
|
||||
|
||||
def remove(self):
|
||||
self.lot.donor = ""
|
||||
self.lot.save()
|
||||
|
||||
return
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
# Generated by Django 5.0.6 on 2025-03-07 10:47
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("lot", "0009_lotsubscription_lotsubscription_unique_lot_user"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="lot",
|
||||
name="donor",
|
||||
field=models.CharField(blank=True, max_length=64, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="lotsubscription",
|
||||
name="is_circuit_manager",
|
||||
field=models.BooleanField(default=False, verbose_name="is circuit manager"),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="lotsubscription",
|
||||
name="is_shop",
|
||||
field=models.BooleanField(default=False, verbose_name="is shop"),
|
||||
),
|
||||
]
|
|
@ -1,7 +1,6 @@
|
|||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from utils.constants import (
|
||||
STR_SM_SIZE,
|
||||
STR_SIZE,
|
||||
STR_EXTEND_SIZE,
|
||||
)
|
||||
|
@ -36,6 +35,7 @@ class Lot(models.Model):
|
|||
owner = models.ForeignKey(Institution, on_delete=models.CASCADE)
|
||||
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
|
||||
type = models.ForeignKey(LotTag, on_delete=models.CASCADE)
|
||||
donor = models.CharField(max_length=STR_SIZE, blank=True, null=True)
|
||||
|
||||
def add(self, v):
|
||||
if DeviceLot.objects.filter(lot=self, device_id=v).exists():
|
||||
|
@ -65,6 +65,8 @@ class LotProperty(Property):
|
|||
class LotSubscription(models.Model):
|
||||
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)
|
||||
|
||||
class Meta:
|
||||
constraints = [
|
||||
|
|
|
@ -16,4 +16,6 @@ urlpatterns = [
|
|||
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"),
|
||||
path("<int:pk>/donor/add", views.AddDonorView.as_view(), name="add_donor"),
|
||||
path("<int:pk>/donor/del", views.DelDonorView.as_view(), name="del_donor"),
|
||||
]
|
||||
|
|
80
lot/views.py
80
lot/views.py
|
@ -12,7 +12,7 @@ from django.views.generic.edit import (
|
|||
)
|
||||
from dashboard.mixins import DashboardView
|
||||
from lot.models import Lot, LotTag, LotProperty, LotSubscription
|
||||
from lot.forms import LotsForm, LotSubscriptionForm
|
||||
from lot.forms import LotsForm, LotSubscriptionForm, AddDonorForm
|
||||
|
||||
class NewLotView(DashboardView, CreateView):
|
||||
template_name = "new_lot.html"
|
||||
|
@ -281,15 +281,13 @@ class SubscriptLotMixing(DashboardView, FormView):
|
|||
title = _("Subscription")
|
||||
breadcrumb = "Lot / Subscription"
|
||||
form_class = LotSubscriptionForm
|
||||
lot = None
|
||||
|
||||
|
||||
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
|
||||
)
|
||||
self.get_lot()
|
||||
context.update({
|
||||
'lot': self.lot,
|
||||
"action": _("Subscribe")
|
||||
|
@ -301,8 +299,16 @@ class SubscriptLotMixing(DashboardView, FormView):
|
|||
self.success_url = reverse_lazy('dashboard:lot', args=[self.pk])
|
||||
kwargs = super().get_form_kwargs()
|
||||
kwargs["institution"] = self.request.user.institution
|
||||
kwargs["lot_pk"] = self.pk
|
||||
return kwargs
|
||||
|
||||
def get_lot(self):
|
||||
self.lot = get_object_or_404(
|
||||
Lot,
|
||||
owner=self.request.user.institution,
|
||||
id=self.pk
|
||||
)
|
||||
|
||||
|
||||
class SubscriptLotView(SubscriptLotMixing):
|
||||
|
||||
|
@ -325,3 +331,67 @@ class UnsubscriptLotView(SubscriptLotMixing):
|
|||
form.remove()
|
||||
response = super().form_valid(form)
|
||||
return response
|
||||
|
||||
|
||||
class AddDonorView(DashboardView, FormView):
|
||||
template_name = "subscription.html"
|
||||
title = _("Add Donor")
|
||||
breadcrumb = "Lot / {}".format(title)
|
||||
form_class = AddDonorForm
|
||||
lot = None
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
self.pk = self.kwargs.get('pk')
|
||||
context = super().get_context_data(**kwargs)
|
||||
if not self.lot:
|
||||
self.get_lot()
|
||||
|
||||
context.update({
|
||||
'lot': self.lot,
|
||||
"action": _("Add")
|
||||
})
|
||||
return context
|
||||
|
||||
def form_valid(self, form):
|
||||
form.save()
|
||||
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])
|
||||
self.get_lot()
|
||||
kwargs = super().get_form_kwargs()
|
||||
kwargs["institution"] = self.request.user.institution
|
||||
kwargs["lot"] = self.lot
|
||||
donor = self.lot.donor or ""
|
||||
kwargs["initial"] = {"user": donor}
|
||||
return kwargs
|
||||
|
||||
def get_lot(self):
|
||||
self.lot = get_object_or_404(
|
||||
Lot,
|
||||
owner=self.request.user.institution,
|
||||
id=self.pk
|
||||
)
|
||||
|
||||
|
||||
class DelDonorView(AddDonorView):
|
||||
title = _("Remove Donor")
|
||||
breadcrumb = "Lot / {}".format(title)
|
||||
|
||||
def form_valid(self, form):
|
||||
response = super().form_valid(form)
|
||||
form.remove()
|
||||
return response
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super().get_form_kwargs()
|
||||
donor = kwargs["lot"].donor
|
||||
kwargs["initial"] = {"user": donor}
|
||||
return kwargs
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context["action"] = _("Remove")
|
||||
return context
|
||||
|
|
|
@ -11,16 +11,6 @@ class Migration(migrations.Migration):
|
|||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='user',
|
||||
name='is_circuit_manager',
|
||||
field=models.BooleanField(default=False, verbose_name='is circuit manager'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='user',
|
||||
name='is_shop',
|
||||
field=models.BooleanField(default=False, verbose_name='is shop'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='user',
|
||||
name='institution',
|
||||
|
|
|
@ -66,36 +66,6 @@ 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,
|
||||
password=password,
|
||||
commit=False
|
||||
)
|
||||
|
||||
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,
|
||||
password=password,
|
||||
commit=False
|
||||
)
|
||||
|
||||
user.is_shop = True
|
||||
user.save(using=self._db)
|
||||
return user
|
||||
|
||||
|
||||
class User(AbstractBaseUser):
|
||||
email = models.EmailField(
|
||||
|
@ -109,8 +79,6 @@ class User(AbstractBaseUser):
|
|||
accept_gdpr = models.BooleanField(default=False)
|
||||
is_active = models.BooleanField(_("is active"), default=True)
|
||||
is_admin = models.BooleanField(_("is admin"), default=False)
|
||||
is_circuit_manager = models.BooleanField(_("is circuit manager"), default=False)
|
||||
is_shop = models.BooleanField(_("is shop"), default=False)
|
||||
institution = models.ForeignKey(Institution, on_delete=models.CASCADE, related_name="users")
|
||||
|
||||
objects = UserManager()
|
||||
|
|
Loading…
Reference in a new issue