1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-06-25 11:00:31 +02:00

Limit upload file size

This commit is contained in:
Yohann D'ANELLO
2020-05-12 18:36:36 +02:00
parent 150f555d9d
commit f01537719b
2 changed files with 73 additions and 32 deletions

View File

@ -3,6 +3,7 @@ import re
from django import forms
from django.db.models import Q
from django.template.defaultfilters import filesizeformat
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
@ -139,9 +140,23 @@ class SolutionForm(forms.ModelForm):
problem = forms.ChoiceField(
label=_("Problem"),
choices=[(str(i), _("Problem #{problem:d}").format(problem=i)) for i in range(1, 9)],
choices=[(str(i), _("Problem #%(problem)d") % {"problem": i}) for i in range(1, 9)],
)
def clean_file(self):
content = self.cleaned_data['file']
content_type = content.content_type
if content_type in ["application/pdf"]:
if content.size > 5 * 2 ** 20:
raise forms.ValidationError(
_('Please keep filesize under %(max_size)s. Current filesize %(current_size)s') % {
"max_size": filesizeformat(2 * 2 ** 20),
"current_size": filesizeformat(content.size)
})
else:
raise forms.ValidationError(_('The file should be a PDF file.'))
return content
class Meta:
model = Solution
fields = ('file', 'problem',)
@ -152,6 +167,20 @@ class SynthesisForm(forms.ModelForm):
Form to upload a synthesis.
"""
def clean_file(self):
content = self.cleaned_data['file']
content_type = content.content_type
if content_type in ["application/pdf"]:
if content.size > 5 * 2 ** 20:
raise forms.ValidationError(
_('Please keep filesize under %(max_size)s. Current filesize %(current_size)s') % {
"max_size": filesizeformat(2 * 2 ** 20),
"current_size": filesizeformat(content.size)
})
else:
raise forms.ValidationError(_('The file should be a PDF file.'))
return content
class Meta:
model = Synthesis
fields = ('file', 'source', 'round',)