summaryrefslogtreecommitdiffstats
path: root/app.py
blob: 177023875fb320e4cd5cf95aa56ab59d89a94b16 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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 werkzeug import secure_filename
from datetime import date


fit = Fit('static/fit.git')

app = Flask(__name__)
app.config.from_object('settings')

ALLOWED_EXTENSIONS = ['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'zip', 'gs', 'gz' ]

MODULES= [
  ('alp1', 'ALP1 - Funktionale Programmierung'),
  ('alp2', 'ALP2 - Objektorientierte Programmierung'),
  ('alp3', 'ALP3 - Datenstrukturen'),
  ('alp4', 'ALP4 - Nichtsequentielle Programmierung'),
  ('alp5', 'ALP5 - Netzprogrammierung'),
  ('ti1', 'TI1 - Grundlagen der Technischen Informatik'),
  ('ti2', 'TI2 - Rechnerarchitektur'),
  ('ti3', 'TI3 - Betriebs- und Kommunikationssysteme'),
  ('mafi1', 'MafI1 - Logik und Diskrete Mathematik'),
  ('mafi2', 'MafI2 - Analysis'),
  ('mafi3', 'MafI3 - Lineare Algebra'),
  ('gti', 'Grundlagen der Theoretischen Informatik'),
  ('dbs', 'Datenbanksysteme'),
  ('swt', 'Softwaretechnik'),
  ('aws', 'Anwendungssysteme')
  ]

TERM_START_YEAR = 1999
YEARS = [(str(x),x) for x in xrange(date.today().year, TERM_START_YEAR, -1)]

class UploadForm(Form):
    exam   = FileField('Klausur', validators=[file_required()])
    module = SelectField(
                'Kurs',
                validators=[Required()],
                choices = MODULES
             )
    year   = SelectField(
                'Jahr',
                validators=[],
                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')

      if field.data.content_length > app.config['MAX_CONTENT_LENGTH']:
        raise ValidationError('File is too big')


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


@app.route('/upload', methods=['GET', 'POST'])
def upload():
  form = UploadForm()

  if form.validate_on_submit():
      module = dict(MODULES)[form.module.data]
      year = form.year.data
      filename = secure_filename(form.exam.data.filename)
      path = os.path.join(module,year,filename).encode('ascii')

      try:
        oid =  fit.add_file(form.exam.data.stream.getvalue(), path)
      except:
        oid =  fit.add_file(form.exam.data.stream.read(), path)

      flash("File %s saved." % filename)

      return redirect(url_for('list', module = module))

  return render_template('upload.html', form = form)

@app.route('/files/<oid>')
def show(oid):
  data = fit.get_file(oid)
  mime = magic.Magic(mime=True)
  header = { 'Content-Type' : mime.from_buffer(data[:1024]) }
  return data, 200, header


@app.route('/modules')
@app.route('/modules/<module>')
def list(module=None):
  if module:
    return render_template('module_show.html', module=module, entries=fit.get_module(module))

  return render_template('module_list.html', modules=fit.get_modules())


@app.route('/', methods=['GET', 'POST'])
def index():
    return list() 

if __name__ == "__main__":
    app.run()