summaryrefslogtreecommitdiffstats
path: root/app/forms.py
blob: e562df2df8e5c0c0b8c6fe8712975ec0e1ffb1db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -*- coding: utf-8 -*-

from datetime import date
from flask import current_app
from flask.ext.wtf import Form
from wtforms import TextField, FileField, SelectField, validators
from wtforms.validators import ValidationError

class UploadForm(Form):
    """ Upload Form class for validation """
    study = TextField('Studiengang')
    exam   = FileField('Klausur')
    module = SelectField('Kurs')
    module_new = TextField('Modulname', validators=[validators.Optional(),
    validators.Length(min=5)])
    year   = SelectField(
                'Jahr',
                validators=[validators.Required()],
                choices = [ (str(x),x) for x in 
                  #xrange(date.today().year, current_app.config['FORM_START_YEAR']-1, -1)
                  xrange(date.today().year, 2000, -1)
                ]
           )

    def validate_exam(form, field):
      exts = current_app.config['ALLOWED_EXTENSIONS']
      ext = map(field.data.filename.endswith, exts)

      if not any(ext):
        raise ValidationError(u'Ungültiger Dateityp')

      if field.data.content_length > current_app.config['MAX_CONTENT_LENGTH']:
        raise ValidationError(u'Zu große Datei')
    
    def validate_module(form, field):
      modules = dict(current_app.config['STUDIES'][form.study.data])
      data = form.module.data
      if data not in modules or data == '':
        raise ValidationError(u'Bitte wähle ein Modul!')