cutprogress.py 2.79 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
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
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

	# filter to single user?
	if user is not None:
		user = query('SELECT * FROM users WHERE id = ?', user)[0]

	# column headers: courses
	courses = query('''
		SELECT courses.id, courses.handle, courses.short, courses.responsible
		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:
			people = [{ 'realname': 'Niemand' }]

		course['responsible'] = people

	# fetch lectures
	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.semester = ?
			AND date <= DATE('now')
			AND NOT lectures.norecording
		GROUP BY lectures.id
		ORDER BY date DESC, lectures.time ASC, lectures.id ASC
		''', semester)

	# 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
90
	return render_template('cutprogress.html',
91
92
93
94
95
96
97
98
99
100
		# dropdown selection
		allsemester=allsemester, # options
		semester=semester, # choice
		user=user,

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