2025-03-03 12:29:27 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.core.exceptions import ValidationError
|
2024-07-09 15:31:24 +00:00
|
|
|
from django import forms
|
2025-03-03 12:29:27 +00:00
|
|
|
from user.models import User
|
2025-03-07 15:54:35 +00:00
|
|
|
from lot.models import Lot, LotSubscription
|
2024-07-09 15:31:24 +00:00
|
|
|
|
2024-07-30 20:54:00 +00:00
|
|
|
|
2024-07-09 15:31:24 +00:00
|
|
|
class LotsForm(forms.Form):
|
|
|
|
lots = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=Lot.objects.all(),
|
|
|
|
widget=forms.CheckboxSelectMultiple,
|
|
|
|
)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
self._lots = self.cleaned_data.get("lots")
|
|
|
|
return self._lots
|
2025-03-03 12:29:27 +00:00
|
|
|
|
2024-07-09 15:31:24 +00:00
|
|
|
def save(self, commit=True):
|
|
|
|
if not commit:
|
|
|
|
return
|
2024-07-19 15:40:01 +00:00
|
|
|
|
2024-07-09 15:31:24 +00:00
|
|
|
for dev in self.devices:
|
|
|
|
for lot in self._lots:
|
2024-07-19 15:40:01 +00:00
|
|
|
lot.add(dev.id)
|
2024-07-09 15:31:24 +00:00
|
|
|
return
|
2024-07-10 08:24:40 +00:00
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
for dev in self.devices:
|
|
|
|
for lot in self._lots:
|
2024-07-19 15:40:01 +00:00
|
|
|
lot.remove(dev.id)
|
2024-07-10 08:24:40 +00:00
|
|
|
return
|
2025-03-03 12:29:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LotSubscriptionForm(forms.Form):
|
|
|
|
user = forms.CharField()
|
|
|
|
type = forms.ChoiceField(
|
|
|
|
choices=[
|
|
|
|
("circuit_manager", _("Circuit Manager")),
|
|
|
|
("shop", _("Shop")),
|
|
|
|
],
|
|
|
|
widget=forms.Select(attrs={'class': 'form-control'}),
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.institution = kwargs.pop("institution")
|
2025-03-07 15:54:35 +00:00
|
|
|
self.lot_pk = kwargs.pop("lot_pk")
|
2025-03-03 12:29:27 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self):
|
2025-03-07 15:54:35 +00:00
|
|
|
self.form_user = self.cleaned_data.get("user")
|
2025-03-03 12:29:27 +00:00
|
|
|
self._type = self.cleaned_data.get("type")
|
|
|
|
|
2025-03-07 15:54:35 +00:00
|
|
|
self._user = User.objects.filter(email=self.form_user).first()
|
|
|
|
if self._user and self._user.institution != self.institution:
|
2025-03-03 12:29:27 +00:00
|
|
|
txt = _("This user is from another institution")
|
|
|
|
raise ValidationError(txt)
|
|
|
|
return
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
if not commit:
|
|
|
|
return
|
|
|
|
|
2025-03-07 15:54:35 +00:00
|
|
|
if not self._user:
|
|
|
|
self._user = User.objects.create_user(
|
|
|
|
self.form_user,
|
2025-03-03 12:29:27 +00:00
|
|
|
self.institution,
|
|
|
|
)
|
|
|
|
# TODO
|
|
|
|
# self.send_email()
|
|
|
|
|
|
|
|
if self._type == "circuit_manager":
|
2025-03-07 15:54:35 +00:00
|
|
|
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
|
|
|
|
)
|
2025-03-03 12:29:27 +00:00
|
|
|
|
|
|
|
if self._type == "shop":
|
2025-03-07 15:54:35 +00:00
|
|
|
slot = LotSubscription.objects.filter(
|
|
|
|
user=self._user,
|
|
|
|
lot_id=self.lot_pk,
|
|
|
|
is_shop=True
|
|
|
|
)
|
|
|
|
if slot:
|
|
|
|
return
|
2025-03-03 12:29:27 +00:00
|
|
|
|
2025-03-07 15:54:35 +00:00
|
|
|
LotSubscription.objects.create(
|
|
|
|
user=self._user,
|
|
|
|
lot_id=self.lot_pk,
|
|
|
|
is_shop=True
|
|
|
|
)
|
2025-03-03 12:29:27 +00:00
|
|
|
|
|
|
|
def remove(self):
|
2025-03-07 15:54:35 +00:00
|
|
|
if not self._user:
|
2025-03-03 12:29:27 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if self._type == "circuit_manager":
|
2025-03-07 15:54:35 +00:00
|
|
|
lot_subscription = LotSubscription.objects.filter(
|
|
|
|
user=self._user,
|
|
|
|
lot_id=self.lot_pk,
|
|
|
|
is_circuit_manager=True
|
|
|
|
)
|
2025-03-03 12:29:27 +00:00
|
|
|
|
2025-03-07 15:54:35 +00:00
|
|
|
elif self._type == "shop":
|
|
|
|
lot_subscription = LotSubscription.objects.filter(
|
|
|
|
user=self._user,
|
|
|
|
lot_id=self.lot_pk,
|
|
|
|
is_circuit_manager=True
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2025-03-03 12:29:27 +00:00
|
|
|
|
2025-03-07 15:54:35 +00:00
|
|
|
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()
|
2025-03-03 12:29:27 +00:00
|
|
|
|
|
|
|
return
|