server.py 1.69 KB
Newer Older
1
#!/bin/python
2
3
from flask import Flask, render_template, g
import mysql.connector
4
import sqlite3
5
6
import config

7
8
app = Flask(__name__)

9
10
11
12
13
14
15
16
17
18
# Row wrapper for sqlite
def dict_factory(cursor, row):
	d = {}
	for idx, col in enumerate(cursor.description):
		if type(row[idx]) == str:
			d[col[0].split('.')[-1]] = row[idx].replace('\\n','\n')
		else:
			d[col[0].split('.')[-1]] = row[idx]
	return d

19
def query(operation, *params):
20
21
22
23
24
25
26
27
28
29
30
31
32
	if config.db_engine == 'mysql':
		if 'db' not in g or not g.db.is_connected():
			g.db = mysql.connector.connect(user=config.db_user, password=config.db_passwd, host=config.db_host, database=config.db_db)
		cur = g.db.cursor(dictionary=True)
		cur.execute(operation.replace('?', '%s'), params)
		return cur.fetchall()
	else:
		if 'db' not in g or not g.db.is_connected():
			g.db = sqlite3.connect(config.db_file)
			g.db.row_factory = dict_factory
		cur = g.db.cursor()
		cur.execute(operation, params)
		return cur.fetchall()
33

34
35
@app.route('/')
def index():
36
	return render_template('index.html', latestvideos=query('''
37
				SELECT lectures.*, max(videos.time_updated) AS lastvidtime, courses.short, courses.downloadable, courses.title AS coursetitle
38
39
40
				FROM lectures
				LEFT JOIN videos ON (videos.lecture_id = lectures.id)
				LEFT JOIN courses on (courses.id = lectures.course_id)
41
				WHERE (? OR (courses.visible AND courses.listed AND lectures.visible AND videos.visible))
42
43
44
45
				GROUP BY videos.lecture_id
				ORDER BY lastvidtime DESC
				LIMIT 5
			''', False))
46

Andreas Valder's avatar
Andreas Valder committed
47
48
49
50
51
52
53
54
@app.route('/videos')
def videos():
	return render_template('videos.html', active_page='videos')

@app.route('/faq')
def faq():
	return render_template('faq.html',  active_page='faq')

55
56
57
if __name__ == '__main__':
	app.debug = True
	app.run()