diff options
Diffstat (limited to 'app/main.py')
-rw-r--r-- | app/main.py | 114 |
1 files changed, 69 insertions, 45 deletions
diff --git a/app/main.py b/app/main.py index cf6a99e..ef4e549 100644 --- a/app/main.py +++ b/app/main.py @@ -4,32 +4,59 @@ import os, sys from flask import Blueprint, render_template, request, flash, redirect,\ url_for, current_app, g from werkzeug import secure_filename -from .forms import UploadForm +from wtforms.validators import ValidationError +from .backend import Storage main = Blueprint('main', __name__) +def get_studies(): + """ + Add all existing courses of backend to study list. + This list is used to fill form values with courses. + """ + studies = getattr(g, '_studies', None) + if studies is None: + studies = g._studies = {} + it = current_app.config['STUDIES'].items() + for i, (abbr, courses) in enumerate(it): + path_rel = os.path.join('studies', abbr) + path_app = os.path.join('app', 'static', path_rel) + studies[abbr] = Storage(path_rel, path_app) + + # iterate over all courses in our backend + for course in g._studies[abbr].get_courses(): + # check if course is already listed + entry = (course.encode('ascii', errors='ignore'), course) + if entry not in courses: + current_app.config['STUDIES'][abbr].append(entry) + + current_app.config['STUDIES'][abbr].sort() + return studies + @main.route('/<study>/upload/', methods=['GET', 'POST']) -@main.route('/<study>/upload/<module>', methods=['GET', 'POST']) -def upload(study, module = None): +@main.route('/<study>/upload/<course>', methods=['GET', 'POST']) +def upload(study, course = None): + from .forms import UploadForm form = UploadForm() form.study.data = study - form.module.choices = current_app.config['STUDIES'][study] - if 'new' not in dict(form.module.choices): - form.module.choices.append(('', u'---')) - form.module.choices.append(('new', u'neues Modul hinzufügen')) - + # dynamically fill form values + form.course.choices = current_app.config['STUDIES'][study] + if 'new' not in dict(form.course.choices): + form.course.choices.append(('', u'---')) + form.course.choices.append(('new', u'neuen Kurs hinzufügen')) if form.validate_on_submit(): - if form.module.data == 'new': - module = form.module_new.data - slug = module.encode('ascii', errors='ignore') - i = len(current_app.config['STUDIES'][study]) - 2 - current_app.config['STUDIES'][study].insert(i, (slug,module)) + if form.course.data == 'new': + course = form.course_new.data + slug = course.encode('ascii', errors='ignore') + current_app.config['STUDIES'][study].append((slug,course)) + current_app.config['STUDIES'][study].sort() + else: - module = dict(current_app.config['STUDIES'][study])[form.module.data] + course = dict(current_app.config['STUDIES'][study])[form.course.data] year = form.year.data filename = secure_filename(form.exam.data.filename) @@ -38,45 +65,42 @@ def upload(study, module = None): except: data = form.exam.data.stream.read() - g.studies[study].add_file(module, year, filename, data) - flash("Datei %s gespeichert." % filename) - - return redirect(url_for('.study_index', study = study, module = module)) - - try: form.module.data = [k for (k,v) in form.module.choices if v == module][0] - except: pass - - return render_template('upload.html', - study = study, form = form, module=module) - + backend = get_studies()[study] + if not backend.exam_exists(course, year, filename): + backend.add_exam(course, year, filename, data) + flash(u'Datei %s gespeichert.' % filename) + return redirect(url_for('.courses_show', study = study, course = course)) + else: + flash(u'Datei mit gleichem Namen existiert schon!', 'error') + try: + form.course.data = [k for (k,v) in form.course.choices if v == course][0] + except: + pass -@main.route('/<study>/files/<module>/<year>/<filename>') -def study_show(study, module, year, filename): - mime_type, data = g.studies[study].get_file(module,year,filename) - header = { 'Content-Type' : mime_type } - return data, 200, header + return render_template('upload.html', study = study, form = form, + course = course) -@main.route('/<study>/modules/') -@main.route('/<study>/modules/<module>') -def study_index(study, module=None): - if module: - entries = sorted(g.studies[study].get_module(module), reverse=True) - return render_template('module_show.html', - study = study, module=module, entries=entries - ) +@main.route('/<study>/courses/') +@main.route('/<study>/courses/<course>') +def courses_show(study, course = None): + """ Lists all courses or exams for a course """ + backend = get_studies()[study] + if course: + entries = sorted(backend.get_exams(course), reverse = True) + return render_template('exams.html', study = study, course = course, + entries=entries) - modules = g.studies[study].get_modules() - return render_template('module_list.html', study = study, modules=modules) + courses = sorted(backend.get_courses()) + return render_template('courses.html', study = study, courses = courses) @main.route('/') def index(): + """ Lists all course of studies """ get_img_path = lambda x: os.path.join('studies', x, 'logo.png') - studies = [(name,get_img_path(name)) for name,m in current_app.config['STUDIES'].items()] + it = current_app.config['STUDIES'].items() + studies = [(name, get_img_path(name)) for name,m in it] - return render_template( - 'index.html', - studies = studies - ) + return render_template('index.html', studies = studies) |