Fork of elimage with specific modifications for my instance.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.9 KiB

#!/usr/bin/env python3
# vim: set fileencoding=utf-8:
import sqlite3
import logging
import time
from config import DB
__all__ = ['model']
def log(fn):
def wrapped(*args, **kwds):
return fn(*args, **kwds)
except Exception as e:
return wrapped
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
class Model:
def __init__(self, db):
self.conn = sqlite3.connect(db)
self.conn.row_factory = dict_factory
self.cur = self.conn.cursor()
def get_user_by_id(self, id):
row = self.cur.execute('select * from user where id=?', (id,))
return row.fetchone()
def get_user_by_ip(self, ip):
row = self.cur.execute('select * from user where ip=?', (ip,))
return row.fetchone()
def get_image_by_name(self, name):
row = self.cur.execute('select * from image where name=?', (name,))
return row.fetchone()
def get_image_by_uid(self, uid):
row = self.cur.execute('select * from image where uid=?', (uid,))
return row.fetchall()
def add_user(self, ip):
self.cur.execute('insert into user(ip) values (?)', (ip,))
return self.cur.lastrowid
def add_image(self, uid, fname, filename, size):
self.cur.execute('''insert into image (uid, name, time, filename, size)
values (?, ?, ?, ?, ?)''',
(uid, fname, int(time.time()), filename, size))
def block_user(self, id, block=1):
self.cur.execute('update user set blocked=? where id=?', (block, id))
unblock_user = lambda self, id: self.block_user(id, block=0)
def isBlocked(self, type, data):
method = getattr(self, 'get_user_by_'+type, None)
if callable(method):
row = method(data)
return row['blocked'] == 1
return None
model = Model(DB)