Skip to content
Snippets Groups Projects
Commit 8b65b7b1 authored by Julian Rother's avatar Julian Rother
Browse files

Improved compatability with mysql

parent 9b84296c
Branches
No related tags found
No related merge requests found
from server import * from server import *
import sqlite3 import sqlite3
import re import re
import datetime
if config['DB_ENGINE'] == 'sqlite': if config['DB_ENGINE'] == 'sqlite':
created = not os.path.exists(config['SQLITE_DB']) created = not os.path.exists(config['SQLITE_DB'])
...@@ -23,6 +24,25 @@ def dict_factory(cursor, row): ...@@ -23,6 +24,25 @@ def dict_factory(cursor, row):
d[col[0].split('.')[-1]] = row[idx] d[col[0].split('.')[-1]] = row[idx]
return d 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): def query(operation, *params):
if config['DB_ENGINE'] == 'mysql': if config['DB_ENGINE'] == 'mysql':
import mysql.connector import mysql.connector
...@@ -33,7 +53,7 @@ def query(operation, *params): ...@@ -33,7 +53,7 @@ def query(operation, *params):
request.db.execute(operation.replace('?', '%s'), params) request.db.execute(operation.replace('?', '%s'), params)
elif config['DB_ENGINE'] == 'sqlite': elif config['DB_ENGINE'] == 'sqlite':
if 'db' not in g: 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.row_factory = dict_factory
g.db.isolation_level = None g.db.isolation_level = None
if not hasattr(request, 'db'): if not hasattr(request, 'db'):
......
...@@ -233,9 +233,8 @@ def schedule(): ...@@ -233,9 +233,8 @@ def schedule():
curcol=0; curcol=0;
freecol=[]; freecol=[];
for l in i['lectures']: for l in i['lectures']:
l['time_asdate'] = datetime.strptime(l['time'],'%Y-%m-%d %H:%M:%S') l['time_end'] = l['time']+timedelta(minutes=l['duration'])
l['end_asdate'] = l['time_asdate']+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])):
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])):
if l[1]: if l[1]:
curcol += 1 curcol += 1
if curcol > maxcol: if curcol > maxcol:
...@@ -270,5 +269,5 @@ def stats(): ...@@ -270,5 +269,5 @@ def stats():
@register_navbar('Changelog', 'book') @register_navbar('Changelog', 'book')
@mod_required @mod_required
def log(): 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) return render_template('log.html', changelog=changelog)
...@@ -14,15 +14,15 @@ ...@@ -14,15 +14,15 @@
{% if ((loop.index - 1) is divisibleby 4) %} <td rowspan="4" style="vertical-align: top;">{{ t.strftime("%H:%M") }}</td> {% endif %} {% 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 d in days if (d.index < 5) or (d.lectures|length) > 0 %}
{% for i in range(1,d.maxcol+1) %} {% 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;"> <td rowspan="{{l.duration / 15}}" style="background: lightgrey;">
<p class="small"> <p class="small">
<strong><a href="{{url_for('course', id=l['course_id'])}}#lecture-{{l.id}}">{{l.short}}</a></strong><br> <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> {{l.place}}</p>
</td> </td>
{% else %} {% 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 %} {% else %}
<td></td> <td></td>
{% endfor %} {% endfor %}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment