diff --git a/apps/participation/templatetags/__init__.py b/apps/participation/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/participation/templatetags/calendar.py b/apps/participation/templatetags/calendar.py new file mode 100644 index 0000000..62cc0aa --- /dev/null +++ b/apps/participation/templatetags/calendar.py @@ -0,0 +1,12 @@ +from django import template + +from ..models import Phase + + +def current_phase(nb): + phase = Phase.current_phase() + return phase is not None and phase.phase_number == nb + + +register = template.Library() +register.filter("current_phase", current_phase) diff --git a/apps/registration/forms.py b/apps/registration/forms.py index 140002f..18d76eb 100644 --- a/apps/registration/forms.py +++ b/apps/registration/forms.py @@ -4,7 +4,6 @@ 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 @@ -29,11 +28,6 @@ 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 bc65bb7..e43aabc 100644 --- a/apps/registration/tests.py +++ b/apps/registration/tests.py @@ -84,26 +84,16 @@ class TestRegistration(TestCase): """ Ensure that the signup form is working successfully. """ - 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) + response = self.client.get(reverse("registration:signup")) + self.assertEqual(response.status_code, 403) Phase.objects.filter(phase_number__gte=2).update(start=timezone.now() + timedelta(days=1), end=timezone.now() + timedelta(days=2)) + response = self.client.get(reverse("registration:signup")) + self.assertEqual(response.status_code, 200) + # Incomplete form response = self.client.post(reverse("registration:signup"), data=dict( last_name="Toto", diff --git a/apps/registration/views.py b/apps/registration/views.py index 91d13b6..7fe6707 100644 --- a/apps/registration/views.py +++ b/apps/registration/views.py @@ -13,6 +13,7 @@ from django.utils.http import urlsafe_base64_decode from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, DetailView, RedirectView, TemplateView, UpdateView, View from magic import Magic +from participation.models import Phase from .forms import CoachRegistrationForm, PhotoAuthorizationForm, SignupForm, StudentRegistrationForm, UserForm from .models import StudentRegistration @@ -27,6 +28,15 @@ class SignupView(CreateView): template_name = "registration/signup.html" extra_context = dict(title=_("Sign up")) + def dispatch(self, request, *args, **kwargs): + """ + The signup view is available only during the first phase. + """ + current_phase = Phase.current_phase() + if not current_phase or current_phase.phase_number >= 2: + raise PermissionDenied(_("You can't register now.")) + return super().dispatch(request, *args, **kwargs) + def get_context_data(self, **kwargs): context = super().get_context_data() diff --git a/corres2math/templates/base.html b/corres2math/templates/base.html index 8cd6fc7..b5ece84 100644 --- a/corres2math/templates/base.html +++ b/corres2math/templates/base.html @@ -1,4 +1,4 @@ -{% load static i18n static %} +{% load static i18n static calendar %} {% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %} @@ -122,9 +122,11 @@ {% endif %} {% if not user.is_authenticated %} - + {% if 1|current_phase %} + + {% endif %}