Browse Source

added caching to permission checks and scss compilation in SVG

master
phryk 11 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():
except poobrains.auth.AccessDenied:
pass

for url, caption in poobrains.auth.Page.main_menu_entries():
menu.append(url, caption)

return menu




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

@@ -390,49 +390,79 @@ class Permission(poobrains.helpers.ChildAware):
def __init__(self, instance):
self.instance = instance
self.check = self.instance_check
self._check_values = {}


@classmethod
def check(cls, user):

msg = "Permission %s denied." % cls.__name__
granted = None

# check user-assigned permission state
if cls.__name__ in user.own_permissions:
access = user.own_permissions[cls.__name__]

if access == 'deny':
raise AccessDenied(msg)

elif access == 'grant':
return True
if access == 'grant':
#return True
granted = True

elif access == 'deny':
#raise AccessDenied(msg)
granted = False

# check if user is member of any groups with 'deny' for this permission
# group_deny = GroupPermission.select().join(Group).join(UserGroup).join(User).where(UserGroup.user == user, GroupPermission.permission == cls.__name__, GroupPermission.access == 'deny').count()
group_deny = False
for group in user.groups:
if cls.__name__ in group.own_permissions and group.own_permissions[cls.__name__] == 'deny':
group_deny = True
break

if group_deny:
raise AccessDenied(msg)

#group_grant = GroupPermission.select().join(Group).join(UserGroup).join(User).where(UserGroup.user == user, GroupPermission.permission == cls.__name__, GroupPermission.access == 'grant').count()
group_grant = False
for group in user.groups:
if cls.__name__ in group.own_permissions and group.own_permissions[cls.__name__] == 'grant':
group_grant = True
break

if group_grant:
if granted == None:
group_deny = False
for group in user.groups:
if cls.__name__ in group.own_permissions and group.own_permissions[cls.__name__] == 'deny':
group_deny = True
break

if group_deny:
#raise AccessDenied(msg)
granted = False

#group_grant = GroupPermission.select().join(Group).join(UserGroup).join(User).where(UserGroup.user == user, GroupPermission.permission == cls.__name__, GroupPermission.access == 'grant').count()
group_grant = False
for group in user.groups:
if cls.__name__ in group.own_permissions and group.own_permissions[cls.__name__] == 'grant':
group_grant = True
break

if group_grant:
#return True
granted = True

if granted == True:
return True

raise AccessDenied(msg)


def instance_check(self, user):
return self.__class__.check(user)

if user in self._check_values:

granted = self._check_values[user]

if not granted:
raise AccessDenied("Permission %s denied." % self.__class__.__name__)

return granted

else:

try:
granted = self.__class__.check(user)
self._check_values[user] = granted
except AccessDenied:
self._check_values[user] = False
raise

return granted
@classmethod
@@ -1850,6 +1880,8 @@ class Page(Owned):
path = poobrains.storage.fields.CharField(unique=True)
title = poobrains.storage.fields.CharField()
content = poobrains.md.MarkdownField()
main_menu = poobrains.storage.fields.BooleanField(default=False)
main_menu_caption = poobrains.storage.fields.CharField(null=True)

def instance_url(self, mode='full', quiet=None, **url_params):
@@ -1883,6 +1915,17 @@ class Page(Owned):
return instance.view(mode=mode, handle=handle, **kwargs)


@classmethod
def main_menu_entries(cls):

items = []

for page in cls.select().where(cls.main_menu == True):
items.append((page.path, page.main_menu_caption or page.title))

return items


app.site.add_view(Page, '/<regex(".*"):path>', mode='full')




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

@@ -7,6 +7,7 @@ import poobrains.auth
class SVG(poobrains.auth.Protected):
handle = None # needed so that app.expose registers a route with extra param, this is kinda hacky…
_css_cache = None
class Meta:

@@ -24,7 +25,12 @@ class SVG(poobrains.auth.Protected):
super(SVG, self).__init__(**kwargs)

self.handle = handle
self.style = Markup(app.scss_compiler.compile_string("@import 'svg';"))

if not app.debug and self.__class__._css_cache != None:
self.style = self.__class__._css_cache
else:
self.style = Markup(app.scss_compiler.compile_string("@import 'svg';"))
self.__class__._css_cache = self.style
def templates(self, mode=None):


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

@@ -479,7 +479,7 @@ class Plot(base.SVG):

@property
def grid_y(self):
app.debugger.set_trace()
#app.debugger.set_trace()
if self.span_y == 0:
return [self.min_y]



Loading…
Cancel
Save