Compare commits

...

3 Commits

7 changed files with 80 additions and 30 deletions

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

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)
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
#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_deny:
#raise AccessDenied(msg)
granted = False
if group_grant:
#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')

View File

@ -71,7 +71,7 @@ class BaseForm(poobrains.rendering.Renderable, metaclass=FormMeta):
value = getattr(attr, propname)
if not callable(value):
value = copy.deepcopy(value)
elif propname in ('default', 'choices'):
elif propname in ('default', 'choices', 'value'):
value = value() # handle arguments to Fields that are optionally callable; ex: Checkbox(default=lambda: bool(random.randint(0,1)))
kw[propname] = value

View File

@ -80,9 +80,6 @@ class BaseField(object, metaclass=poobrains.helpers.MetaCompatibility):
if not choices is None:
self.choices = choices # leave choices of other __init__ intact. <- TODO: what other __init__? did i mean statically defined properties in subclasses?
#if callable(self.choices):
# self.choices = self.choices()
if not default is None:
self.default = default
@ -90,9 +87,6 @@ class BaseField(object, metaclass=poobrains.helpers.MetaCompatibility):
if self.default is None and self.multi:
self.default = []
#if callable(self.default):
# self.default = self.default()
if not value is None:
self.value = value

View File

@ -443,8 +443,12 @@ class ASVIterator(object):
self.asv = asv
self.fd = codecs.open(self.asv.filepath, 'r', encoding='utf-8')
self.keys = self.next_list()
def __iter__(self):
return self
def __del__(self):
self.fd.close()

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):

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]