Browse Source

added caching to permission checks and scss compilation in SVG

master
phryk 2 months ago
parent
commit
58035c4ee7
4 changed files with 76 additions and 24 deletions
  1. 3
    0
      example.py
  2. 65
    22
      poobrains/auth/__init__.py
  3. 7
    1
      poobrains/svg/base.py
  4. 1
    1
      poobrains/svg/plot.py

+ 3
- 0
example.py View File

@@ -79,6 +79,9 @@ def menu_main():
79 79
     except poobrains.auth.AccessDenied:
80 80
         pass
81 81
 
82
+    for url, caption in poobrains.auth.Page.main_menu_entries():
83
+        menu.append(url, caption)
84
+
82 85
     return menu
83 86
 
84 87
 

+ 65
- 22
poobrains/auth/__init__.py View File

@@ -390,49 +390,79 @@ class Permission(poobrains.helpers.ChildAware):
390 390
     def __init__(self, instance):
391 391
         self.instance = instance
392 392
         self.check = self.instance_check
393
+        self._check_values = {}
393 394
 
394 395
 
395 396
     @classmethod
396 397
     def check(cls, user):
397 398
 
398 399
         msg = "Permission %s denied." % cls.__name__
400
+        granted = None
399 401
 
400 402
         # check user-assigned permission state
401 403
         if cls.__name__ in user.own_permissions:
402 404
             access = user.own_permissions[cls.__name__]
403 405
 
404
-            if access == 'deny':
405
-                raise AccessDenied(msg)
406 406
 
407
-            elif access == 'grant':
408
-                return True
407
+            if access == 'grant':
408
+                #return True
409
+                granted = True
410
+
411
+            elif access == 'deny':
412
+                #raise AccessDenied(msg)
413
+                granted = False
409 414
 
410 415
         # check if user is member of any groups with 'deny' for this permission
411 416
         # group_deny = GroupPermission.select().join(Group).join(UserGroup).join(User).where(UserGroup.user == user, GroupPermission.permission == cls.__name__, GroupPermission.access == 'deny').count()
412
-        group_deny = False
413
-        for group in user.groups:
414
-            if cls.__name__ in group.own_permissions and group.own_permissions[cls.__name__] == 'deny':
415
-                group_deny = True
416
-                break
417
-
418
-        if group_deny:
419
-            raise AccessDenied(msg)
420 417
 
421
-        #group_grant = GroupPermission.select().join(Group).join(UserGroup).join(User).where(UserGroup.user == user, GroupPermission.permission == cls.__name__, GroupPermission.access == 'grant').count()
422
-        group_grant = False
423
-        for group in user.groups:
424
-            if cls.__name__ in group.own_permissions and group.own_permissions[cls.__name__] == 'grant':
425
-                group_grant = True
426
-                break
427
-
428
-        if group_grant:
418
+        if granted == None:
419
+            group_deny = False
420
+            for group in user.groups:
421
+                if cls.__name__ in group.own_permissions and group.own_permissions[cls.__name__] == 'deny':
422
+                    group_deny = True
423
+                    break
424
+
425
+            if group_deny:
426
+                #raise AccessDenied(msg)
427
+                granted = False
428
+
429
+            #group_grant = GroupPermission.select().join(Group).join(UserGroup).join(User).where(UserGroup.user == user, GroupPermission.permission == cls.__name__, GroupPermission.access == 'grant').count()
430
+            group_grant = False
431
+            for group in user.groups:
432
+                if cls.__name__ in group.own_permissions and group.own_permissions[cls.__name__] == 'grant':
433
+                    group_grant = True
434
+                    break
435
+
436
+            if group_grant:
437
+                #return True
438
+                granted = True
439
+
440
+        if granted == True:
429 441
             return True
430
-
431 442
         raise AccessDenied(msg)
432 443
 
433 444
 
434 445
     def instance_check(self, user):
435
-        return self.__class__.check(user)
446
+
447
+        if user in self._check_values:
448
+
449
+            granted = self._check_values[user]
450
+
451
+            if not granted:
452
+                raise AccessDenied("Permission %s denied." % self.__class__.__name__)
453
+
454
+            return granted
455
+
456
+        else:
457
+
458
+            try: 
459
+                granted = self.__class__.check(user)
460
+                self._check_values[user] = granted
461
+            except AccessDenied:
462
+                self._check_values[user] = False
463
+                raise
464
+
465
+            return granted
436 466
     
437 467
     
438 468
     @classmethod
@@ -1850,6 +1880,8 @@ class Page(Owned):
1850 1880
     path = poobrains.storage.fields.CharField(unique=True)
1851 1881
     title = poobrains.storage.fields.CharField()
1852 1882
     content = poobrains.md.MarkdownField()
1883
+    main_menu = poobrains.storage.fields.BooleanField(default=False)
1884
+    main_menu_caption = poobrains.storage.fields.CharField(null=True)
1853 1885
 
1854 1886
     
1855 1887
     def instance_url(self, mode='full', quiet=None, **url_params):
@@ -1883,6 +1915,17 @@ class Page(Owned):
1883 1915
         return instance.view(mode=mode, handle=handle, **kwargs)
1884 1916
 
1885 1917
 
1918
+    @classmethod
1919
+    def main_menu_entries(cls):
1920
+
1921
+        items = []
1922
+
1923
+        for page in cls.select().where(cls.main_menu == True):
1924
+            items.append((page.path, page.main_menu_caption or page.title))
1925
+
1926
+        return items
1927
+
1928
+
1886 1929
 app.site.add_view(Page, '/<regex(".*"):path>', mode='full')
1887 1930
 
1888 1931
 

+ 7
- 1
poobrains/svg/base.py View File

@@ -7,6 +7,7 @@ import poobrains.auth
7 7
 class SVG(poobrains.auth.Protected):
8 8
     
9 9
     handle = None # needed so that app.expose registers a route with extra param, this is kinda hacky…
10
+    _css_cache = None
10 11
     
11 12
     class Meta:
12 13
 
@@ -24,7 +25,12 @@ class SVG(poobrains.auth.Protected):
24 25
         super(SVG, self).__init__(**kwargs)
25 26
 
26 27
         self.handle = handle
27
-        self.style = Markup(app.scss_compiler.compile_string("@import 'svg';"))
28
+
29
+        if not app.debug and self.__class__._css_cache != None:
30
+            self.style = self.__class__._css_cache
31
+        else:
32
+            self.style = Markup(app.scss_compiler.compile_string("@import 'svg';"))
33
+            self.__class__._css_cache = self.style
28 34
     
29 35
     
30 36
     def templates(self, mode=None):

+ 1
- 1
poobrains/svg/plot.py View File

@@ -479,7 +479,7 @@ class Plot(base.SVG):
479 479
 
480 480
     @property
481 481
     def grid_y(self):
482
-        app.debugger.set_trace()
482
+        #app.debugger.set_trace()
483 483
         if self.span_y == 0:
484 484
             return [self.min_y]
485 485
 

Loading…
Cancel
Save