Fix tests & the method again

This commit is contained in:
Julien Constant 2023-09-06 19:43:49 +02:00
parent 0aa6ad841b
commit 471e179d24
5 changed files with 60 additions and 29 deletions

View File

@ -43,32 +43,36 @@ def get_git_revision_short_hash() -> str:
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
return "" return ""
def get_start_of_semester(today=date.today()) -> date:
def get_start_of_semester(today=date.today()):
""" """
This function computes the start date of the semester with respect to the given date (default is today), Determine in which semester the given date is and return the start date of
and the start date given in settings.SITH_START_DATE. the corresponding semester
It takes the nearest past start date.
Exemples: with SITH_START_DATE = (8, 15) Args:
Today -> Start date today (date, optional): The date to test. Defaults to date.today().
2015-03-17 -> 2015-02-15
2015-01-11 -> 2014-08-15 Returns:
date: The start date of the semester where the given date belongs
Context:
- if the date is between 15/08 and 31/12 -> autumn semester
- if the date is between 01/01 and 15/02 -> autumn semester where the year is the one before of the given date
- else between 15/02 and 15/08 -> spring semester
""" """
year = today.year autumn_month, autumn_day = settings.SITH_SEMESTER_START_AUTUMN
spring_month, spring_day = settings.SITH_SEMESTER_START_SPRING
# Get the start date of the autumn semester based on settings.SITH_START_DATE if today > date(today.year, autumn_month, autumn_day) and today < date(
autumn_start = date(year, settings.SITH_START_DATE[0], settings.SITH_START_DATE[1]) today.year + 1, 1, 1
# Get the start date of the spring semester, 6 months from autumn_start ):
spring_start = autumn_start.replace(month=(autumn_start.month + 6) % 12) return date(today.year, autumn_month, autumn_day)
# Ensure that spring_start represents the earlier semester start date if today >= date(today.year, 1, 1) and today < date(
if autumn_start > spring_start: today.year, spring_month, spring_day
spring_start, autumn_start = autumn_start, spring_start ):
return date(today.year - 1, autumn_month, autumn_day)
# Determine the appropriate semester start date based on the current date return date(today.year, spring_month, spring_day)
# If today is earlier than spring_start, return the autumn semester start date of the previous year
# Otherwise, return the spring semester start date of the current year
return autumn_start.replace(year=year - 1) if today < spring_start else spring_start
def get_semester(d=date.today()): def get_semester(d=date.today()):

View File

@ -13,6 +13,7 @@
# OR WITHIN THE LOCAL FILE "LICENSE" # OR WITHIN THE LOCAL FILE "LICENSE"
# #
# #
from datetime import date, timedelta
import json import json
import re import re
import string import string
@ -25,7 +26,9 @@ from django.utils.timezone import timedelta
from club.models import Club from club.models import Club
from core.models import User from core.models import User
from core.utils import get_start_of_semester
from counter.models import Counter, Customer, BillingInfo, Permanency, Selling, Product from counter.models import Counter, Customer, BillingInfo, Permanency, Selling, Product
from django.conf import settings
from sith.settings import SITH_MAIN_CLUB from sith.settings import SITH_MAIN_CLUB
@ -260,6 +263,29 @@ class CounterStatsTest(TestCase):
s.customer = root_customer s.customer = root_customer
s.save(allow_negative=True) s.save(allow_negative=True)
def test_get_start_of_semester(self):
autumn_month, autumn_day = settings.SITH_SEMESTER_START_AUTUMN
spring_month, spring_day = settings.SITH_SEMESTER_START_SPRING
t1_autumn_day = date(2025, 1, 1)
t2_autumn_day = date(2024, 9, 1)
t1_spring_day = date(2023, 3, 1)
t2_spring_day = date(2023, spring_month, spring_day)
self.assertTrue(
get_start_of_semester(t1_autumn_day) == date(2024, autumn_month, autumn_day)
)
self.assertTrue(
get_start_of_semester(t2_autumn_day) == date(2024, autumn_month, autumn_day)
)
self.assertTrue(
get_start_of_semester(t1_spring_day) == date(2023, spring_month, spring_day)
)
self.assertTrue(
get_start_of_semester(t2_spring_day) == date(2023, spring_month, spring_day)
)
def test_not_authenticated_user_fail(self): def test_not_authenticated_user_fail(self):
# Test with not login user # Test with not login user
response = self.client.get(reverse("counter:stats", args=[self.counter.id])) response = self.client.get(reverse("counter:stats", args=[self.counter.id]))

View File

@ -1354,7 +1354,7 @@ class CounterStatView(DetailView, CounterAdminMixin):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
"""Add stats to the context""" """Add stats to the context"""
counter = self.object counter: Counter = self.object
semester_start = get_start_of_semester() semester_start = get_start_of_semester()
office_hours = counter.get_top_barmen() office_hours = counter.get_top_barmen()
kwargs = super(CounterStatView, self).get_context_data(**kwargs) kwargs = super(CounterStatView, self).get_context_data(**kwargs)

View File

@ -327,7 +327,8 @@ SITH_CLUB_ROOT_PAGE = "clubs"
# Define the date in the year serving as reference for the subscriptions calendar # Define the date in the year serving as reference for the subscriptions calendar
# (month, day) # (month, day)
SITH_START_DATE = (8, 15) # 15th August SITH_SEMESTER_START_AUTUMN = (8, 15) # 15 August
SITH_SEMESTER_START_SPRING = (2, 15) # 15 February
# Used to determine the valid promos # Used to determine the valid promos
SITH_SCHOOL_START_YEAR = 1999 SITH_SCHOOL_START_YEAR = 1999

View File

@ -114,12 +114,12 @@ class Subscription(models.Model):
return "No user - " + str(self.pk) return "No user - " + str(self.pk)
@staticmethod @staticmethod
def compute_start(d=None, duration=1, user=None): def compute_start(d: date = None, duration: int = 1, user: User = None) -> date:
""" """
This function computes the start date of the subscription with respect to the given date (default is today), This function computes the start date of the subscription with respect to the given date (default is today),
and the start date given in settings.SITH_START_DATE. and the start date given in settings.SITH_SEMESTER_START_AUTUMN.
It takes the nearest past start date. It takes the nearest past start date.
Exemples: with SITH_START_DATE = (8, 15) Exemples: with SITH_SEMESTER_START_AUTUMN = (8, 15)
Today -> Start date Today -> Start date
2015-03-17 -> 2015-02-15 2015-03-17 -> 2015-02-15
2015-01-11 -> 2014-08-15 2015-01-11 -> 2014-08-15
@ -135,9 +135,9 @@ class Subscription(models.Model):
return get_start_of_semester(d) return get_start_of_semester(d)
@staticmethod @staticmethod
def compute_end(duration, start=None, user=None): def compute_end(duration: int, start: date = None, user: User = None) -> date:
""" """
This function compute the end date of the subscription given a start date and a duration in number of semestre This function compute the end date of the subscription given a start date and a duration in number of semester
Exemple: Exemple:
Start - Duration -> End date Start - Duration -> End date
2015-09-18 - 1 -> 2016-03-18 2015-09-18 - 1 -> 2016-03-18
@ -153,7 +153,7 @@ class Subscription(models.Model):
days=math.ceil((6 * duration - round(6 * duration)) * 30), days=math.ceil((6 * duration - round(6 * duration)) * 30),
) )
def can_be_edited_by(self, user): def can_be_edited_by(self, user: User):
return user.is_board_member or user.is_root return user.is_board_member or user.is_root
def is_valid_now(self): def is_valid_now(self):