From 3e2569222ac42ceba4cb2adf4c37084d571cd5cf Mon Sep 17 00:00:00 2001 From: augustin64 Date: Tue, 10 Oct 2023 11:14:16 +0200 Subject: [PATCH] Split utils.py into multiple files --- partitioncloud/modules/classes/album.py | 144 ++++++++ partitioncloud/modules/classes/partition.py | 97 +++++ partitioncloud/modules/classes/user.py | 156 ++++++++ partitioncloud/modules/utils.py | 387 +------------------- 4 files changed, 401 insertions(+), 383 deletions(-) create mode 100644 partitioncloud/modules/classes/album.py create mode 100644 partitioncloud/modules/classes/partition.py create mode 100644 partitioncloud/modules/classes/user.py diff --git a/partitioncloud/modules/classes/album.py b/partitioncloud/modules/classes/album.py new file mode 100644 index 0000000..966a7df --- /dev/null +++ b/partitioncloud/modules/classes/album.py @@ -0,0 +1,144 @@ +import os + +from ..db import get_db + + + +class Album(): + def __init__(self, uuid=None, id=None): + db = get_db() + if uuid is not None: + self.uuid = uuid + data = db.execute( + """ + SELECT id, name FROM album + WHERE uuid = ? + """, + (self.uuid,) + ).fetchone() + if data is None: + raise LookupError + self.id = data["id"] + self.name = data["name"] + + elif id is not None: + self.id = id + data = db.execute( + """ + SELECT uuid, name FROM album + WHERE id = ? + """, + (self.id,) + ).fetchone() + if data is None: + raise LookupError + self.uuid = data["uuid"] + self.name = data["name"] + + else: + raise LookupError + + self.users = None + + + def get_users(self): + """ + Renvoie les utilisateurs liés à l'album + """ + db = get_db() + return db.execute( + """ + SELECT * FROM user + JOIN contient_user ON user_id = user.id + JOIN album ON album.id = album_id + WHERE album.uuid = ? + """, + (self.uuid,) + ).fetchall() + + def get_partitions(self): + """ + Renvoie les partitions liées à l'album + """ + db = get_db() + return db.execute( + """ + SELECT partition.uuid, partition.name, partition.author, partition.user_id FROM partition + JOIN contient_partition ON partition_uuid = partition.uuid + JOIN album ON album.id = album_id + WHERE album.uuid = ? + """, + (self.uuid,), + ).fetchall() + + + def delete(self): + """ + Supprimer l'album + """ + db = get_db() + db.execute( + """ + DELETE FROM album + WHERE uuid = ? + """, + (self.uuid,) + ) + db.execute( + """ + DELETE FROM contient_user + WHERE album_id = ? + """, + (self.id,) + ) + db.execute( + """ + DELETE FROM contient_partition + WHERE album_id = ? + """, + (self.id,) + ) + db.commit() + # Delete orphan partitions + partitions = db.execute( + """ + SELECT partition.uuid FROM partition + WHERE NOT EXISTS ( + SELECT NULL FROM contient_partition + WHERE partition.uuid = partition_uuid + ) + """ + ) + for partition in partitions.fetchall(): + os.remove(f"partitioncloud/partitions/{partition['uuid']}.pdf") + if os.path.exists(f"partitioncloud/static/thumbnails/{partition['uuid']}.jpg"): + os.remove(f"partitioncloud/static/thumbnails/{partition['uuid']}.jpg") + + partitions = db.execute( + """ + DELETE FROM partition + WHERE uuid IN ( + SELECT partition.uuid FROM partition + WHERE NOT EXISTS ( + SELECT NULL FROM contient_partition + WHERE partition.uuid = partition_uuid + ) + ) + """ + ) + db.commit() + + + def add_partition(self, partition_uuid): + """ + Ajoute une partition à l'album à partir de son uuid + """ + db = get_db() + db.execute( + """ + INSERT INTO contient_partition (partition_uuid, album_id) + VALUES (?, ?) + """, + (partition_uuid, self.id), + ) + db.commit() diff --git a/partitioncloud/modules/classes/partition.py b/partitioncloud/modules/classes/partition.py new file mode 100644 index 0000000..8128706 --- /dev/null +++ b/partitioncloud/modules/classes/partition.py @@ -0,0 +1,97 @@ +import os +from flask import current_app + +from ..db import get_db +from .user import User + + + +class Partition(): + def __init__(self, uuid=None): + db = get_db() + if uuid is not None: + self.uuid = uuid + data = db.execute( + """ + SELECT * FROM partition + WHERE uuid = ? + """, + (self.uuid,) + ).fetchone() + if data is None: + raise LookupError + self.name = data["name"] + self.author = data["author"] + self.body = data["body"] + self.user_id = data["user_id"] + self.source = data["source"] + else: + raise LookupError + + def delete(self): + db = get_db() + db.execute( + """ + DELETE FROM contient_partition + WHERE partition_uuid = ? + """, + (self.uuid,) + ) + db.commit() + + os.remove(f"partitioncloud/partitions/{self.uuid}.pdf") + if os.path.exists(f"partitioncloud/static/thumbnails/{self.uuid}.jpg"): + os.remove(f"partitioncloud/static/thumbnails/{self.uuid}.jpg") + + partitions = db.execute( + """ + DELETE FROM partition + WHERE uuid = ? + """, + (self.uuid,) + ) + db.commit() + + def update(self, name=None, author="", body=""): + if name is None: + return Exception("name cannot be None") + + db = get_db() + db.execute( + """ + UPDATE partition + SET name = ?, + author = ?, + body = ? + WHERE uuid = ? + """, + (name, author, body, self.uuid) + ) + db.commit() + + def get_user(self): + db = get_db() + user = db.execute( + """ + SELECT * FROM user + JOIN partition ON user_id = user.id + WHERE partition.uuid = ? + """, + (self.uuid,), + ).fetchone() + + if user is None: + raise LookupError + + return User(user_id=user["id"]) + + def get_albums(self): + db = get_db() + return db.execute( + """ + SELECT * FROM album + JOIN contient_partition ON album.id = album_id + WHERE partition_uuid = ? + """, + (self.uuid,), + ).fetchall() \ No newline at end of file diff --git a/partitioncloud/modules/classes/user.py b/partitioncloud/modules/classes/user.py new file mode 100644 index 0000000..044b123 --- /dev/null +++ b/partitioncloud/modules/classes/user.py @@ -0,0 +1,156 @@ +from flask import current_app + +from ..db import get_db +from .album import Album + + +# Variables defined in the CSS +colors = [ + "--color-rosewater", + "--color-flamingo", + "--color-pink", + "--color-mauve", + "--color-red", + "--color-maroon", + "--color-peach", + "--color-yellow", + "--color-green", + "--color-teal", + "--color-sky", + "--color-sapphire", + "--color-blue", + "--color-lavender" +] + + +class User(): + def __init__(self, user_id=None, name=None): + self.id = user_id + self.username = name + self.albums = None + self.partitions = None + self.max_queries = 0 + + db = get_db() + if self.id is None and self.username is None: + self.username = "" + self.access_level = -1 + + else: + if self.id is not None: + data = db.execute( + """ + SELECT * FROM user + WHERE id = ? + """, + (self.id,) + ).fetchone() + elif self.username is not None: + data = db.execute( + """ + SELECT * FROM user + WHERE username = ? + """, + (self.username,) + ).fetchone() + + self.id = data["id"] + self.username = data["username"] + self.access_level = data["access_level"] + self.color = self.get_color() + if self.access_level == 1: + self.max_queries = 10 + else: + self.max_queries = current_app.config["MAX_ONLINE_QUERIES"] + + + def is_participant(self, album_uuid): + db = get_db() + + return len(db.execute( + """ + SELECT album.id FROM album + JOIN contient_user ON album_id = album.id + JOIN user ON user_id = user.id + WHERE user.id = ? AND album.uuid = ? + """, + (self.id, album_uuid) + ).fetchall()) == 1 + + + def get_albums(self, force_reload=False): + if self.albums is None or force_reload: + db = get_db() + if self.access_level == 1: + self.albums = db.execute( + """ + SELECT * FROM album + """ + ).fetchall() + else: + self.albums = db.execute( + """ + SELECT album.id, name, uuid FROM album + JOIN contient_user ON album_id = album.id + JOIN user ON user_id = user.id + WHERE user.id = ? + """, + (self.id,), + ).fetchall() + return self.albums + + + def get_partitions(self, force_reload=False): + if self.partitions is None or force_reload: + db = get_db() + if self.access_level == 1: + self.partitions = db.execute( + """ + SELECT * FROM partition + """ + ).fetchall() + else: + self.partitions = db.execute( + """ + SELECT * FROM partition + JOIN user ON user_id = user.id + WHERE user.id = ? + """, + (self.id,), + ).fetchall() + return self.partitions + + def join_album(self, album_uuid): + db = get_db() + album = Album(uuid=album_uuid) + + db.execute( + """ + INSERT INTO contient_user (user_id, album_id) + VALUES (?, ?) + """, + (self.id, album.id) + ) + db.commit() + + def quit_album(self, album_uuid): + db = get_db() + album = Album(uuid=album_uuid) + + db.execute( + """ + DELETE FROM contient_user + WHERE user_id = ? + AND album_id = ? + """, + (self.id, album.id) + ) + db.commit() + + + def get_color(self): + if len(colors) == 0: + integer = hash(self.username) % 16777215 + return "#" + str(hex(integer))[2:] + else: + return f"var({colors[hash(self.username) %len(colors)]})" \ No newline at end of file diff --git a/partitioncloud/modules/utils.py b/partitioncloud/modules/utils.py index 1cd3b48..9013335 100644 --- a/partitioncloud/modules/utils.py +++ b/partitioncloud/modules/utils.py @@ -1,390 +1,11 @@ #!/usr/bin/python3 import os + from .db import get_db -from flask import current_app - -# Variables defined in the CSS -colors = [ - "--color-rosewater", - "--color-flamingo", - "--color-pink", - "--color-mauve", - "--color-red", - "--color-maroon", - "--color-peach", - "--color-yellow", - "--color-green", - "--color-teal", - "--color-sky", - "--color-sapphire", - "--color-blue", - "--color-lavender" -] - -class User(): - def __init__(self, user_id=None, name=None): - self.id = user_id - self.username = name - self.albums = None - self.partitions = None - self.max_queries = 0 - - db = get_db() - if self.id is None and self.username is None: - self.username = "" - self.access_level = -1 - - else: - if self.id is not None: - data = db.execute( - """ - SELECT * FROM user - WHERE id = ? - """, - (self.id,) - ).fetchone() - elif self.username is not None: - data = db.execute( - """ - SELECT * FROM user - WHERE username = ? - """, - (self.username,) - ).fetchone() - - self.id = data["id"] - self.username = data["username"] - self.access_level = data["access_level"] - self.color = self.get_color() - if self.access_level == 1: - self.max_queries = 10 - else: - self.max_queries = current_app.config["MAX_ONLINE_QUERIES"] - - - def is_participant(self, album_uuid): - db = get_db() - - return len(db.execute( - """ - SELECT album.id FROM album - JOIN contient_user ON album_id = album.id - JOIN user ON user_id = user.id - WHERE user.id = ? AND album.uuid = ? - """, - (self.id, album_uuid) - ).fetchall()) == 1 - - - def get_albums(self, force_reload=False): - if self.albums is None or force_reload: - db = get_db() - if self.access_level == 1: - self.albums = db.execute( - """ - SELECT * FROM album - """ - ).fetchall() - else: - self.albums = db.execute( - """ - SELECT album.id, name, uuid FROM album - JOIN contient_user ON album_id = album.id - JOIN user ON user_id = user.id - WHERE user.id = ? - """, - (self.id,), - ).fetchall() - return self.albums - - - def get_partitions(self, force_reload=False): - if self.partitions is None or force_reload: - db = get_db() - if self.access_level == 1: - self.partitions = db.execute( - """ - SELECT * FROM partition - """ - ).fetchall() - else: - self.partitions = db.execute( - """ - SELECT * FROM partition - JOIN user ON user_id = user.id - WHERE user.id = ? - """, - (self.id,), - ).fetchall() - return self.partitions - - def join_album(self, album_uuid): - db = get_db() - album = Album(uuid=album_uuid) - - db.execute( - """ - INSERT INTO contient_user (user_id, album_id) - VALUES (?, ?) - """, - (self.id, album.id) - ) - db.commit() - - def quit_album(self, album_uuid): - db = get_db() - album = Album(uuid=album_uuid) - - db.execute( - """ - DELETE FROM contient_user - WHERE user_id = ? - AND album_id = ? - """, - (self.id, album.id) - ) - db.commit() - - - def get_color(self): - if len(colors) == 0: - integer = hash(self.username) % 16777215 - return "#" + str(hex(integer))[2:] - else: - return f"var({colors[hash(self.username) %len(colors)]})" - - - -class Album(): - def __init__(self, uuid=None, id=None): - db = get_db() - if uuid is not None: - self.uuid = uuid - data = db.execute( - """ - SELECT id, name FROM album - WHERE uuid = ? - """, - (self.uuid,) - ).fetchone() - if data is None: - raise LookupError - self.id = data["id"] - self.name = data["name"] - - elif id is not None: - self.id = id - data = db.execute( - """ - SELECT uuid, name FROM album - WHERE id = ? - """, - (self.id,) - ).fetchone() - if data is None: - raise LookupError - self.uuid = data["uuid"] - self.name = data["name"] - - else: - raise LookupError - - self.users = None - - - def get_users(self): - """ - Renvoie les utilisateurs liés à l'album - """ - db = get_db() - return db.execute( - """ - SELECT * FROM user - JOIN contient_user ON user_id = user.id - JOIN album ON album.id = album_id - WHERE album.uuid = ? - """, - (self.uuid,) - ).fetchall() - - def get_partitions(self): - """ - Renvoie les partitions liées à l'album - """ - db = get_db() - return db.execute( - """ - SELECT partition.uuid, partition.name, partition.author, partition.user_id FROM partition - JOIN contient_partition ON partition_uuid = partition.uuid - JOIN album ON album.id = album_id - WHERE album.uuid = ? - """, - (self.uuid,), - ).fetchall() - - - def delete(self): - """ - Supprimer l'album - """ - db = get_db() - db.execute( - """ - DELETE FROM album - WHERE uuid = ? - """, - (self.uuid,) - ) - db.execute( - """ - DELETE FROM contient_user - WHERE album_id = ? - """, - (self.id,) - ) - db.execute( - """ - DELETE FROM contient_partition - WHERE album_id = ? - """, - (self.id,) - ) - db.commit() - # Delete orphan partitions - partitions = db.execute( - """ - SELECT partition.uuid FROM partition - WHERE NOT EXISTS ( - SELECT NULL FROM contient_partition - WHERE partition.uuid = partition_uuid - ) - """ - ) - for partition in partitions.fetchall(): - os.remove(f"partitioncloud/partitions/{partition['uuid']}.pdf") - if os.path.exists(f"partitioncloud/static/thumbnails/{partition['uuid']}.jpg"): - os.remove(f"partitioncloud/static/thumbnails/{partition['uuid']}.jpg") - - partitions = db.execute( - """ - DELETE FROM partition - WHERE uuid IN ( - SELECT partition.uuid FROM partition - WHERE NOT EXISTS ( - SELECT NULL FROM contient_partition - WHERE partition.uuid = partition_uuid - ) - ) - """ - ) - db.commit() - - - def add_partition(self, partition_uuid): - """ - Ajoute une partition à l'album à partir de son uuid - """ - db = get_db() - db.execute( - """ - INSERT INTO contient_partition (partition_uuid, album_id) - VALUES (?, ?) - """, - (partition_uuid, self.id), - ) - db.commit() - - -class Partition(): - def __init__(self, uuid=None): - db = get_db() - if uuid is not None: - self.uuid = uuid - data = db.execute( - """ - SELECT * FROM partition - WHERE uuid = ? - """, - (self.uuid,) - ).fetchone() - if data is None: - raise LookupError - self.name = data["name"] - self.author = data["author"] - self.body = data["body"] - self.user_id = data["user_id"] - self.source = data["source"] - else: - raise LookupError - - def delete(self): - db = get_db() - db.execute( - """ - DELETE FROM contient_partition - WHERE partition_uuid = ? - """, - (self.uuid,) - ) - db.commit() - - os.remove(f"partitioncloud/partitions/{self.uuid}.pdf") - if os.path.exists(f"partitioncloud/static/thumbnails/{self.uuid}.jpg"): - os.remove(f"partitioncloud/static/thumbnails/{self.uuid}.jpg") - - partitions = db.execute( - """ - DELETE FROM partition - WHERE uuid = ? - """, - (self.uuid,) - ) - db.commit() - - def update(self, name=None, author="", body=""): - if name is None: - return Exception("name cannot be None") - - db = get_db() - db.execute( - """ - UPDATE partition - SET name = ?, - author = ?, - body = ? - WHERE uuid = ? - """, - (name, author, body, self.uuid) - ) - db.commit() - - def get_user(self): - db = get_db() - user = db.execute( - """ - SELECT * FROM user - JOIN partition ON user_id = user.id - WHERE partition.uuid = ? - """, - (self.uuid,), - ).fetchone() - - if user is None: - raise LookupError - - return User(user_id=user["id"]) - - def get_albums(self): - db = get_db() - return db.execute( - """ - SELECT * FROM album - JOIN contient_partition ON album.id = album_id - WHERE partition_uuid = ? - """, - (self.uuid,), - ).fetchall() +from .classes.user import User +from .classes.album import Album +from .classes.partition import Partition