Commit bdd28846 authored by Julian Rother's avatar Julian Rother

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 *
import datetime
@register_navbar('Schnittfortschritt User', icon='spinner', iconlib='fa', userendpoint=True, endpoint='cutprogress_user')
@register_navbar('Schnittfortschritt', icon='spinner', iconlib='fa')
......@@ -7,19 +6,15 @@ import datetime
@app.route('/internal/user/<int:user>/cutprogress', endpoint='cutprogress_user')
@mod_required
def cutprogress(user=None):
# use request argument, default to latest semester
allsemester = query('SELECT DISTINCT semester from courses ORDER BY semester DESC')
semester = request.values.get('semester', allsemester[0]['semester'])
# find courses for semester
courses = query('''
SELECT courses.id, courses.handle, courses.short
FROM courses
WHERE semester = ?
ORDER by id DESC
''', semester)
# list of people responsible for course
# Fetch list of people responsible for every course
for course in courses:
people = query('''
SELECT users.*
......@@ -28,27 +23,22 @@ def cutprogress(user=None):
WHERE responsible.course_id = ?
ORDER BY users.realname ASC
''', course['id'])
if not people:
people = [{ 'realname': 'Niemand', 'id': -1 }]
course['responsible'] = people
# 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
# Fetch lectures for all courses
lectures = []
for c in courses:
lectures += query('''
SELECT
lectures.id,
lectures.course_id,
lectures.time, DATE(lectures.time) as date,
lectures.time,
lectures.title,
COUNT(videos.id) as videos_total,
COUNT(videos.visible) as videos_visible
......@@ -56,25 +46,18 @@ def cutprogress(user=None):
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 lectures.time <= ?
AND NOT lectures.norecording
GROUP BY lectures.id
ORDER BY date DESC, lectures.time ASC, lectures.id ASC
''', c['id'])
# 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
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)
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
# Sort into cells
tablebody = [
{
'date': date, # row header
......@@ -83,23 +66,19 @@ def cutprogress(user=None):
[ # 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']])
if lecture['course_id'] == course['id'] and lecture['time'].date() == date
]
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',
# dropdown selection
allsemester=allsemester, # options
semester=semester, # choice
user=query('SELECT * FROM users WHERE id = ?', user)[0] if user else None,
# content
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