1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-05 02:06:52 +00:00

Enable registration form

This commit is contained in:
Yohann D'ANELLO 2020-09-22 12:27:03 +02:00
parent 21a71f37f8
commit 0eef01ba9f
16 changed files with 207 additions and 18 deletions

View File

@ -0,0 +1 @@
default_app_config = 'participation.apps.ParticipationConfig'

View File

@ -0,0 +1 @@
urlpatterns = []

View File

@ -0,0 +1 @@
default_app_config = 'registration.apps.RegistrationConfig'

View File

@ -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")

View File

@ -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',)

View File

@ -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'),
),
]

View File

@ -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"),
)

View File

@ -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)

View File

@ -0,0 +1,9 @@
from django.urls import path
from .views import SignupView
app_name = "registration"
urlpatterns = [
path("signup", SignupView.as_view(), name="signup"),
]

View File

@ -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")

View File

@ -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

View File

@ -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 <yohann.danello@animath.fr>\n"
"Language-Team: LANGUAGE <LL@li.org>\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"

View File

@ -1,6 +1,4 @@
bcrypt
Django~=3.0
django-allauth
django-bootstrap-datepicker-plus
django-crispy-forms
django-extensions

View File

@ -86,6 +86,9 @@
</li>
{% endif %}
{% if not user.is_authenticated %}
<li class="nav-item active">
<a class="nav-link" href="{% url "registration:signup" %}"><i class="fas fa-user-plus"></i> {% trans "Register" %}</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="{% url "login" %}"><i class="fas fa-sign-in-alt"></i> {% trans "Log in" %}</a>
</li>

View File

@ -21,7 +21,7 @@
</div>
<div class="col-sm text-right">
<div class="btn-group-vertical">
<a class="btn btn-primary btn-lg" href="/inscription" role="button">Inscris-toi maintenant !</a>
<a class="btn btn-primary btn-lg" href="{% url "registration:signup" %}" role="button">Inscris-toi maintenant !</a>
<a class="btn btn-light btn-lg" href="{% url "login" %}" role="button">J'ai déjà un compte</a>
</div>
</div>

View File

@ -10,8 +10,35 @@
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<div id="student_registration_form">
{{ student_registration_form|crispy }}
</div>
<div id="coach_registration_form" class="d-none">
{{ coach_registration_form|crispy }}
</div>
<button class="btn btn-success" type="submit">
{% trans "Sign up" %}
</button>
</form>
{% endblock %}
{% block extrajavascript %}
<script>
$(document).ready(function() {
$("#id_role").change(function() {
let selected_role = $("#id_role :selected");
if (selected_role.val() === "participant") {
$("#student_registration_form").removeClass("d-none");
$("#coach_registration_form").addClass("d-none");
}
else {
$("#student_registration_form").addClass("d-none");
$("#coach_registration_form").removeClass("d-none");
}
});
$("#student_registration_form :input").removeAttr("required");
$("#coach_registration_form :input").removeAttr("required");
});
</script>
{% endblock %}