diff --git a/server.py b/server.py index 5fac9fcd6343591a400536ad0b308fb000701401..153082cc41020407228658fdb0673dddefc24968 100755 --- a/server.py +++ b/server.py @@ -1,16 +1,35 @@ #!/bin/python from flask import Flask, render_template, g import mysql.connector +import sqlite3 import config app = Flask(__name__) +# Row wrapper for sqlite +def dict_factory(cursor, row): + d = {} + for idx, col in enumerate(cursor.description): + if type(row[idx]) == str: + d[col[0].split('.')[-1]] = row[idx].replace('\\n','\n') + else: + d[col[0].split('.')[-1]] = row[idx] + return d + def query(operation, *params): - if 'db' not in g or not g.db.is_connected(): - g.db = mysql.connector.connect(user=config.db_user, password=config.db_passwd, host=config.db_host, database=config.db_db) - cur = g.db.cursor(dictionary=True) - cur.execute(operation, params) - return cur.fetchall() + if config.db_engine == 'mysql': + if 'db' not in g or not g.db.is_connected(): + g.db = mysql.connector.connect(user=config.db_user, password=config.db_passwd, host=config.db_host, database=config.db_db) + cur = g.db.cursor(dictionary=True) + cur.execute(operation.replace('?', '%s'), params) + return cur.fetchall() + else: + if 'db' not in g or not g.db.is_connected(): + g.db = sqlite3.connect(config.db_file) + g.db.row_factory = dict_factory + cur = g.db.cursor() + cur.execute(operation, params) + return cur.fetchall() @app.route('/') def index(): @@ -19,8 +38,7 @@ def index(): FROM lectures LEFT JOIN videos ON (videos.lecture_id = lectures.id) LEFT JOIN courses on (courses.id = lectures.course_id) - WHERE (videos.time_updated >= SUBDATE(NOW(), 14)) - AND (%s OR (courses.visible AND courses.listed AND lectures.visible AND videos.visible)) + WHERE (? OR (courses.visible AND courses.listed AND lectures.visible AND videos.visible)) GROUP BY videos.lecture_id ORDER BY lastvidtime DESC LIMIT 5