-
Notifications
You must be signed in to change notification settings - Fork 532
/
sitetrees.py
272 lines (258 loc) · 10 KB
/
sitetrees.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# -*- coding: utf-8 -*-
from django.apps import apps
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import activate
from sitetree.sitetreeapp import register_i18n_trees
from sitetree.utils import item, tree
from ralph.admin.sites import ralph_site
# To generate and display the menu, use the command:
# ralph sitetreeload
# ralph sitetree_resync_apps
activate(settings.LANGUAGE_CODE)
def ralph_item(*args, **kwargs):
kwargs.setdefault('access_loggedin', True)
# create access_by_perms entries by iterating through all children
# and extracting app and model name from it
# permission is created in '<app>.{add|change|view}_<model>' format
access_by_perms = kwargs.get('access_by_perms', [])
if isinstance(access_by_perms, (str, int)):
access_by_perms = [access_by_perms]
for child in kwargs.get('children', []):
if hasattr(child, '_model') and hasattr(child, '_app'):
model = child._model.lower()
app = child._app.lower()
access_by_perms.extend([
'{}.add_{}'.format(app, model),
'{}.change_{}'.format(app, model),
'{}.view_{}'.format(app, model),
])
elif hasattr(child, 'permissions'):
access_by_perms.extend(child.permissions)
if access_by_perms:
kwargs['access_by_perms'] = list(set(access_by_perms))
return item(*args, **kwargs)
extra_views = ralph_site.get_extra_view_menu_items()
def get_menu_items_for_admin(name, perm):
# TODO: detailed permissions for extra views
return [
ralph_item(access_by_perms=perm, **view) for view in extra_views[name]
]
def section(section_name, app, model):
app, model = map(str.lower, [app, model])
model_class = apps.get_model(app, model)
change_perm = '{}.change_{}'.format(app, model)
item = ralph_item(
title=section_name,
url='admin:{}_{}_changelist'.format(app, model),
access_by_perms='{}.view_{}'.format(app, model),
perms_mode_all=False,
children=[
ralph_item(
title=_('Add {}'.format(
model_class._meta.verbose_name.lower()
)),
url='admin:{}_{}_add'.format(app, model),
access_by_perms='{}.add_{}'.format(app, model),
),
ralph_item(
title='{{ original }}',
url='admin:{}_{}_change original.id'.format(app, model),
access_by_perms=change_perm,
children=get_menu_items_for_admin(
'{}_{}'.format(app, model),
change_perm
),
),
]
)
# save app and model info to create permissions entries later
item._app = app
item._model = model
return item
sitetrees = [
tree('ralph_admin', items=[
ralph_item(
title=_('Data Center'),
url='#',
url_as_pattern=False,
perms_mode_all=False,
children=[
section(_('Hardware'), 'data_center', 'DataCenterAsset'),
section(_('Racks'), 'data_center', 'Rack'),
section(_('Cloud projects'), 'virtual', 'CloudProject'),
section(_('Cloud hosts'), 'virtual', 'CloudHost'),
section(_('Data Centers'), 'data_center', 'DataCenter'),
section(_('Databases'), 'data_center', 'Database'),
section(_('Disk Shares'), 'data_center', 'DiskShare'),
section(_('Accessories'), 'data_center', 'Accessory'),
section(_('Rack Accessories'), 'data_center', 'RackAccessory'),
section(_('Server Rooms'), 'data_center', 'ServerRoom'),
section(_('VIPs'), 'data_center', 'VIP'),
section(_('Virtual Servers'), 'virtual', 'VirtualServer'),
section(_('IP Addresses'), 'data_center', 'ipaddress'),
],
),
ralph_item(
title=_('DC Visualization'),
url='dc_view',
access_by_perms='accounts.can_view_extra_datacenterview'
),
ralph_item(
title=_('Back Office'),
url='#',
url_as_pattern=False,
perms_mode_all=False,
children=[
section(_('Hardware'), 'back_office', 'backofficeasset'),
section(_('Warehouses'), 'back_office', 'warehouse'),
section(
_('Office Infrastructures'),
'back_office',
'officeinfrastructure'
),
]
),
ralph_item(
title=_('Licenses'),
url='#',
url_as_pattern=False,
perms_mode_all=False,
children=[
section(_('Licences'), 'licences', 'Licence'),
section(_('Types'), 'licences', 'LicenceType'),
section(_('Software'), 'licences', 'Software'),
]
),
ralph_item(
title=_('Domains'),
url='#',
url_as_pattern=False,
perms_mode_all=False,
children=[
section(_('Domains'), 'domains', 'Domain'),
section(_('Contracts'), 'domains', 'DomainContract'),
section(_('Registrants'), 'domains', 'DomainRegistrant'),
section(_('Owners'), 'accounts', 'RalphUser'),
]
),
ralph_item(
title=_('Supports'),
url='#',
url_as_pattern=False,
perms_mode_all=False,
children=[
section(_('Supports'), 'supports', 'Support'),
section(_('Types'), 'supports', 'SupportType'),
]
),
ralph_item(
title=_('Reports'),
url='#',
url_as_pattern=False,
perms_mode_all=False,
children=[
ralph_item(
title=_('Category model'),
url='category_model_report',
access_by_perms=(
'accounts.can_view_extra_categorymodelreport'
),
),
ralph_item(
title=_('Category model status'),
url='category_model__status_report',
access_by_perms=(
'accounts.can_view_extra_categorymodelstatusreport'
),
),
ralph_item(
title=_('Manufacturer category model'),
url='manufactured_category_model_report',
access_by_perms=(
'accounts.'
'can_view_extra_manufacturercategorymodelreport'
),
),
ralph_item(
title=_('Status model'),
url='status_model_report',
access_by_perms=(
'accounts.can_view_extra_statusmodelreport'
),
),
ralph_item(
title=_('Asset - relations'),
url='asset-relations',
access_by_perms=(
'accounts.can_view_extra_assetrelationsreport'
),
),
ralph_item(
title=_('Licence - relations'),
url='licence-relations',
access_by_perms=(
'accounts.can_view_extra_licencerelationsreport'
),
),
ralph_item(
title=_('Failures'),
url='failures-report',
access_by_perms=(
'accounts.can_view_extra_failurereport'
),
),
]
),
ralph_item(
title=_('Operations'),
url='#',
url_as_pattern=False,
perms_mode_all=False,
children=[
section(_('Changes'), 'operations', 'Change'),
section(_('Problems'), 'operations', 'Problem'),
section(_('Incidents'), 'operations', 'Incident'),
section(_('Failures'), 'operations', 'Failure'),
section(_('All'), 'operations', 'Operation'),
section(_('Types'), 'operations', 'OperationType'),
]
),
ralph_item(
title=_('Dashboards'),
url='#',
url_as_pattern=False,
perms_mode_all=False,
children=[
section(_('Dashboards'), 'dashboards', 'Dashboard'),
section(_('Graphs'), 'dashboards', 'Graph'),
]
),
ralph_item(
title=_('Settings'),
url='#',
url_as_pattern=False,
perms_mode_all=False,
children=[
section(_('Asset model'), 'assets', 'AssetModel'),
section(_('Asset category'), 'assets', 'Category'),
section(_('Manufacturer'), 'assets', 'Manufacturer'),
section(_('Business segment'), 'assets', 'BusinessSegment'),
section(_('Profit center'), 'assets', 'ProfitCenter'),
section(_('Service'), 'assets', 'Service'),
section(_('Environment'), 'assets', 'Environment'),
section(_('Budget info'), 'assets', 'BudgetInfo'),
section(
_('Service Environment'), 'assets', 'ServiceEnvironment'
),
section(_('Asset holder'), 'assets', 'AssetHolder'),
section(_('Users list'), 'accounts', 'RalphUser'),
section(_('Groups list'), 'auth', 'Group'),
section(_('Regions'), 'accounts', 'Region'),
section(_('Transitions'), 'transitions', 'TransitionModel'),
section(_('Report template'), 'reports', 'Report'),
]
)
])
]
register_i18n_trees(['ralph_admin'])