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,15 +59,17 @@ class User():
).fetchall()) == 1 ).fetchall()) == 1
def get_albums(self): def get_albums(self, force_reload=False):
if self.albums is None or force_reload:
db = get_db() db = get_db()
if self.access_level == 1: if self.access_level == 1:
return db.execute( self.albums = db.execute(
""" """
SELECT * FROM album SELECT * FROM album
""" """
).fetchall() ).fetchall()
return db.execute( else:
self.albums = db.execute(
""" """
SELECT album.id, name, uuid FROM album SELECT album.id, name, uuid FROM album
JOIN contient_user ON album_id = album.id JOIN contient_user ON album_id = album.id
@ -69,17 +78,20 @@ class User():
""", """,
(self.id,), (self.id,),
).fetchall() ).fetchall()
return self.albums
def get_partitions(self): def get_partitions(self, force_reload=False):
if self.partitions is None or force_reload:
db = get_db() db = get_db()
if self.access_level == 1: if self.access_level == 1:
return db.execute( self.partitions = db.execute(
""" """
SELECT * FROM partition SELECT * FROM partition
""" """
).fetchall() ).fetchall()
return db.execute( else:
self.partitions = db.execute(
""" """
SELECT * FROM partition SELECT * FROM partition
JOIN user ON user_id = user.id JOIN user ON user_id = user.id
@ -87,7 +99,7 @@ class User():
""", """,
(self.id,), (self.id,),
).fetchall() ).fetchall()
return self.partitions
def join_album(self, album_uuid): def join_album(self, album_uuid):
db = get_db() db = get_db()