2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# Copyright 2023 © AE UTBM
|
|
|
|
# ae@utbm.fr / ae.info@utbm.fr
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# This file is part of the website of the UTBM Student Association (AE UTBM),
|
|
|
|
# https://ae.utbm.fr.
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2024-09-22 23:37:25 +00:00
|
|
|
# You can find the source code of the website at https://github.com/ae-utbm/sith
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
|
2024-09-23 08:25:27 +00:00
|
|
|
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
|
2023-04-04 16:39:45 +00:00
|
|
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2024-06-24 11:07:36 +00:00
|
|
|
import random
|
2015-12-15 16:50:50 +00:00
|
|
|
|
2024-06-24 11:07:36 +00:00
|
|
|
from django import forms
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import PermissionDenied, ValidationError
|
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.views.generic.edit import CreateView, FormView
|
2016-08-19 21:24:23 +00:00
|
|
|
|
2016-02-05 15:59:42 +00:00
|
|
|
from core.models import User
|
2024-07-21 14:16:40 +00:00
|
|
|
from core.views.forms import SelectDate, SelectDateTime
|
2024-10-20 20:36:55 +00:00
|
|
|
from core.views.widgets.select import AutoCompleteSelectUser
|
2024-06-24 11:07:36 +00:00
|
|
|
from subscription.models import Subscription
|
2016-02-05 15:59:42 +00:00
|
|
|
|
2017-06-06 21:27:57 +00:00
|
|
|
|
|
|
|
class SelectionDateForm(forms.Form):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2024-07-21 14:16:40 +00:00
|
|
|
self.fields["start_date"] = forms.DateTimeField(
|
|
|
|
label=_("Start date"), widget=SelectDateTime, required=True
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2024-07-21 14:16:40 +00:00
|
|
|
self.fields["end_date"] = forms.DateTimeField(
|
|
|
|
label=_("End date"), widget=SelectDateTime, required=True
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2017-06-06 21:27:57 +00:00
|
|
|
|
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
class SubscriptionForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Subscription
|
2018-10-04 19:29:19 +00:00
|
|
|
fields = ["member", "subscription_type", "payment_method", "location"]
|
2024-10-20 20:36:55 +00:00
|
|
|
widgets = {"member": AutoCompleteSelectUser}
|
2015-12-15 16:50:50 +00:00
|
|
|
|
2016-07-06 22:52:30 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2024-10-20 20:36:55 +00:00
|
|
|
self.fields["member"].required = False
|
2024-09-01 13:07:30 +00:00
|
|
|
self.fields |= forms.fields_for_model(
|
|
|
|
User,
|
|
|
|
fields=["first_name", "last_name", "email", "date_of_birth"],
|
|
|
|
widgets={"date_of_birth": SelectDate},
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2016-07-06 22:52:30 +00:00
|
|
|
|
2016-08-19 21:24:23 +00:00
|
|
|
def clean_member(self):
|
|
|
|
subscriber = self.cleaned_data.get("member")
|
|
|
|
if subscriber:
|
2016-12-10 00:58:30 +00:00
|
|
|
subscriber = User.objects.filter(id=subscriber.id).first()
|
2016-08-19 21:24:23 +00:00
|
|
|
return subscriber
|
|
|
|
|
2016-07-06 22:52:30 +00:00
|
|
|
def clean(self):
|
2024-06-27 12:46:43 +00:00
|
|
|
cleaned_data = super().clean()
|
2018-10-04 19:29:19 +00:00
|
|
|
if (
|
|
|
|
cleaned_data.get("member") is None
|
|
|
|
and "last_name" not in self.errors.as_data()
|
|
|
|
and "first_name" not in self.errors.as_data()
|
|
|
|
and "email" not in self.errors.as_data()
|
2018-10-15 22:44:32 +00:00
|
|
|
and "date_of_birth" not in self.errors.as_data()
|
2018-10-04 19:29:19 +00:00
|
|
|
):
|
2016-07-06 22:52:30 +00:00
|
|
|
self.errors.pop("member", None)
|
2016-09-02 10:29:50 +00:00
|
|
|
if self.errors:
|
|
|
|
return cleaned_data
|
2016-12-10 00:58:30 +00:00
|
|
|
if User.objects.filter(email=cleaned_data.get("email")).first() is not None:
|
2018-10-04 19:29:19 +00:00
|
|
|
self.add_error(
|
|
|
|
"email",
|
|
|
|
ValidationError(_("A user with that email address already exists")),
|
|
|
|
)
|
2016-07-06 22:52:30 +00:00
|
|
|
else:
|
2018-10-04 19:29:19 +00:00
|
|
|
u = User(
|
|
|
|
last_name=self.cleaned_data.get("last_name"),
|
|
|
|
first_name=self.cleaned_data.get("first_name"),
|
|
|
|
email=self.cleaned_data.get("email"),
|
2018-10-15 22:44:32 +00:00
|
|
|
date_of_birth=self.cleaned_data.get("date_of_birth"),
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2016-07-06 22:52:30 +00:00
|
|
|
u.generate_username()
|
2016-08-31 16:40:17 +00:00
|
|
|
u.set_password(str(random.randrange(1000000, 10000000)))
|
2016-07-06 22:52:30 +00:00
|
|
|
u.save()
|
|
|
|
cleaned_data["member"] = u
|
|
|
|
elif cleaned_data.get("member") is not None:
|
|
|
|
self.errors.pop("last_name", None)
|
|
|
|
self.errors.pop("first_name", None)
|
|
|
|
self.errors.pop("email", None)
|
2018-10-15 22:44:32 +00:00
|
|
|
self.errors.pop("date_of_birth", None)
|
2016-07-06 22:52:30 +00:00
|
|
|
if cleaned_data.get("member") is None:
|
2024-10-15 09:36:26 +00:00
|
|
|
# This should be handled here,
|
|
|
|
# but it is done in the Subscription model's clean method
|
2016-07-06 22:52:30 +00:00
|
|
|
# TODO investigate why!
|
2018-10-04 19:29:19 +00:00
|
|
|
raise ValidationError(
|
|
|
|
_(
|
2024-10-15 09:36:26 +00:00
|
|
|
"You must either choose an existing "
|
|
|
|
"user or create a new one properly"
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
|
|
|
)
|
2016-07-06 22:52:30 +00:00
|
|
|
return cleaned_data
|
|
|
|
|
2017-06-12 08:10:42 +00:00
|
|
|
|
2016-08-29 17:48:29 +00:00
|
|
|
class NewSubscription(CreateView):
|
2018-10-04 19:29:19 +00:00
|
|
|
template_name = "subscription/subscription.jinja"
|
2015-12-15 16:50:50 +00:00
|
|
|
form_class = SubscriptionForm
|
2016-03-22 10:42:00 +00:00
|
|
|
|
2016-08-29 17:48:29 +00:00
|
|
|
def dispatch(self, request, *arg, **kwargs):
|
2017-07-21 22:40:51 +00:00
|
|
|
if request.user.can_create_subscription:
|
2024-09-01 13:07:30 +00:00
|
|
|
return super().dispatch(request, *arg, **kwargs)
|
2016-08-29 17:48:29 +00:00
|
|
|
raise PermissionDenied
|
|
|
|
|
2016-03-22 10:42:00 +00:00
|
|
|
def get_initial(self):
|
2024-10-15 09:36:26 +00:00
|
|
|
if "member" in self.request.GET:
|
2018-10-04 19:29:19 +00:00
|
|
|
return {
|
|
|
|
"member": self.request.GET["member"],
|
|
|
|
"subscription_type": "deux-semestres",
|
|
|
|
}
|
|
|
|
return {"subscription_type": "deux-semestres"}
|
2016-07-05 23:32:34 +00:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
2016-07-28 10:41:29 +00:00
|
|
|
form.instance.subscription_start = Subscription.compute_start(
|
2018-10-04 19:29:19 +00:00
|
|
|
duration=settings.SITH_SUBSCRIPTIONS[form.instance.subscription_type][
|
|
|
|
"duration"
|
|
|
|
],
|
|
|
|
user=form.instance.member,
|
|
|
|
)
|
2016-07-05 23:32:34 +00:00
|
|
|
form.instance.subscription_end = Subscription.compute_end(
|
2018-10-04 19:29:19 +00:00
|
|
|
duration=settings.SITH_SUBSCRIPTIONS[form.instance.subscription_type][
|
|
|
|
"duration"
|
|
|
|
],
|
2017-09-04 09:25:28 +00:00
|
|
|
start=form.instance.subscription_start,
|
2018-10-04 19:29:19 +00:00
|
|
|
user=form.instance.member,
|
2017-06-12 08:10:42 +00:00
|
|
|
)
|
2024-06-27 12:46:43 +00:00
|
|
|
return super().form_valid(form)
|
2016-07-06 22:52:30 +00:00
|
|
|
|
2017-06-02 13:02:28 +00:00
|
|
|
|
2017-06-06 21:27:57 +00:00
|
|
|
class SubscriptionsStatsView(FormView):
|
2017-06-02 13:02:28 +00:00
|
|
|
template_name = "subscription/stats.jinja"
|
2017-06-06 21:27:57 +00:00
|
|
|
form_class = SelectionDateForm
|
2017-06-02 13:02:28 +00:00
|
|
|
|
|
|
|
def dispatch(self, request, *arg, **kwargs):
|
2017-06-06 21:27:57 +00:00
|
|
|
import datetime
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2017-06-06 21:27:57 +00:00
|
|
|
self.start_date = datetime.datetime.today()
|
|
|
|
self.end_date = self.start_date
|
2024-06-27 12:46:43 +00:00
|
|
|
res = super().dispatch(request, *arg, **kwargs)
|
2017-06-02 13:02:28 +00:00
|
|
|
if request.user.is_root or request.user.is_board_member:
|
|
|
|
return res
|
|
|
|
raise PermissionDenied
|
|
|
|
|
2017-06-06 21:27:57 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.form = self.get_form()
|
2018-10-04 19:29:19 +00:00
|
|
|
self.start_date = self.form["start_date"]
|
|
|
|
self.end_date = self.form["end_date"]
|
2024-06-27 12:46:43 +00:00
|
|
|
res = super().post(request, *args, **kwargs)
|
2017-06-06 21:27:57 +00:00
|
|
|
if request.user.is_root or request.user.is_board_member:
|
|
|
|
return res
|
|
|
|
raise PermissionDenied
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
init = {
|
2018-10-04 19:29:19 +00:00
|
|
|
"start_date": self.start_date.strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
"end_date": self.end_date.strftime("%Y-%m-%d %H:%M:%S"),
|
2017-06-06 21:27:57 +00:00
|
|
|
}
|
|
|
|
return init
|
|
|
|
|
2017-06-02 13:02:28 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
from subscription.models import Subscription
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2024-06-27 12:46:43 +00:00
|
|
|
kwargs = super().get_context_data(**kwargs)
|
2018-10-04 19:29:19 +00:00
|
|
|
kwargs["subscriptions_total"] = Subscription.objects.filter(
|
|
|
|
subscription_end__gte=self.end_date, subscription_start__lte=self.start_date
|
|
|
|
)
|
|
|
|
kwargs["subscriptions_types"] = settings.SITH_SUBSCRIPTIONS
|
|
|
|
kwargs["payment_types"] = settings.SITH_COUNTER_PAYMENT_METHOD
|
|
|
|
kwargs["locations"] = settings.SITH_SUBSCRIPTION_LOCATIONS
|
2017-06-02 13:02:28 +00:00
|
|
|
return kwargs
|
2017-06-06 21:27:57 +00:00
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
2018-10-04 19:29:19 +00:00
|
|
|
return reverse_lazy("subscriptions:stats")
|