django-utils

personal collection of django utilities
git clone https://git.ce9e.org/django-utils.git

commit
8b2b7e3406add5207b9f9bf2d6424b7276ad8423
parent
954fc05f94f4b1ae0f4a10c183cceaa0f7f0350c
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2018-10-30 07:38
cache context permissions

Diffstat

M utils/context_groups.py 17 +++++++++++++++++

1 files changed, 17 insertions, 0 deletions


diff --git a/utils/context_groups.py b/utils/context_groups.py

@@ -63,7 +63,24 @@ class BasicObjectBackend(BaseBackend):
   63    63         return user.has_perm(perm)
   64    64 
   65    65 
   -1    66 def perm_cache(key):
   -1    67     def decorator(fn):
   -1    68         def wrapper(self, user, obj=None):
   -1    69             if obj:
   -1    70                 perm_cache_name = '_{}:{}_perm_cache'.format(key, obj.pk)
   -1    71             else:
   -1    72                 perm_cache_name = '_{}_perm_cache'.format(key)
   -1    73 
   -1    74             if not hasattr(user, perm_cache_name):
   -1    75                 perms = fn(self, user, obj=obj)
   -1    76                 setattr(user, perm_cache_name, perms)
   -1    77             return getattr(user, perm_cache_name)
   -1    78         return wrapper
   -1    79     return decorator
   -1    80 
   -1    81 
   66    82 class ContextPermissionBackend(BaseBackend):
   -1    83     @perm_cache('context')
   67    84     def get_group_permissions(self, user, obj=None):
   68    85         perms = set()
   69    86         if user.is_active and isinstance(obj, Context):