From 0eef01ba9ffb9aaebcbed8a964ee56a507914e40 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 22 Sep 2020 12:27:03 +0200 Subject: [PATCH] Enable registration form --- apps/participation/__init__.py | 1 + apps/participation/urls.py | 1 + apps/registration/__init__.py | 1 + apps/registration/apps.py | 6 +++ apps/registration/forms.py | 44 +++++++++++++++++++ .../migrations/0002_auto_20200921_1948.py | 29 ++++++++++++ apps/registration/models.py | 26 +++++++---- apps/registration/signals.py | 10 +++++ apps/registration/urls.py | 9 ++++ apps/registration/views.py | 42 +++++++++++++++++- corres2math/urls.py | 2 + locale/fr/LC_MESSAGES/django.po | 20 ++++++--- requirements.txt | 2 - templates/base.html | 3 ++ templates/index.html | 2 +- templates/registration/signup.html | 27 ++++++++++++ 16 files changed, 207 insertions(+), 18 deletions(-) create mode 100644 apps/participation/urls.py create mode 100644 apps/registration/forms.py create mode 100644 apps/registration/migrations/0002_auto_20200921_1948.py create mode 100644 apps/registration/signals.py create mode 100644 apps/registration/urls.py diff --git a/apps/participation/__init__.py b/apps/participation/__init__.py index e69de29..e784201 100644 --- a/apps/participation/__init__.py +++ b/apps/participation/__init__.py @@ -0,0 +1 @@ +default_app_config = 'participation.apps.ParticipationConfig' diff --git a/apps/participation/urls.py b/apps/participation/urls.py new file mode 100644 index 0000000..637600f --- /dev/null +++ b/apps/participation/urls.py @@ -0,0 +1 @@ +urlpatterns = [] diff --git a/apps/registration/__init__.py b/apps/registration/__init__.py index e69de29..7e6a996 100644 --- a/apps/registration/__init__.py +++ b/apps/registration/__init__.py @@ -0,0 +1 @@ +default_app_config = 'registration.apps.RegistrationConfig' diff --git a/apps/registration/apps.py b/apps/registration/apps.py index 127d8e4..36bf8da 100644 --- a/apps/registration/apps.py +++ b/apps/registration/apps.py @@ -1,5 +1,11 @@ from django.apps import AppConfig +from django.db.models.signals import pre_save, post_save class RegistrationConfig(AppConfig): name = 'registration' + + def ready(self): + from registration.signals import set_username, create_admin_registration + pre_save.connect(set_username, "auth.User") + post_save.connect(create_admin_registration, "auth.User") diff --git a/apps/registration/forms.py b/apps/registration/forms.py new file mode 100644 index 0000000..a3e8047 --- /dev/null +++ b/apps/registration/forms.py @@ -0,0 +1,44 @@ +from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.models import User +from django import forms +from django.utils.translation import gettext_lazy as _ + +from registration.models import StudentRegistration, CoachRegistration, AdminRegistration + + +class SignupForm(UserCreationForm): + role = forms.ChoiceField( + label=lambda: _("role").capitalize(), + choices=lambda: [ + ("participant", _("participant").capitalize()), + ("coach", _("coach").capitalize()), + ], + ) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields["first_name"].required = True + self.fields["last_name"].required = True + self.fields["email"].required = True + + class Meta: + model = User + fields = ('first_name', 'last_name', 'email', 'password1', 'password2', 'role',) + + +class StudentRegistrationForm(forms.ModelForm): + class Meta: + model = StudentRegistration + fields = ('student_class', 'school', 'give_contact_to_animath',) + + +class CoachRegistrationForm(forms.ModelForm): + class Meta: + model = CoachRegistration + fields = ('professional_activity', 'give_contact_to_animath',) + + +class AdminRegistrationForm(forms.ModelForm): + class Meta: + model = AdminRegistration + fields = ('role', 'give_contact_to_animath',) diff --git a/apps/registration/migrations/0002_auto_20200921_1948.py b/apps/registration/migrations/0002_auto_20200921_1948.py new file mode 100644 index 0000000..759bbac --- /dev/null +++ b/apps/registration/migrations/0002_auto_20200921_1948.py @@ -0,0 +1,29 @@ +# Generated by Django 3.1.1 on 2020-09-21 17:48 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('participation', '0001_initial'), + ('registration', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='registration', + name='team', + ), + migrations.AddField( + model_name='coachregistration', + name='team', + field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='coachs', to='participation.team', verbose_name='team'), + ), + migrations.AddField( + model_name='studentregistration', + name='team', + field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='students', to='participation.team', verbose_name='team'), + ), + ] diff --git a/apps/registration/models.py b/apps/registration/models.py index 5ac3680..a8df9da 100644 --- a/apps/registration/models.py +++ b/apps/registration/models.py @@ -10,14 +10,6 @@ class Registration(PolymorphicModel): verbose_name=_("user"), ) - team = models.ForeignKey( - "participation.Team", - on_delete=models.PROTECT, - null=True, - default=None, - verbose_name=_("team"), - ) - give_contact_to_animath = models.BooleanField( default=False, verbose_name=_("Grant Animath to contact me in the future about other actions"), @@ -37,6 +29,15 @@ class Registration(PolymorphicModel): class StudentRegistration(Registration): + team = models.ForeignKey( + "participation.Team", + related_name="students", + on_delete=models.PROTECT, + null=True, + default=None, + verbose_name=_("team"), + ) + student_class = models.IntegerField( choices=[ (12, _("12th grade")), @@ -61,6 +62,15 @@ class StudentRegistration(Registration): class CoachRegistration(Registration): + team = models.ForeignKey( + "participation.Team", + related_name="coachs", + on_delete=models.PROTECT, + null=True, + default=None, + verbose_name=_("team"), + ) + professional_activity = models.TextField( verbose_name=_("professional activity"), ) diff --git a/apps/registration/signals.py b/apps/registration/signals.py new file mode 100644 index 0000000..65fd535 --- /dev/null +++ b/apps/registration/signals.py @@ -0,0 +1,10 @@ +from registration.models import AdminRegistration + + +def set_username(instance, **_): + instance.username = instance.email + + +def create_admin_registration(instance, **_): + if instance.is_superuser: + AdminRegistration.objects.get_or_create(user=instance) diff --git a/apps/registration/urls.py b/apps/registration/urls.py new file mode 100644 index 0000000..514ea77 --- /dev/null +++ b/apps/registration/urls.py @@ -0,0 +1,9 @@ +from django.urls import path + +from .views import SignupView + +app_name = "registration" + +urlpatterns = [ + path("signup", SignupView.as_view(), name="signup"), +] diff --git a/apps/registration/views.py b/apps/registration/views.py index 91ea44a..a0602e7 100644 --- a/apps/registration/views.py +++ b/apps/registration/views.py @@ -1,3 +1,41 @@ -from django.shortcuts import render +from django.contrib.auth.models import User +from django.db import transaction +from django.urls import reverse_lazy +from django.views.generic import CreateView -# Create your views here. +from .forms import SignupForm, StudentRegistrationForm, CoachRegistrationForm + + +class SignupView(CreateView): + model = User + form_class = SignupForm + template_name = "registration/signup.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data() + + context["student_registration_form"] = StudentRegistrationForm(self.request.POST or None) + context["coach_registration_form"] = CoachRegistrationForm(self.request.POST or None) + + return context + + @transaction.atomic + def form_valid(self, form): + role = form.cleaned_data["role"] + if role == "participant": + registration_form = StudentRegistrationForm(self.request.POST) + else: + registration_form = CoachRegistrationForm(self.request.POST) + + if not registration_form.is_valid(): + return self.form_invalid(form) + + ret = super().form_valid(form) + registration = registration_form.instance + registration.user = form.instance + registration.save() + + return ret + + def get_success_url(self): + return reverse_lazy("index") diff --git a/corres2math/urls.py b/corres2math/urls.py index ab9e636..fc82571 100644 --- a/corres2math/urls.py +++ b/corres2math/urls.py @@ -27,6 +27,8 @@ urlpatterns = [ path('accounts/', include('django.contrib.auth.urls')), path('api/', include('api.urls')), + path('participation/', include('participation.urls')), + path('registration/', include('registration.urls')), ] handler400 = bad_request diff --git a/locale/fr/LC_MESSAGES/django.po b/locale/fr/LC_MESSAGES/django.po index b59d4ca..4579841 100644 --- a/locale/fr/LC_MESSAGES/django.po +++ b/locale/fr/LC_MESSAGES/django.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: Corres2math\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-09-21 17:51+0200\n" +"POT-Creation-Date: 2020-09-21 18:11+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: Yohann D'ANELLO \n" "Language-Team: LANGUAGE \n" @@ -183,6 +183,20 @@ msgstr "vidéo" msgid "videos" msgstr "vidéos" +#: apps/registration/forms.py:9 +msgid "role" +msgstr "rôle" + +#: apps/registration/forms.py:11 +#, fuzzy +#| msgid "participation" +msgid "participant" +msgstr "participation" + +#: apps/registration/forms.py:12 apps/registration/models.py:70 +msgid "coach" +msgstr "encadrant" + #: apps/registration/models.py:23 msgid "Grant Animath to contact me in the future about other actions" msgstr "" @@ -237,10 +251,6 @@ msgstr "inscriptions d'élève" msgid "professional activity" msgstr "activité professionnelle" -#: apps/registration/models.py:70 -msgid "coach" -msgstr "encadrant" - #: apps/registration/models.py:73 msgid "coach registration" msgstr "inscription d'encadrant" diff --git a/requirements.txt b/requirements.txt index c3358c4..ce7709d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,4 @@ -bcrypt Django~=3.0 -django-allauth django-bootstrap-datepicker-plus django-crispy-forms django-extensions diff --git a/templates/base.html b/templates/base.html index eceb437..e1869a0 100644 --- a/templates/base.html +++ b/templates/base.html @@ -86,6 +86,9 @@ {% endif %} {% if not user.is_authenticated %} + diff --git a/templates/index.html b/templates/index.html index 2e299f2..a2a2549 100644 --- a/templates/index.html +++ b/templates/index.html @@ -21,7 +21,7 @@ diff --git a/templates/registration/signup.html b/templates/registration/signup.html index ed100d0..82a9431 100644 --- a/templates/registration/signup.html +++ b/templates/registration/signup.html @@ -10,8 +10,35 @@
{% csrf_token %} {{ form|crispy }} +
+ {{ student_registration_form|crispy }} +
+
+ {{ coach_registration_form|crispy }} +
{% endblock %} + +{% block extrajavascript %} + +{% endblock %}