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 user.models import User
from lot.models import Lot
@ -26,3 +29,63 @@ class LotsForm(forms.Form):
for lot in self._lots:
lot.remove(dev.id)
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 %}
<div class="form-actions-no-box">
<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>
</form>

View file

@ -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
from lot.forms import LotsForm, LotSubscriptionForm
class NewLotView(DashboardView, CreateView):
template_name = "new_lot.html"
@ -275,12 +275,13 @@ class DeleteLotPropertyView(DashboardView, DeleteView):
# Redirect back to the original URL
return redirect(self.success_url)
class SubscriptLotView(DashboardView, CreateView):
class SubscriptLotMixing(DashboardView, FormView):
template_name = "subscription.html"
title = _("Subscription")
breadcrumb = "Lot / Subscription"
model = LotSubscription
fields = ("user",)
form_class = LotSubscriptionForm
def get_context_data(self, **kwargs):
self.pk = self.kwargs.get('pk')
@ -291,54 +292,36 @@ class SubscriptLotView(DashboardView, CreateView):
)
context.update({
'lot': self.lot,
"action": _("Subscribe")
})
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):
self.pk = self.kwargs.get('pk')
self.success_url = reverse_lazy('dashboard:lot', args=[self.pk])
kwargs = super().get_form_kwargs()
kwargs["institution"] = self.request.user.institution
return kwargs
class UnsubscriptLotView(DashboardView, CreateView):
template_name = "subscription.html"
title = _("Subscription")
breadcrumb = "Lot / Subscription"
model = LotSubscription
fields = ("key", "value")
class SubscriptLotView(SubscriptLotMixing):
def form_valid(self, form):
form.instance.user = self.request.user
form.instance.lot = self.lot
self.success_url = reverse_lazy('lot', args=[self.lot.pk])
form.save()
response = super().form_valid(form)
return response
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
self.subscription = get_object_or_404(self.model, pk=pk, user=self.request.user)
self.lot = self.subscription.lot
self.success_url = reverse_lazy('lot:suscription', args=[pk])
kwargs = super().get_form_kwargs()
return kwargs
class UnsubscriptLotView(SubscriptLotMixing):
title = _("Unsubscription")
breadcrumb = "Lot / Unsubscription"
def get_context_data(self, **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