ADD: added sub module dav_submission for supporting Manuel Hark with
his 150 Jahre DAV submission process.
This commit is contained in:
108
dav_submission/forms.py
Normal file
108
dav_submission/forms.py
Normal file
@@ -0,0 +1,108 @@
|
||||
# -*- 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'Lorem'))
|
||||
|
||||
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(label=_(u'Beschreibung'),
|
||||
help_text=_(u'Lorem'),
|
||||
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]
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user