2020-12-27 10:49:54 +00:00
|
|
|
# Copyright (C) 2020 by Animath
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-12-29 15:14:56 +00:00
|
|
|
|
2020-12-27 10:49:54 +00:00
|
|
|
from django import forms
|
2024-06-07 16:39:16 +00:00
|
|
|
from django.conf import settings
|
2020-12-27 10:49:54 +00:00
|
|
|
from django.contrib.auth.forms import UserCreationForm
|
|
|
|
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 _
|
|
|
|
|
2023-02-19 23:23:18 +00:00
|
|
|
from .models import CoachRegistration, ParticipantRegistration, Payment, \
|
2021-01-30 15:24:30 +00:00
|
|
|
StudentRegistration, VolunteerRegistration
|
2020-12-27 10:49:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SignupForm(UserCreationForm):
|
|
|
|
"""
|
|
|
|
Signup form to registers participants and coaches
|
|
|
|
They can choose the role at the registration.
|
|
|
|
"""
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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',)
|
|
|
|
|
|
|
|
|
2021-01-14 20:07:09 +00:00
|
|
|
class AddOrganizerForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
Signup form to registers volunteers
|
|
|
|
"""
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2023-02-19 23:23:18 +00:00
|
|
|
fields = ('first_name', 'last_name', 'email',)
|
2021-01-14 20:07:09 +00:00
|
|
|
|
|
|
|
|
2020-12-27 10:49:54 +00:00
|
|
|
class UserForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
Replace the default user form to require the first name, last name and the email.
|
|
|
|
The username is always equal to the email.
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
2023-04-10 07:56:16 +00:00
|
|
|
def clean_email(self):
|
|
|
|
"""
|
|
|
|
Ensure that the email address is unique.
|
|
|
|
"""
|
|
|
|
email = self.data["email"]
|
|
|
|
if User.objects.filter(email=email).exclude(pk=self.instance.pk).exists():
|
|
|
|
self.add_error("email", _("This email address is already used."))
|
|
|
|
return email
|
|
|
|
|
2020-12-27 10:49:54 +00:00
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ('first_name', 'last_name', 'email',)
|
|
|
|
|
|
|
|
|
|
|
|
class StudentRegistrationForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
A student can update its class, its school and if it allows Animath to contact him/her later.
|
|
|
|
"""
|
2023-02-19 18:18:25 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields["birth_date"].widget = forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d')
|
2024-06-07 16:39:16 +00:00
|
|
|
if not settings.SUGGEST_ANIMATH:
|
|
|
|
del self.fields["give_contact_to_animath"]
|
2023-02-19 18:18:25 +00:00
|
|
|
|
2020-12-27 10:49:54 +00:00
|
|
|
class Meta:
|
|
|
|
model = StudentRegistration
|
2024-06-07 12:52:09 +00:00
|
|
|
fields = ('team', 'student_class', 'birth_date', 'gender', 'address', 'zip_code', 'city', 'country',
|
|
|
|
'phone_number', 'school', 'health_issues', 'housing_constraints',
|
|
|
|
'responsible_name', 'responsible_phone', 'responsible_email', 'give_contact_to_animath',
|
|
|
|
'email_confirmed',)
|
2020-12-27 10:49:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PhotoAuthorizationForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
Form to send a photo authorization.
|
|
|
|
"""
|
|
|
|
def clean_photo_authorization(self):
|
|
|
|
if "photo_authorization" in self.files:
|
|
|
|
file = self.files["photo_authorization"]
|
|
|
|
if file.size > 2e6:
|
|
|
|
raise ValidationError(_("The uploaded file size must be under 2 Mo."))
|
|
|
|
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"]
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields["photo_authorization"].widget = FileInput()
|
|
|
|
|
|
|
|
class Meta:
|
2021-01-30 15:24:30 +00:00
|
|
|
model = ParticipantRegistration
|
2020-12-27 10:49:54 +00:00
|
|
|
fields = ('photo_authorization',)
|
|
|
|
|
|
|
|
|
2024-04-07 09:40:58 +00:00
|
|
|
class PhotoAuthorizationFinalForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
Form to send a photo authorization.
|
|
|
|
"""
|
|
|
|
def clean_photo_authorization_final(self):
|
|
|
|
if "photo_authorization_final" in self.files:
|
|
|
|
file = self.files["photo_authorization_final"]
|
|
|
|
if file.size > 2e6:
|
|
|
|
raise ValidationError(_("The uploaded file size must be under 2 Mo."))
|
|
|
|
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_final"]
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields["photo_authorization_final"].widget = FileInput()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ParticipantRegistration
|
|
|
|
fields = ('photo_authorization_final',)
|
|
|
|
|
|
|
|
|
2020-12-29 15:14:56 +00:00
|
|
|
class HealthSheetForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
Form to send a health sheet.
|
|
|
|
"""
|
|
|
|
def clean_health_sheet(self):
|
|
|
|
if "health_sheet" in self.files:
|
|
|
|
file = self.files["health_sheet"]
|
|
|
|
if file.size > 2e6:
|
|
|
|
raise ValidationError(_("The uploaded file size must be under 2 Mo."))
|
|
|
|
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["health_sheet"]
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields["health_sheet"].widget = FileInput()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = StudentRegistration
|
|
|
|
fields = ('health_sheet',)
|
|
|
|
|
|
|
|
|
2023-02-19 23:38:57 +00:00
|
|
|
class VaccineSheetForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
Form to send a vaccine sheet.
|
|
|
|
"""
|
|
|
|
def clean_vaccine_sheet(self):
|
|
|
|
if "vaccine_sheet" in self.files:
|
|
|
|
file = self.files["vaccine_sheet"]
|
|
|
|
if file.size > 2e6:
|
|
|
|
raise ValidationError(_("The uploaded file size must be under 2 Mo."))
|
|
|
|
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["vaccine_sheet"]
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields["vaccine_sheet"].widget = FileInput()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = StudentRegistration
|
|
|
|
fields = ('vaccine_sheet',)
|
|
|
|
|
|
|
|
|
2020-12-29 15:14:56 +00:00
|
|
|
class ParentalAuthorizationForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
Form to send a parental authorization.
|
|
|
|
"""
|
|
|
|
def clean_parental_authorization(self):
|
|
|
|
if "parental_authorization" in self.files:
|
|
|
|
file = self.files["parental_authorization"]
|
|
|
|
if file.size > 2e6:
|
|
|
|
raise ValidationError(_("The uploaded file size must be under 2 Mo."))
|
|
|
|
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["parental_authorization"]
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields["parental_authorization"].widget = FileInput()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = StudentRegistration
|
|
|
|
fields = ('parental_authorization',)
|
|
|
|
|
|
|
|
|
2024-04-07 09:40:58 +00:00
|
|
|
class ParentalAuthorizationFinalForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
Form to send a parental authorization.
|
|
|
|
"""
|
|
|
|
def clean_parental_authorization(self):
|
|
|
|
if "parental_authorization_final" in self.files:
|
|
|
|
file = self.files["parental_authorization_final"]
|
|
|
|
if file.size > 2e6:
|
|
|
|
raise ValidationError(_("The uploaded file size must be under 2 Mo."))
|
|
|
|
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["parental_authorization"]
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields["parental_authorization_final"].widget = FileInput()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = StudentRegistration
|
|
|
|
fields = ('parental_authorization_final',)
|
|
|
|
|
|
|
|
|
2020-12-27 10:49:54 +00:00
|
|
|
class CoachRegistrationForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
A coach can tell its professional activity.
|
|
|
|
"""
|
2024-06-07 16:39:16 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if not settings.SUGGEST_ANIMATH:
|
|
|
|
del self.fields["give_contact_to_animath"]
|
|
|
|
|
2020-12-27 10:49:54 +00:00
|
|
|
class Meta:
|
|
|
|
model = CoachRegistration
|
2024-06-07 12:52:09 +00:00
|
|
|
fields = ('team', 'gender', 'address', 'zip_code', 'city', 'country', 'phone_number',
|
2024-01-20 17:26:52 +00:00
|
|
|
'last_degree', 'professional_activity', 'health_issues', 'housing_constraints',
|
2024-01-16 21:26:00 +00:00
|
|
|
'give_contact_to_animath', 'email_confirmed',)
|
2020-12-27 10:49:54 +00:00
|
|
|
|
|
|
|
|
2020-12-28 11:59:12 +00:00
|
|
|
class VolunteerRegistrationForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
A volunteer can also tell its professional activity.
|
|
|
|
"""
|
2024-06-07 16:39:16 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if not settings.SUGGEST_ANIMATH:
|
|
|
|
del self.fields["give_contact_to_animath"]
|
|
|
|
|
2020-12-28 11:59:12 +00:00
|
|
|
class Meta:
|
|
|
|
model = VolunteerRegistration
|
2023-02-19 23:23:18 +00:00
|
|
|
fields = ('professional_activity', 'admin', 'give_contact_to_animath', 'email_confirmed',)
|
2021-01-18 19:02:49 +00:00
|
|
|
|
|
|
|
|
2024-02-18 21:36:01 +00:00
|
|
|
class PaymentAdminForm(forms.ModelForm):
|
2021-01-18 19:02:49 +00:00
|
|
|
"""
|
|
|
|
Indicate payment information
|
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields["valid"].widget.choices[0] = ('unknown', _("Pending"))
|
2024-02-24 22:05:21 +00:00
|
|
|
payment_type = kwargs.get('data', {}).get('type', "")
|
|
|
|
self.fields['receipt'].required = payment_type in ["scholarship", "bank_transfer"]
|
|
|
|
self.fields['additional_information'].required = payment_type in ["other"]
|
2021-01-18 19:02:49 +00:00
|
|
|
|
2024-02-12 21:30:27 +00:00
|
|
|
def clean_receipt(self):
|
|
|
|
if "receipt" in self.files:
|
|
|
|
file = self.files["receipt"]
|
2021-01-18 19:02:49 +00:00
|
|
|
if file.size > 2e6:
|
|
|
|
raise ValidationError(_("The uploaded file size must be under 2 Mo."))
|
|
|
|
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."))
|
2024-02-12 21:30:27 +00:00
|
|
|
return self.cleaned_data["receipt"]
|
2021-01-18 19:02:49 +00:00
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super().clean()
|
|
|
|
|
2024-02-12 21:30:27 +00:00
|
|
|
if "type" in cleaned_data and cleaned_data['type'] in ["scholarship", "bank_transfer"] \
|
2024-02-18 21:36:01 +00:00
|
|
|
and "receipt" not in self.files and not self.instance.receipt:
|
2024-02-12 21:30:27 +00:00
|
|
|
self.add_error("receipt", _("You must upload your receipt."))
|
2021-01-18 19:02:49 +00:00
|
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Payment
|
2024-02-12 21:30:27 +00:00
|
|
|
fields = ('type', 'receipt', 'additional_information', 'valid',)
|
2024-02-18 21:36:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PaymentForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
Indicate payment information
|
|
|
|
"""
|
|
|
|
def __init__(self, payment_type, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields['type'].widget = forms.HiddenInput(attrs={'value': payment_type})
|
|
|
|
self.fields['receipt'].required = payment_type in ["scholarship", "bank_transfer"]
|
|
|
|
self.fields['additional_information'].required = payment_type in ["other"]
|
|
|
|
|
|
|
|
def clean_receipt(self):
|
|
|
|
if "receipt" in self.files:
|
|
|
|
file = self.files["receipt"]
|
|
|
|
if file.size > 2e6:
|
|
|
|
raise ValidationError(_("The uploaded file size must be under 2 Mo."))
|
|
|
|
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["receipt"]
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super().clean()
|
|
|
|
|
|
|
|
if "type" in cleaned_data and cleaned_data['type'] in ["scholarship", "bank_transfer"] \
|
|
|
|
and "receipt" not in self.files and not self.instance.receipt:
|
|
|
|
self.add_error("receipt", _("You must upload your receipt."))
|
|
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Payment
|
|
|
|
fields = ('type', 'receipt', 'additional_information',)
|