Created
October 31, 2014 03:17
-
-
Save hidashun/9d342370af267d978c21 to your computer and use it in GitHub Desktop.
django1.7.1の管理画面でモデルを登録順に連番を振って表示する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import OrderedDict | |
from django.apps import apps | |
from django.contrib.admin import AdminSite | |
from django.core.urlresolvers import reverse, NoReverseMatch | |
from django.template.response import TemplateResponse | |
from django.utils.text import capfirst | |
from django.views.decorators.cache import never_cache | |
import six | |
class OrderedModelAdminSite(AdminSite): | |
def __init__(self, name='admin', app_name='admin'): | |
super(OrderedModelAdminSite, self).__init__(name=name, app_name=app_name) | |
self._registry = OrderedDict() | |
@never_cache | |
def index(self, request, extra_context=None): | |
""" | |
Displays the main admin index page, which lists all of the installed | |
apps that have been registered in this site. | |
""" | |
app_dict = {} | |
user = request.user | |
for i, v in enumerate(self._registry.items()): | |
model, model_admin = v | |
app_label = model._meta.app_label | |
has_module_perms = user.has_module_perms(app_label) | |
if has_module_perms: | |
perms = model_admin.get_model_perms(request) | |
# Check whether user has any perm for this module. | |
# If so, add the module to the model_list. | |
if True in perms.values(): | |
info = (app_label, model._meta.model_name) | |
model_dict = { | |
'name': '{:003d}. '.format(i + 1) + str(capfirst(model._meta.verbose_name_plural)), | |
'object_name': model._meta.object_name, | |
'perms': perms, | |
} | |
if perms.get('change', False): | |
try: | |
model_dict['admin_url'] = reverse('admin:%s_%s_changelist' % info, current_app=self.name) | |
except NoReverseMatch: | |
pass | |
if perms.get('add', False): | |
try: | |
model_dict['add_url'] = reverse('admin:%s_%s_add' % info, current_app=self.name) | |
except NoReverseMatch: | |
pass | |
if app_label in app_dict: | |
app_dict[app_label]['models'].append(model_dict) | |
else: | |
app_dict[app_label] = { | |
'name': apps.get_app_config(app_label).verbose_name, | |
'app_label': app_label, | |
'app_url': reverse('admin:app_list', kwargs={'app_label': app_label}, current_app=self.name), | |
'has_module_perms': has_module_perms, | |
'models': [model_dict], | |
} | |
# Sort the apps alphabetically. | |
app_list = list(six.itervalues(app_dict)) | |
app_list.sort(key=lambda x: x['name'].lower()) | |
# Sort the models alphabetically within each app. | |
# for app in app_list: | |
# app['models'].sort(key=lambda x: x['name']) | |
context = dict( | |
self.each_context(), | |
title=self.index_title, | |
app_list=app_list, | |
) | |
context.update(extra_context or {}) | |
return TemplateResponse(request, self.index_template or | |
'admin/index.html', context, | |
current_app=self.name) | |
admin.site = OrderedModelAdminSite() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment