From 88b07dd8a0a526bd17fa4e50919c6caa2e93d03f Mon Sep 17 00:00:00 2001 From: tiyn Date: Tue, 2 Aug 2022 00:58:52 +0200 Subject: [PATCH] src: database update for item --- src/content.py | 24 ++++++++++++------------ src/database.py | 46 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/content.py b/src/content.py index 3e03e65..42cc593 100644 --- a/src/content.py +++ b/src/content.py @@ -39,7 +39,7 @@ def gen_arch_string(): entries.reverse() for entry in entries: ident = entry[0] - title = db.get_item_by_id(entry[1])[1] + title = db.get_item_by_id(entry[1]).name year = entry[2] rating = entry[4] username = db.get_user_by_id(entry[5]).name @@ -67,7 +67,7 @@ def gen_user_string(name): """ content_string = "" last_year = "" - entries = db.get_entries_by_name(name) + entries = db.get_entries_by_user(name) if entries is None: return "" entries.sort(key=lambda y: y[1]) @@ -76,7 +76,7 @@ def gen_user_string(name): entries.reverse() for entry in entries: ident = entry[0] - title = db.get_item_by_id(entry[1])[1] + title = db.get_item_by_id(entry[1]).name year = entry[2] rating = entry[4] username = db.get_user_by_id(entry[5]).name @@ -110,7 +110,7 @@ def gen_index_string(): entries.reverse() for entry in entries: ident = entry[0] - title = db.get_item_by_id(entry[1])[1] + title = db.get_item_by_id(entry[1]).name year = entry[2] text = entry[3] rating = entry[4] @@ -142,13 +142,13 @@ def gen_stand_string(ident): entry = db.get_entry_by_id(ident) content_string = "" if entry is not None: - ident = entry[0] - title = db.get_item_by_id(entry[1])[1] - year = entry[2] - text = entry[3] - rating = entry[4] - username = db.get_user_by_id(entry[5]).name - reviewed = entry[6] + ident = entry.id + title = db.get_item_by_id(entry.item_id).name + year = entry.date + text = entry.text + rating = entry.rating + username = db.get_user_by_id(entry.user_id).name + reviewed = entry.reviewed content_string += "

" + title + \ " (" + year + ") " content_string += rating_to_star(rating) @@ -179,7 +179,7 @@ def get_rss_string(): entries.reverse() for entry in entries: ident = entry[0] - title = db.get_item_by_id(entry[1])[1] + title = db.get_item_by_id(entry[1]).name year = entry[2] text = entry[3] rating = entry[4] diff --git a/src/database.py b/src/database.py index 2c051f7..7e79bba 100644 --- a/src/database.py +++ b/src/database.py @@ -26,6 +26,28 @@ class User(): def get_id(self): return self.id +class Item(): + + def __init__(self, name): + self.name = name + self.id = None + + def set_id(self, ident): + self.id = ident + +class Entry(): + + def __init__(self, item_id, date, text, rating, user_id, reviewed): + self.item_id = item_id + self.date = date + self.text = text + self.rating = rating + self.user_id = user_id + self.reviewed = reviewed + + def set_id(self, ident): + self.id = ident + class Database: @@ -118,9 +140,13 @@ class Database: crs = db.cursor() query = "SELECT * FROM " + self.ENTRY_TABLE_FILE + " WHERE id = ?" crs.execute(query, (ident, )) - return crs.fetchone() + fetched = crs.fetchone() + if fetched is None: + return None + else: + return self.db_to_entry(*fetched) - def get_entries_by_name(self, name): + def get_entries_by_user(self, name): db = self.connect() crs = db.cursor() query = "SELECT * FROM " + self.ENTRY_TABLE_FILE + \ @@ -134,7 +160,11 @@ class Database: crs = db.cursor() query = "SELECT * FROM " + self.ITEM_TABLE_FILE + " WHERE id = ?" crs.execute(query, (ident, )) - return crs.fetchone() + fetched = crs.fetchone() + if fetched is None: + return None + else: + return self.db_to_item(*fetched) def get_user_by_id(self, ident): db = self.connect() @@ -162,3 +192,13 @@ class Database: user = User(name, pass_hash) user.set_id(ident) return user + + def db_to_item(self, ident, name): + item = Item(name) + item.set_id(ident) + return item + + def db_to_entry(self, ident, item_id, date, text, rating, user_id, reviewed): + entry = Entry(item_id, date, text, rating, user_id, reviewed) + entry.set_id(ident) + return entry