121 lines
5.1 KiB
Python
121 lines
5.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
from django import forms
|
|
from django.apps import apps
|
|
from django.utils.translation import ugettext, ugettext_lazy as _
|
|
|
|
app_config = apps.get_containing_app_config(__package__)
|
|
|
|
|
|
class UploadForm(forms.Form):
|
|
name = forms.CharField(max_length=1024,
|
|
label=_(u'Dein Name'),
|
|
help_text=_(u'Wenn wir wissen, wie du heißt, wird uns das echt weiter helfen'))
|
|
email_address = forms.EmailField(label=_(u'Deine E-Mail-Adresse'),
|
|
help_text=_(u'Damit wir dich für Rückfragen kontaktieren können'))
|
|
|
|
group = forms.CharField(max_length=1024,
|
|
required=False,
|
|
label=_(u'DAV Gruppe'),
|
|
help_text=_(u'Optional, falls du aktiv in einer der Sektionsgruppen bist'))
|
|
|
|
title = forms.CharField(max_length=60,
|
|
label=_(u'Titel deines Beitrags / Stichwort'),
|
|
help_text=u'%s<br />%s' % (
|
|
_(u'Lorem'),
|
|
_(u'Maximal 60 Zeichen')
|
|
))
|
|
|
|
description = forms.CharField(max_length=150,
|
|
label=_(u'Beschreibung'),
|
|
help_text=u'%s<br />%s' % (
|
|
_(u'kommt zum Bild, falls es ausgestellt wird'),
|
|
_(u'Maximal 150 Zeichen')
|
|
),
|
|
widget=forms.Textarea(attrs={'rows': 2}))
|
|
|
|
files = forms.FileField(label=_(u'Dateien'),
|
|
help_text=_(u'Lorem'),
|
|
widget=forms.ClearableFileInput(attrs={'multiple': True}))
|
|
|
|
accepted = forms.BooleanField(required=False,
|
|
label=_(u'Ja, lorem!'))
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(UploadForm, self).__init__(*args, **kwargs)
|
|
|
|
self.fields['title'].widget.attrs['placeholder'] = \
|
|
u'Climb & Bold - Nacktbesteigung der Nose'[:self.fields['title'].max_length]
|
|
|
|
self.fields['group'].widget.attrs['placeholder'] = \
|
|
ugettext(u'Kann frei gelassen werden')[:self.fields['title'].max_length]
|
|
|
|
help_text = self.fields['files'].help_text
|
|
if app_config.settings.max_files:
|
|
help_text += u'<br />%s' % (ugettext(u'Lade bis zu %d Dateien hoch')
|
|
% app_config.settings.max_files)
|
|
if app_config.settings.max_file_size_mib:
|
|
help_text += u'<br />%s' % (ugettext(u'Einzelne Dateien dürfen maximal %d MiB groß sein')
|
|
% app_config.settings.max_file_size_mib)
|
|
if app_config.settings.max_total_size_mib:
|
|
help_text += u'<br />%s' % (ugettext(u'Alle Dateien zusammen dürfen maximal %d MiB groß sein')
|
|
% app_config.settings.max_total_size_mib)
|
|
self.fields['files'].help_text = help_text
|
|
|
|
def clean_files(self):
|
|
not_allowed_file_names = (app_config.settings.metadata_file_name,)
|
|
max_files = app_config.settings.max_files
|
|
max_file_size_mib = app_config.settings.max_file_size_mib
|
|
max_total_size_mib = app_config.settings.max_total_size_mib
|
|
|
|
validation_errors = []
|
|
|
|
files = self.files.getlist('files')
|
|
max_file_size = max_file_size_mib * 1024 * 1024
|
|
size_total = 0
|
|
n_files = 0
|
|
for file in files:
|
|
if file.name in not_allowed_file_names:
|
|
ve = forms.ValidationError(
|
|
ugettext(u'Dateiname nicht erlaubt: %s') % file.name,
|
|
code='filename_not_allowed',
|
|
)
|
|
validation_errors.append(ve)
|
|
if max_file_size and file.size > max_file_size:
|
|
ve = forms.ValidationError(
|
|
ugettext(u'Die Datei ist insgesamt zu groß:'
|
|
u' %(name)s (> %(max_mib)s MiB)') % {'name': file.name, 'max_mib': max_file_size_mib},
|
|
code='file_to_big',
|
|
)
|
|
validation_errors.append(ve)
|
|
size_total += file.size
|
|
n_files += 1
|
|
|
|
max_total_size = max_total_size_mib * 1024 * 1024
|
|
if max_total_size and size_total > max_total_size:
|
|
ve = forms.ValidationError(
|
|
ugettext(u'Dein Beitrag ist zu groß (%s MiB)') % max_total_size_mib,
|
|
code='files_to_big',
|
|
)
|
|
validation_errors.append(ve)
|
|
|
|
if max_files and n_files > max_files:
|
|
ve = forms.ValidationError(
|
|
ugettext(u'Dein Beitrag enthält mehr als %d Dateien') % max_files,
|
|
code='files_to_big',
|
|
)
|
|
validation_errors.append(ve)
|
|
|
|
if validation_errors:
|
|
raise forms.ValidationError(validation_errors)
|
|
|
|
return files
|
|
|
|
def clean_accepted(self):
|
|
val = self.cleaned_data.get('accepted')
|
|
if not val:
|
|
raise forms.ValidationError(
|
|
ugettext(u'Sag ja! Du musst!'),
|
|
code='not_accepted',
|
|
)
|
|
return val
|