diff options
-rw-r--r-- | app.py | 44 | ||||
-rw-r--r-- | static/style.css | 11 | ||||
-rw-r--r-- | templates/upload.html | 24 |
3 files changed, 50 insertions, 29 deletions
@@ -5,8 +5,8 @@ import magic, os from fit import Fit from flask import Flask, render_template, request, flash, redirect, \ url_for,jsonify -from flask.ext.wtf import Form, TextField, Required, FileField, SelectField,\ - ValidationError, file_required +from flask.ext.wtf import Form, TextField, FileField, SelectField,\ + validators, ValidationError from werkzeug import secure_filename from datetime import date @@ -18,8 +18,7 @@ app.config.from_object('settings') ALLOWED_EXTENSIONS = ['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'zip', 'gs', 'gz' ] -MODULES= [ - ('new', u'neues Modul hinzufügen'), +FORM_MODULE_LIST= [ ('alp1', u'ALP1 - Funktionale Programmierung'), ('alp2', u'ALP2 - Objektorientierte Programmierung'), ('alp3', u'ALP3 - Datenstrukturen'), @@ -34,8 +33,10 @@ MODULES= [ ('gti', u'Grundlagen der Theoretischen Informatik'), ('dbs', u'Datenbanksysteme'), ('swt', u'Softwaretechnik'), - ('aws', u'Anwendungssysteme') - ] + ('aws', u'Anwendungssysteme'), + ('', u'---'), + ('new', u'neues Modul hinzufügen') +] TERM_START_YEAR = 1999 YEARS = [(str(x),x) for x in xrange(date.today().year, TERM_START_YEAR, -1)] @@ -45,25 +46,30 @@ YEARS = [(str(x),x) for x in xrange(date.today().year, TERM_START_YEAR, -1)] class UploadForm(Form): """ Upload Form class for validation """ exam = FileField('Klausur') - module = SelectField( - 'Kurs', - validators=[Required()], - choices = MODULES - ) - module_new = TextField('Modulname', default='Modulname') + module = SelectField('Kurs', choices = FORM_MODULE_LIST) + module_new = TextField('Modulname', validators=[validators.Optional(), + validators.Length(min=5)]) year = SelectField( 'Jahr', - validators=[Required()], + validators=[validators.Required()], choices = YEARS ) def validate_exam(form, field): ext = map(lambda x: field.data.filename.endswith(x), ALLOWED_EXTENSIONS) if not any(ext): - raise ValidationError('Invalid file type') + raise ValidationError(u'Ungültiger Dateityp') if field.data.content_length > app.config['MAX_CONTENT_LENGTH']: - raise ValidationError('File is too big') + raise ValidationError(u'Zu große Datei') + + def validate_module(form, field): + modules = dict(FORM_MODULE_LIST) + data = form.module.data + if module not in modules or data == '': + raise ValidationError(u'Bitte wähle ein Modul!') + + @@ -75,9 +81,9 @@ def upload(): if form.module.data == 'new': module = form.module_new.data slug = module.encode('ascii', errors='ignore') - MODULES.append((slug,module)) + FORM_MODULE_LIST.append((slug,module)) else: - module = dict(MODULES)[form.module.data] + module = dict(FORM_MODULE_LIST)[form.module.data] year = form.year.data filename = secure_filename(form.exam.data.filename) @@ -119,12 +125,12 @@ def index(module=None): if __name__ == "__main__": - modules = dict(MODULES) + modules = dict(FORM_MODULE_LIST) # extend module list with git values for module in fit.get_modules(): # check if module is already listed if all(map(lambda (k,v): v != module, modules.items())): slug = module.encode('ascii', errors='ignore') - MODULES.append((slug, module)) + FORM_MODULE_LIST.insert(len(FORM_MODULE_LIST) - 2, (slug, module)) app.run() diff --git a/static/style.css b/static/style.css index aaaf464..a46689e 100644 --- a/static/style.css +++ b/static/style.css @@ -120,13 +120,22 @@ a:hover { span.error { font-weight: bold; color: #F00; + display: block; + margin-left: 200px; } select { font-family: monospace; width: 350px; border: 1px solid #ccc; - padding: 2px 5px; + padding: 5px 5px; background-color: #fff; } +#module_new { + width: 200px; + margin-left: 10px; + padding: 5px 5px; + border: 1px solid #ccc; +} + diff --git a/templates/upload.html b/templates/upload.html index 2ad5224..2032323 100644 --- a/templates/upload.html +++ b/templates/upload.html @@ -5,16 +5,18 @@ {% macro render_fields(field, extra_field) %} <p> <label>{{ field.label }}</label> - {{ field(**kwargs)|safe }} - {% if field.errors %} - <span class="error"> - {% for error in field.errors %}{{ error }}{% endfor %} - </span> - {% endif %} + {{ field|safe }} {% if extra_field %} {{extra_field(**kwargs)|safe}} {% endif %} + + {% if field.errors or extra_field.errors %} + <span class="error"> + {% for error in field.errors %}{{ error }}{% endfor %} + {% for error in extra_field.errors %}{{ error }}{% endfor %} + </span> + {% endif %} </p> {% endmacro %} @@ -24,9 +26,13 @@ <script type="text/javascript"> $(document).ready(function() { - $('#module').change(function(){ + var elem = $('#module_new'); + var module = $('#module'); + if(module.val() != 'new') + elem.hide(); + + module.change(function(){ var value = $(this).val(); - var elem = $('#module_new'); if(value == 'new') elem.fadeIn(); else @@ -38,7 +44,7 @@ {{ form.csrf_token }} {{ render_field(form.exam) }} - {{ render_fields(form.module, form.module_new) }} + {{ render_fields(form.module, form.module_new, placeholder='Modulname') }} {{ render_field(form.year) }} <p> |