From d3e18a8fbb897033e04133d296ea512b819ec67b Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sun, 15 Nov 2020 01:40:20 +0100 Subject: [PATCH] We can only register during the first phase --- apps/participation/models.py | 10 ++++------ apps/participation/tests.py | 2 +- apps/registration/forms.py | 6 ++++++ apps/registration/tests.py | 20 ++++++++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/apps/participation/models.py b/apps/participation/models.py index c19f9f8..55e808c 100644 --- a/apps/participation/models.py +++ b/apps/participation/models.py @@ -65,7 +65,7 @@ class Team(models.Model): "education", raise_error=False, ) - if self.pk and self.participation.valid: + if self.pk and self.participation.valid: # pragma: no cover get_sympa_client().subscribe(self.email, "equipes", False, f"Equipe {self.name}") get_sympa_client().subscribe(self.email, f"probleme-{self.participation.problem}", False, f"Equipe {self.name}") @@ -76,7 +76,7 @@ class Team(models.Model): """ Drop the Sympa mailing list, if the team is empty or if the trigram changed. """ - if self.participation.valid: + if self.participation.valid: # pragma: no cover get_sympa_client().unsubscribe(self.email, "equipes", False) get_sympa_client().unsubscribe(self.email, f"probleme-{self.participation.problem}", False) else: @@ -292,10 +292,8 @@ class Phase(models.Model): qs = Phase.objects.filter(start__lte=timezone.now(), end__gte=timezone.now()) if qs.exists(): return qs.get() - qs = Phase.objects.order_by("phase_number").all() - if timezone.now() < qs.first().start: - return qs.first() - return qs.last() + qs = Phase.objects.filter(start__lte=timezone.now()).order_by("phase_number").all() + return qs.last() if qs.exists() else None def __str__(self): return _("Phase {phase_number:d} starts on {start:%Y-%m-%d %H:%M} and ends on {end:%Y-%m-%d %H:%M}")\ diff --git a/apps/participation/tests.py b/apps/participation/tests.py index 4a6d119..ee4fd75 100644 --- a/apps/participation/tests.py +++ b/apps/participation/tests.py @@ -575,7 +575,7 @@ class TestStudentParticipation(TestCase): for i in range(1, 5): Phase.objects.filter(phase_number=i).update(start=timezone.now() + timedelta(days=2 * i), end=timezone.now() + timedelta(days=2 * i + 1)) - self.assertEqual(Phase.current_phase().phase_number, 1) + self.assertEqual(Phase.current_phase(), None) # We are after the end for i in range(1, 5): diff --git a/apps/registration/forms.py b/apps/registration/forms.py index 18d76eb..140002f 100644 --- a/apps/registration/forms.py +++ b/apps/registration/forms.py @@ -4,6 +4,7 @@ from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.forms import FileInput from django.utils.translation import gettext_lazy as _ +from participation.models import Phase from .models import AdminRegistration, CoachRegistration, StudentRegistration @@ -28,6 +29,11 @@ class SignupForm(UserCreationForm): self.fields["last_name"].required = True self.fields["email"].required = True + def clean(self): + if Phase.current_phase() is None or Phase.current_phase().phase_number >= 2: + self.add_error(None, _("You can't register now.")) + return super().clean() + class Meta: model = User fields = ('first_name', 'last_name', 'email', 'password1', 'password2', 'role',) diff --git a/apps/registration/tests.py b/apps/registration/tests.py index 4c464c9..bc65bb7 100644 --- a/apps/registration/tests.py +++ b/apps/registration/tests.py @@ -1,3 +1,4 @@ +from datetime import timedelta import os from corres2math.tokens import email_validation_token @@ -7,8 +8,10 @@ from django.contrib.sites.models import Site from django.core.management import call_command from django.test import TestCase from django.urls import reverse +from django.utils import timezone from django.utils.encoding import force_bytes from django.utils.http import urlsafe_base64_encode +from participation.models import Phase from .models import AdminRegistration, CoachRegistration, StudentRegistration @@ -84,6 +87,23 @@ class TestRegistration(TestCase): response = self.client.get(reverse("registration:signup")) self.assertEqual(response.status_code, 200) + # After first phase + response = self.client.post(reverse("registration:signup"), data=dict( + last_name="Toto", + first_name="Toto", + email="toto@example.com", + password1="azertyuiopazertyuiop", + password2="azertyuiopazertyuiop", + role="participant", + student_class=12, + school="God", + give_contact_to_animath=False, + )) + self.assertEqual(response.status_code, 200) + + Phase.objects.filter(phase_number__gte=2).update(start=timezone.now() + timedelta(days=1), + end=timezone.now() + timedelta(days=2)) + # Incomplete form response = self.client.post(reverse("registration:signup"), data=dict( last_name="Toto",