mirror of
https://gitlab.com/animath/si/plateforme.git
synced 2025-06-21 14:38:24 +02:00
Register new organizers
This commit is contained in:
@ -45,6 +45,38 @@ class SignupForm(UserCreationForm):
|
||||
fields = ('first_name', 'last_name', 'email', 'password1', 'password2', 'role',)
|
||||
|
||||
|
||||
class AddOrganizerForm(forms.ModelForm):
|
||||
"""
|
||||
Signup form to registers volunteers
|
||||
"""
|
||||
type = forms.ChoiceField(
|
||||
label=lambda: _("role").capitalize(),
|
||||
choices=lambda: [
|
||||
("volunteer", _("volunteer").capitalize()),
|
||||
("admin", _("admin").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', 'type',)
|
||||
|
||||
|
||||
class UserForm(forms.ModelForm):
|
||||
"""
|
||||
Replace the default user form to require the first name, last name and the email.
|
||||
|
@ -18,10 +18,15 @@ class RegistrationTable(tables.Table):
|
||||
accessor="user__last_name",
|
||||
)
|
||||
|
||||
def order_type(self, queryset, desc):
|
||||
types = ["volunteerregistration__adminregistration", "volunteerregistration", "participantregistration"]
|
||||
return queryset.order_by(*(("" if desc else "-") + t for t in types)), True
|
||||
|
||||
class Meta:
|
||||
attrs = {
|
||||
'class': 'table table-condensed table-striped',
|
||||
}
|
||||
model = Registration
|
||||
fields = ('last_name', 'user__first_name', 'user__email', 'type',)
|
||||
order_by = ('volunteerregistration__adminregistration', 'volunteerregistration', 'last_name', 'first_name',)
|
||||
template_name = 'django_tables2/bootstrap4.html'
|
||||
|
43
apps/registration/templates/registration/add_organizer.html
Normal file
43
apps/registration/templates/registration/add_organizer.html
Normal file
@ -0,0 +1,43 @@
|
||||
<!-- templates/signup.html -->
|
||||
{% extends 'base.html' %}
|
||||
{% load crispy_forms_filters %}
|
||||
{% load i18n %}
|
||||
{% block title %}{% trans "Add organizer" %}{% endblock %}
|
||||
|
||||
{% block extracss %}
|
||||
{{ volunteer_registration_form.media }}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h2>{% trans "Add organizer" %}</h2>
|
||||
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
{{ form|crispy }}
|
||||
<div id="registration_form"></div>
|
||||
<button class="btn btn-success" type="submit">
|
||||
{% trans "Add organizer" %}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div id="volunteer_registration_form" class="d-none">
|
||||
{{ volunteer_registration_form|crispy }}
|
||||
</div>
|
||||
<div id="admin_registration_form" class="d-none">
|
||||
{{ admin_registration_form|crispy }}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
{% block extrajavascript %}
|
||||
<script>
|
||||
$("#id_role").change(function() {
|
||||
let selected_role = $("#id_role :selected");
|
||||
if (selected_role.val() === "volunteer") {
|
||||
$("#registration_form").html($("#volunteer_registration_form").html());
|
||||
}
|
||||
else {
|
||||
$("#registration_form").html($("#admin_registration_form").html());
|
||||
}
|
||||
}).change();
|
||||
</script>
|
||||
{% endblock %}
|
@ -1,7 +1,14 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% load django_tables2 %}
|
||||
{% load django_tables2 i18n %}
|
||||
|
||||
{% block content %}
|
||||
{% if user.registration.is_admin %}
|
||||
<button href="{% url "registration:add_organizer" %}" class="btn btn-block btn-secondary">
|
||||
<i class="fas fa-user-plus"></i> {% trans "Add organizer" %}
|
||||
</button>
|
||||
<hr>
|
||||
{% endif %}
|
||||
|
||||
{% render_table table %}
|
||||
{% endblock %}
|
||||
|
@ -3,8 +3,8 @@
|
||||
|
||||
from django.urls import path
|
||||
|
||||
from .views import MyAccountDetailView, ResetAdminView, SignupView, UserDetailView, UserImpersonateView, \
|
||||
UserListView, UserResendValidationEmailView, UserUpdateView, UserUploadHealthSheetView, \
|
||||
from .views import AddOrganizerView, MyAccountDetailView, ResetAdminView, SignupView, UserDetailView,\
|
||||
UserImpersonateView, UserListView, UserResendValidationEmailView, UserUpdateView, UserUploadHealthSheetView, \
|
||||
UserUploadParentalAuthorizationView, UserUploadPhotoAuthorizationView, UserValidateView, \
|
||||
UserValidationEmailSentView
|
||||
|
||||
@ -12,6 +12,7 @@ app_name = "registration"
|
||||
|
||||
urlpatterns = [
|
||||
path("signup/", SignupView.as_view(), name="signup"),
|
||||
path("add-organizer/", AddOrganizerView.as_view(), name="add_organizer"),
|
||||
path('validate_email/sent/', UserValidationEmailSentView.as_view(), name='email_validation_sent'),
|
||||
path('validate_email/resend/<int:pk>/', UserResendValidationEmailView.as_view(),
|
||||
name='email_validation_resend'),
|
||||
|
@ -21,8 +21,9 @@ from participation.models import Solution, Synthesis
|
||||
from tfjm.tokens import email_validation_token
|
||||
from tfjm.views import AdminMixin, UserMixin
|
||||
|
||||
from .forms import CoachRegistrationForm, HealthSheetForm, ParentalAuthorizationForm, PhotoAuthorizationForm,\
|
||||
SignupForm, StudentRegistrationForm, UserForm
|
||||
from .forms import AddOrganizerForm, AdminRegistrationForm, CoachRegistrationForm, HealthSheetForm, \
|
||||
ParentalAuthorizationForm, PhotoAuthorizationForm, SignupForm, StudentRegistrationForm, UserForm, \
|
||||
VolunteerRegistrationForm
|
||||
from .models import Registration, StudentRegistration, ParticipantRegistration
|
||||
from .tables import RegistrationTable
|
||||
|
||||
@ -73,6 +74,47 @@ class SignupView(CreateView):
|
||||
return reverse_lazy("registration:email_validation_sent")
|
||||
|
||||
|
||||
class AddOrganizerView(AdminMixin, CreateView):
|
||||
model = User
|
||||
form_class = AddOrganizerForm
|
||||
template_name = "registration/add_organizer.html"
|
||||
extra_context = dict(title=_("Add organizer"))
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data()
|
||||
|
||||
context["volunteer_registration_form"] = VolunteerRegistrationForm(self.request.POST or None)
|
||||
context["admin_registration_form"] = AdminRegistrationForm(self.request.POST or None)
|
||||
|
||||
del context["volunteer_registration_form"].fields["email_confirmed"]
|
||||
del context["admin_registration_form"].fields["email_confirmed"]
|
||||
|
||||
return context
|
||||
|
||||
@transaction.atomic
|
||||
def form_valid(self, form):
|
||||
role = form.cleaned_data["type"]
|
||||
if role == "admin":
|
||||
registration_form = AdminRegistrationForm(self.request.POST)
|
||||
else:
|
||||
registration_form = VolunteerRegistrationForm(self.request.POST)
|
||||
del registration_form.fields["email_confirmed"]
|
||||
|
||||
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("registration:email_validation_sent")
|
||||
|
||||
|
||||
|
||||
class UserValidateView(TemplateView):
|
||||
"""
|
||||
A view to validate the email address.
|
||||
|
Reference in New Issue
Block a user