plateforme-corres2math/apps/registration/forms.py

105 lines
3.2 KiB
Python
Raw Normal View History

from django import forms
2020-09-22 10:27:03 +00:00
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
2020-09-24 20:40:10 +00:00
from django.core.exceptions import ValidationError
2020-09-27 10:36:37 +00:00
from django.forms import FileInput
2020-09-22 10:27:03 +00:00
from django.utils.translation import gettext_lazy as _
from .models import AdminRegistration, CoachRegistration, StudentRegistration
2020-09-22 10:27:03 +00:00
class SignupForm(UserCreationForm):
2020-10-30 18:46:46 +00:00
"""
Signup form to registers participants and coaches
They can choose the role at the registration.
"""
2020-09-22 10:27:03 +00:00
role = forms.ChoiceField(
label=lambda: _("role").capitalize(),
choices=lambda: [
("participant", _("participant").capitalize()),
("coach", _("coach").capitalize()),
],
)
def clean_email(self):
"""
Ensure that the email address is unique.
"""
email = self.data["email"]
if User.objects.filter(email=email).exists():
self.add_error("email", _("This email address is already used."))
return email
2020-09-22 10:27:03 +00:00
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',)
2020-09-24 16:39:55 +00:00
class UserForm(forms.ModelForm):
2020-10-30 18:46:46 +00:00
"""
Replace the default user form to require the first name, last name and the email.
The username is always equal to the email.
"""
2020-09-24 16:39:55 +00:00
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',)
2020-09-22 10:27:03 +00:00
class StudentRegistrationForm(forms.ModelForm):
2020-10-30 18:46:46 +00:00
"""
A student can update its class, its school and if it allows Animath to contact him/her later.
"""
2020-09-22 10:27:03 +00:00
class Meta:
model = StudentRegistration
fields = ('student_class', 'school', 'give_contact_to_animath',)
2020-09-24 20:40:10 +00:00
class PhotoAuthorizationForm(forms.ModelForm):
2020-10-30 18:46:46 +00:00
"""
Form to send a photo authorization.
"""
2020-09-24 20:40:10 +00:00
def clean_photo_authorization(self):
file = self.files["photo_authorization"]
if file.content_type not in ["application/pdf", "image/png", "image/jpeg"]:
raise ValidationError(_("The uploaded file must be a PDF, PNG of JPEG file."))
return self.cleaned_data["photo_authorization"]
2020-09-27 10:36:37 +00:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["photo_authorization"].widget = FileInput()
2020-09-24 20:40:10 +00:00
class Meta:
model = StudentRegistration
fields = ('photo_authorization',)
2020-09-22 10:27:03 +00:00
class CoachRegistrationForm(forms.ModelForm):
2020-10-30 18:46:46 +00:00
"""
A coach can tell its professional activity.
"""
2020-09-22 10:27:03 +00:00
class Meta:
model = CoachRegistration
fields = ('professional_activity', 'give_contact_to_animath',)
class AdminRegistrationForm(forms.ModelForm):
2020-10-30 18:46:46 +00:00
"""
Admins can tell everything they want.
"""
2020-09-22 10:27:03 +00:00
class Meta:
model = AdminRegistration
fields = ('role', 'give_contact_to_animath',)