summaryrefslogtreecommitdiffstats
path: root/app/main.py
blob: cdc8d0bb6cca49930350bfc97b3da12afe47c956 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
         )