Commit 30a210b6 authored by Julian Rother's avatar Julian Rother
Browse files

Added sqlite support and made query compatible

parent db535aca
#!/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
......
Supports Markdown
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