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