From e51674e76cdf94381fcc535b77e8bf277479fab1 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 12 Jan 2021 17:56:40 +0100 Subject: [PATCH] Display solutions and syntheses --- apps/registration/views.py | 48 ++++++++++++++++++++++++++++++++++++++ tfjm/urls.py | 8 ++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/apps/registration/views.py b/apps/registration/views.py index cf2287a..b30437d 100644 --- a/apps/registration/views.py +++ b/apps/registration/views.py @@ -16,6 +16,8 @@ from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, DetailView, RedirectView, TemplateView, UpdateView, View from django_tables2 import SingleTableView from magic import Magic + +from participation.models import Solution, Synthesis from tfjm.tokens import email_validation_token from tfjm.views import AdminMixin, UserMixin @@ -341,6 +343,52 @@ class ParentalAuthorizationView(LoginRequiredMixin, View): return FileResponse(open(path, "rb"), content_type=mime_type, filename=true_file_name) +class SolutionView(LoginRequiredMixin, View): + """ + Display the sent solution. + """ + def get(self, request, *args, **kwargs): + filename = kwargs["filename"] + path = f"media/solutions/{filename}" + if not os.path.exists(path): + raise Http404 + solution = Solution.objects.get(file__endswith=filename) + # user = request.user + # if False: + # FIXME Check ACL + # raise PermissionDenied + # Guess mime type of the file + mime = Magic(mime=True) + mime_type = mime.from_file(path) + ext = mime_type.split("/")[1].replace("jpeg", "jpg") + # Replace file name + true_file_name = str(solution) + f".{ext}" + return FileResponse(open(path, "rb"), content_type=mime_type, filename=true_file_name) + + +class SynthesisView(LoginRequiredMixin, View): + """ + Display the sent synthesis. + """ + def get(self, request, *args, **kwargs): + filename = kwargs["filename"] + path = f"media/syhntheses/{filename}" + if not os.path.exists(path): + raise Http404 + solution = Synthesis.objects.get(file__endswith=filename) + # user = request.user + # if False: + # FIXME Check ACL + # raise PermissionDenied + # Guess mime type of the file + mime = Magic(mime=True) + mime_type = mime.from_file(path) + ext = mime_type.split("/")[1].replace("jpeg", "jpg") + # Replace file name + true_file_name = str(solution) + f".{ext}" + return FileResponse(open(path, "rb"), content_type=mime_type, filename=true_file_name) + + class UserImpersonateView(LoginRequiredMixin, RedirectView): """ An administrator can log in through this page as someone else, and act as this other person. diff --git a/tfjm/urls.py b/tfjm/urls.py index e17c56a..42aa854 100644 --- a/tfjm/urls.py +++ b/tfjm/urls.py @@ -21,7 +21,8 @@ from django.contrib import admin from django.urls import include, path from django.views.defaults import bad_request, page_not_found, permission_denied, server_error from django.views.generic import TemplateView -from registration.views import HealthSheetView, ParentalAuthorizationView, PhotoAuthorizationView +from registration.views import HealthSheetView, ParentalAuthorizationView, PhotoAuthorizationView, \ + SolutionView, SynthesisView from .views import AdminSearchView @@ -45,6 +46,11 @@ urlpatterns = [ path('media/authorization/parental//', ParentalAuthorizationView.as_view(), name='parental_authorization'), + path('media/solutions//', SolutionView.as_view(), + name='solution'), + path('media/syntheses//', SynthesisView.as_view(), + name='synthesis'), + path('', include('eastereggs.urls')), ]