Cache user albums and partitions

This commit is contained in:
augustin64 2023-06-10 16:50:33 +02:00
parent b5d83e3c50
commit 781134f4a4

View File

@ -2,12 +2,15 @@
import os import os
from .db import get_db from .db import get_db
from flask import current_app
class User(): class User():
def __init__(self, user_id=None, name=None): def __init__(self, user_id=None, name=None):
self.id = user_id self.id = user_id
self.username = name self.username = name
self.albums = None self.albums = None
self.partitions = None self.partitions = None
self.max_queries = 0
db = get_db() db = get_db()
if self.id is None and self.username is None: if self.id is None and self.username is None:
@ -36,6 +39,10 @@ class User():
self.username = data["username"] self.username = data["username"]
self.access_level = data["access_level"] self.access_level = data["access_level"]
self.color = self.get_color() 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): def is_participant(self, album_uuid):
@ -52,42 +59,47 @@ class User():
).fetchall()) == 1 ).fetchall()) == 1
def get_albums(self): def get_albums(self, force_reload=False):
db = get_db() if self.albums is None or force_reload:
if self.access_level == 1: db = get_db()
return db.execute( if self.access_level == 1:
""" self.albums = db.execute(
SELECT * FROM album """
""" SELECT * FROM album
).fetchall() """
return db.execute( ).fetchall()
""" else:
SELECT album.id, name, uuid FROM album self.albums = db.execute(
JOIN contient_user ON album_id = album.id """
JOIN user ON user_id = user.id SELECT album.id, name, uuid FROM album
WHERE user.id = ? JOIN contient_user ON album_id = album.id
""", JOIN user ON user_id = user.id
(self.id,), WHERE user.id = ?
).fetchall() """,
(self.id,),
).fetchall()
return self.albums
def get_partitions(self): def get_partitions(self, force_reload=False):
db = get_db() if self.partitions is None or force_reload:
if self.access_level == 1: db = get_db()
return db.execute( if self.access_level == 1:
""" self.partitions = db.execute(
SELECT * FROM partition """
""" SELECT * FROM partition
).fetchall() """
return db.execute( ).fetchall()
""" else:
SELECT * FROM partition self.partitions = db.execute(
JOIN user ON user_id = user.id """
WHERE user.id = ? SELECT * FROM partition
""", JOIN user ON user_id = user.id
(self.id,), WHERE user.id = ?
).fetchall() """,
(self.id,),
).fetchall()
return self.partitions
def join_album(self, album_uuid): def join_album(self, album_uuid):
db = get_db() db = get_db()