Skip to content
Snippets Groups Projects
Commit a9132df6 authored by Martin Mareš's avatar Martin Mareš
Browse files

Rights: Databázový filtr na role

Obdoba Gatekeeper.rights_for(), ale pro DB.
parent 0208899c
No related branches found
No related tags found
No related merge requests found
...@@ -733,7 +733,7 @@ class UserRole(Base): ...@@ -733,7 +733,7 @@ class UserRole(Base):
return " ".join(parts) 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: 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) return ((at is None or self.place_id == at.place_id)
and (self.year is None or year is None or self.year == year) and (self.year is None or year is None or self.year == year)
......
...@@ -539,3 +539,33 @@ class Gatekeeper: ...@@ -539,3 +539,33 @@ class Gatekeeper:
return True return True
return False 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment