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
|
|
|
from django.conf import settings
|
2024-11-18 23:41:49 +00:00
|
|
|
from django.contrib.auth.mixins import UserPassesTestMixin
|
2024-11-18 17:00:31 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2024-11-18 23:41:49 +00:00
|
|
|
from django.urls import reverse, reverse_lazy
|
|
|
|
from django.utils.timezone import localdate
|
|
|
|
from django.views.generic import CreateView, DetailView, TemplateView
|
|
|
|
from django.views.generic.edit import FormView
|
|
|
|
|
|
|
|
from subscription.forms import (
|
|
|
|
SelectionDateForm,
|
|
|
|
SubscriptionExistingUserForm,
|
|
|
|
SubscriptionNewUserForm,
|
|
|
|
)
|
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
|
|
|
|
2024-11-18 23:41:49 +00:00
|
|
|
class CanCreateSubscriptionMixin(UserPassesTestMixin):
|
|
|
|
def test_func(self):
|
|
|
|
return self.request.user.can_create_subscription
|
|
|
|
|
|
|
|
|
|
|
|
class NewSubscription(CanCreateSubscriptionMixin, TemplateView):
|
2018-10-04 19:29:19 +00:00
|
|
|
template_name = "subscription/subscription.jinja"
|
2016-03-22 10:42:00 +00:00
|
|
|
|
2024-11-18 23:41:49 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
return super().get_context_data(**kwargs) | {
|
|
|
|
"existing_user_form": SubscriptionExistingUserForm(),
|
|
|
|
"new_user_form": SubscriptionNewUserForm(),
|
|
|
|
"existing_user_post_url": reverse("subscription:fragment-existing-user"),
|
|
|
|
"new_user_post_url": reverse("subscription:fragment-new-user"),
|
|
|
|
}
|
2016-08-29 17:48:29 +00:00
|
|
|
|
2024-11-18 23:41:49 +00:00
|
|
|
|
|
|
|
class CreateSubscriptionFragment(CanCreateSubscriptionMixin, CreateView):
|
|
|
|
template_name = "subscription/fragments/creation_form.jinja"
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse(
|
|
|
|
"subscription:creation-success", kwargs={"subscription_id": self.object.id}
|
2017-06-12 08:10:42 +00:00
|
|
|
)
|
2024-11-18 23:41:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CreateSubscriptionExistingUserFragment(CreateSubscriptionFragment):
|
|
|
|
"""Create a subscription for a user who already exists."""
|
|
|
|
|
|
|
|
form_class = SubscriptionExistingUserForm
|
|
|
|
extra_context = {"post_url": reverse_lazy("subscription:fragment-existing-user")}
|
|
|
|
|
|
|
|
|
|
|
|
class CreateSubscriptionNewUserFragment(CreateSubscriptionFragment):
|
|
|
|
"""Create a subscription for a user who already exists."""
|
|
|
|
|
|
|
|
form_class = SubscriptionNewUserForm
|
|
|
|
extra_context = {"post_url": reverse_lazy("subscription:fragment-new-user")}
|
|
|
|
|
|
|
|
|
|
|
|
class SubscriptionCreatedFragment(CanCreateSubscriptionMixin, DetailView):
|
|
|
|
template_name = "subscription/fragments/creation_success.jinja"
|
|
|
|
model = Subscription
|
|
|
|
pk_url_kwarg = "subscription_id"
|
|
|
|
context_object_name = "subscription"
|
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
|
2024-11-18 23:41:49 +00:00
|
|
|
success_url = reverse_lazy("subscriptions:stats")
|
2017-06-02 13:02:28 +00:00
|
|
|
|
|
|
|
def dispatch(self, request, *arg, **kwargs):
|
2024-11-18 23:41:49 +00:00
|
|
|
self.start_date = localdate()
|
2017-06-06 21:27:57 +00:00
|
|
|
self.end_date = self.start_date
|
2017-06-02 13:02:28 +00:00
|
|
|
if request.user.is_root or request.user.is_board_member:
|
2024-11-18 23:41:49 +00:00
|
|
|
return super().dispatch(request, *arg, **kwargs)
|
2017-06-02 13:02:28 +00:00
|
|
|
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-11-18 23:41:49 +00:00
|
|
|
return super().post(request, *args, **kwargs)
|
2017-06-06 21:27:57 +00:00
|
|
|
|
|
|
|
def get_initial(self):
|
2024-11-18 23:41:49 +00:00
|
|
|
return {
|
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
|
|
|
}
|
|
|
|
|
2017-06-02 13:02:28 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
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
|