partitioncloud-server/partitioncloud/modules/classes/user.py

230 lines
6.6 KiB
Python
Raw Normal View History

2023-10-10 11:14:16 +02:00
from flask import current_app
from ..db import get_db
from .album import Album
2023-10-11 17:15:49 +02:00
from .groupe import Groupe
2023-10-10 11:14:16 +02:00
# 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
2023-10-11 17:15:49 +02:00
self.groupes = None
2023-10-10 11:14:16 +02:00
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
2023-12-15 11:36:34 +01:00
2023-10-10 11:14:16 +02:00
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()
2023-12-15 11:36:34 +01:00
2023-10-10 11:14:16 +02:00
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"]
2023-10-11 17:15:49 +02:00
def is_participant(self, album_uuid, exclude_groupe=False):
2023-10-10 11:14:16 +02:00
db = get_db()
2023-12-15 11:36:34 +01:00
2023-10-11 17:15:49 +02:00
return (len(db.execute( # Is participant directly in the album
2023-10-10 11:14:16 +02:00
"""
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)
2023-12-15 11:36:34 +01:00
).fetchall()) == 1 or
2023-10-11 17:15:49 +02:00
# Is participant in a group that has this album
((not exclude_groupe) and (len(db.execute(
"""
SELECT album.id FROM album
JOIN groupe_contient_album
JOIN groupe_contient_user
JOIN user
ON user_id = user.id
AND groupe_contient_user.groupe_id = groupe_contient_album.groupe_id
AND album.id = album_id
WHERE user.id = ? AND album.uuid = ?
""",
(self.id, album_uuid)
).fetchall()) >= 1))
)
2023-10-10 11:14:16 +02:00
def get_albums(self, force_reload=False):
if self.albums is None or force_reload:
db = get_db()
if self.access_level == 1:
2023-10-11 17:15:49 +02:00
# On récupère tous les albums qui ne sont pas dans un groupe
2023-10-10 11:14:16 +02:00
self.albums = db.execute(
"""
SELECT * FROM album
2023-10-11 17:15:49 +02:00
LEFT JOIN groupe_contient_album
ON album_id=album.id
WHERE album_id IS NULL
2023-10-10 11:14:16 +02:00
"""
).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
2023-10-11 17:15:49 +02:00
def get_groupes(self, force_reload=False):
if self.groupes is None or force_reload:
db = get_db()
if self.access_level == 1:
data = db.execute(
"""
SELECT uuid FROM groupe
"""
).fetchall()
else:
data = db.execute(
"""
SELECT uuid FROM groupe
JOIN groupe_contient_user ON groupe.id = groupe_id
JOIN user ON user_id = user.id
WHERE user.id = ?
""",
(self.id,),
).fetchall()
self.groupes = [Groupe(i["uuid"]) for i in data]
return self.groupes
2023-10-10 11:14:16 +02:00
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
2023-12-15 11:36:34 +01:00
2023-10-10 11:14:16 +02:00
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()
2023-10-11 17:15:49 +02:00
def join_groupe(self, groupe_uuid):
db = get_db()
groupe = Groupe(uuid=groupe_uuid)
db.execute(
"""
INSERT INTO groupe_contient_user (groupe_id, user_id)
VALUES (?, ?)
""",
(groupe.id, self.id)
)
db.commit()
2023-10-10 11:14:16 +02:00
def quit_album(self, album_uuid):
db = get_db()
db.execute(
"""
DELETE FROM contient_user
2023-10-26 23:38:09 +02:00
JOIN album
ON album.id = album_id
2023-10-10 11:14:16 +02:00
WHERE user_id = ?
2023-10-26 23:38:09 +02:00
AND album.uuid = ?
2023-10-10 11:14:16 +02:00
""",
2023-10-26 23:38:09 +02:00
(self.id, album_uuid)
2023-10-10 11:14:16 +02:00
)
db.commit()
2023-10-11 17:15:49 +02:00
def quit_groupe(self, groupe_uuid):
db = get_db()
groupe = Groupe(uuid=groupe_uuid)
db.execute(
"""
DELETE FROM groupe_contient_user
WHERE user_id = ?
AND groupe_id = ?
""",
(self.id, groupe.id)
)
db.commit()
2023-10-10 11:14:16 +02:00
def get_color(self):
if len(colors) == 0:
integer = hash(self.username) % 16777215
return "#" + str(hex(integer))[2:]
2023-12-15 11:36:34 +01:00
return f"var({colors[hash(self.username) %len(colors)]})"