# -*- coding: utf-8 -*- 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 main = Blueprint('main', __name__) @main.route('//upload/', methods=['GET', 'POST']) @main.route('//upload/', methods=['GET', 'POST']) def upload(study, module = None): 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')) 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)) else: module = dict(current_app.config['STUDIES'][study])[form.module.data] year = form.year.data filename = secure_filename(form.exam.data.filename) try: data = form.exam.data.stream.getvalue() 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) @main.route('//files///') 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 @main.route('//modules/') @main.route('//modules/') 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 ) modules = g.studies[study].get_modules() return render_template('module_list.html', study = study, modules=modules) @main.route('/') def index(): 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()] return render_template( 'index.html', studies = studies )