From 079e4589e6751b9e829457af63558d6237493896 Mon Sep 17 00:00:00 2001
From: Martin Mares <mj@ucw.cz>
Date: Tue, 16 Mar 2021 16:28:08 +0100
Subject: [PATCH] =?UTF-8?q?Rights:=20Opraveno=20ke=C5=A1ov=C3=A1n=C3=AD=20?=
 =?UTF-8?q?pr=C3=A1v?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 mo/rights.py | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/mo/rights.py b/mo/rights.py
index 34313af3..29553db0 100644
--- a/mo/rights.py
+++ b/mo/rights.py
@@ -155,6 +155,11 @@ class Rights:
         ris = " ".join([r.name for r in self.rights])
         return f"Rights(uid={self.user.user_id} is_admin={self.user.is_admin} roles=<{ros}> rights=<{ris}>)"
 
+    def _clone_from(self, src: 'Rights'):
+        self.user = src.user
+        self.user_roles = src.user_roles
+        self.rights = src.rights
+
     def have_right(self, right: Right) -> bool:
         if self.user.is_admin:
             return True
@@ -303,20 +308,18 @@ class Gatekeeper:
     def rights_for(
             self, place: Optional[db.Place] = None, year: Optional[int] = None,
             cat: Optional[str] = None, seq: Optional[int] = None,
-            min_role: Optional[db.RoleType] = None,
-            to_rights: Optional[Rights] = None) -> Rights:
+            min_role: Optional[db.RoleType] = None) -> Rights:
         """Posbírá role a práva, která se vztahují k danému místu (možno i tranzitivně) a soutěži.
         Pokud place=None, omezení role na místo se nebere v úvahu.
         Pokud year==None, vyhovují role s libovolným ročníkem; pokud year=0, vyhovují jen ty s neuvedeným ročníkem.
         Podobně cat a seq.
-        Pokud min_role!=None, tak se uvažují jen role, které jsou v hierarchii alespoň na úrovni min_role.
-        Pokud to_rights!=None, tak vyplní existující objekt typu Rights."""
+        Pokud min_role!=None, tak se uvažují jen role, které jsou v hierarchii alespoň na úrovni min_role."""
 
         cache_key = (place.place_id if place is not None else None, year, cat, seq, min_role)
-        #if cache_key in self.rights_cache:
-        #    return self.rights_cache[cache_key]
+        if cache_key in self.rights_cache:
+            return self.rights_cache[cache_key]
 
-        rights = to_rights or Rights()
+        rights = Rights()
         rights.user = self.user
         rights.user_roles = []
         rights.rights = set()
@@ -354,29 +357,27 @@ class Gatekeeper:
             place = db.get_root_place()
         rights = RoundRights()
         rights.round = round
-        self.rights_for(
+        rights._clone_from(self.rights_for(
             place=place,
             year=round.year,
             cat=round.category,
             seq=round.seq,
-            to_rights=rights,
-        )
+        ))
         return rights
 
     def rights_for_contest(self, contest: db.Contest, site: Optional[db.Place] = None) -> ContestRights:
         rights = ContestRights()
         rights.contest = contest
-        self.rights_for(
+        rights._clone_from(self.rights_for(
             place=site or contest.place,
             year=contest.round.year,
             cat=contest.round.category,
             seq=contest.round.seq,
-            to_rights=rights,
-        )
+        ))
         return rights
 
     def can_set_role(self, role: db.UserRole):
-        # We will use get_for but we need to slightly modify its behavior. Can
+        # We will use rights_for but we need to slightly modify its behavior. Can
         # set role with * year only if current user has role with * year, thus we
         # need to pass year=0 for * year (cat and seq similarly).
         rights = self.rights_for(
-- 
GitLab