]> git.siccegge.de Git - fachschaftentool.git/blob - application.py
fachschaftstoolupdate
[fachschaftentool.git] / application.py
1 #!/usr/bin/python2
2 # -*- coding: utf-8 -*-
3 import os
4 import os.path
5 import sqlite3
6 import tarfile
7 import StringIO
8
9 from flask import Flask, session, redirect, url_for, escape, request, render_template, g, make_response
10
11 app = Flask(__name__)
12
13 DATABASE = os.path.join(os.path.dirname(__file__), '..', 'data', 'fachschaften.sqlite')
14 #DATABASE = os.path.join(os.path.dirname(__file__), 'fachschaften.sqlite')
15
16 def get_db():
17 db = sqlite3.connect(DATABASE)
18 db.row_factory = sqlite3.Row
19 return db
20
21 @app.route('/')
22 def index():
23 db = get_db()
24 cursor = db.cursor()
25 fachschaften = cursor.execute("SELECT * from fachschaft")
26 return render_template('index.html', fachschaften=fachschaften)
27
28 @app.route('/show/<int:id>/')
29 def show(id):
30 db = get_db()
31 cursor = db.cursor()
32 fachschaft = cursor.execute("SELECT * from fachschaft WHERE rowid = ?", [id]).fetchone()
33 studiengang = cursor.execute("SELECT * from studiengang WHERE fachschaft = ?", [fachschaft['rowid']]).fetchall()
34 return render_template('show.html', fachschaft=fachschaft, studiengaenge=studiengang)
35
36 @app.route('/add/')
37 def add():
38 return render_template('add.html')
39
40 @app.route('/addp/', methods=['POST'])
41 def add_process():
42 db = get_db()
43 cursor = db.cursor()
44 data = request.form
45
46 cursor.execute("INSERT INTO fachschaft "
47 "(langname, kurzname, land, stadt, studenten, "
48 "organ, vertreten, organisation, fachschaftler, homepage) "
49 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
50 [data['unilang'],
51 data['unikurz'],
52 data['bundesland'],
53 data['stadt'],
54 data['gesamtstudis'],
55 data['fsorgan'],
56 data['vertretenestudis'],
57 data['organisationsform'],
58 data['aktive'],
59 data['homepage']])
60 rowid = cursor.lastrowid
61 for i in range(0, 10):
62 if data['studiengang%d' % i] != '':
63 cursor.execute("INSERT INTO studiengang "
64 "(fachschaft, name, anf_dieses, anf_vorjahr) "
65 "VALUES (?, ?, ?, ?)",
66 [int(rowid),
67 data['studiengang%d' % i],
68 data['anfaenger%d' % i],
69 data['vorjahr%d' % i]])
70 db.commit()
71 return redirect(url_for('show', id = rowid))
72
73 @app.route('/download/')
74 def download():
75 db = get_db()
76 cursor = db.cursor()
77 universities = []
78 data = StringIO.StringIO()
79 tar = tarfile.TarFile(mode='w', fileobj=data)
80 for fachschaft in cursor.execute("SELECT * from fachschaft").fetchall():
81 studiengaenge = cursor.execute("SELECT * from studiengang WHERE fachschaft = ?", [fachschaft['rowid']]).fetchall()
82 fdata = render_template('fachschaft.tex',
83 fachschaft = fachschaft,
84 studiengaenge = studiengaenge)
85 fdata = fdata.encode('utf-8')
86 filename = '%s_%s.tex' % (fachschaft['kurzname'].strip().lower().replace(' ', '_').replace('/','_').replace(u'ä', 'ae').replace(u'ü', 'ue').replace(u'ö', 'oe'),
87 fachschaft['rowid'])
88 info = tarfile.TarInfo(name='fachschaften/%s' % filename)
89 info.size = len(fdata)
90 tar.addfile(info, StringIO.StringIO(fdata))
91 universities.append(filename)
92
93 # liste.tex
94 fdata = ('\n'.join(['\input{fachschaften/%s}' % (u) for u in universities])).encode('utf-8')
95 info = tarfile.TarInfo(name='liste.tex')
96 info.size = len(fdata)
97 tar.addfile(info, StringIO.StringIO(fdata))
98
99 # master.tex
100 fdata = file(os.path.join(os.path.dirname(__file__), 'templates', 'master.tex')).read()
101 info = tarfile.TarInfo(name='master.tex')
102 info.size = len(fdata)
103 tar.addfile(info, StringIO.StringIO(fdata))
104
105 # kiflogo.pdf
106 fdata = file(os.path.join(os.path.dirname(__file__), 'templates', 'kiflogo.pdf')).read()
107 info = tarfile.TarInfo(name='kiflogo.pdf')
108 info.size = len(fdata)
109 tar.addfile(info, StringIO.StringIO(fdata))
110
111
112 tar.close()
113 responseobj=make_response(data.getvalue())
114 responseobj.mimetype="application/x-tar"
115 return responseobj
116
117 if __name__ == '__main__':
118 app.run(debug=True)