chapters.py 2.73 KB
Newer Older
1
import json
Andreas Valder's avatar
Andreas Valder committed
2
3
from server import *

4
5
@job_handler('probe', 'probe-raw')
def import_xmp_chapters(jobid, jobtype, data, state, status):
6
	if 'lecture_id' not in data or not data.get('import-chapters', False):
7
8
9
10
11
12
13
14
15
16
17
		return
	times = set()
	# Only add new chapters, deleted chapters are taken into account here
	for chapter in query('SELECT * FROM chapters WHERE lecture_id = ?', data['lecture_id']):
		for offset in range(5):
			times.add(chapter['time']-offset)
			times.add(chapter['time']+offset)
	for chapter in status.get('xmp_chapters', []):
		if int(chapter['time']) in times:
			continue
		modify('INSERT INTO chapters (lecture_id, time, text, visible, time_created, time_updated) VALUES (?, ?, ?, 0, ?, ?)',
Andreas Valder's avatar
Andreas Valder committed
18
19
			data['lecture_id'], int(chapter['time']), chapter['text'],
			datetime.now(), datetime.now())
20

Andreas Valder's avatar
Andreas Valder committed
21
22
23
24
@app.route('/internal/newchapter/<int:lectureid>', methods=['POST', 'GET'])
def suggest_chapter(lectureid):
	time = request.values['time']
	text = request.values['text']
Andreas Valder's avatar
Andreas Valder committed
25
	assert time and text
Andreas Valder's avatar
Andreas Valder committed
26
	try:
Andreas Valder's avatar
Andreas Valder committed
27
28
		x = datetime.strptime(time, '%H:%M:%S')
		time = timedelta(hours=x.hour, minutes=x.minute, seconds=x.second).total_seconds()
Andreas Valder's avatar
Andreas Valder committed
29
30
		time = int(time)
	except ValueError:
31
32
33
		if 'ref' in request.values:
			flash('Falsches Zeitformat, "%H:%M:%S" wird erwartet. Z.B. "01:39:42" für eine Kapitel bei Stunde 1, Minute 39, Sekunde 42')
			return redirect(request.values['ref'])
Andreas Valder's avatar
Andreas Valder committed
34
35
		return 'Wrong time format, "%H:%M:%S" is expected', 400

Andreas Valder's avatar
Andreas Valder committed
36
37
38
	submitter = None
	if not ismod():
		submitter = request.environ['REMOTE_ADDR']
39
40
	lecture = query('SELECT * FROM lectures WHERE id = ?', lectureid)[0]
	course = query('SELECT * FROM courses WHERE id = ?', lecture['course_id'])[0]
Andreas Valder's avatar
Andreas Valder committed
41
42
	id = modify('INSERT INTO chapters (lecture_id, time, text, time_created, time_updated, created_by, submitted_by) VALUES (?, ?, ?, ?, ?, ?, ?)',
				lectureid, time, text, datetime.now(), datetime.now(), session.get('user', {'dbid':None})['dbid'], submitter)
43
44
	chapter = query('SELECT * FROM chapters WHERE id = ?', id)[0]
	notify_mods('chapter_submitted', course['id'], course=course, lecture=lecture, chapter=chapter)
Andreas Valder's avatar
Andreas Valder committed
45
46
	if 'ref' in request.values:
		return redirect(request.values['ref'])
47
	return 'OK', 200
Andreas Valder's avatar
Andreas Valder committed
48
49
50
51
52
53
54
55
56
57
58

@app.route('/internal/chapters/<int:lectureid>')
def chapters(lectureid):
	chapters = query("SELECT * FROM chapters WHERE lecture_id = ? AND NOT deleted AND (visible OR ?) ORDER BY time DESC", lectureid, ismod())
	if not chapters:
		return 'No chapters found', 404
	last = None
	for c in chapters:
		c['start'] = c['time']
		c['end'] = last['start'] if last else 9999
		last = c
59
	if 'json' in request.values:
60
		return Response(json.dumps([{'time': c['time'], 'text': c['text']} for c in chapters]), mimetype='application/json')
Andreas Valder's avatar
Andreas Valder committed
61
	return Response(render_template('chapters.srt', chapters=chapters), 200, {'Content-Type':'text/vtt'})