2
2
3
3
from django .conf import settings
4
4
from django .contrib import admin
5
+ from django .core .exceptions import ImproperlyConfigured
5
6
from django .core .urlresolvers import resolve
6
7
7
8
@@ -11,30 +12,30 @@ def init_config(self, request, app_list):
11
12
self .request = request
12
13
self .app_list = app_list
13
14
14
- self .config = getattr (settings , 'ADMIN_REORDER' )
15
+ self .config = getattr (settings , 'ADMIN_REORDER' , None )
15
16
if not self .config :
16
17
# ADMIN_REORDER settings is not defined.
17
- raise NameError ('ADMIN_REORDER config is not defined.' )
18
+ raise ImproperlyConfigured ('ADMIN_REORDER config is not defined.' )
18
19
19
20
if not isinstance (self .config , (tuple , list )):
20
- raise TypeError ('ADMIN_REORDER config parameter must be '
21
- 'tuple or list. Got %s' % repr (self .config ))
21
+ raise ImproperlyConfigured (
22
+ 'ADMIN_REORDER config parameter must be tuple or list. '
23
+ 'Got {config}' .format (config = self .config ))
22
24
23
25
admin_index = admin .site .index (request )
24
26
try :
25
27
# try to get all installed models
26
- _app_list = admin_index .context_data ['app_list' ]
27
- except Exception :
28
+ app_list = admin_index .context_data ['app_list' ]
29
+ except KeyError :
28
30
# use app_list from context if this fails
29
- _app_list = app_list
31
+ pass
30
32
31
33
# Flatten all models from apps
32
34
self .models_list = []
33
- for app in _app_list :
35
+ for app in app_list :
34
36
for model in app ['models' ]:
35
- model ['model_name' ] = \
36
- self .get_model_name (app ['app_label' ],
37
- model ['object_name' ])
37
+ model ['model_name' ] = self .get_model_name (
38
+ app ['app_label' ], model ['object_name' ])
38
39
self .models_list .append (model )
39
40
40
41
def get_app_list (self ):
@@ -130,7 +131,7 @@ def process_template_response(self, request, response):
130
131
131
132
try :
132
133
app_list = response .context_data ['app_list' ]
133
- except Exception :
134
+ except KeyError :
134
135
# there is no app_list! nothing to reorder
135
136
return response
136
137
0 commit comments