cutprogress.py 2.62 KB
Newer Older
1 2
from server import *

3
@register_navbar('Schnittfortschritt User', icon='spinner', iconlib='fa', userendpoint=True, endpoint='cutprogress_user')
4
@register_navbar('Schnittfortschritt', icon='spinner', iconlib='fa')
5
@app.route('/internal/cutprogress', endpoint="cutprogress")
6
@app.route('/internal/user/<int:user>/cutprogress', endpoint='cutprogress_user')
7 8
@mod_required
def cutprogress(user=None):
Andreas Valder's avatar
Andreas Valder committed
9
	allsemester = query('SELECT DISTINCT semester from courses ORDER BY semester DESC')
10
	semester = request.values.get('semester', allsemester[0]['semester'])
11
	courses = query('''
12
		SELECT courses.id, courses.handle, courses.short
13 14 15 16
		FROM courses
		WHERE semester = ?
		ORDER by id DESC
		''', semester)
17
	# Fetch list of people responsible for every course
18 19 20 21 22 23 24 25 26
	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:
27
			people = [{ 'realname': 'Niemand', 'id': -1 }]
28
		course['responsible'] = people
29 30 31 32 33
	if user is not None:
		courses = [
			c for c in courses
			if user in (r['id'] for r in c['responsible'])
		]
34
	# Fetch lectures for all courses
35 36 37 38 39 40
	lectures = []
	for c in courses:
		lectures += query('''
			SELECT
				lectures.id,
				lectures.course_id,
41
				lectures.time,
42 43 44 45 46 47 48
				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 = ?
49
				AND lectures.time <= ?
50 51
				AND NOT lectures.norecording
			GROUP BY lectures.id
52 53 54 55
			ORDER BY lectures.time ASC, lectures.id ASC
			''', c['id'], datetime.now())
	# Generate list of days, figure out when weeks change
	dates = sorted({row['time'].date() for row in lectures}, reverse=True)
56 57 58 59
	is_new_weeks = [
		False if (i == 0) else thisdate.isocalendar()[1] != dates[i-1].isocalendar()[1]
		for i, thisdate in enumerate(dates)
	]
60
	# Sort into cells
61 62 63 64 65 66 67 68
	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
69
					if lecture['course_id'] == course['id'] and lecture['time'].date() == date
70 71 72 73
				]
				for course in courses
			]
		}
74
		for date, is_new_week in zip(dates, is_new_weeks)
75
	]
Andreas Valder's avatar
Andreas Valder committed
76
	return render_template('cutprogress.html',
77 78 79
		# dropdown selection
		allsemester=allsemester, # options
		semester=semester, # choice
Christoph Rackwitz's avatar
Christoph Rackwitz committed
80
		user=query('SELECT * FROM users WHERE id = ?', user)[0] if user else None,
81 82
		# content
		courses=courses,
83
		tablebody=tablebody
84
	)