summaryrefslogtreecommitdiffstats
path: root/app/forms.py
blob: 23321799cb81c1c09673fd0a011cefad937fa9fb (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
# -*- 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

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

    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_course(form, field):
      courses = dict(current_app.config['STUDIES'][form.study.data])
      data = form.course.data
      if data not in courses or data == '':
        raise ValidationError(u'Bitte wähle einen Kurs!')