Fix immutable default variable in get_start_of_semester (#656)

Le serveur ne percevait pas le changement de semestre, parce
que la valeur par défaut passée à la fonction `get_start_of_semester()` était une fonction appelée une seule fois, lors du lancement du serveur. Bref, c'était ça : https://beta.ruff.rs/docs/rules/function-call-in-default-argument/

---------

Co-authored-by: imperosol <thgirod@hotmail.com>
This commit is contained in:
Julien Constant
2023-09-07 23:11:58 +02:00
committed by GitHub
parent 544b0248b2
commit 38295e591d
12 changed files with 194 additions and 68 deletions

View File

@ -15,20 +15,19 @@
#
import os
import subprocess
import re
# Image utils
from io import BytesIO
import subprocess
from datetime import date
from PIL import ExifTags
# Image utils
from io import BytesIO
from typing import Optional
import PIL
from django.conf import settings
from django.core.files.base import ContentFile
from PIL import ExifTags
from django.utils import timezone
def get_git_revision_short_hash() -> str:
@ -44,34 +43,54 @@ def get_git_revision_short_hash() -> str:
return ""
def get_start_of_semester(d=date.today()):
def get_start_of_semester(today: Optional[date] = None) -> date:
"""
This function computes the start date of the semester with respect to the given date (default is today),
and the start date given in settings.SITH_START_DATE.
It takes the nearest past start date.
Exemples: with SITH_START_DATE = (8, 15)
Today -> Start date
2015-03-17 -> 2015-02-15
2015-01-11 -> 2014-08-15
Return the date of the start of the semester of the given date.
If no date is given, return the start date of the current semester.
The current semester is computed as follows:
- If the date is between 15/08 and 31/12 => Autumn semester.
- If the date is between 01/01 and 15/02 => Autumn semester of the previous year.
- If the date is between 15/02 and 15/08 => Spring semester
:param today: the date to use to compute the semester. If None, use today's date.
:return: the date of the start of the semester
"""
today = d
year = today.year
start = date(year, settings.SITH_START_DATE[0], settings.SITH_START_DATE[1])
start2 = start.replace(month=(start.month + 6) % 12)
spring, autumn = min(start, start2), max(start, start2)
if today > autumn: # autumn semester
if today is None:
today = timezone.now().date()
autumn = date(today.year, *settings.SITH_SEMESTER_START_AUTUMN)
spring = date(today.year, *settings.SITH_SEMESTER_START_SPRING)
if today >= autumn: # between 15/08 (included) and 31/12 -> autumn semester
return autumn
if today > spring: # spring semester
if today >= spring: # between 15/02 (included) and 15/08 -> spring semester
return spring
return autumn.replace(year=year - 1) # autumn semester of last year
# between 01/01 and 15/02 -> autumn semester of the previous year
return autumn.replace(year=autumn.year - 1)
def get_semester(d=date.today()):
def get_semester_code(d: Optional[date] = None) -> str:
"""
Return the semester code of the given date.
If no date is given, return the semester code of the current semester.
The semester code is an upper letter (A for autumn, P for spring),
followed by the last two digits of the year.
For example, the autumn semester of 2018 is "A18".
:param d: the date to use to compute the semester. If None, use today's date.
:return: the semester code corresponding to the given date
"""
if d is None:
d = timezone.now().date()
start = get_start_of_semester(d)
if start.month <= 6:
return "P" + str(start.year)[-2:]
else:
if (start.month, start.day) == settings.SITH_SEMESTER_START_AUTUMN:
return "A" + str(start.year)[-2:]
return "P" + str(start.year)[-2:]
def file_exist(path):