-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadmin.py
88 lines (71 loc) · 2.43 KB
/
admin.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
# Copyright Collab 2013-2018
# See LICENSE for details.
"""
Custom admin used for testing.
"""
from __future__ import unicode_literals
from django.conf.urls import url
try:
from django.core.urlresolvers import reverse
except ImportError:
from django.urls import reverse
from django.template.response import TemplateResponse
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin import AdminSite
from .models import Location
class CustomAdminSite(AdminSite):
site_header = 'Custom admin site'
def custom_view(self, request):
"""
An additional admin view.
"""
context = dict(
# include common variables for rendering the admin template
self.admin_site.each_context(request),
)
return TemplateResponse(request, 'sometemplate.html', context)
def get_urls(self):
"""
Extend admin URL patterns.
"""
urls = super(CustomAdminSite, self).get_urls()
extra_urls = [
url(r'^customview/$', self.custom_view, name='customview')
]
return urls + extra_urls
def get_app_list(self, request):
app_list = super(CustomAdminSite, self).get_app_list(request)
# categorize apps
categorized_list = [
{'label': _('General'), 'apps': []},
{'label': _('Custom'), 'apps': []},
]
def add_app(key, app):
for i, dic in enumerate(categorized_list):
if dic['label'] == key:
categorized_list[i]['apps'].append(app)
break
for category in categorized_list:
for app in app_list:
name = app['name']
label = category['label']
if label == _('General'):
if name in ['Admin_Appmenu']:
add_app(label, app)
# add custom links for superusers
if request.user.is_superuser:
categorized_list[1]['apps'].append(
{
'name': _('Custom views'),
'models': [
{
'name': _('My custom view'),
'admin_url': reverse('admin:customview')
}
]}
)
else:
categorized_list = []
return categorized_list
admin_site = CustomAdminSite(name='customadmin')
admin_site.register(Location)