subscription circularity manager and shop

This commit is contained in:
Cayo Puigdefabregas 2025-03-03 13:29:27 +01:00
parent 72e3d24c04
commit 9826d8cd2d
3 changed files with 88 additions and 42 deletions

View file

@ -1,4 +1,7 @@
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError
from django import forms from django import forms
from user.models import User
from lot.models import Lot from lot.models import Lot
@ -11,7 +14,7 @@ class LotsForm(forms.Form):
def clean(self): def clean(self):
self._lots = self.cleaned_data.get("lots") self._lots = self.cleaned_data.get("lots")
return self._lots return self._lots
def save(self, commit=True): def save(self, commit=True):
if not commit: if not commit:
return return
@ -26,3 +29,63 @@ class LotsForm(forms.Form):
for lot in self._lots: for lot in self._lots:
lot.remove(dev.id) lot.remove(dev.id)
return return
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

View file

@ -25,7 +25,7 @@
{% bootstrap_form form %} {% bootstrap_form form %}
<div class="form-actions-no-box"> <div class="form-actions-no-box">
<a class="btn btn-grey" href="{% url 'dashboard:lot' lot.id %}">{% translate "Cancel" %}</a> <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' %}" /> <input class="btn btn-green-admin" type="submit" name="submit" value="{{ action }}" />
</div> </div>
</form> </form>

View file

@ -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 from lot.forms import LotsForm, LotSubscriptionForm
class NewLotView(DashboardView, CreateView): class NewLotView(DashboardView, CreateView):
template_name = "new_lot.html" template_name = "new_lot.html"
@ -275,12 +275,13 @@ 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):
class SubscriptLotMixing(DashboardView, FormView):
template_name = "subscription.html" template_name = "subscription.html"
title = _("Subscription") title = _("Subscription")
breadcrumb = "Lot / Subscription" breadcrumb = "Lot / Subscription"
model = LotSubscription form_class = LotSubscriptionForm
fields = ("user",)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
self.pk = self.kwargs.get('pk') self.pk = self.kwargs.get('pk')
@ -291,54 +292,36 @@ class SubscriptLotView(DashboardView, CreateView):
) )
context.update({ context.update({
'lot': self.lot, 'lot': self.lot,
"action": _("Subscribe")
}) })
return context return context
def get_form(self):
form = super().get_form()
users_subscript = self.model.objects.filter(
lot=self.lot).values_list('user', flat=True)
queryset = form.fields["user"].queryset
form.fields["user"].queryset = queryset.filter(
institutions__institution=self.request.user.institution,
).exclude(id__in=users_subscript)
return form
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): def get_form_kwargs(self):
self.pk = self.kwargs.get('pk') self.pk = self.kwargs.get('pk')
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
return kwargs return kwargs
class UnsubscriptLotView(DashboardView, CreateView): class SubscriptLotView(SubscriptLotMixing):
template_name = "subscription.html"
title = _("Subscription")
breadcrumb = "Lot / Subscription"
model = LotSubscription
fields = ("key", "value")
def form_valid(self, form): def form_valid(self, form):
form.instance.user = self.request.user form.save()
form.instance.lot = self.lot
self.success_url = reverse_lazy('lot', args=[self.lot.pk])
response = super().form_valid(form) response = super().form_valid(form)
return response return response
def get_form_kwargs(self):
pk = self.kwargs.get('pk') class UnsubscriptLotView(SubscriptLotMixing):
self.subscription = get_object_or_404(self.model, pk=pk, user=self.request.user) title = _("Unsubscription")
self.lot = self.subscription.lot breadcrumb = "Lot / Unsubscription"
self.success_url = reverse_lazy('lot:suscription', args=[pk])
kwargs = super().get_form_kwargs() def get_context_data(self, **kwargs):
return kwargs context = super().get_context_data(**kwargs)
context["action"] = _("Unsubscribe")
return context
def form_valid(self, form):
form.remove()
response = super().form_valid(form)
return response