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 math
|
2015-12-15 16:50:50 +00:00
|
|
|
from datetime import date, timedelta
|
2024-06-24 11:07:36 +00:00
|
|
|
|
|
|
|
from dateutil.relativedelta import relativedelta
|
2015-12-15 16:50:50 +00:00
|
|
|
from django.conf import settings
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.contrib.auth.forms import PasswordResetForm
|
2016-01-29 14:20:00 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.db import models
|
2019-10-06 11:28:56 +00:00
|
|
|
from django.urls import reverse
|
2024-10-02 22:21:16 +00:00
|
|
|
from django.utils.timezone import localdate
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2018-09-01 15:45:13 +00:00
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
from core.models import User
|
2017-06-12 20:53:25 +00:00
|
|
|
from core.utils import get_start_of_semester
|
2015-12-15 16:50:50 +00:00
|
|
|
|
2016-08-27 05:10:48 +00:00
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
def validate_type(value):
|
2024-10-15 09:36:26 +00:00
|
|
|
if value not in settings.SITH_SUBSCRIPTIONS:
|
2018-10-04 19:29:19 +00:00
|
|
|
raise ValidationError(_("Bad subscription type"))
|
2015-12-15 16:50:50 +00:00
|
|
|
|
2017-06-12 08:10:42 +00:00
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
def validate_payment(value):
|
2016-04-20 00:07:01 +00:00
|
|
|
if value not in settings.SITH_SUBSCRIPTION_PAYMENT_METHOD:
|
2018-10-04 19:29:19 +00:00
|
|
|
raise ValidationError(_("Bad payment method"))
|
2015-12-15 16:50:50 +00:00
|
|
|
|
2017-06-12 08:10:42 +00:00
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
class Subscription(models.Model):
|
2019-10-05 17:05:56 +00:00
|
|
|
member = models.ForeignKey(
|
|
|
|
User, related_name="subscriptions", on_delete=models.CASCADE
|
|
|
|
)
|
2018-10-04 19:29:19 +00:00
|
|
|
subscription_type = models.CharField(
|
|
|
|
_("subscription type"),
|
|
|
|
max_length=255,
|
|
|
|
choices=(
|
|
|
|
(k, v["name"]) for k, v in sorted(settings.SITH_SUBSCRIPTIONS.items())
|
|
|
|
),
|
|
|
|
)
|
|
|
|
subscription_start = models.DateField(_("subscription start"))
|
|
|
|
subscription_end = models.DateField(_("subscription end"))
|
|
|
|
payment_method = models.CharField(
|
|
|
|
_("payment method"),
|
|
|
|
max_length=255,
|
|
|
|
choices=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD,
|
|
|
|
)
|
|
|
|
location = models.CharField(
|
|
|
|
choices=settings.SITH_SUBSCRIPTION_LOCATIONS,
|
|
|
|
max_length=20,
|
|
|
|
verbose_name=_("location"),
|
|
|
|
)
|
2015-12-15 16:50:50 +00:00
|
|
|
|
2016-01-29 14:20:00 +00:00
|
|
|
class Meta:
|
2018-10-04 19:29:19 +00:00
|
|
|
ordering = ["subscription_start"]
|
2016-01-29 14:20:00 +00:00
|
|
|
|
2024-06-27 13:48:07 +00:00
|
|
|
def __str__(self):
|
|
|
|
if hasattr(self, "member") and self.member is not None:
|
|
|
|
return f"{self.member.username} - {self.pk}"
|
|
|
|
else:
|
|
|
|
return f"No user - {self.pk}"
|
2016-01-29 14:20:00 +00:00
|
|
|
|
2022-11-28 16:03:46 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
super().save()
|
2016-07-19 22:28:49 +00:00
|
|
|
from counter.models import Customer
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2022-11-23 11:23:17 +00:00
|
|
|
_, created = Customer.get_or_create(self.member)
|
|
|
|
if created:
|
2018-10-04 19:29:19 +00:00
|
|
|
form = PasswordResetForm({"email": self.member.email})
|
2016-08-31 16:40:17 +00:00
|
|
|
if form.is_valid():
|
2018-10-04 19:29:19 +00:00
|
|
|
form.save(
|
|
|
|
use_https=True,
|
|
|
|
email_template_name="core/new_user_email.jinja",
|
|
|
|
subject_template_name="core/new_user_email_subject.jinja",
|
|
|
|
from_email="ae@utbm.fr",
|
|
|
|
)
|
2016-08-10 14:23:12 +00:00
|
|
|
self.member.make_home()
|
2015-12-15 16:50:50 +00:00
|
|
|
|
2016-01-29 14:20:00 +00:00
|
|
|
def get_absolute_url(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
return reverse("core:user_edit", kwargs={"user_id": self.member.pk})
|
2016-01-29 14:20:00 +00:00
|
|
|
|
2024-06-27 13:48:07 +00:00
|
|
|
def clean(self):
|
2024-10-02 22:21:16 +00:00
|
|
|
today = localdate()
|
2024-11-18 23:41:49 +00:00
|
|
|
threshold = timedelta(weeks=settings.SITH_SUBSCRIPTION_END)
|
|
|
|
# a user may subscribe if :
|
|
|
|
# - he/she is not currently subscribed
|
|
|
|
# - its current subscription ends in less than a few weeks
|
|
|
|
overlapping_subscriptions = Subscription.objects.exclude(pk=self.pk).filter(
|
|
|
|
member=self.member,
|
|
|
|
subscription_start__lte=today,
|
|
|
|
subscription_end__gte=today + threshold,
|
2024-06-27 13:48:07 +00:00
|
|
|
)
|
2024-11-18 23:41:49 +00:00
|
|
|
if overlapping_subscriptions.exists():
|
|
|
|
raise ValidationError(
|
|
|
|
_("You can not subscribe many time for the same period")
|
|
|
|
)
|
2016-01-28 16:42:22 +00:00
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
@staticmethod
|
2024-10-15 09:36:26 +00:00
|
|
|
def compute_start(
|
2024-11-18 23:41:49 +00:00
|
|
|
d: date | None = None, duration: int | float = 1, user: User | None = None
|
2024-10-15 09:36:26 +00:00
|
|
|
) -> date:
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Computes the start date of the subscription.
|
|
|
|
|
|
|
|
The computation is done with respect to the given date (default is today)
|
2023-09-07 21:11:58 +00:00
|
|
|
and the start date given in settings.SITH_SEMESTER_START_AUTUMN.
|
2015-12-15 16:50:50 +00:00
|
|
|
It takes the nearest past start date.
|
2023-09-07 21:11:58 +00:00
|
|
|
Exemples: with SITH_SEMESTER_START_AUTUMN = (8, 15)
|
2015-12-15 16:50:50 +00:00
|
|
|
Today -> Start date
|
|
|
|
2015-03-17 -> 2015-02-15
|
2024-07-12 07:34:16 +00:00
|
|
|
2015-01-11 -> 2014-08-15.
|
2015-12-15 16:50:50 +00:00
|
|
|
"""
|
2017-08-29 13:06:52 +00:00
|
|
|
if not d:
|
|
|
|
d = date.today()
|
2017-09-04 09:25:28 +00:00
|
|
|
if user is not None and user.subscriptions.exists():
|
2018-04-16 12:46:13 +00:00
|
|
|
last = user.subscriptions.last()
|
|
|
|
if last.is_valid_now():
|
|
|
|
d = last.subscription_end
|
2017-06-12 08:10:42 +00:00
|
|
|
if duration <= 2: # Sliding subscriptions for 1 or 2 semesters
|
2016-07-28 10:41:29 +00:00
|
|
|
return d
|
2017-06-12 20:53:25 +00:00
|
|
|
return get_start_of_semester(d)
|
2015-12-15 16:50:50 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2024-10-15 09:36:26 +00:00
|
|
|
def compute_end(
|
2024-11-18 23:41:49 +00:00
|
|
|
duration: int | float, start: date | None = None, user: User | None = None
|
2024-10-15 09:36:26 +00:00
|
|
|
) -> date:
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Compute the end date of the subscription.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
duration:
|
|
|
|
the duration of the subscription, in semester
|
|
|
|
(for example, 2 => 2 semesters => 1 year)
|
|
|
|
start: The start date of the subscription
|
|
|
|
user: the user which is (or will be) subscribed
|
|
|
|
|
|
|
|
Exemples:
|
2015-12-15 16:50:50 +00:00
|
|
|
Start - Duration -> End date
|
|
|
|
2015-09-18 - 1 -> 2016-03-18
|
|
|
|
2015-09-18 - 2 -> 2016-09-18
|
|
|
|
2015-09-18 - 3 -> 2017-03-18
|
2024-07-12 07:34:16 +00:00
|
|
|
2015-09-18 - 4 -> 2017-09-18.
|
2015-12-15 16:50:50 +00:00
|
|
|
"""
|
|
|
|
if start is None:
|
2017-09-04 09:25:28 +00:00
|
|
|
start = Subscription.compute_start(duration=duration, user=user)
|
|
|
|
|
2018-10-04 19:29:19 +00:00
|
|
|
return start + relativedelta(
|
|
|
|
months=round(6 * duration),
|
|
|
|
days=math.ceil((6 * duration - round(6 * duration)) * 30),
|
|
|
|
)
|
|
|
|
|
2023-09-07 21:11:58 +00:00
|
|
|
def can_be_edited_by(self, user: User):
|
2023-05-02 10:36:59 +00:00
|
|
|
return user.is_board_member or user.is_root
|
2016-03-22 10:42:00 +00:00
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
def is_valid_now(self):
|
2023-03-04 14:01:08 +00:00
|
|
|
return self.subscription_start <= date.today() <= self.subscription_end
|
2024-11-18 23:41:49 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def semester_duration(self) -> float:
|
|
|
|
"""Duration of this subscription, in number of semester.
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
The `Subscription` object doesn't have to actually exist
|
|
|
|
in the database to access this property
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
```py
|
|
|
|
subscription = Subscription(subscription_type="deux-semestres")
|
|
|
|
assert subscription.semester_duration == 2.0
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
return settings.SITH_SUBSCRIPTIONS[self.subscription_type]["duration"]
|