mirror of
https://github.com/ae-utbm/sith.git
synced 2026-01-11 13:30:08 +00:00
add more default user infos on first subscription
This commit is contained in:
@@ -54,6 +54,8 @@ from django.utils.translation import gettext_lazy as _
|
|||||||
from phonenumber_field.modelfields import PhoneNumberField
|
from phonenumber_field.modelfields import PhoneNumberField
|
||||||
from PIL import Image, ImageOps
|
from PIL import Image, ImageOps
|
||||||
|
|
||||||
|
from core.utils import get_last_promo
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from django.core.files.uploadedfile import UploadedFile
|
from django.core.files.uploadedfile import UploadedFile
|
||||||
from pydantic import NonNegativeInt
|
from pydantic import NonNegativeInt
|
||||||
@@ -86,12 +88,11 @@ class Group(AuthGroup):
|
|||||||
|
|
||||||
|
|
||||||
def validate_promo(value: int) -> None:
|
def validate_promo(value: int) -> None:
|
||||||
start_year = settings.SITH_SCHOOL_START_YEAR
|
last_promo = get_last_promo()
|
||||||
delta = (localdate() + timedelta(days=180)).year - start_year
|
if not 0 < value <= last_promo:
|
||||||
if value < 0 or delta < value:
|
|
||||||
raise ValidationError(
|
raise ValidationError(
|
||||||
_("%(value)s is not a valid promo (between 0 and %(end)s)"),
|
_("%(value)s is not a valid promo (between 0 and %(end)s)"),
|
||||||
params={"value": value, "end": delta},
|
params={"value": value, "end": last_promo},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ from django.contrib.auth.hashers import make_password
|
|||||||
from django.contrib.auth.models import Permission
|
from django.contrib.auth.models import Permission
|
||||||
from django.core import mail
|
from django.core import mail
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
from django.core.mail import EmailMessage
|
from django.core.mail import EmailMessage
|
||||||
from django.test import Client, RequestFactory, TestCase
|
from django.test import Client, RequestFactory, TestCase
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
@@ -35,8 +36,8 @@ from pytest_django.asserts import assertInHTML, assertRedirects
|
|||||||
from antispam.models import ToxicDomain
|
from antispam.models import ToxicDomain
|
||||||
from club.models import Club, Membership
|
from club.models import Club, Membership
|
||||||
from core.markdown import markdown
|
from core.markdown import markdown
|
||||||
from core.models import AnonymousUser, Group, Page, User
|
from core.models import AnonymousUser, Group, Page, User, validate_promo
|
||||||
from core.utils import get_semester_code, get_start_of_semester
|
from core.utils import get_last_promo, get_semester_code, get_start_of_semester
|
||||||
from core.views import AllowFragment
|
from core.views import AllowFragment
|
||||||
from counter.models import Customer
|
from counter.models import Customer
|
||||||
from sith import settings
|
from sith import settings
|
||||||
@@ -523,6 +524,21 @@ class TestDateUtils(TestCase):
|
|||||||
assert get_start_of_semester() == autumn_2023
|
assert get_start_of_semester() == autumn_2023
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("current_date", "promo"),
|
||||||
|
[("2020-10-01", 22), ("2025-03-01", 26), ("2000-11-11", 2)],
|
||||||
|
)
|
||||||
|
def test_get_last_promo(current_date: str, promo: int):
|
||||||
|
with freezegun.freeze_time(current_date):
|
||||||
|
assert get_last_promo() == promo
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("promo", [0, 24])
|
||||||
|
def test_promo_validator(promo: int):
|
||||||
|
with freezegun.freeze_time("2021-10-01"), pytest.raises(ValidationError):
|
||||||
|
validate_promo(promo)
|
||||||
|
|
||||||
|
|
||||||
def test_allow_fragment_mixin():
|
def test_allow_fragment_mixin():
|
||||||
class TestAllowFragmentView(AllowFragment, ContextMixin, View):
|
class TestAllowFragmentView(AllowFragment, ContextMixin, View):
|
||||||
def get(self, *args, **kwargs):
|
def get(self, *args, **kwargs):
|
||||||
|
|||||||
@@ -112,6 +112,16 @@ def get_semester_code(d: date | None = None) -> str:
|
|||||||
return "P" + str(start.year)[-2:]
|
return "P" + str(start.year)[-2:]
|
||||||
|
|
||||||
|
|
||||||
|
def get_last_promo() -> int:
|
||||||
|
"""Get the latest promo at the time the function is called.
|
||||||
|
|
||||||
|
For example, if called in october 2022 return 24,
|
||||||
|
if called in march 2026 return 27, etc.
|
||||||
|
"""
|
||||||
|
start_year = settings.SITH_SCHOOL_START_YEAR
|
||||||
|
return (localdate() + timedelta(days=180)).year - start_year
|
||||||
|
|
||||||
|
|
||||||
def is_image(file: UploadedFile):
|
def is_image(file: UploadedFile):
|
||||||
try:
|
try:
|
||||||
im = PIL.Image.open(file.file)
|
im = PIL.Image.open(file.file)
|
||||||
@@ -186,7 +196,7 @@ def exif_auto_rotate(image):
|
|||||||
|
|
||||||
def get_client_ip(request: HttpRequest) -> str | None:
|
def get_client_ip(request: HttpRequest) -> str | None:
|
||||||
headers = (
|
headers = (
|
||||||
"X_FORWARDED_FOR", # Common header for proixes
|
"X_FORWARDED_FOR", # Common header for proxies
|
||||||
"FORWARDED", # Standard header defined by RFC 7239.
|
"FORWARDED", # Standard header defined by RFC 7239.
|
||||||
"REMOTE_ADDR", # Default IP Address (direct connection)
|
"REMOTE_ADDR", # Default IP Address (direct connection)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from django.core.exceptions import ValidationError
|
|||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from core.models import User
|
from core.models import User
|
||||||
|
from core.utils import get_last_promo
|
||||||
from core.views.forms import SelectDate, SelectDateTime
|
from core.views.forms import SelectDate, SelectDateTime
|
||||||
from core.views.widgets.ajax_select import AutoCompleteSelectUser
|
from core.views.widgets.ajax_select import AutoCompleteSelectUser
|
||||||
from subscription.models import Subscription
|
from subscription.models import Subscription
|
||||||
@@ -125,8 +126,17 @@ class SubscriptionNewUserForm(SubscriptionForm):
|
|||||||
"deux-semestres",
|
"deux-semestres",
|
||||||
"cursus-tronc-commun",
|
"cursus-tronc-commun",
|
||||||
"cursus-branche",
|
"cursus-branche",
|
||||||
|
"cursus-alternant",
|
||||||
]:
|
]:
|
||||||
member.role = "STUDENT"
|
member.role = "STUDENT"
|
||||||
|
member.school = "UTBM"
|
||||||
|
if self.cleaned_data.get("subscription_type") == "cursus-tronc-commun":
|
||||||
|
member.promo = get_last_promo()
|
||||||
|
if self.cleaned_data.get("subscription_type") in [
|
||||||
|
"cursus-branche",
|
||||||
|
"cursus-alternant",
|
||||||
|
]:
|
||||||
|
member.promo = get_last_promo() - 2
|
||||||
member.generate_username()
|
member.generate_username()
|
||||||
member.set_password(secrets.token_urlsafe(nbytes=10))
|
member.set_password(secrets.token_urlsafe(nbytes=10))
|
||||||
self.instance.member = member
|
self.instance.member = member
|
||||||
|
|||||||
Reference in New Issue
Block a user