From a757104fa7b5e5d67d9daba52849e8d5fcb54bd2 Mon Sep 17 00:00:00 2001 From: Martin Mares <mj@ucw.cz> Date: Wed, 29 Jan 2025 20:24:17 +0100 Subject: [PATCH] =?UTF-8?q?Rights:=20Datab=C3=A1zov=C3=BD=20filtr=20na=20r?= =?UTF-8?q?ole?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Obdoba Gatekeeper.rights_for(), ale pro DB. --- mo/db.py | 2 +- mo/rights.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/mo/db.py b/mo/db.py index 5e246f09..e6f9edde 100644 --- a/mo/db.py +++ b/mo/db.py @@ -733,7 +733,7 @@ class UserRole(Base): return " ".join(parts) - # XXX: Tatáž logika je v DB dotazu v org_index() + # XXX: Tatáž logika je v DB dotazu v org_index() a v mo.rights.filter_query_rights_for def applies_to(self, at: Optional[Place] = None, year: Optional[int] = None, cat: Optional[str] = None, seq: Optional[int] = None) -> bool: return ((at is None or self.place_id == at.place_id) and (self.year is None or year is None or self.year == year) diff --git a/mo/rights.py b/mo/rights.py index 8f1fd28a..30079566 100644 --- a/mo/rights.py +++ b/mo/rights.py @@ -539,3 +539,33 @@ class Gatekeeper: return True return False + + +def filter_query_rights_for( + q: Query, + place: Optional[db.Place] = None, + place_ids: Optional[List[int]] = None, + year: Optional[int] = config.CURRENT_YEAR, + cat: Optional[str] = None, + seq: Optional[int] = None) -> Query: + + if place is not None: + q = q.filter(db.UserRole.place_id == place.place_id) + if place_ids is not None: + q = q.filter(db.UserRole.place_id.in_(place_ids)) + + if year is not None: + q = q.filter(or_(db.UserRole.year == year, db.UserRole.year == None)) + + if cat is not None: + cats = [cat] + if cat in "ABC": + cats.append('S') + elif cat.startswith('Z'): + cats.append('Z') + q = q.filter(or_(db.UserRole.category.in_(cats), db.UserRole.category == None)) + + if seq is not None: + q = q.filter(or_(db.UserRole.seq == seq, db.UserRole.seq == None)) + + return q -- GitLab