Aller au contenu

Views

PAYMENT_METHOD = [('CHECK', _('Check')), ('CASH', _('Cash')), ('CARD', _('Credit card'))] module-attribute

SelectionDateForm(*args, **kwargs)

Bases: Form

Source code in subscription/forms.py
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.fields["start_date"] = forms.DateTimeField(
        label=_("Start date"), widget=SelectDateTime, required=True
    )
    self.fields["end_date"] = forms.DateTimeField(
        label=_("End date"), widget=SelectDateTime, required=True
    )

SubscriptionExistingUserForm(*args, **kwargs)

Bases: SubscriptionForm

Form to add a subscription to an existing user.

Source code in subscription/forms.py
def __init__(self, *args, **kwargs):
    initial = kwargs.pop("initial", {})
    if "subscription_type" not in initial:
        initial["subscription_type"] = "deux-semestres"
    if "payment_method" not in initial:
        initial["payment_method"] = "CARD"
    super().__init__(*args, initial=initial, **kwargs)

SubscriptionNewUserForm(*args, **kwargs)

Bases: SubscriptionForm

Form to create subscriptions with the user they belong to.

Examples:

```py assert not User.objects.filter(email=request.POST.get("email")).exists() form = SubscriptionNewUserForm(request.POST) if form.is_valid(): form.save()

now the user exists and is subscribed

user = User.objects.get(email=request.POST.get("email")) assert user.is_subscribed

Source code in subscription/forms.py
def __init__(self, *args, **kwargs):
    initial = kwargs.pop("initial", {})
    if "subscription_type" not in initial:
        initial["subscription_type"] = "deux-semestres"
    if "payment_method" not in initial:
        initial["payment_method"] = "CARD"
    super().__init__(*args, initial=initial, **kwargs)

Subscription

Bases: Model

semester_duration: float property

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:

subscription = Subscription(subscription_type="deux-semestres")
assert subscription.semester_duration == 2.0

compute_start(d=None, duration=1, user=None) staticmethod

Computes the start date of the subscription.

The computation is done with respect to the given date (default is today) and the start date given in settings.SITH_SEMESTER_START_AUTUMN. It takes the nearest past start date. Exemples: with SITH_SEMESTER_START_AUTUMN = (8, 15) Today -> Start date 2015-03-17 -> 2015-02-15 2015-01-11 -> 2014-08-15.

Source code in subscription/models.py
@staticmethod
def compute_start(
    d: date | None = None, duration: int | float = 1, user: User | None = None
) -> date:
    """Computes the start date of the subscription.

    The computation is done with respect to the given date (default is today)
    and the start date given in settings.SITH_SEMESTER_START_AUTUMN.
    It takes the nearest past start date.
    Exemples: with SITH_SEMESTER_START_AUTUMN = (8, 15)
        Today      -> Start date
        2015-03-17 -> 2015-02-15
        2015-01-11 -> 2014-08-15.
    """
    if not d:
        d = date.today()
    if user is not None and user.subscriptions.exists():
        last = user.subscriptions.last()
        if last.is_valid_now():
            d = last.subscription_end
    if duration <= 2:  # Sliding subscriptions for 1 or 2 semesters
        return d
    return get_start_of_semester(d)

compute_end(duration, start=None, user=None) staticmethod

Compute the end date of the subscription.

Parameters:

Name Type Description Default
duration int | float

the duration of the subscription, in semester (for example, 2 => 2 semesters => 1 year)

required
start date | None

The start date of the subscription

None
user User | None

the user which is (or will be) subscribed

None
Exemples

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 2015-09-18 - 4 -> 2017-09-18.

Source code in subscription/models.py
@staticmethod
def compute_end(
    duration: int | float, start: date | None = None, user: User | None = None
) -> date:
    """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:
        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
        2015-09-18 - 4 -> 2017-09-18.
    """
    if start is None:
        start = Subscription.compute_start(duration=duration, user=user)

    return start + relativedelta(
        months=round(6 * duration),
        days=math.ceil((6 * duration - round(6 * duration)) * 30),
    )

CanCreateSubscriptionMixin

Bases: UserPassesTestMixin

NewSubscription

Bases: CanCreateSubscriptionMixin, TemplateView

CreateSubscriptionFragment

Bases: CanCreateSubscriptionMixin, CreateView

CreateSubscriptionExistingUserFragment

Bases: CreateSubscriptionFragment

Create a subscription for a user who already exists.

CreateSubscriptionNewUserFragment

Bases: CreateSubscriptionFragment

Create a subscription for a user who already exists.

SubscriptionCreatedFragment

Bases: CanCreateSubscriptionMixin, DetailView

SubscriptionsStatsView

Bases: FormView