Commit bdd28846 authored by Julian Rother's avatar Julian Rother
Browse files

Cleanup and fixes for merge request crackwitz/website-cutprogress-reformat

Date and time functions in SQL are highly incompatible between different
db engines. All queries must work with sqlite and mysql/mariadb.
parent 985a5447
from server import * from server import *
import datetime
@register_navbar('Schnittfortschritt User', icon='spinner', iconlib='fa', userendpoint=True, endpoint='cutprogress_user') @register_navbar('Schnittfortschritt User', icon='spinner', iconlib='fa', userendpoint=True, endpoint='cutprogress_user')
@register_navbar('Schnittfortschritt', icon='spinner', iconlib='fa') @register_navbar('Schnittfortschritt', icon='spinner', iconlib='fa')
...@@ -7,19 +6,15 @@ import datetime ...@@ -7,19 +6,15 @@ import datetime
@app.route('/internal/user/<int:user>/cutprogress', endpoint='cutprogress_user') @app.route('/internal/user/<int:user>/cutprogress', endpoint='cutprogress_user')
@mod_required @mod_required
def cutprogress(user=None): def cutprogress(user=None):
# use request argument, default to latest semester
allsemester = query('SELECT DISTINCT semester from courses ORDER BY semester DESC') allsemester = query('SELECT DISTINCT semester from courses ORDER BY semester DESC')
semester = request.values.get('semester', allsemester[0]['semester']) semester = request.values.get('semester', allsemester[0]['semester'])
# find courses for semester
courses = query(''' courses = query('''
SELECT courses.id, courses.handle, courses.short SELECT courses.id, courses.handle, courses.short
FROM courses FROM courses
WHERE semester = ? WHERE semester = ?
ORDER by id DESC ORDER by id DESC
''', semester) ''', semester)
# Fetch list of people responsible for every course
# list of people responsible for course
for course in courses: for course in courses:
people = query(''' people = query('''
SELECT users.* SELECT users.*
...@@ -28,27 +23,22 @@ def cutprogress(user=None): ...@@ -28,27 +23,22 @@ def cutprogress(user=None):
WHERE responsible.course_id = ? WHERE responsible.course_id = ?
ORDER BY users.realname ASC ORDER BY users.realname ASC
''', course['id']) ''', course['id'])
if not people: if not people:
people = [{ 'realname': 'Niemand', 'id': -1 }] people = [{ 'realname': 'Niemand', 'id': -1 }]
course['responsible'] = people course['responsible'] = people
# filter for responsibility
if user is not None: if user is not None:
courses = [ courses = [
c for c in courses c for c in courses
if user in (r['id'] for r in c['responsible']) if user in (r['id'] for r in c['responsible'])
] ]
# Fetch lectures for all courses
# fetch lectures for courses
lectures = [] lectures = []
for c in courses: for c in courses:
lectures += query(''' lectures += query('''
SELECT SELECT
lectures.id, lectures.id,
lectures.course_id, lectures.course_id,
lectures.time, DATE(lectures.time) as date, lectures.time,
lectures.title, lectures.title,
COUNT(videos.id) as videos_total, COUNT(videos.id) as videos_total,
COUNT(videos.visible) as videos_visible COUNT(videos.visible) as videos_visible
...@@ -56,25 +46,18 @@ def cutprogress(user=None): ...@@ -56,25 +46,18 @@ def cutprogress(user=None):
JOIN courses ON courses.id = lectures.course_id JOIN courses ON courses.id = lectures.course_id
LEFT JOIN videos ON lectures.id = videos.lecture_id LEFT JOIN videos ON lectures.id = videos.lecture_id
WHERE courses.id = ? WHERE courses.id = ?
AND date <= DATE('now') AND lectures.time <= ?
AND NOT lectures.norecording AND NOT lectures.norecording
GROUP BY lectures.id GROUP BY lectures.id
ORDER BY date DESC, lectures.time ASC, lectures.id ASC ORDER BY lectures.time ASC, lectures.id ASC
''', c['id']) ''', c['id'], datetime.now())
# Generate list of days, figure out when weeks change
# sort dates, figure out when weeks change dates = sorted({row['time'].date() for row in lectures}, reverse=True)
dates = sorted({
datetime.datetime.strptime(row['date'], '%Y-%m-%d')
for row in lectures
}, reverse=True)
# check week numbers
is_new_weeks = [ is_new_weeks = [
False if (i == 0) else thisdate.isocalendar()[1] != dates[i-1].isocalendar()[1] False if (i == 0) else thisdate.isocalendar()[1] != dates[i-1].isocalendar()[1]
for i, thisdate in enumerate(dates) for i, thisdate in enumerate(dates)
] ]
# Sort into cells
# sort into cells
tablebody = [ tablebody = [
{ {
'date': date, # row header 'date': date, # row header
...@@ -83,23 +66,19 @@ def cutprogress(user=None): ...@@ -83,23 +66,19 @@ def cutprogress(user=None):
[ # this list is a cell [ # this list is a cell
lecture lecture
for lecture in lectures for lecture in lectures
if lecture['course_id'] == course['id'] and datetime.datetime.strptime(lecture['date'], '%Y-%m-%d') == date if lecture['course_id'] == course['id'] and lecture['time'].date() == date
and (user is None or user in [r['id'] for r in course['responsible']])
] ]
for course in courses for course in courses
] ]
} }
for date,is_new_week in zip(dates, is_new_weeks) for date, is_new_week in zip(dates, is_new_weeks)
] ]
return render_template('cutprogress.html', return render_template('cutprogress.html',
# dropdown selection # dropdown selection
allsemester=allsemester, # options allsemester=allsemester, # options
semester=semester, # choice semester=semester, # choice
user=query('SELECT * FROM users WHERE id = ?', user)[0] if user else None, user=query('SELECT * FROM users WHERE id = ?', user)[0] if user else None,
# content # content
courses=courses, courses=courses,
dates=dates, tablebody=tablebody
tablebody=tablebody,
) )
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment