diff --git a/db.py b/db.py index 4213d1709d4d0704e44ccdbf2ecc85356e0e906c..55484b9e831550c75331bff91e1445236505ea46 100644 --- a/db.py +++ b/db.py @@ -1,6 +1,7 @@ from server import * import sqlite3 import re +import datetime if config['DB_ENGINE'] == 'sqlite': created = not os.path.exists(config['SQLITE_DB']) @@ -23,6 +24,25 @@ def dict_factory(cursor, row): d[col[0].split('.')[-1]] = row[idx] return d +# From sqlite3 module, but with error catching +def sqlite_convert_timestamp(val): + try: + datepart, timepart = val.split(b" ") + year, month, day = map(int, datepart.split(b"-")) + timepart_full = timepart.split(b".") + hours, minutes, seconds = map(int, timepart_full[0].split(b":")) + if len(timepart_full) == 2: + microseconds = int('{:0<6.6}'.format(timepart_full[1].decode())) + else: + microseconds = 0 + val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds) + except: + val = None + return val + +sqlite3.register_converter('datetime', sqlite_convert_timestamp) +sqlite3.register_converter('timestamp', sqlite_convert_timestamp) + def query(operation, *params): if config['DB_ENGINE'] == 'mysql': import mysql.connector @@ -33,7 +53,7 @@ def query(operation, *params): request.db.execute(operation.replace('?', '%s'), params) elif config['DB_ENGINE'] == 'sqlite': if 'db' not in g: - g.db = sqlite3.connect(config['SQLITE_DB']) + g.db = sqlite3.connect(config['SQLITE_DB'], detect_types=sqlite3.PARSE_DECLTYPES) g.db.row_factory = dict_factory g.db.isolation_level = None if not hasattr(request, 'db'): diff --git a/server.py b/server.py index 9520a0dc5e26d01c52f2f12bb5b3f3c1c4f8fe39..81d2316d65b225bb9b996e9d9afe76b36dc9d7e8 100755 --- a/server.py +++ b/server.py @@ -233,9 +233,8 @@ def schedule(): curcol=0; freecol=[]; for l in i['lectures']: - l['time_asdate'] = datetime.strptime(l['time'],'%Y-%m-%d %H:%M:%S') - l['end_asdate'] = l['time_asdate']+timedelta(minutes=l['duration']) - for l in sorted([(l['time_asdate'],True,l) for l in i['lectures']] + [(l['end_asdate'],False,l) for l in i['lectures']],key=lambda t:(t[0],t[1])): + l['time_end'] = l['time']+timedelta(minutes=l['duration']) + for l in sorted([(l['time'],True,l) for l in i['lectures']] + [(l['time_end'],False,l) for l in i['lectures']],key=lambda t:(t[0],t[1])): if l[1]: curcol += 1 if curcol > maxcol: @@ -270,5 +269,5 @@ def stats(): @register_navbar('Changelog', 'book') @mod_required def log(): - changelog = query('SELECT * FROM changelog LEFT JOIN users ON (changelog.who = users.id) ORDER BY "when" DESC LIMIT 10') + changelog = query('SELECT * FROM changelog LEFT JOIN users ON (changelog.who = users.id) ORDER BY `when` DESC LIMIT 10') return render_template('log.html', changelog=changelog) diff --git a/templates/schedule.html b/templates/schedule.html index 42328d4bd527fbab070ddee436528edc77ff7335..fd0753cecace35f4dcea3cc57ea6c98269b1631a 100644 --- a/templates/schedule.html +++ b/templates/schedule.html @@ -14,15 +14,15 @@ {% if ((loop.index - 1) is divisibleby 4) %} <td rowspan="4" style="vertical-align: top;">{{ t.strftime("%H:%M") }}</td> {% endif %} {% for d in days if (d.index < 5) or (d.lectures|length) > 0 %} {% for i in range(1,d.maxcol+1) %} - {% for l in d.lectures|selectattr('schedule_col','equalto',i) if (((l.time_asdate.time() > t) and (l.time_asdate.time() < times[time_loop.index+1])) != (l.time_asdate.time() == t ) ) %} + {% for l in d.lectures|selectattr('schedule_col','equalto',i) if (((l.time.time() > t) and (l.time.time() < times[time_loop.index+1])) != (l.time.time() == t ) ) %} <td rowspan="{{l.duration / 15}}" style="background: lightgrey;"> <p class="small"> <strong><a href="{{url_for('course', id=l['course_id'])}}#lecture-{{l.id}}">{{l.short}}</a></strong><br> - {{l.time_asdate.strftime("%H:%M")}} - {{l.end_asdate.strftime("%H:%M")}}<br> + {{l.time.strftime("%H:%M")}} - {{l.time_end.strftime("%H:%M")}}<br> {{l.place}}</p> </td> {% else %} - {% for l in d.lectures|selectattr('schedule_col','equalto',i) if (l.time_asdate.time() < t) and (l.end_asdate.time() > t) %} + {% for l in d.lectures|selectattr('schedule_col','equalto',i) if (l.time.time() < t) and (l.time_end.time() > t) %} {% else %} <td></td> {% endfor %}