0
# Core Admin Interface
1
2
Core administrative interface classes including the main Admin collection, base views for custom pages, and menu system components.
3
4
## Capabilities
5
6
### Admin Collection Management
7
8
The `Admin` class serves as the main administrative interface manager, coordinating views, menu structure, and Flask application integration.
9
10
```python { .api }
11
class Admin:
12
def __init__(
13
self,
14
app=None,
15
name=None,
16
url=None,
17
subdomain=None,
18
index_view=None,
19
translations_path=None,
20
endpoint=None,
21
static_url_path=None,
22
base_template=None,
23
template_mode=None,
24
category_icon_classes=None
25
):
26
"""
27
Initialize admin interface.
28
29
Args:
30
app (Flask, optional): Flask application instance
31
name (str, optional): Admin interface name for title/branding
32
url (str, optional): Admin URL prefix (default: '/admin')
33
subdomain (str, optional): Subdomain for admin interface
34
index_view (AdminIndexView, optional): Custom index page view
35
translations_path (str, optional): Path to translation files
36
endpoint (str, optional): Admin blueprint endpoint
37
static_url_path (str, optional): Static files URL path
38
base_template (str, optional): Base template path
39
template_mode (str, optional): Template theme ('bootstrap2', 'bootstrap3', 'bootstrap4')
40
category_icon_classes (dict, optional): Icon classes for categories
41
"""
42
43
def add_view(self, view):
44
"""
45
Add view to admin interface.
46
47
Args:
48
view (BaseView): Admin view instance
49
"""
50
51
def add_views(self, *args):
52
"""
53
Add multiple views to admin interface.
54
55
Args:
56
*args: Variable number of view instances
57
"""
58
59
def add_category(self, name, class_name=None, icon_type=None, icon_value=None):
60
"""
61
Add menu category.
62
63
Args:
64
name (str): Category name
65
class_name (str, optional): CSS class for category
66
icon_type (str, optional): Icon type ('glyph', 'fa', 'image', 'image-url')
67
icon_value (str, optional): Icon identifier
68
"""
69
70
def add_sub_category(self, name, parent_name):
71
"""
72
Add submenu category.
73
74
Args:
75
name (str): Subcategory name
76
parent_name (str): Parent category name
77
"""
78
79
def add_link(self, link):
80
"""
81
Add menu link.
82
83
Args:
84
link (MenuLink): Menu link instance
85
"""
86
87
def add_links(self, *args):
88
"""
89
Add multiple menu links.
90
91
Args:
92
*args: Variable number of MenuLink instances
93
"""
94
95
def init_app(self, app, index_view=None, endpoint=None, url=None):
96
"""
97
Initialize admin with Flask application.
98
99
Args:
100
app (Flask): Flask application instance
101
index_view (AdminIndexView, optional): Custom index page
102
endpoint (str, optional): Blueprint endpoint
103
url (str, optional): URL prefix
104
"""
105
106
def menu(self):
107
"""
108
Get menu hierarchy.
109
110
Returns:
111
list: Menu items and categories
112
"""
113
114
def menu_links(self):
115
"""
116
Get menu links.
117
118
Returns:
119
list: Menu link items
120
"""
121
```
122
123
### Base View Classes
124
125
Foundation classes for creating custom administrative views with URL routing, template rendering, and access control.
126
127
```python { .api }
128
def expose(url='/', methods=('GET',)):
129
"""
130
Decorator to expose view methods with URL routing.
131
132
Args:
133
url (str): URL pattern (default: '/')
134
methods (tuple): HTTP methods (default: ('GET',))
135
136
Returns:
137
function: Decorated method
138
"""
139
140
class BaseView:
141
def __init__(
142
self,
143
name=None,
144
category=None,
145
endpoint=None,
146
url=None,
147
static_folder=None,
148
static_url_path=None,
149
menu_class_name=None,
150
menu_icon_type=None,
151
menu_icon_value=None
152
):
153
"""
154
Initialize base admin view.
155
156
Args:
157
name (str, optional): Display name in menu
158
category (str, optional): Menu category
159
endpoint (str, optional): Blueprint endpoint prefix
160
url (str, optional): URL prefix for view
161
static_folder (str, optional): Static files folder path
162
static_url_path (str, optional): Static files URL path
163
menu_class_name (str, optional): CSS class for menu item
164
menu_icon_type (str, optional): Icon type
165
menu_icon_value (str, optional): Icon identifier
166
"""
167
168
def create_blueprint(self, admin):
169
"""
170
Create Flask blueprint for view.
171
172
Args:
173
admin (Admin): Admin instance
174
175
Returns:
176
Blueprint: Flask blueprint
177
"""
178
179
def render(self, template, **kwargs):
180
"""
181
Render template with admin context.
182
183
Args:
184
template (str): Template path
185
**kwargs: Template variables
186
187
Returns:
188
str: Rendered HTML
189
"""
190
191
def is_visible(self):
192
"""
193
Check if view should be visible in menu.
194
195
Returns:
196
bool: True if visible
197
"""
198
199
def is_accessible(self):
200
"""
201
Check if view is accessible (permission check).
202
203
Returns:
204
bool: True if accessible
205
"""
206
207
def inaccessible_callback(self, name, **kwargs):
208
"""
209
Handle inaccessible view requests.
210
211
Args:
212
name (str): Endpoint name
213
**kwargs: URL parameters
214
"""
215
216
def get_url(self, endpoint, **kwargs):
217
"""
218
Generate URL for endpoint.
219
220
Args:
221
endpoint (str): Endpoint name
222
**kwargs: URL parameters
223
224
Returns:
225
str: Generated URL
226
"""
227
228
class AdminIndexView(BaseView):
229
def __init__(
230
self,
231
name=None,
232
category=None,
233
endpoint=None,
234
url=None,
235
template='admin/index.html',
236
menu_class_name=None,
237
menu_icon_type=None,
238
menu_icon_value=None
239
):
240
"""
241
Initialize admin index view.
242
243
Args:
244
name (str, optional): Display name
245
category (str, optional): Menu category
246
endpoint (str, optional): Blueprint endpoint
247
url (str, optional): URL pattern
248
template (str, optional): Index template path
249
menu_class_name (str, optional): Menu CSS class
250
menu_icon_type (str, optional): Icon type
251
menu_icon_value (str, optional): Icon identifier
252
"""
253
254
@expose('/')
255
def index(self):
256
"""
257
Index view method.
258
259
Returns:
260
str: Rendered index page
261
"""
262
```
263
264
### Menu System Components
265
266
Menu system classes for creating hierarchical navigation with categories, links, and custom items.
267
268
```python { .api }
269
class BaseMenu:
270
def __init__(
271
self,
272
name,
273
class_name=None,
274
icon_type=None,
275
icon_value=None,
276
target=None
277
):
278
"""
279
Initialize base menu item.
280
281
Args:
282
name (str): Menu item name
283
class_name (str, optional): CSS class
284
icon_type (str, optional): Icon type
285
icon_value (str, optional): Icon identifier
286
target (str, optional): Link target
287
"""
288
289
def add_child(self, menu):
290
"""
291
Add child menu item.
292
293
Args:
294
menu (BaseMenu): Child menu item
295
"""
296
297
def get_url(self):
298
"""
299
Get menu URL (abstract method).
300
301
Returns:
302
str: Menu URL
303
"""
304
305
def is_category(self):
306
"""
307
Check if item is category.
308
309
Returns:
310
bool: True if category
311
"""
312
313
def is_active(self, view):
314
"""
315
Check if menu is active for current view.
316
317
Args:
318
view (BaseView): Current view
319
320
Returns:
321
bool: True if active
322
"""
323
324
def is_visible(self):
325
"""
326
Check menu visibility.
327
328
Returns:
329
bool: True if visible
330
"""
331
332
def is_accessible(self):
333
"""
334
Check menu accessibility.
335
336
Returns:
337
bool: True if accessible
338
"""
339
340
def get_children(self):
341
"""
342
Get accessible, visible child menu items.
343
344
Returns:
345
list: Child menu items
346
"""
347
348
class MenuCategory(BaseMenu):
349
"""
350
Menu category container for grouping related items.
351
"""
352
353
def get_url(self):
354
"""
355
Get category URL.
356
357
Returns:
358
None: Categories don't have URLs
359
"""
360
361
def is_category(self):
362
"""
363
Check if item is category.
364
365
Returns:
366
bool: Always True for categories
367
"""
368
369
class MenuView(BaseMenu):
370
def __init__(self, name, view=None, cache=True):
371
"""
372
Initialize admin view menu item.
373
374
Args:
375
name (str): Menu item name
376
view (BaseView, optional): Associated view
377
cache (bool, optional): Enable URL caching
378
"""
379
380
def get_url(self):
381
"""
382
Get view URL with caching.
383
384
Returns:
385
str: View URL
386
"""
387
388
def is_active(self, view):
389
"""
390
Check if menu corresponds to active view.
391
392
Args:
393
view (BaseView): Current view
394
395
Returns:
396
bool: True if active
397
"""
398
399
class MenuLink(BaseMenu):
400
def __init__(
401
self,
402
name,
403
url=None,
404
endpoint=None,
405
category=None,
406
class_name=None,
407
icon_type=None,
408
icon_value=None,
409
target=None
410
):
411
"""
412
Initialize external link menu item.
413
414
Args:
415
name (str): Link text
416
url (str, optional): Direct URL
417
endpoint (str, optional): Flask endpoint
418
category (str, optional): Menu category
419
class_name (str, optional): CSS class
420
icon_type (str, optional): Icon type
421
icon_value (str, optional): Icon identifier
422
target (str, optional): Link target (_blank, _self, etc.)
423
"""
424
425
def get_url(self):
426
"""
427
Get link URL.
428
429
Returns:
430
str: Link URL
431
"""
432
433
class SubMenuCategory(MenuCategory):
434
"""
435
Submenu category with dropdown styling.
436
"""
437
```
438
439
## Constants
440
441
Icon type constants for menu customization.
442
443
```python { .api }
444
ICON_TYPE_GLYPH = 'glyph' # Bootstrap glyph icons
445
ICON_TYPE_FONT_AWESOME = 'fa' # Font Awesome icons
446
ICON_TYPE_IMAGE = 'image' # Images relative to Flask static
447
ICON_TYPE_IMAGE_URL = 'image-url' # External image URLs
448
```