Reformat db too
This commit is contained in:
parent
7d8c37f75c
commit
23e90a4413
@ -1,5 +1,6 @@
|
||||
import sqlite3
|
||||
|
||||
|
||||
class DatabaseManager:
|
||||
def __init__(self, database_name):
|
||||
self.conn = sqlite3.connect(database_name)
|
||||
@ -14,6 +15,7 @@ class DatabaseManager:
|
||||
def close(self):
|
||||
self.conn.close()
|
||||
|
||||
|
||||
class BaseORM:
|
||||
def __init__(self, db_manager, table_name, id, **kwargs):
|
||||
self.db_manager = db_manager
|
||||
@ -31,8 +33,9 @@ class BaseORM:
|
||||
|
||||
class Category(BaseORM):
|
||||
def __init__(self, db_manager, id, name, activity_count):
|
||||
super().__init__(db_manager, "categories", id, name=name,
|
||||
activity_count=activity_count)
|
||||
super().__init__(
|
||||
db_manager, "categories", id, name=name, activity_count=activity_count
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def list_categories(db_manager, filter_query=None):
|
||||
@ -65,7 +68,8 @@ class Category(BaseORM):
|
||||
@staticmethod
|
||||
def get_by_id(db_manager, category_id):
|
||||
cursor = db_manager.get_cursor()
|
||||
cursor.execute("""
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT
|
||||
categories.id,
|
||||
categories.name,
|
||||
@ -78,7 +82,9 @@ class Category(BaseORM):
|
||||
categories.id = activities.category_id
|
||||
WHERE
|
||||
categories.id = ?
|
||||
""", (category_id,))
|
||||
""",
|
||||
(category_id,),
|
||||
)
|
||||
|
||||
row = cursor.fetchone()
|
||||
if row:
|
||||
@ -88,7 +94,9 @@ class Category(BaseORM):
|
||||
|
||||
class Activity(BaseORM):
|
||||
def __init__(self, db_manager, id, name, category_id, category_name, facts_count):
|
||||
super().__init__(db_manager, "activities", id, name=name, category_id=category_id)
|
||||
super().__init__(
|
||||
db_manager, "activities", id, name=name, category_id=category_id
|
||||
)
|
||||
self.category_name = category_name
|
||||
self.facts_count = facts_count
|
||||
|
||||
@ -97,14 +105,17 @@ class Activity(BaseORM):
|
||||
|
||||
print(f"moving from {self.id} to {to_activity.id}")
|
||||
|
||||
cursor.execute("""
|
||||
cursor.execute(
|
||||
"""
|
||||
UPDATE
|
||||
facts
|
||||
SET
|
||||
activity_id = ?
|
||||
WHERE
|
||||
activity_id = ?
|
||||
""", (to_activity.id, self.id))
|
||||
""",
|
||||
(to_activity.id, self.id),
|
||||
)
|
||||
|
||||
self.conn.commit()
|
||||
|
||||
@ -142,12 +153,15 @@ class Activity(BaseORM):
|
||||
cursor.execute(sql)
|
||||
|
||||
rows = cursor.fetchall()
|
||||
return [Activity(db_manager, row[0], row[1], row[2], row[3], row[4]) for row in rows]
|
||||
return [
|
||||
Activity(db_manager, row[0], row[1], row[2], row[3], row[4]) for row in rows
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def get_by_id(db_manager, activity_id):
|
||||
cursor = db_manager.get_cursor()
|
||||
cursor.execute("""
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT
|
||||
activities.id,
|
||||
activities.name,
|
||||
@ -166,7 +180,9 @@ class Activity(BaseORM):
|
||||
activities.id = facts.activity_id
|
||||
WHERE
|
||||
activities.id = ?
|
||||
""", (activity_id,))
|
||||
""",
|
||||
(activity_id,),
|
||||
)
|
||||
|
||||
row = cursor.fetchone()
|
||||
if row:
|
||||
|
Loading…
Reference in New Issue
Block a user