Ticket #5500: adminindex.diff

File adminindex.diff, 4.7 KB (added by Jannis Leidel, 17 years ago)

Implemented template tag admin_index in django.contrib.admin.templatetags.admin_list which is now also used by django.core.management.commands.adminindex

  • django/core/management/commands/adminindex.py

     
    11from django.core.management.base import AppCommand
    2 from django.utils.encoding import force_unicode
    3 from django.utils.text import capfirst
    42
    5 MODULE_TEMPLATE = '''    {%% if perms.%(app)s.%(addperm)s or perms.%(app)s.%(changeperm)s %%}
    6     <tr>
    7         <th>{%% if perms.%(app)s.%(changeperm)s %%}<a href="%(app)s/%(mod)s/">{%% endif %%}%(name)s{%% if perms.%(app)s.%(changeperm)s %%}</a>{%% endif %%}</th>
    8         <td class="x50">{%% if perms.%(app)s.%(addperm)s %%}<a href="%(app)s/%(mod)s/add/" class="addlink">{%% endif %%}Add{%% if perms.%(app)s.%(addperm)s %%}</a>{%% endif %%}</td>
    9         <td class="x75">{%% if perms.%(app)s.%(changeperm)s %%}<a href="%(app)s/%(mod)s/" class="changelink">{%% endif %%}Change{%% if perms.%(app)s.%(changeperm)s %%}</a>{%% endif %%}</td>
    10     </tr>
    11     {%% endif %%}'''
    12 
    133class Command(AppCommand):
    144    help = 'Prints the admin-index template snippet for the given app name(s).'
    155
    166    def handle_app(self, app, **options):
    17         from django.db.models import get_models
    18         output = []
    19         app_models = get_models(app)
    20         app_label = app_models[0]._meta.app_label
    21         output.append('{%% if perms.%s %%}' % app_label)
    22         output.append('<div class="module"><h2>%s</h2><table>' % app_label.title())
    23         for model in app_models:
    24             if model._meta.admin:
    25                 output.append(MODULE_TEMPLATE % {
    26                     'app': app_label,
    27                     'mod': model._meta.module_name,
    28                     'name': force_unicode(capfirst(model._meta.verbose_name_plural)),
    29                     'addperm': model._meta.get_add_permission(),
    30                     'changeperm': model._meta.get_change_permission(),
    31                 })
    32         output.append('</table></div>')
    33         output.append('{% endif %}')
    34         return '\n'.join(output)
     7        from django.contrib.admin.templatetags.admin_list import admin_index
     8        return admin_index(app)
  • django/contrib/admin/templatetags/admin_list.py

     
    1616
    1717DOT = '.'
    1818
     19MODULE_TEMPLATE = '''    {%% if perms.%(app)s.%(addperm)s or perms.%(app)s.%(changeperm)s %%}
     20    <tr>
     21        <th>{%% if perms.%(app)s.%(changeperm)s %%}<a href="%(app)s/%(mod)s/">{%% endif %%}%(name)s{%% if perms.%(app)s.%(changeperm)s %%}</a>{%% endif %%}</th>
     22        <td class="x50">{%% if perms.%(app)s.%(addperm)s %%}<a href="%(app)s/%(mod)s/add/" class="addlink">{%% endif %%}Add{%% if perms.%(app)s.%(addperm)s %%}</a>{%% endif %%}</td>
     23        <td class="x75">{%% if perms.%(app)s.%(changeperm)s %%}<a href="%(app)s/%(mod)s/" class="changelink">{%% endif %%}Change{%% if perms.%(app)s.%(changeperm)s %%}</a>{%% endif %%}</td>
     24    </tr>
     25    {%% endif %%}'''
     26
     27def admin_index(app):
     28    from django.db.models import get_models
     29    output = []
     30    app_models = get_models(app)
     31    app_label = app_models[0]._meta.app_label
     32    output.append('{%% if perms.%s %%}' % app_label)
     33    output.append('<div class="module"><h2>%s</h2><table>' % app_label.title())
     34    for model in app_models:
     35        if model._meta.admin:
     36            output.append(MODULE_TEMPLATE % {
     37                'app': app_label,
     38                'mod': model._meta.module_name,
     39                'name': force_unicode(capfirst(model._meta.verbose_name_plural)),
     40                'addperm': model._meta.get_add_permission(),
     41                'changeperm': model._meta.get_change_permission(),
     42            })
     43    output.append('</table></div>')
     44    output.append('{% endif %}')
     45    return '\n'.join(output)
     46admin_index = register.simple_tag(admin_index)
     47
    1948def paginator_number(cl,i):
    2049    if i == DOT:
    2150        return u'... '
  • docs/django-admin.txt

     
    8383Use admin-index template snippets if you want to customize the look and feel of
    8484your admin's index page. See `Tutorial 2`_ for more information.
    8585
     86**New in Django development version**
     87
     88You can also use a template tag to show the admin index in your own templates::
     89
     90    {% load admin_list %}
     91    {% admin_index %}
     92
     93Just make sure ``django.contrib.admin`` is in your ``INSTALLED_APS`` setting.
     94
    8695.. _Tutorial 2: ../tutorial02/
    8796
    8897createcachetable <tablename>
Back to Top