1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-05 01:26:54 +00:00

Get ZIP archive of all authorizations of one team

This commit is contained in:
Yohann D'ANELLO 2020-09-27 18:40:45 +02:00
parent 84a7ec6fb5
commit 5c76e5f0db
3 changed files with 42 additions and 1 deletions

View File

@ -1,7 +1,7 @@
from django.urls import path
from .views import CreateTeamView, JoinTeamView, MyParticipationDetailView, MyTeamDetailView, ParticipationDetailView,\
TeamDetailView, TeamUpdateView, UploadVideoView
TeamAuthorizationsView, TeamDetailView, TeamUpdateView, UploadVideoView
app_name = "participation"
@ -12,6 +12,7 @@ urlpatterns = [
path("team/", MyTeamDetailView.as_view(), name="my_team_detail"),
path("team/<int:pk>/", TeamDetailView.as_view(), name="team_detail"),
path("team/<int:pk>/update/", TeamUpdateView.as_view(), name="update_team"),
path("team/<int:pk>/authorizations/", TeamAuthorizationsView.as_view(), name="team_authorizations"),
path("detail/", MyParticipationDetailView.as_view(), name="my_participation_detail"),
path("detail/<int:pk>/", ParticipationDetailView.as_view(), name="participation_detail"),
path("detail/upload-video/<int:pk>/", UploadVideoView.as_view(), name="upload_video"),

View File

@ -1,9 +1,14 @@
from io import BytesIO
from zipfile import ZipFile
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.http import FileResponse, HttpResponse
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, DetailView, FormView, RedirectView, UpdateView
from magic import Magic
from .forms import JoinTeamForm, ParticipationForm, TeamForm, UploadVideoForm
from .models import Participation, Team, Video
@ -117,6 +122,33 @@ class TeamUpdateView(LoginRequiredMixin, UpdateView):
return reverse_lazy("participation:team_detail", args=(self.object.pk,))
class TeamAuthorizationsView(LoginRequiredMixin, DetailView):
model = Team
def dispatch(self, request, *args, **kwargs):
user = request.user
if user.registration.is_admin or user.registration.participates and user.registration.team.pk == kwargs["pk"]:
return super().dispatch(request, *args, **kwargs)
raise PermissionDenied
def get(self, request, *args, **kwargs):
team = self.get_object()
output = BytesIO()
zf = ZipFile(output, "w")
for student in team.students.all():
magic = Magic(mime=True)
mime_type = magic.from_file("media/" + student.photo_authorization.name)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
zf.write("media/" + student.photo_authorization.name,
_("Photo authorization of {student}.{ext}").format(student=str(student), 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.write(output.getvalue())
return response
class MyParticipationDetailView(LoginRequiredMixin, RedirectView):
def get_redirect_url(self, *args, **kwargs):
user = self.request.user

View File

@ -170,6 +170,14 @@ class TestRegistration(TestCase):
args=(self.student.registration.photo_authorization.name.split('/')[-1],)))
self.assertEqual(response.status_code, 200)
from participation.models import Team
team = Team.objects.create(name="Test", trigram="TES")
self.student.registration.team = team
self.student.registration.save()
response = self.client.get(reverse("participation:team_authorizations", args=(team.pk,)))
self.assertEqual(response.status_code, 200)
self.assertEqual(response["content-type"], "application/zip")
self.student.registration.photo_authorization.delete()
def test_string_render(self):