1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-06-23 16:36:37 +02:00

Restructure add juree page

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello
2024-03-23 11:23:02 +01:00
parent 833b300fde
commit 81c2df7f10
7 changed files with 239 additions and 218 deletions

View File

@ -12,6 +12,7 @@ from zipfile import ZipFile
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.exceptions import PermissionDenied
from django.core.mail import send_mail
@ -776,55 +777,75 @@ class PoolDownloadView(VolunteerMixin, DetailView):
return response
class PoolAddJurysView(VolunteerMixin, FormView, DetailView):
class PoolJuryView(VolunteerMixin, FormView, DetailView):
"""
This view lets organizers set jurys for a pool, without multiplying clicks.
"""
model = Pool
form_class = AddJuryForm
template_name = 'participation/pool_add_jurys.html'
template_name = 'participation/pool_jury.html'
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return self.handle_no_permission()
if request.user.registration.is_admin or request.user.registration.is_volunteer \
and self.get_object().tournament in request.user.registration.organized_tournaments.all():
return super().dispatch(request, *args, **kwargs)
return self.handle_no_permission()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = _("Jurys of {pool}").format(pool=self.object)
context['title'] = _("Jury of pool {pool} for {tournament} with teams {teams}") \
.format(pool=f"{self.object.get_letter_display()}{self.object.round}",
tournament=self.object.tournament.name,
teams=", ".join(participation.team.trigram for participation in self.object.participations.all()))
return context
@transaction.atomic
def form_valid(self, form):
self.object = self.get_object()
# Save the user object first
form.save()
user = form.instance
# Create associated registration object to the new user
reg = VolunteerRegistration.objects.create(
user=user,
professional_activity="Juré⋅e du tournoi " + self.object.tournament.name,
)
if user.id:
# The user already exists, so we don't recreate it
user.refresh_from_db()
reg = user.registration
if reg.participates:
form.add_error(None, _("This user already exists, but is a participant."))
return self.form_invalid(form)
else:
# Save the user object first
form.save()
# Create associated registration object to the new user
reg = VolunteerRegistration.objects.create(
user=user,
professional_activity="Juré⋅e du tournoi " + self.object.tournament.name,
)
reg.send_email_validation_link()
# Generate new password for the user
password = get_random_string(16)
user.set_password(password)
user.save()
# Send welcome mail
subject = "[TFJM²] " + str(_("New TFJM² jury account"))
site = Site.objects.first()
message = render_to_string('registration/mails/add_organizer.txt', dict(user=user,
inviter=self.request.user,
password=password,
domain=site.domain))
html = render_to_string('registration/mails/add_organizer.html', dict(user=user,
inviter=self.request.user,
password=password,
domain=site.domain))
user.email_user(subject, message, html_message=html)
# Add the user in the jury
self.object.juries.add(reg)
self.object.save()
reg.send_email_validation_link()
# Generate new password for the user
password = get_random_string(16)
user.set_password(password)
user.save()
# Send welcome mail
subject = "[TFJM²] " + str(_("New TFJM² jury account"))
site = Site.objects.first()
message = render_to_string('registration/mails/add_organizer.txt', dict(user=user,
inviter=self.request.user,
password=password,
domain=site.domain))
html = render_to_string('registration/mails/add_organizer.html', dict(user=user,
inviter=self.request.user,
password=password,
domain=site.domain))
user.email_user(subject, message, html_message=html)
# Add notification
messages.success(self.request, _("The jury {name} has been successfully added!")
.format(name=f"{user.first_name} {user.last_name}"))
@ -837,7 +858,30 @@ class PoolAddJurysView(VolunteerMixin, FormView, DetailView):
return super().form_invalid(form)
def get_success_url(self):
return reverse_lazy('participation:pool_add_jurys', args=(self.kwargs['pk'],))
return reverse_lazy('participation:pool_jury', args=(self.kwargs['pk'],))
class PoolRemoveJuryView(VolunteerMixin, DetailView):
model = Pool
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return self.handle_no_permission()
if request.user.registration.is_admin or request.user.registration.is_volunteer \
and self.get_object().tournament in request.user.registration.organized_tournaments.all():
return super().dispatch(request, *args, **kwargs)
return self.handle_no_permission()
def get(self, request, *args, **kwargs):
pool = self.get_object()
if not pool.juries.filter(pk=kwargs['jury_id']).exists():
raise Http404
jury = pool.juries.get(pk=kwargs['jury_id'])
pool.juries.remove(jury)
pool.save()
messages.success(request, _("The jury {name} has been successfully removed!")
.format(name=f"{jury.user.first_name} {jury.user.last_name}"))
return redirect(reverse_lazy('participation:pool_jury', args=(pool.pk,)))
class PoolUploadNotesView(VolunteerMixin, FormView, DetailView):