mirror of
https://gitlab.com/animath/si/plateforme-corres2math.git
synced 2025-06-21 20:38:22 +02:00
Better photo authorization upload
This commit is contained in:
@ -2,6 +2,7 @@ from django import forms
|
||||
from django.contrib.auth.forms import UserCreationForm
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.forms import FileInput
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from .models import AdminRegistration, CoachRegistration, StudentRegistration
|
||||
@ -52,6 +53,10 @@ class PhotoAuthorizationForm(forms.ModelForm):
|
||||
raise ValidationError(_("The uploaded file must be a PDF, PNG of JPEG file."))
|
||||
return self.cleaned_data["photo_authorization"]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields["photo_authorization"].widget = FileInput()
|
||||
|
||||
class Meta:
|
||||
model = StudentRegistration
|
||||
fields = ('photo_authorization',)
|
||||
|
@ -1,3 +1,5 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from corres2math.tokens import email_validation_token
|
||||
from django.contrib.sites.models import Site
|
||||
from django.db import models
|
||||
@ -68,7 +70,7 @@ class Registration(PolymorphicModel):
|
||||
|
||||
|
||||
def get_random_filename(instance, filename):
|
||||
return get_random_string(64)
|
||||
return "authorization/photo/" + get_random_string(64)
|
||||
|
||||
|
||||
class StudentRegistration(Registration):
|
||||
|
@ -1,3 +1,10 @@
|
||||
import mimetypes
|
||||
import os
|
||||
|
||||
from django.http import Http404, HttpResponse, FileResponse
|
||||
from django.views.generic.base import View
|
||||
from magic import Magic
|
||||
|
||||
from corres2math.tokens import email_validation_token
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
@ -164,5 +171,26 @@ class UserUploadPhotoAuthorizationView(LoginRequiredMixin, UpdateView):
|
||||
form_class = PhotoAuthorizationForm
|
||||
template_name = "registration/upload_photo_authorization.html"
|
||||
|
||||
@transaction.atomic
|
||||
def form_valid(self, form):
|
||||
old_instance = StudentRegistration.objects.get(pk=self.object.pk)
|
||||
if old_instance.photo_authorization:
|
||||
old_instance.photo_authorization.delete()
|
||||
return super().form_valid(form)
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse_lazy("registration:user_detail", args=(self.object.user.pk,))
|
||||
|
||||
|
||||
class PhotoAuthorizationView(LoginRequiredMixin, View):
|
||||
def get(self, request, *args, **kwargs):
|
||||
filename = kwargs["filename"]
|
||||
path = f"media/authorization/photo/{filename}"
|
||||
if not os.path.exists(path):
|
||||
raise Http404
|
||||
student = StudentRegistration.objects.get(photo_authorization__endswith=filename)
|
||||
mime = Magic(mime=True)
|
||||
mime_type = mime.from_file(path)
|
||||
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
|
||||
true_file_name = _("Photo authorization of {student}.{ext}").format(student=str(student), ext=ext)
|
||||
return FileResponse(open(path, "rb"), content_type=mime_type, filename=true_file_name)
|
||||
|
Reference in New Issue
Block a user