Skip to content

Commit 6de6a72

Browse files
committed
Minor refactor.
@todo rewrite this package.
1 parent 4c1eb8d commit 6de6a72

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

admin_reorder/middleware.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from django.conf import settings
44
from django.contrib import admin
5+
from django.core.exceptions import ImproperlyConfigured
56
from django.core.urlresolvers import resolve
67

78

@@ -11,30 +12,30 @@ def init_config(self, request, app_list):
1112
self.request = request
1213
self.app_list = app_list
1314

14-
self.config = getattr(settings, 'ADMIN_REORDER')
15+
self.config = getattr(settings, 'ADMIN_REORDER', None)
1516
if not self.config:
1617
# ADMIN_REORDER settings is not defined.
17-
raise NameError('ADMIN_REORDER config is not defined.')
18+
raise ImproperlyConfigured('ADMIN_REORDER config is not defined.')
1819

1920
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))
2224

2325
admin_index = admin.site.index(request)
2426
try:
2527
# 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:
2830
# use app_list from context if this fails
29-
_app_list = app_list
31+
pass
3032

3133
# Flatten all models from apps
3234
self.models_list = []
33-
for app in _app_list:
35+
for app in app_list:
3436
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'])
3839
self.models_list.append(model)
3940

4041
def get_app_list(self):
@@ -130,7 +131,7 @@ def process_template_response(self, request, response):
130131

131132
try:
132133
app_list = response.context_data['app_list']
133-
except Exception:
134+
except KeyError:
134135
# there is no app_list! nothing to reorder
135136
return response
136137

0 commit comments

Comments
 (0)