mirror of
https://github.com/tiyn/container-critique.git
synced 2025-04-03 00:27:48 +02:00
src: database update for user
This commit is contained in:
parent
894474fdee
commit
2110d22c02
17
src/app.py
17
src/app.py
@ -43,7 +43,6 @@ def index():
|
|||||||
|
|
||||||
@app.route("/archive")
|
@app.route("/archive")
|
||||||
def archive():
|
def archive():
|
||||||
entries = db.get_entries()
|
|
||||||
content = con_gen.gen_arch_string()
|
content = con_gen.gen_arch_string()
|
||||||
return render_template("archive.html", content_string=content)
|
return render_template("archive.html", content_string=content)
|
||||||
|
|
||||||
@ -73,9 +72,9 @@ def feed():
|
|||||||
|
|
||||||
@login.user_loader
|
@login.user_loader
|
||||||
def load_user(ident):
|
def load_user(ident):
|
||||||
db_user = db.get_user_by_id(ident)
|
user = db.get_user_by_id(ident)
|
||||||
if db_user is not None:
|
if user is not None:
|
||||||
return db.db_to_user(*db_user)
|
return user
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@ -85,9 +84,9 @@ def login():
|
|||||||
return redirect(url_for("index"))
|
return redirect(url_for("index"))
|
||||||
form = LoginForm()
|
form = LoginForm()
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
db_user = db.get_user_by_name(form.username.data)
|
user = db.get_user_by_name(form.username.data)
|
||||||
if db_user is not None:
|
print(user)
|
||||||
user = db.db_to_user(*db_user)
|
if user is not None:
|
||||||
if user.check_password(form.password.data):
|
if user.check_password(form.password.data):
|
||||||
login_user(user)
|
login_user(user)
|
||||||
return redirect(url_for("index"))
|
return redirect(url_for("index"))
|
||||||
@ -108,8 +107,8 @@ def register():
|
|||||||
return redirect(url_for("index"))
|
return redirect(url_for("index"))
|
||||||
form = RegisterForm()
|
form = RegisterForm()
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
db_user = db.get_user_by_name(form.username.data)
|
user = db.get_user_by_name(form.username.data)
|
||||||
if db_user is None:
|
if user is None:
|
||||||
user = User(form.username.data)
|
user = User(form.username.data)
|
||||||
user.set_password(form.password.data)
|
user.set_password(form.password.data)
|
||||||
ident = db.insert_user(user)
|
ident = db.insert_user(user)
|
||||||
|
@ -39,10 +39,10 @@ def gen_arch_string():
|
|||||||
entries.reverse()
|
entries.reverse()
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
ident = entry[0]
|
ident = entry[0]
|
||||||
title = entry[1]
|
title = db.get_item_by_id(entry[1])[1]
|
||||||
year = entry[2]
|
year = entry[2]
|
||||||
rating = entry[4]
|
rating = entry[4]
|
||||||
username = db.get_user_by_id(entry[5])[1]
|
username = db.get_user_by_id(entry[5]).name
|
||||||
if year != last_year:
|
if year != last_year:
|
||||||
if last_year != "":
|
if last_year != "":
|
||||||
content_string += "</ul>\n"
|
content_string += "</ul>\n"
|
||||||
@ -76,10 +76,10 @@ def gen_user_string(name):
|
|||||||
entries.reverse()
|
entries.reverse()
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
ident = entry[0]
|
ident = entry[0]
|
||||||
title = entry[1]
|
title = db.get_item_by_id(entry[1])[1]
|
||||||
year = entry[2]
|
year = entry[2]
|
||||||
rating = entry[4]
|
rating = entry[4]
|
||||||
username = db.get_user_by_id(entry[5])[1]
|
username = db.get_user_by_id(entry[5]).name
|
||||||
if year != last_year:
|
if year != last_year:
|
||||||
if last_year != "":
|
if last_year != "":
|
||||||
content_string += "</ul>\n"
|
content_string += "</ul>\n"
|
||||||
@ -110,11 +110,11 @@ def gen_index_string():
|
|||||||
entries.reverse()
|
entries.reverse()
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
ident = entry[0]
|
ident = entry[0]
|
||||||
title = entry[1]
|
title = db.get_item_by_id(entry[1])[1]
|
||||||
year = entry[2]
|
year = entry[2]
|
||||||
text = entry[3]
|
text = entry[3]
|
||||||
rating = entry[4]
|
rating = entry[4]
|
||||||
username = db.get_user_by_id(entry[5])[1]
|
username = db.get_user_by_id(entry[5]).name
|
||||||
reviewed = entry[6]
|
reviewed = entry[6]
|
||||||
content_string += "<div class=\"entry\">\n"
|
content_string += "<div class=\"entry\">\n"
|
||||||
content_string += "<h1 id=\"" + str(ident) + "\"><a href=\"" + \
|
content_string += "<h1 id=\"" + str(ident) + "\"><a href=\"" + \
|
||||||
@ -143,11 +143,11 @@ def gen_stand_string(ident):
|
|||||||
content_string = ""
|
content_string = ""
|
||||||
if entry is not None:
|
if entry is not None:
|
||||||
ident = entry[0]
|
ident = entry[0]
|
||||||
title = entry[1]
|
title = db.get_item_by_id(entry[1])[1]
|
||||||
year = entry[2]
|
year = entry[2]
|
||||||
text = entry[3]
|
text = entry[3]
|
||||||
rating = entry[4]
|
rating = entry[4]
|
||||||
username = db.get_user_by_id(entry[5])[1]
|
username = db.get_user_by_id(entry[5]).name
|
||||||
reviewed = entry[6]
|
reviewed = entry[6]
|
||||||
content_string += "<h1>" + title + \
|
content_string += "<h1>" + title + \
|
||||||
" (" + year + ") "
|
" (" + year + ") "
|
||||||
@ -179,11 +179,11 @@ def get_rss_string():
|
|||||||
entries.reverse()
|
entries.reverse()
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
ident = entry[0]
|
ident = entry[0]
|
||||||
title = entry[1]
|
title = db.get_item_by_id(entry[1])[1]
|
||||||
year = entry[2]
|
year = entry[2]
|
||||||
text = entry[3]
|
text = entry[3]
|
||||||
rating = entry[4]
|
rating = entry[4]
|
||||||
username = db.get_user_by_id(entry[5])[1]
|
username = db.get_user_by_id(entry[5]).name
|
||||||
reviewed = entry[6]
|
reviewed = entry[6]
|
||||||
content_string += "<item>\n"
|
content_string += "<item>\n"
|
||||||
content_string += "<title>" + title + "(" + year + ") " + \
|
content_string += "<title>" + title + "(" + year + ") " + \
|
||||||
|
@ -8,20 +8,20 @@ class User():
|
|||||||
|
|
||||||
def __init__(self, name, pass_hash=None):
|
def __init__(self, name, pass_hash=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.id = 0
|
self.id = None
|
||||||
self.is_active = True
|
self.is_active = True
|
||||||
self.is_authenticated = True
|
self.is_authenticated = True
|
||||||
self.is_anonymous = False
|
self.is_anonymous = False
|
||||||
self.pass_hash = pass_hash
|
self.pass_hash = pass_hash
|
||||||
|
|
||||||
def set_password(self, password):
|
def set_password(self, password):
|
||||||
self.pass_hash = generate_password_hash(password)
|
self.pass_hash = password
|
||||||
|
|
||||||
def set_id(self, ident):
|
def set_id(self, ident):
|
||||||
self.id = ident
|
self.id = ident
|
||||||
|
|
||||||
def check_password(self, password):
|
def check_password(self, password):
|
||||||
return check_password_hash(self.pass_hash, password)
|
return self.pass_hash == password
|
||||||
|
|
||||||
def get_id(self):
|
def get_id(self):
|
||||||
return self.id
|
return self.id
|
||||||
@ -32,6 +32,7 @@ class Database:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.USER_TABLE_FILE = 'USERS'
|
self.USER_TABLE_FILE = 'USERS'
|
||||||
self.ENTRY_TABLE_FILE = 'ENTRIES'
|
self.ENTRY_TABLE_FILE = 'ENTRIES'
|
||||||
|
self.ITEM_TABLE_FILE = 'ITEMS'
|
||||||
self.DB_DIR = os.path.dirname("./data/")
|
self.DB_DIR = os.path.dirname("./data/")
|
||||||
self.setup_db()
|
self.setup_db()
|
||||||
|
|
||||||
@ -52,20 +53,23 @@ class Database:
|
|||||||
"name CHAR(32) NOT NULL UNIQUE," + \
|
"name CHAR(32) NOT NULL UNIQUE," + \
|
||||||
"password CHAR(32) NOT NULL)"
|
"password CHAR(32) NOT NULL)"
|
||||||
crs.execute(query)
|
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 + \
|
query = "CREATE TABLE IF NOT EXISTS " + self.ENTRY_TABLE_FILE + \
|
||||||
"(id INTEGER PRIMARY KEY AUTOINCREMENT," + \
|
"(id INTEGER PRIMARY KEY AUTOINCREMENT," + \
|
||||||
"name CHAR(64) NOT NULL," + \
|
"item_id INTEGER NOT NULL REFERENCES " + self.ITEM_TABLE_FILE + "(id)," + \
|
||||||
"date CHAR(4) NOT NULL," + \
|
"date CHAR(4) NOT NULL," + \
|
||||||
"text TEXT NOT NULL," + \
|
"text TEXT NOT NULL," + \
|
||||||
"rating INTEGER NOT NULL," +\
|
"rating INTEGER NOT NULL," +\
|
||||||
"user_id INTEGER," +\
|
"user_id INTEGER REFERENCES " + self.USER_TABLE_FILE + "(id),"\
|
||||||
"reviewed CHAR(10) NOT NULL," +\
|
"reviewed CHAR(10) NOT NULL)"
|
||||||
"FOREIGN KEY(user_id) REFERENCES " + self.USER_TABLE_FILE + "(id))"
|
|
||||||
crs.execute(query)
|
crs.execute(query)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
def insert_user(self, user):
|
def insert_user(self, user):
|
||||||
if self.check_user_name(user.name) and user.pass_hash is not None:
|
if self.get_user_by_name(user.name) is None and user.pass_hash is not None:
|
||||||
db = self.connect()
|
db = self.connect()
|
||||||
crs = db.cursor()
|
crs = db.cursor()
|
||||||
query = "INSERT INTO " + self.USER_TABLE_FILE + \
|
query = "INSERT INTO " + self.USER_TABLE_FILE + \
|
||||||
@ -79,19 +83,21 @@ class Database:
|
|||||||
def insert_entry(self, name, date, text, rating, user_id=None):
|
def insert_entry(self, name, date, text, rating, user_id=None):
|
||||||
db = self.connect()
|
db = self.connect()
|
||||||
crs = db.cursor()
|
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')
|
reviewed = dt.today().strftime('%Y-%m-%d')
|
||||||
query = "INSERT INTO " + self.ENTRY_TABLE_FILE + \
|
query = "INSERT INTO " + self.ENTRY_TABLE_FILE + \
|
||||||
"(`name`,`date`, `text`, `rating`, `user_id`, `reviewed`)" + \
|
"(`item_id`,`date`, `text`, `rating`, `user_id`, `reviewed`)" + \
|
||||||
"VALUES (?, ?, ?, ?, ?, ?)"
|
"VALUES (?, ?, ?, ?, ?, ?)"
|
||||||
crs.execute(query, (name, date, text, rating, user_id, reviewed))
|
crs.execute(query, (item_id, date, text, rating, user_id, reviewed))
|
||||||
db.commit()
|
db.commit()
|
||||||
return crs.lastrowid
|
return crs.lastrowid
|
||||||
|
|
||||||
def check_user_name(self, name):
|
|
||||||
if self.get_user_by_name(name) is None:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def delete_entry(self, ident):
|
def delete_entry(self, ident):
|
||||||
db = self.connect()
|
db = self.connect()
|
||||||
crs = db.cursor()
|
crs = db.cursor()
|
||||||
@ -107,6 +113,13 @@ class Database:
|
|||||||
crs.execute(query)
|
crs.execute(query)
|
||||||
return crs.fetchall()
|
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):
|
def get_entries_by_name(self, name):
|
||||||
db = self.connect()
|
db = self.connect()
|
||||||
crs = db.cursor()
|
crs = db.cursor()
|
||||||
@ -116,10 +129,10 @@ class Database:
|
|||||||
crs.execute(query, (name, ))
|
crs.execute(query, (name, ))
|
||||||
return crs.fetchall()
|
return crs.fetchall()
|
||||||
|
|
||||||
def get_entry_by_id(self, ident):
|
def get_item_by_id(self, ident):
|
||||||
db = self.connect()
|
db = self.connect()
|
||||||
crs = db.cursor()
|
crs = db.cursor()
|
||||||
query = "SELECT * FROM " + self.ENTRY_TABLE_FILE + " WHERE id = ?"
|
query = "SELECT * FROM " + self.ITEM_TABLE_FILE + " WHERE id = ?"
|
||||||
crs.execute(query, (ident, ))
|
crs.execute(query, (ident, ))
|
||||||
return crs.fetchone()
|
return crs.fetchone()
|
||||||
|
|
||||||
@ -128,14 +141,22 @@ class Database:
|
|||||||
crs = db.cursor()
|
crs = db.cursor()
|
||||||
query = "SELECT * FROM " + self.USER_TABLE_FILE + " WHERE id = ?"
|
query = "SELECT * FROM " + self.USER_TABLE_FILE + " WHERE id = ?"
|
||||||
crs.execute(query, (ident, ))
|
crs.execute(query, (ident, ))
|
||||||
return crs.fetchone()
|
fetched = crs.fetchone()
|
||||||
|
if fetched is None:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return self.db_to_user(*fetched)
|
||||||
|
|
||||||
def get_user_by_name(self, name):
|
def get_user_by_name(self, name):
|
||||||
db = self.connect()
|
db = self.connect()
|
||||||
crs = db.cursor()
|
crs = db.cursor()
|
||||||
query = "SELECT * FROM " + self.USER_TABLE_FILE + " WHERE name = ?"
|
query = "SELECT * FROM " + self.USER_TABLE_FILE + " WHERE name = ?"
|
||||||
crs.execute(query, (name, ))
|
crs.execute(query, (name, ))
|
||||||
return crs.fetchone()
|
fetched = crs.fetchone()
|
||||||
|
if fetched is None:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return self.db_to_user(*fetched)
|
||||||
|
|
||||||
def db_to_user(self, ident, name, pass_hash):
|
def db_to_user(self, ident, name, pass_hash):
|
||||||
user = User(name, pass_hash)
|
user = User(name, pass_hash)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user