cutprogress.py 2.93 KB
Newer Older
1
from server import *
2
import datetime
3

4
@register_navbar('Schnittfortschritt User', icon='spinner', iconlib='fa', userendpoint=True, endpoint='cutprogress_user')
5
@register_navbar('Schnittfortschritt', icon='spinner', iconlib='fa')
Andreas Valder's avatar
Andreas Valder committed
6
@app.route('/internal/cutprogress', endpoint="cutprogress")
7
@app.route('/internal/user/<int:user>/cutprogress', endpoint='cutprogress_user')
8
9
@mod_required
def cutprogress(user=None):
10
	# use request argument, default to latest semester
Andreas Valder's avatar
Andreas Valder committed
11
	allsemester = query('SELECT DISTINCT semester from courses ORDER BY semester DESC')
12
	semester = request.values.get('semester', allsemester[0]['semester'])
13

14
	# find courses for semester
15
	courses = query('''
16
		SELECT courses.id, courses.handle, courses.short
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
		FROM courses
		WHERE semester = ?
		ORDER by id DESC
		''', semester)

	# list of people responsible for course
	for course in courses:
		people = query('''
			SELECT users.*
			FROM users
			JOIN responsible ON responsible.user_id = users.id
			WHERE responsible.course_id = ?
			ORDER BY users.realname ASC
			''', course['id'])

		if not people:
33
			people = [{ 'realname': 'Niemand', 'id': -1 }]
34
35
36

		course['responsible'] = people

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
	# filter for responsibility
	if user is not None:
		courses = [
			c for c in courses
			if user in (r['id'] for r in c['responsible'])
		]

	# fetch lectures for courses
	lectures = []
	for c in courses:
		lectures += query('''
			SELECT
				lectures.id,
				lectures.course_id,
				lectures.time, DATE(lectures.time) as date,
				lectures.title,
				COUNT(videos.id) as videos_total,
				COUNT(videos.visible) as videos_visible
			FROM lectures
			JOIN courses ON courses.id = lectures.course_id
			LEFT JOIN videos ON lectures.id = videos.lecture_id
			WHERE courses.id = ?
				AND date <= DATE('now')
				AND NOT lectures.norecording
			GROUP BY lectures.id
			ORDER BY date DESC, lectures.time ASC, lectures.id ASC
			''', c['id'])
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

	# sort dates, figure out when weeks change
	dates = sorted({
		datetime.datetime.strptime(row['date'], '%Y-%m-%d')
		for row in lectures
	}, reverse=True)

	# check week numbers
	is_new_weeks = [
		False if (i == 0) else thisdate.isocalendar()[1] != dates[i-1].isocalendar()[1]
		for i, thisdate in enumerate(dates)
	]

	# sort into cells
	tablebody = [
		{
			'date': date, # row header
			'is_new_week': is_new_week,
			'cells': [ # this is the body of the row
				[ # this list is a cell
					lecture
					for lecture in lectures
					if lecture['course_id'] == course['id'] and datetime.datetime.strptime(lecture['date'], '%Y-%m-%d') == date
					and (user is None or user in [r['id'] for r in course['responsible']])
				]
				for course in courses
			]
		}
		for date,is_new_week in zip(dates, is_new_weeks)
	]

Andreas Valder's avatar
Andreas Valder committed
95
	return render_template('cutprogress.html',
96
97
98
		# dropdown selection
		allsemester=allsemester, # options
		semester=semester, # choice
Christoph Rackwitz's avatar
Christoph Rackwitz committed
99
		user=query('SELECT * FROM users WHERE id = ?', user)[0] if user else None,
100
101
102
103
104
105

		# content
		courses=courses,
		dates=dates,
		tablebody=tablebody,
	)