from django import forms 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 _ from participation.models import Phase from .models import AdminRegistration, CoachRegistration, StudentRegistration 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 __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 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',) 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 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. """ class Meta: model = StudentRegistration fields = ('student_class', 'school', 'give_contact_to_animath',) class PhotoAuthorizationForm(forms.ModelForm): """ Form to send a photo authorization. """ 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"] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["photo_authorization"].widget = FileInput() class Meta: model = StudentRegistration fields = ('photo_authorization',) class CoachRegistrationForm(forms.ModelForm): """ A coach can tell its professional activity. """ class Meta: model = CoachRegistration fields = ('professional_activity', 'give_contact_to_animath',) class AdminRegistrationForm(forms.ModelForm): """ Admins can tell everything they want. """ class Meta: model = AdminRegistration fields = ('role', 'give_contact_to_animath',)