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
|
2024-07-09 15:31:24 +00:00
|
|
|
from lot.models import Lot
|
|
|
|
|
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")
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
self._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:
|
|
|
|
txt = _("This user is from another institution")
|
|
|
|
raise ValidationError(txt)
|
|
|
|
return
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
if not commit:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not self.user:
|
|
|
|
self.user = User.objects.create_user(
|
|
|
|
self._user,
|
|
|
|
self.institution,
|
|
|
|
commit=False
|
|
|
|
)
|
|
|
|
# TODO
|
|
|
|
# self.send_email()
|
|
|
|
|
|
|
|
if self._type == "circuit_manager":
|
|
|
|
self.user.is_circuit_manager = True
|
|
|
|
|
|
|
|
if self._type == "shop":
|
|
|
|
self.user.is_shop = True
|
|
|
|
|
|
|
|
self.user.save()
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
if not self.user:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._type == "circuit_manager":
|
|
|
|
self.user.is_circuit_manager = False
|
|
|
|
|
|
|
|
if self._type == "shop":
|
|
|
|
self.user.is_shop = False
|
|
|
|
|
|
|
|
self.user.save()
|
|
|
|
|
|
|
|
return
|