1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-06-21 03:58:26 +02:00

Detect when a solution has more than 30 pages

This commit is contained in:
Yohann D'ANELLO
2021-01-19 00:32:34 +01:00
parent 8515153be7
commit d5e7295981
3 changed files with 86 additions and 51 deletions

View File

@ -3,6 +3,7 @@
import re
from PyPDF3 import PdfFileReader
from bootstrap_datepicker_plus import DatePickerInput, DateTimePickerInput
from django import forms
from django.core.exceptions import ValidationError
@ -115,6 +116,19 @@ class TournamentForm(forms.ModelForm):
class SolutionForm(forms.ModelForm):
def clean_file(self):
if "file" in self.files:
file = self.files["file"]
if file.size > 2e6:
raise ValidationError(_("The uploaded file size must be under 5 Mo."))
if file.content_type != "application/pdf":
raise ValidationError(_("The uploaded file must be a PDF file."))
pdf_reader = PdfFileReader(file)
pages = len(pdf_reader.pages)
if pages > 30:
raise ValidationError(_("The PDF file must not have more than 30 pages."))
return self.cleaned_data["photo_authorization"]
def save(self, commit=True):
"""
Don't save a solution with this way. Use a view instead