-
Notifications
You must be signed in to change notification settings - Fork 832
/
sidebar.py
71 lines (57 loc) · 1.81 KB
/
sidebar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from jadi import interface, service
from aj.auth import authorize, SecurityError
@interface
class SidebarItemProvider():
"""
Interface for providing sidebar items.
"""
def __init__(self, context):
pass
def provide(self):
"""
Should return a list of sidebar items, each in the following format::
{
'id': 'optional-id',
'attach': 'category:general', # id of the attachment point or None for top level
'name': 'Dashboard',
'icon': 'bar-chart',
'url': '/view/dashboard',
'children': [
...
]
}
:returns: list(dict)
"""
return []
@service
class Sidebar():
def __init__(self, context):
self.context = context
def build(self):
"""
Returns a complete tree of sidebar items.
:returns: dict
"""
sidebar = {
'id': None,
'children': [],
}
def find_id(_id, e=sidebar):
if 'id' in e and e['id'] == _id:
return e
for c in e.get('children', []):
f = find_id(_id, e=c)
if f:
return f
for provider in SidebarItemProvider.all(self.context):
for item in provider.provide():
if 'url' in item:
try:
authorize(f'sidebar:view:{item["url"]}').check()
except SecurityError:
continue
attach_to = find_id(item['attach'])
if not attach_to:
raise Exception(f'Attachment point not found: {item["attach"]}')
attach_to['children'].append(item)
return sidebar