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 import forms
from django.db.models import Q from django.db.models import Q
from django.template.defaultfilters import filesizeformat
from django.utils import timezone from django.utils import timezone
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -139,9 +140,23 @@ class SolutionForm(forms.ModelForm):
problem = forms.ChoiceField( problem = forms.ChoiceField(
label=_("Problem"), 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: class Meta:
model = Solution model = Solution
fields = ('file', 'problem',) fields = ('file', 'problem',)
@ -152,6 +167,20 @@ class SynthesisForm(forms.ModelForm):
Form to upload a synthesis. 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: class Meta:
model = Synthesis model = Synthesis
fields = ('file', 'source', 'round',) fields = ('file', 'source', 'round',)

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: TFJM2\n" "Project-Id-Version: TFJM2\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-05-11 14:17+0200\n" "POT-Creation-Date: 2020-05-12 18:25+0200\n"
"PO-Revision-Date: 2020-04-29 02:30+0000\n" "PO-Revision-Date: 2020-04-29 02:30+0000\n"
"Last-Translator: Yohann D'ANELLO <yohann.danello@animath.fr>\n" "Last-Translator: Yohann D'ANELLO <yohann.danello@animath.fr>\n"
"Language-Team: fr <LL@li.org>\n" "Language-Team: fr <LL@li.org>\n"
@ -233,7 +233,8 @@ msgstr "solutions"
#: apps/member/models.py:291 #: apps/member/models.py:291
#, python-brace-format #, python-brace-format
msgid "Solution of team {trigram} for problem {problem} for final" msgid "Solution of team {trigram} for problem {problem} for final"
msgstr "Solution de l'équipe {trigram} pour le problème {problem} pour la finale" msgstr ""
"Solution de l'équipe {trigram} pour le problème {problem} pour la finale"
#: apps/member/models.py:294 #: apps/member/models.py:294
#, python-brace-format #, python-brace-format
@ -330,7 +331,7 @@ msgstr "Tous les profils"
msgid "Orphaned profiles" msgid "Orphaned profiles"
msgstr "Profils orphelins" msgstr "Profils orphelins"
#: apps/member/views.py:244 apps/tournament/forms.py:22 templates/base.html:83 #: apps/member/views.py:244 apps/tournament/forms.py:23 templates/base.html:83
msgid "Organizers" msgid "Organizers"
msgstr "Organisateurs" msgstr "Organisateurs"
@ -341,85 +342,96 @@ msgstr "Organisateurs"
msgid "tournament" msgid "tournament"
msgstr "tournoi" msgstr "tournoi"
#: apps/tournament/forms.py:30 #: apps/tournament/forms.py:31
msgid "This tournament already exists." msgid "This tournament already exists."
msgstr "Ce tournoi existe déjà." msgstr "Ce tournoi existe déjà."
#: apps/tournament/forms.py:32 #: apps/tournament/forms.py:33
msgid "The final tournament was already defined." msgid "The final tournament was already defined."
msgstr "Le tournoi de la finale est déjà défini." msgstr "Le tournoi de la finale est déjà défini."
#: apps/tournament/forms.py:64 #: apps/tournament/forms.py:65
msgid "This organizer already exist." msgid "This organizer already exist."
msgstr "Cet organisateur existe déjà." msgstr "Cet organisateur existe déjà."
#: apps/tournament/forms.py:93 #: apps/tournament/forms.py:94
msgid "The trigram must be composed of three upcase letters." msgid "The trigram must be composed of three upcase letters."
msgstr "Le trigramme doit être composé de trois lettres en majuscules." msgstr "Le trigramme doit être composé de trois lettres en majuscules."
#: apps/tournament/forms.py:97 #: apps/tournament/forms.py:98
msgid "This trigram is already used." msgid "This trigram is already used."
msgstr "Ce trigramme est déjà utilisé." msgstr "Ce trigramme est déjà utilisé."
#: apps/tournament/forms.py:100 #: apps/tournament/forms.py:101
msgid "This name is already used." msgid "This name is already used."
msgstr "Ce nom est déjà utilisé." msgstr "Ce nom est déjà utilisé."
#: apps/tournament/forms.py:103 #: apps/tournament/forms.py:104
msgid "This tournament is already closed." msgid "This tournament is already closed."
msgstr "Ce tournoi est déjà fermé." msgstr "Ce tournoi est déjà fermé."
#: apps/tournament/forms.py:114 #: apps/tournament/forms.py:115
msgid "Access code" msgid "Access code"
msgstr "Code d'accès" msgstr "Code d'accès"
#: apps/tournament/forms.py:122 #: apps/tournament/forms.py:123
msgid "The access code must be composed of 6 alphanumeric characters." msgid "The access code must be composed of 6 alphanumeric characters."
msgstr "Le code d'accès doit être composé de 6 caractères alphanumériques." msgstr "Le code d'accès doit être composé de 6 caractères alphanumériques."
#: apps/tournament/forms.py:126 #: apps/tournament/forms.py:127
msgid "This access code is invalid." msgid "This access code is invalid."
msgstr "Ce code d'accès est invalide." msgstr "Ce code d'accès est invalide."
#: apps/tournament/forms.py:129 #: apps/tournament/forms.py:130
msgid "The team is already validated." msgid "The team is already validated."
msgstr "L'équipe est déjà validée." msgstr "L'équipe est déjà validée."
#: apps/tournament/forms.py:141 #: apps/tournament/forms.py:142
msgid "Problem" msgid "Problem"
msgstr "Problème" msgstr "Problème"
#: apps/tournament/forms.py:142 #: apps/tournament/forms.py:143
#, python-brace-format #, python-format
msgid "Problem #{problem:d}" msgid "Problem #%(problem)d"
msgstr "Problème n°{problem:d}" msgstr "Problème n°%(problem)d"
#: apps/tournament/forms.py:168 apps/tournament/forms.py:181 #: apps/tournament/forms.py:152 apps/tournament/forms.py:176
#: apps/tournament/forms.py:194 #, python-format
msgid ""
"Please keep filesize under %(max_size)s. Current filesize %(current_size)s"
msgstr ""
"Merci de ne pas dépasser les %(max_size)s. Le fichier envoyé pèse %(current_size)s."
#: apps/tournament/forms.py:157 apps/tournament/forms.py:181
msgid "The file should be a PDF file."
msgstr "Ce fichier doit être au format PDF."
#: apps/tournament/forms.py:197 apps/tournament/forms.py:210
#: apps/tournament/forms.py:223
msgid "Choose a team..." msgid "Choose a team..."
msgstr "Choisir une équipe ..." msgstr "Choisir une équipe ..."
#: apps/tournament/forms.py:169 #: apps/tournament/forms.py:198
msgid "Team 1" msgid "Team 1"
msgstr "Équipe 1" msgstr "Équipe 1"
#: apps/tournament/forms.py:176 #: apps/tournament/forms.py:205
msgid "Problem defended by team 1" msgid "Problem defended by team 1"
msgstr "Problème défendu par l'équipe 1" msgstr "Problème défendu par l'équipe 1"
#: apps/tournament/forms.py:182 #: apps/tournament/forms.py:211
msgid "Team 2" msgid "Team 2"
msgstr "Équipe 2" msgstr "Équipe 2"
#: apps/tournament/forms.py:189 #: apps/tournament/forms.py:218
msgid "Problem defended by team 2" msgid "Problem defended by team 2"
msgstr "Problème défendu par l'équipe 2" msgstr "Problème défendu par l'équipe 2"
#: apps/tournament/forms.py:195 #: apps/tournament/forms.py:224
msgid "Team 3" msgid "Team 3"
msgstr "Équipe 3" msgstr "Équipe 3"
#: apps/tournament/forms.py:202 #: apps/tournament/forms.py:231
msgid "Problem defended by team 3" msgid "Problem defended by team 3"
msgstr "Problème défendu par l'équipe 3" msgstr "Problème défendu par l'équipe 3"
@ -437,8 +449,8 @@ msgid ""
"List of all organizers that can see and manipulate data of the tournament " "List of all organizers that can see and manipulate data of the tournament "
"and the teams." "and the teams."
msgstr "" msgstr ""
"Liste des organisateurs qui peuvent manipuler les données du tournoi et " "Liste des organisateurs qui peuvent manipuler les données du tournoi et des "
"des équipes." "équipes."
#: apps/tournament/models.py:29 templates/tournament/tournament_detail.html:15 #: apps/tournament/models.py:29 templates/tournament/tournament_detail.html:15
msgid "size" msgid "size"
@ -509,8 +521,8 @@ msgid ""
"The trigram should be composed of 3 capitalize letters, that is a funny " "The trigram should be composed of 3 capitalize letters, that is a funny "
"acronym for the team." "acronym for the team."
msgstr "" msgstr ""
"Le trigramme doit être composé de trois lettres en majuscule, qui doit " "Le trigramme doit être composé de trois lettres en majuscule, qui doit être "
"être un acronyme amusant représentant l'équipe." "un acronyme amusant représentant l'équipe."
#: apps/tournament/models.py:183 #: apps/tournament/models.py:183
msgid "The tournament where the team is registered." msgid "The tournament where the team is registered."