diff options
author | Nico Geyso <nico.geyso@fu-berlin.de> | 2014-12-07 18:09:58 +0100 |
---|---|---|
committer | Nico Geyso <nico.geyso@fu-berlin.de> | 2014-12-07 18:09:58 +0100 |
commit | 4ea12c4b97fc7775ab5ae3cb4607f715b880aede (patch) | |
tree | bdb798ffd244bee68b62d70da068ca795da766e6 /app/main.py | |
parent | ab487c574b6ac405a869528516bed55473e93c5f (diff) | |
download | klausuren-4ea12c4b97fc7775ab5ae3cb4607f715b880aede.tar.gz klausuren-4ea12c4b97fc7775ab5ae3cb4607f715b880aede.tar.bz2 klausuren-4ea12c4b97fc7775ab5ae3cb4607f715b880aede.zip |
Remove dependency of pygit2 instead use directory listings
Diffstat (limited to 'app/main.py')
-rw-r--r-- | app/main.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..cdc8d0b --- /dev/null +++ b/app/main.py @@ -0,0 +1,81 @@ +# -*- 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('/<study>/upload/', methods=['GET', 'POST']) +@main.route('/<study>/upload/<module>', 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) + path = os.path.join(module,year,filename) + + try: + oid = g.studies[study].add_file(form.exam.data.stream.getvalue(), path) + except: + oid = g.studies[study].add_file(form.exam.data.stream.read(), path) + + 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('/<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 + + +@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 + ) + + 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 + ) |