server.py 8.75 KB
Newer Older
Julian Rother's avatar
Julian Rother committed
1
#!/bin/python
2
from flask import Flask, g, request, url_for, redirect, session, render_template, flash
3
from functools import wraps
4
from datetime import date, timedelta, datetime, time
5
import os
6

7
app = Flask(__name__)
8

9
config = app.config
10
11
config['DB_SCHEMA'] = 'db_schema.sql'
config['DB_DATA'] = 'db_example.sql'
12
13
14
config['DB_ENGINE'] = 'sqlite'
config['SQLITE_DB'] = 'db.sqlite'
config['SQLITE_INIT_SCHEMA'] = True
15
config['SQLITE_INIT_DATA'] = False
16
config['DEBUG'] = False
17
config['VIDEOPREFIX'] = 'https://videoag.fsmpi.rwth-aachen.de'
18
19
20
if __name__ == '__main__':
	config['SQLITE_INIT_DATA'] = True
	config['DEBUG'] = True
21
config.from_pyfile('config.py', silent=True)
Julian Rother's avatar
Julian Rother committed
22

23
from db import query, searchquery, ldapauth, ldapget
Julian Rother's avatar
Julian Rother committed
24

25
26
app.jinja_env.globals['videoprefix'] = config['VIDEOPREFIX']
mod_endpoints = []
Julian Rother's avatar
Julian Rother committed
27

28
29
30
31
32
33
def ismod(*args):
	return ('user' in session)

app.jinja_env.globals['ismod'] = ismod

def mod_required(func):
34
	mod_endpoints.append(func.__name__)
35
36
	@wraps(func)
	def decorator(*args, **kwargs):
37
		if not ismod():
38
39
40
41
42
43
			flash('Diese Funktion ist nur für Moderatoren verfügbar!')
			return redirect(url_for('login', ref=request.url))
		else:
			return func(*args, **kwargs)
	return decorator

44
app.jinja_env.globals['navbar'] = []
45
def register_navbar(name, icon=None):
46
	def wrapper(func):
47
48
49
		endpoint = func.__name__
		app.jinja_env.globals['navbar'].append((endpoint, name, icon,
					not endpoint in mod_endpoints))
50
51
52
		return func
	return wrapper

53
@app.route('/')
54
@register_navbar('Home', icon='home')
55
def index():
56
	return render_template('index.html', latestvideos=query('''
57
				SELECT lectures.*, max(videos.time_updated) AS lastvidtime, courses.short, courses.downloadable, courses.title AS coursetitle
58
59
60
				FROM lectures
				LEFT JOIN videos ON (videos.lecture_id = lectures.id)
				LEFT JOIN courses on (courses.id = lectures.course_id)
61
				WHERE (? OR (courses.visible AND courses.listed AND lectures.visible AND videos.visible))
62
63
				GROUP BY videos.lecture_id
				ORDER BY lastvidtime DESC
Andreas Valder's avatar
.    
Andreas Valder committed
64
				LIMIT 6
65
			''', ismod()))
66

Andreas Valder's avatar
Andreas Valder committed
67
@app.route('/videos')
68
@register_navbar('Videos', icon='film')
Andreas Valder's avatar
Andreas Valder committed
69
def videos():
70
71
72
73
	courses = query('SELECT * FROM courses WHERE (? OR (visible AND listed))', ismod())
	for course in courses:
		if course['semester'] == '':
			course['semester'] = 'zeitlos'
Andreas Valder's avatar
Andreas Valder committed
74
75
76
	groupedby = request.args.get('groupedby')
	if groupedby not in ['title','semester','organizer']:
		groupedby = 'semester'
77
	return render_template('videos.html', courses=courses, groupedby=groupedby)
Andreas Valder's avatar
Andreas Valder committed
78
79

@app.route('/faq')
80
@register_navbar('FAQ', icon='question-sign')
Andreas Valder's avatar
Andreas Valder committed
81
def faq():
82
	return render_template('faq.html')
Andreas Valder's avatar
Andreas Valder committed
83

Andreas Valder's avatar
Andreas Valder committed
84
85
@app.route('/play')
def play():
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
	if not 'lectureid' in request.args:
		return redirect(url_for('videos'))
	id = request.args.get('lectureid')
	lectures = query('SELECT * FROM lectures WHERE id = ? AND (? OR visible)', id, ismod())
	videos = query('SELECT * FROM videos WHERE lecture_id = ? AND (? OR visible)', id, ismod())
	if not lectures:
		flash('Diese Vorlesung existiert nicht!')
		return app.view_functions['videos'](), 404
	if not videos:
		flash('Zu dieser Vorlesung wurden noch keine Videos veröffentlicht!')
	courses = query('SELECT * FROM courses WHERE id = ? AND (? OR (visible AND listed))', lectures[0]['course_id'], ismod())
	if not courses:
		flash('Diese Veranstaltung existiert nicht!')
		return app.view_functions['videos'](), 404
	return render_template('play.html', course=courses[0], lecture=lectures[0], videos=videos)
101
102
103
104
105
106
107

@app.route('/search')
def search():
	if 'q' not in request.args:
		return redirect(url_for('index'))
	q = request.args['q']
	courses = searchquery(q, '*', ['title', 'short', 'organizer', 'subject', 'description'],
108
			'courses', 'WHERE (? OR (visible AND listed)) GROUP BY id ORDER BY _score DESC, semester DESC LIMIT 20', ismod())
109
110
111
	lectures = searchquery(q, 'lectures.*, courses.visible AS coursevisible, courses.listed, courses.short, courses.downloadable, courses.title AS coursetitle',
			['lectures.title', 'lectures.comment', 'lectures.speaker', 'courses.short'],
			'lectures LEFT JOIN courses on (courses.id = lectures.course_id)',
112
			'WHERE (? OR (coursevisible AND listed AND visible)) GROUP BY id ORDER BY _score DESC, time DESC LIMIT 30', ismod())
113
	return render_template('search.html', searchtext=request.args['q'], courses=courses, lectures=lectures)
Andreas Valder's avatar
Andreas Valder committed
114

Andreas Valder's avatar
Andreas Valder committed
115
116
@app.route('/course')
def course():
117
118
119
120
121
122
123
124
125
	if not 'courseid' in request.args:
		return redirect(url_for('videos'))
	id = request.args['courseid']
	courses = query('SELECT * FROM courses WHERE handle = ? AND (? OR visible)', id, ismod())
	if not courses:
		flash('Diese Veranstaltung existiert nicht!')
		return app.view_functions['videos'](), 404
	lectures = query('SELECT * FROM lectures WHERE course_id = ? AND (? OR visible)', courses[0]['id'], ismod())
	videos = query('''
Andreas Valder's avatar
Andreas Valder committed
126
			SELECT videos.*, (videos.downloadable AND courses.downloadable) as downloadable, formats.description AS format_description
127
128
129
			FROM videos
			JOIN lectures ON (videos.lecture_id = lectures.id)
			JOIN formats ON (videos.video_format = formats.id)
Andreas Valder's avatar
Andreas Valder committed
130
			JOIN courses ON (lectures.course_id = courses.id)
131
			WHERE lectures.course_id= ? AND (? OR videos.visible)
132
			ORDER BY formats.prio DESC
133
			''', courses[0]['id'], ismod())
134
	return render_template('course.html', course=courses[0], lectures=lectures, videos=videos)
Andreas Valder's avatar
Andreas Valder committed
135

136
@app.route('/login', methods=['GET', 'POST'])
Julian Rother's avatar
Julian Rother committed
137
def login():
138
139
	if request.method == 'GET':
		return render_template('login.html')
Julian Rother's avatar
Julian Rother committed
140
	user, groups = ldapauth(request.form.get('user'), request.form.get('password'))
Julian Rother's avatar
Julian Rother committed
141
	if user and 'users' in groups:
Julian Rother's avatar
Julian Rother committed
142
		session['user'] = ldapget(user)
143
144
	else:
		flash('Login fehlgeschlagen!')
Julian Rother's avatar
Julian Rother committed
145
	return redirect(request.values.get('ref', url_for('index')))
Julian Rother's avatar
Julian Rother committed
146

Julian Rother's avatar
Julian Rother committed
147
@app.route('/logout', methods=['GET', 'POST'])
Julian Rother's avatar
Julian Rother committed
148
149
def logout():
	session.pop('user')
Julian Rother's avatar
Julian Rother committed
150
	return redirect(request.values.get('ref', url_for('index')))
Julian Rother's avatar
Julian Rother committed
151

152
@app.route('/edit', methods=['GET', 'POST'])
153
@mod_required
Julian Rother's avatar
Julian Rother committed
154
155
156
def edit():
	tabs = {
		'courses': ('courses_data', 'id', ['visible', 'listed', 'title', 'short',
Andreas Valder's avatar
Andreas Valder committed
157
				'handle', 'organizer', 'subject', 'semester', 'downloadable',
Julian Rother's avatar
Julian Rother committed
158
159
				'internal', 'responsible']),
		'lectures': ('lectures_data', 'id', ['visible', 'title', 'comment',
Andreas Valder's avatar
Andreas Valder committed
160
				'internal', 'speaker', 'place', 'time', 'duration', 'jumplist']),
161
		'site_texts': ('site_texts', 'key', ['value']),
Andreas Valder's avatar
Andreas Valder committed
162
		'videos': ('videos_data', 'id', ['visible'])
Julian Rother's avatar
Julian Rother committed
163
	}
164
	query('BEGIN')
165
	if request.is_json:
Julian Rother's avatar
Julian Rother committed
166
167
168
169
		changes = request.get_json().items()
	else:
		changes = request.args.items()
	for key, val in changes:
170
		table, id, column = key.split('.', 2)
Julian Rother's avatar
Julian Rother committed
171
172
173
174
		assert table in tabs
		assert column in tabs[table][2]
		query('UPDATE %s SET %s = ? WHERE %s = ?'%(tabs[table][0], column,
					tabs[table][1]), val, id)
175
	query('COMMIT')
176
	return "OK", 200
Julian Rother's avatar
Julian Rother committed
177

178
179
180
181
182
@app.route('/auth')
def auth(): # For use with nginx auth_request
	if 'X-Original-Uri' not in request.headers:
		return 'Internal Server Error', 500
	url = request.headers['X-Original-Uri'].lstrip(config['VIDEOPREFIX'])
183
184
	ip = request.headers.get('X-Real-IP', '')
	videos = query('''SELECT videos.path, videos.id
185
186
187
188
189
190
			FROM videos
			JOIN lectures ON (videos.lecture_id = lectures.id)
			JOIN courses ON (lectures.course_id = courses.id)
			WHERE videos.path = ?
			AND (? OR (courses.visible AND lectures.visible AND videos.visible))''',
			url, ismod())
191
	if videos and (url.startswith('pub') or ismod()):
192
		query('INSERT INTO log VALUES (?, "", ?, "video", ?, ?)', ip, datetime.now(), videos[0]['id'], url)
193
		return "OK", 200
194
	elif url.endswith('jpg'):
195
196
197
		return "OK", 200
	else:
		return "Not allowed", 403
Julian Rother's avatar
Julian Rother committed
198

Andreas Valder's avatar
Andreas Valder committed
199
200

@app.route('/schedule')
201
202
@register_navbar('Drehplan', 'calendar')
@mod_required
Andreas Valder's avatar
Andreas Valder committed
203
def schedule():
204
205
206
207
208
209
	start = date.today() - timedelta(days=date.today().weekday()+7*20)
	days = [{'date': start, 'lectures': [], 'atonce':0 }]
	for i in range(1,7):
		days.append({'date': days[i-1]['date'] + timedelta(days=1), 'lectures':[], 'atonce':0 })
	for i in days:
		# date and times are burning in sqlite
210
211
		s = datetime.combine(i['date'],time())
		e = datetime.combine(i['date'],time(23,59))
212
213
214
215
216
217
		i['lectures'] = query ('SELECT lectures.*,courses.* FROM lectures JOIN courses ON (lectures.course_id = courses.id) WHERE (time < ?) AND (time > ?) ORDER BY time ASC',e,s);
		# sweepline to find out how many lectures overlap
		maxcol=0;
		curcol=0;
		freecol=[];
		for l in i['lectures']:
218
			l['time_asdate'] = datetime.strptime(l['time'],'%Y-%m-%d %H:%M:%S')
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
			l['end_asdate'] = l['time_asdate']+timedelta(minutes=l['duration'])
		for l in sorted([(l['time_asdate'],True,l) for l in i['lectures']] + [(l['end_asdate'],False,l) for l in i['lectures']],key=lambda t:(t[0],t[1])):
			if l[1]:
				curcol += 1
				if curcol > maxcol:
					maxcol = curcol
				if len(freecol) == 0:
					freecol.append(maxcol)
				l[2]['schedule_col'] = freecol.pop()
			else:
				curcol -= 1
				freecol.append(l[2]['schedule_col'])
		i['maxcol'] = max(maxcol,1)
	times=[]
	for i in range(0,int(60*24/15)):
		t = i*15
		times.append(time(int(t/60),t%60))
	
	return render_template('schedule.html',days=days,times=times)