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.

165 lines
5.4 KiB

from datetime import date as dt
2 years ago
import os
import sqlite3
from werkzeug.security import generate_password_hash, check_password_hash
2 years ago
class User():
def __init__(self, name, pass_hash=None):
self.name = name
self.id = None
self.is_active = True
self.is_authenticated = True
self.is_anonymous = False
self.pass_hash = pass_hash
def set_password(self, password):
self.pass_hash = password
def set_id(self, ident):
self.id = ident
def check_password(self, password):
return self.pass_hash == password
def get_id(self):
return self.id
2 years ago
class Database:
def __init__(self):
self.USER_TABLE_FILE = 'USERS'
self.ENTRY_TABLE_FILE = 'ENTRIES'
self.ITEM_TABLE_FILE = 'ITEMS'
self.DB_DIR = os.path.dirname("./data/")
2 years ago
self.setup_db()
def connect(self):
"""
Connect to an existing database instance based on the object
2 years ago
attributes.
"""
path = os.path.join(self.DB_DIR, "data.db")
return sqlite3.connect(path)
def setup_db(self):
"""Creates a database with tables."""
db = self.connect()
crs = db.cursor()
query = "CREATE TABLE IF NOT EXISTS " + self.USER_TABLE_FILE + \
2 years ago
"(id INTEGER PRIMARY KEY AUTOINCREMENT," + \
"name CHAR(32) NOT NULL UNIQUE," + \
"password CHAR(32) NOT NULL)"
crs.execute(query)
query = "CREATE TABLE IF NOT EXISTS " + self.ITEM_TABLE_FILE + \
"(id INTEGER PRIMARY KEY AUTOINCREMENT," + \
"name CHAR(32) NOT NULL UNIQUE)"
crs.execute(query)
query = "CREATE TABLE IF NOT EXISTS " + self.ENTRY_TABLE_FILE + \
"(id INTEGER PRIMARY KEY AUTOINCREMENT," + \
"item_id INTEGER NOT NULL REFERENCES " + self.ITEM_TABLE_FILE + "(id)," + \
"date CHAR(4) NOT NULL," + \
"text TEXT NOT NULL," + \
"rating INTEGER NOT NULL," +\
"user_id INTEGER REFERENCES " + self.USER_TABLE_FILE + "(id),"\
"reviewed CHAR(10) NOT NULL)"
crs.execute(query)
db.commit()
2 years ago
def insert_user(self, user):
if self.get_user_by_name(user.name) is None and user.pass_hash is not None:
2 years ago
db = self.connect()
crs = db.cursor()
2 years ago
query = "INSERT INTO " + self.USER_TABLE_FILE + \
"(`name`,`password`)" + \
2 years ago
"VALUES (?, ?) ON CONFLICT DO NOTHING"
crs.execute(query, (user.name, user.pass_hash))
2 years ago
db.commit()
return crs.lastrowid
return None
2 years ago
def insert_entry(self, name, date, text, rating, user_id=None):
db = self.connect()
crs = db.cursor()
query = "INSERT OR IGNORE INTO " + self.ITEM_TABLE_FILE + \
"(`name`)" + "VALUES (?)"
crs.execute(query, (name, ))
query = "SELECT id FROM " + self.ITEM_TABLE_FILE + \
" WHERE name = ?"
crs.execute(query, (name, ))
item_id = crs.fetchone()[0]
reviewed = dt.today().strftime('%Y-%m-%d')
2 years ago
query = "INSERT INTO " + self.ENTRY_TABLE_FILE + \
"(`item_id`,`date`, `text`, `rating`, `user_id`, `reviewed`)" + \
2 years ago
"VALUES (?, ?, ?, ?, ?, ?)"
crs.execute(query, (item_id, date, text, rating, user_id, reviewed))
db.commit()
return crs.lastrowid
def delete_entry(self, ident):
db = self.connect()
crs = db.cursor()
query = "DELETE FROM " + self.ENTRY_TABLE_FILE + " WHERE id = ?"
crs.execute(query, (ident, ))
db.commit()
return crs.lastrowid
def get_entries(self):
db = self.connect()
crs = db.cursor()
query = "SELECT * FROM " + self.ENTRY_TABLE_FILE
crs.execute(query)
return crs.fetchall()
def get_entry_by_id(self, ident):
db = self.connect()
crs = db.cursor()
query = "SELECT * FROM " + self.ENTRY_TABLE_FILE + " WHERE id = ?"
crs.execute(query, (ident, ))
return crs.fetchone()
def get_entries_by_name(self, name):
db = self.connect()
crs = db.cursor()
query = "SELECT * FROM " + self.ENTRY_TABLE_FILE + \
" WHERE user_id = (SELECT id FROM " + self.USER_TABLE_FILE + \
" WHERE name = ?)"
crs.execute(query, (name, ))
return crs.fetchall()
def get_item_by_id(self, ident):
db = self.connect()
crs = db.cursor()
query = "SELECT * FROM " + self.ITEM_TABLE_FILE + " WHERE id = ?"
crs.execute(query, (ident, ))
return crs.fetchone()
def get_user_by_id(self, ident):
2 years ago
db = self.connect()
crs = db.cursor()
query = "SELECT * FROM " + self.USER_TABLE_FILE + " WHERE id = ?"
2 years ago
crs.execute(query, (ident, ))
fetched = crs.fetchone()
if fetched is None:
return None
else:
return self.db_to_user(*fetched)
2 years ago
def get_user_by_name(self, name):
2 years ago
db = self.connect()
crs = db.cursor()
query = "SELECT * FROM " + self.USER_TABLE_FILE + " WHERE name = ?"
2 years ago
crs.execute(query, (name, ))
fetched = crs.fetchone()
if fetched is None:
return None
else:
return self.db_to_user(*fetched)
2 years ago
def db_to_user(self, ident, name, pass_hash):
user = User(name, pass_hash)
user.set_id(ident)
return user