Uprava queries a dotazovani se pres base query builder, fix nahledu fotky pro editor, cache na fotky

This commit is contained in:
Michal Kváček
2024-02-13 22:59:51 +01:00
parent 51316c492b
commit e28a6fce57
14 changed files with 209 additions and 90 deletions
+31 -8
View File
@@ -1,5 +1,5 @@
from typing import Optional, Type
from sqlalchemy import select, or_
from sqlalchemy import select, or_, and_
from database import models
@@ -13,6 +13,8 @@ class QueryBuilder:
created_by_id: Optional[int] = None,
order_by: Optional[list] = None,
only_public: Optional[bool] = False,
only_my: Optional[bool] = False,
# include_others_public: Optional[bool] = False,
url_slug: Optional[str] = None,
include_deleted: bool = False
):
@@ -24,21 +26,42 @@ class QueryBuilder:
if not include_deleted and hasattr(self.model, "deleted"):
query = query.filter(self.model.deleted.is_(False))
ownership_filters = []
my_filters = []
if only_public and hasattr(self.model, "is_public"):
query = query.filter(self.model.is_public.is_(True))
my_filters.append(self.model.is_public.is_(True))
if hasattr(self.model, "url_slug"):
query = query.filter(self.model.url_slug != '')
elif hasattr(self.model, "created_by_id") and created_by_id:
query = query.filter(or_(
self.model.created_by_id.is_(None),
self.model.created_by_id == created_by_id
))
my_filters.append(self.model.url_slug != '')
if only_my:
if created_by_id and hasattr(self.model, "created_by_id"):
my_filters.append(self.model.created_by_id == created_by_id)
# others_filters = []
# if include_others_public:
# # TODO: Toto jeste neni implementovane nikde v resolverech!
# if hasattr(self.model, "is_public"):
# others_filters.append(self.model.is_public.is_(True))
#
# if hasattr(self.model, "created_by_id"):
# others_filters.append(or_(
# self.model.created_by_id != created_by_id,
# self.model.created_by_id.is_(None)
# ))
if my_filters:
ownership_filters.append(and_(*my_filters))
# if others_filters:
# ownership_filters.append(and_(*others_filters))
query = query.filter(or_(*ownership_filters))
if url_slug is not None and hasattr(self.model, 'url_slug'):
query = query.filter(self.model.url_slug == url_slug)
if order_by:
query = query.order_by(*order_by)
elif hasattr(self.model, "name"):
query = query.order_by(self.model.name)