1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-06-23 19:56:39 +02:00

Add ZIP archive for tournament authorizations

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello
2024-03-26 23:55:29 +01:00
parent a865361117
commit 4583cf46b1
5 changed files with 237 additions and 148 deletions

View File

@ -366,63 +366,91 @@ class MotivationLetterView(LoginRequiredMixin, View):
return FileResponse(open(path, "rb"), content_type=mime_type, filename=true_file_name)
class TeamAuthorizationsView(LoginRequiredMixin, DetailView):
class TeamAuthorizationsView(LoginRequiredMixin, View):
"""
Get as a ZIP archive all the authorizations that are sent
"""
model = Team
def dispatch(self, request, *args, **kwargs):
user = request.user
if not user.is_authenticated:
return super().handle_no_permission()
if 'team_id' in kwargs:
team = Team.objects.get(pk=kwargs["team_id"])
tournament = team.participation.tournament
else:
team = None
tournament = Tournament.objects.get(pk=kwargs["tournament_id"])
if user.registration.is_admin or user.registration.is_volunteer \
and (user.registration in self.get_object().participation.tournament.organizers
or self.get_object().participation.final
and user.registration in Tournament.final_tournament().organizers):
and (user.registration in tournament.organizers
or (team is not None and team.participation.final
and user.registration in Tournament.final_tournament().organizers)):
return super().dispatch(request, *args, **kwargs)
raise PermissionDenied
def get(self, request, *args, **kwargs):
team = self.get_object()
if 'team_id' in kwargs:
team = Team.objects.get(pk=kwargs["team_id"])
teams = [team]
filename = _("Authorizations of team {trigram}.zip").format(trigram=team.trigram)
else:
tournament = Tournament.objects.get(pk=kwargs["tournament_id"])
teams = [p.team for p in tournament.participations.filter(valid=True)]
filename = _("Authorizations of {tournament}.zip").format(tournament=tournament.name)
magic = Magic(mime=True)
output = BytesIO()
zf = ZipFile(output, "w")
for participant in team.participants.all():
if participant.photo_authorization:
mime_type = magic.from_file("media/" + participant.photo_authorization.name)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
zf.write("media/" + participant.photo_authorization.name,
_("Photo authorization of {participant}.{ext}").format(participant=str(participant), ext=ext))
for team in teams:
team_prefix = f"{team.trigram}/" if len(teams) > 1 else ""
if isinstance(participant, StudentRegistration) and participant.parental_authorization:
mime_type = magic.from_file("media/" + participant.parental_authorization.name)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
zf.write("media/" + participant.parental_authorization.name,
_("Parental authorization of {participant}.{ext}")
.format(participant=str(participant), ext=ext))
for participant in team.participants.all():
user_prefix = f"{team_prefix}{participant.user.first_name} {participant.user.last_name}/"
if isinstance(participant, StudentRegistration) and participant.health_sheet:
mime_type = magic.from_file("media/" + participant.health_sheet.name)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
zf.write("media/" + participant.health_sheet.name,
_("Health sheet of {participant}.{ext}").format(participant=str(participant), ext=ext))
if participant.photo_authorization \
and participant.photo_authorization.storage.exists(participant.photo_authorization.path):
mime_type = magic.from_file("media/" + participant.photo_authorization.name)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
zf.write("media/" + participant.photo_authorization.name,
user_prefix + _("Photo authorization of {participant}.{ext}")
.format(participant=str(participant), ext=ext))
if isinstance(participant, StudentRegistration) and participant.vaccine_sheet:
mime_type = magic.from_file("media/" + participant.vaccine_sheet.name)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
zf.write("media/" + participant.vaccine_sheet.name,
_("Vaccine sheet of {participant}.{ext}").format(participant=str(participant), ext=ext))
if participant.is_student and participant.parental_authorization \
and participant.parental_authorization.storage.exists(participant.parental_authorization.path):
mime_type = magic.from_file("media/" + participant.parental_authorization.name)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
zf.write("media/" + participant.parental_authorization.name,
user_prefix + _("Parental authorization of {participant}.{ext}")
.format(participant=str(participant), ext=ext))
if participant.is_student and participant.health_sheet \
and participant.health_sheet.storage.exists(participant.health_sheet.path):
mime_type = magic.from_file("media/" + participant.health_sheet.name)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
zf.write("media/" + participant.health_sheet.name,
user_prefix + _("Health sheet of {participant}.{ext}")
.format(participant=str(participant), ext=ext))
if participant.is_student and participant.vaccine_sheet \
and participant.vaccine_sheet.storage.exists(participant.vaccine_sheet.path):
mime_type = magic.from_file("media/" + participant.vaccine_sheet.name)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
zf.write("media/" + participant.vaccine_sheet.name,
user_prefix + _("Vaccine sheet of {participant}.{ext}")
.format(participant=str(participant), ext=ext))
if team.motivation_letter and team.motivation_letter.storage.exists(team.motivation_letter.path):
mime_type = magic.from_file("media/" + team.motivation_letter.name)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
zf.write("media/" + team.motivation_letter.name,
team_prefix + _("Motivation letter of {team}.{ext}")
.format(team=str(team), ext=ext))
if team.motivation_letter:
mime_type = magic.from_file("media/" + team.motivation_letter.name)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
zf.write("media/" + team.motivation_letter.name,
_("Motivation letter of {team}.{ext}").format(team=str(team), ext=ext))
zf.close()
response = HttpResponse(content_type="application/zip")
response["Content-Disposition"] = "attachment; filename=\"{filename}\"" \
.format(filename=_("Photo authorizations of team {trigram}.zip").format(trigram=team.trigram))
response["Content-Disposition"] = f"attachment; filename=\"{filename}\""
response.write(output.getvalue())
return response