2017-08-13 19:43:33 -04:00
|
|
|
import base64
|
2017-08-15 22:57:44 -04:00
|
|
|
import sqlite3
|
2017-08-13 19:43:33 -04:00
|
|
|
|
2017-09-28 23:16:36 -04:00
|
|
|
import math
|
|
|
|
import time
|
|
|
|
|
2017-09-30 22:35:13 -04:00
|
|
|
import utils
|
|
|
|
|
2017-09-09 13:53:58 -04:00
|
|
|
conn = None
|
|
|
|
|
2017-08-13 19:43:33 -04:00
|
|
|
def dict_factory(cursor, row):
|
|
|
|
d = {}
|
|
|
|
for idx, col in enumerate(cursor.description):
|
|
|
|
if isinstance(row[idx], buffer):
|
|
|
|
d[col[0]] = base64.b64encode(row[idx])
|
|
|
|
else:
|
|
|
|
d[col[0]] = row[idx]
|
|
|
|
|
|
|
|
return d
|
|
|
|
|
2017-08-15 22:57:44 -04:00
|
|
|
def connect(documentPath):
|
|
|
|
global conn
|
|
|
|
conn = sqlite3.connect(documentPath)
|
|
|
|
conn.row_factory = dict_factory
|
2017-08-13 19:43:33 -04:00
|
|
|
|
|
|
|
def insert(tablename, rec):
|
2017-08-27 14:39:26 -04:00
|
|
|
# FIXME: SQL injection!
|
2017-08-13 19:43:33 -04:00
|
|
|
keys = ','.join(rec.keys())
|
|
|
|
question_marks = ','.join(list('?'*len(rec)))
|
|
|
|
values = tuple(rec.values())
|
|
|
|
cursor = execute('INSERT INTO '+tablename+' ('+keys+') VALUES ('+question_marks+')', values)
|
|
|
|
return cursor.lastrowid
|
|
|
|
|
2017-09-12 21:06:09 -04:00
|
|
|
def setOption(name, value):
|
|
|
|
execute("UPDATE options SET opt_value = ? WHERE opt_name = ?", [value, name])
|
|
|
|
|
|
|
|
def getOption(name):
|
|
|
|
return getSingleResult("SELECT opt_value FROM options WHERE opt_name = ?", [name])['opt_value']
|
|
|
|
|
2017-09-28 23:16:36 -04:00
|
|
|
def addAudit(category, request = None, note_id = None, change_from = None, change_to = None, comment = None):
|
2017-09-30 22:35:13 -04:00
|
|
|
now = utils.nowTimestamp()
|
2017-09-28 23:16:36 -04:00
|
|
|
|
|
|
|
browser_id = None
|
|
|
|
|
|
|
|
if request:
|
|
|
|
browser_id = request.headers['x-browser-id']
|
|
|
|
|
|
|
|
execute("INSERT INTO audit_log (date_modified, category, browser_id, note_id, change_from, change_to, comment)"
|
|
|
|
" VALUES (?, ?, ?, ?, ?, ?, ?)", [now, category, browser_id, note_id, change_from, change_to, comment])
|
|
|
|
|
2017-09-30 21:18:23 -04:00
|
|
|
def deleteRecentAudits(category, request, note_id):
|
|
|
|
browser_id = request.headers['x-browser-id']
|
|
|
|
|
2017-09-30 22:35:13 -04:00
|
|
|
delete_cutoff = utils.nowTimestamp() - 10 * 60;
|
2017-09-30 21:18:23 -04:00
|
|
|
|
|
|
|
execute("DELETE FROM audit_log WHERE category = ? AND browser_id = ? AND note_id = ? AND date_modified > ?",
|
|
|
|
[category, browser_id, note_id, delete_cutoff])
|
|
|
|
|
2017-08-13 19:43:33 -04:00
|
|
|
def delete(tablename, note_id):
|
|
|
|
execute("DELETE FROM " + tablename + " WHERE note_id = ?", [note_id])
|
|
|
|
|
|
|
|
def execute(sql, params=[]):
|
|
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute(sql, params)
|
|
|
|
return cursor
|
|
|
|
|
2017-10-09 16:50:36 -04:00
|
|
|
def execute_script(sql):
|
|
|
|
cursor = conn.cursor()
|
|
|
|
cursor.executescript(sql)
|
|
|
|
return cursor
|
|
|
|
|
2017-08-13 19:43:33 -04:00
|
|
|
def getResults(sql, params=[]):
|
|
|
|
cursor = conn.cursor()
|
|
|
|
query = cursor.execute(sql, params)
|
|
|
|
return query.fetchall()
|
|
|
|
|
|
|
|
def getSingleResult(sql, params=()):
|
|
|
|
cursor = conn.cursor()
|
|
|
|
query = cursor.execute(sql, params)
|
|
|
|
return query.fetchone()
|
|
|
|
|
|
|
|
def commit():
|
|
|
|
conn.commit()
|