Ticket #10061: admin-urls.8.diff

File admin-urls.8.diff, 17.1 KB (added by Alex Gaynor, 16 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index ed41c7f..8aff5a7 100644
    a b class ModelAdmin(BaseModelAdmin):  
    217217            url(r'^add/$',
    218218                wrap(self.add_view),
    219219                name='%sadmin_%s_%s_add' % info),
    220             url(r'^(.+)/history/$',
     220            url(r'^(?P<object_id>.+)/history/$',
    221221                wrap(self.history_view),
    222222                name='%sadmin_%s_%s_history' % info),
    223             url(r'^(.+)/delete/$',
     223            url(r'^(?P<object_id>.+)/delete/$',
    224224                wrap(self.delete_view),
    225225                name='%sadmin_%s_%s_delete' % info),
    226             url(r'^(.+)/$',
     226            url(r'^(?P<object_id>.+)/$',
    227227                wrap(self.change_view),
    228228                name='%sadmin_%s_%s_change' % info),
    229229        )
    class ModelAdmin(BaseModelAdmin):  
    437437            'save_as': self.save_as,
    438438            'save_on_top': self.save_on_top,
    439439            'root_path': self.admin_site.root_path,
     440            'admin_site': self.admin_site.name,
    440441        })
    441442        return render_to_response(self.change_form_template or [
    442443            "admin/%s/%s/change_form.html" % (app_label, opts.object_name.lower()),
    class ModelAdmin(BaseModelAdmin):  
    571572            'errors': helpers.AdminErrorList(form, formsets),
    572573            'root_path': self.admin_site.root_path,
    573574            'app_label': opts.app_label,
     575            'admin_site': self.admin_site.name,
    574576        }
    575577        context.update(extra_context or {})
    576578        return self.render_change_form(request, context, add=True)
    class ModelAdmin(BaseModelAdmin):  
    649651            'inline_admin_formsets': inline_admin_formsets,
    650652            'errors': helpers.AdminErrorList(form, formsets),
    651653            'root_path': self.admin_site.root_path,
     654            'admin_site': self.admin_site.name,
    652655            'app_label': opts.app_label,
    653656        }
    654657        context.update(extra_context or {})
    class ModelAdmin(BaseModelAdmin):  
    681684            'cl': cl,
    682685            'has_add_permission': self.has_add_permission(request),
    683686            'root_path': self.admin_site.root_path,
     687            'admin_site': self.admin_site.name,
    684688            'app_label': app_label,
    685689        }
    686690        context.update(extra_context or {})
    class ModelAdmin(BaseModelAdmin):  
    736740            "perms_lacking": perms_needed,
    737741            "opts": opts,
    738742            "root_path": self.admin_site.root_path,
     743            'admin_site': self.admin_site.name,
    739744            "app_label": app_label,
    740745        }
    741746        context.update(extra_context or {})
    class ModelAdmin(BaseModelAdmin):  
    763768            'module_name': capfirst(force_unicode(opts.verbose_name_plural)),
    764769            'object': obj,
    765770            'root_path': self.admin_site.root_path,
     771            'admin_site': self.admin_site.name,
    766772            'app_label': app_label,
    767773        }
    768774        context.update(extra_context or {})
  • django/contrib/admin/sites.py

    diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py
    index 42ce296..d77eebd 100644
    a b from django import http, template  
    33from django.contrib.admin import ModelAdmin
    44from django.contrib.auth import authenticate, login
    55from django.db.models.base import ModelBase
     6from django.core.urlresolvers import reverse
    67from django.core.exceptions import ImproperlyConfigured
    78from django.shortcuts import render_to_response
    89from django.utils.functional import update_wrapper
    class AdminSite(object):  
    3536   
    3637    def __init__(self, name=None):
    3738        self._registry = {} # model_class class -> admin_class instance
    38         # TODO Root path is used to calculate urls under the old root() method
    39         # in order to maintain backwards compatibility we are leaving that in
    40         # so root_path isn't needed, not sure what to do about this.
    41         self.root_path = 'admin/'
     39        self.root_path = None
    4240        if name is None:
    4341            name = ''
    4442        else:
    class AdminSite(object):  
    163161                name='%sadmin_index' % self.name),
    164162            url(r'^logout/$',
    165163                wrap(self.logout),
    166                 name='%sadmin_logout'),
     164                name='%sadmin_logout' % self.name),
    167165            url(r'^password_change/$',
    168166                wrap(self.password_change),
    169167                name='%sadmin_password_change' % self.name),
    class AdminSite(object):  
    197195        Handles the "change password" task -- both form display and validation.
    198196        """
    199197        from django.contrib.auth.views import password_change
     198        if self.root_path is not None:
     199            url = '%spassword_change/done/' % self.root_path
     200        else:
     201            url = reverse('%sadmin_password_change_done' % self.name)
    200202        return password_change(request,
    201             post_change_redirect='%spassword_change/done/' % self.root_path)
     203            post_change_redirect=url)
    202204   
    203205    def password_change_done(self, request):
    204206        """
    class AdminSite(object):  
    328330            'title': _('Site administration'),
    329331            'app_list': app_list,
    330332            'root_path': self.root_path,
     333            'admin_site': self.name
    331334        }
    332335        context.update(extra_context or {})
    333336        return render_to_response(self.index_template or 'admin/index.html', context,
    class AdminSite(object):  
    342345            'app_path': request.get_full_path(),
    343346            'error_message': error_message,
    344347            'root_path': self.root_path,
     348            'admin_site': self.name,
    345349        }
    346350        context.update(extra_context or {})
    347351        return render_to_response(self.login_template or 'admin/login.html', context,
    class AdminSite(object):  
    388392            'title': _('%s administration') % capfirst(app_label),
    389393            'app_list': [app_dict],
    390394            'root_path': self.root_path,
     395            'admin_site': self.name,
    391396        }
    392397        context.update(extra_context or {})
    393398        return render_to_response(self.app_index_template or 'admin/app_index.html', context,
  • django/contrib/admin/templates/admin/base.html

    diff --git a/django/contrib/admin/templates/admin/base.html b/django/contrib/admin/templates/admin/base.html
    index e969d1b..623d769 100644
    a b  
    2525        {% block branding %}{% endblock %}
    2626        </div>
    2727        {% if user.is_authenticated and user.is_staff %}
    28         <div id="user-tools">{% trans 'Welcome,' %} <strong>{% firstof user.first_name user.username %}</strong>. {% block userlinks %}{% url django-admindocs-docroot as docsroot %}{% if docsroot %}<a href="{{ docsroot }}">{% trans 'Documentation' %}</a> / {% endif %}<a href="{{ root_path }}password_change/">{% trans 'Change password' %}</a> / <a href="{{ root_path }}logout/">{% trans 'Log out' %}</a>{% endblock %}</div>
     28        <div id="user-tools">
     29            {% trans 'Welcome,' %}
     30            <strong>
     31                {% firstof user.first_name user.username %}
     32            </strong>.
     33            {% block userlinks %}
     34                {% url django-admindocs-docroot as docsroot %}
     35                {% if docsroot %}
     36                    <a href="{{ docsroot }}">
     37                        {% trans 'Documentation' %}
     38                    </a> /
     39                {% endif %}
     40                {% url admin_site:admin_password_change as password_change_url %}
     41                {% if password_change_url %}
     42                    <a href="{{ password_change_url }}">
     43                {% else %}
     44                    <a href="{{ root_path }}password_change/">
     45                {% endif %}
     46                    {% trans 'Change password' %}
     47                </a> /
     48                {% url admin_site:admin_logout as logout_url %}
     49                {% if logout_url %}
     50                    <a href="{{ logout_url }}">
     51                {% else %}
     52                    <a href="{{ root_path }}logout/">
     53                {% endif %}
     54                    {% trans 'Log out' %}
     55                </a>
     56            {% endblock %}
     57        </div>
    2958        {% endif %}
    3059        {% block nav-global %}{% endblock %}
    3160    </div>
  • django/contrib/auth/admin.py

    diff --git a/django/contrib/auth/admin.py b/django/contrib/auth/admin.py
    index c5326b7..9a13205 100644
    a b class UserAdmin(admin.ModelAdmin):  
    9292            'save_as': False,
    9393            'username_help_text': self.model._meta.get_field('username').help_text,
    9494            'root_path': self.admin_site.root_path,
     95            'admin_site': self.admin_site.name,
    9596            'app_label': self.model._meta.app_label,           
    9697        }, context_instance=template.RequestContext(request))
    9798
    class UserAdmin(admin.ModelAdmin):  
    122123            'save_as': False,
    123124            'show_save': True,
    124125            'root_path': self.admin_site.root_path,
     126            'admin_site': self.admin_site.name,
    125127        }, context_instance=RequestContext(request))
    126128
    127129
  • django/contrib/comments/views/moderation.py

    diff --git a/django/contrib/comments/views/moderation.py b/django/contrib/comments/views/moderation.py
    index 3334b09..880c6d7 100644
    a b from django.core.paginator import Paginator, InvalidPage  
    77from django.http import Http404
    88from django.contrib import comments
    99from django.contrib.comments import signals
     10from django.contrib import admin
    1011
    1112#@login_required
    1213def flag(request, comment_id, next=None):
    def moderation_queue(request):  
    185186        'previous': page - 1,
    186187        'pages': paginator.num_pages,
    187188        'hits' : paginator.count,
    188         'page_range' : paginator.page_range
     189        'page_range' : paginator.page_range,
     190        'admin_site': admin.site.name,
    189191    }, context_instance=template.RequestContext(request))
    190192
    191193moderation_queue = permission_required("comments.can_moderate")(moderation_queue)
  • django/core/urlresolvers.py

    diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py
    index 59dcdcb..7044842 100644
    a b def resolve(path, urlconf=None):  
    254254    return get_resolver(urlconf).resolve(path)
    255255
    256256def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None):
     257    if isinstance(viewname, basestring):
     258        viewname = ''.join(viewname.split(':'))
    257259    args = args or []
    258260    kwargs = kwargs or {}
    259261    if prefix is None:
  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index 0911309..c72fb0b 100644
    a b class URLNode(Node):  
    368368
    369369    def render(self, context):
    370370        from django.core.urlresolvers import reverse, NoReverseMatch
     371        view_name_parts = []
     372        for part in self.view_name[:-1]:
     373            try:
     374                view_name_parts.append(Variable(part).resolve(context))
     375            except VariableDoesNotExist:
     376                view_name_parts.append(part)
     377        view_name_parts.append(self.view_name[-1])
     378        view_name = ':'.join(view_name_parts)
    371379        args = [arg.resolve(context) for arg in self.args]
    372380        kwargs = dict([(smart_str(k,'ascii'), v.resolve(context))
    373381                       for k, v in self.kwargs.items()])
    class URLNode(Node):  
    379387        # {% url ... as var %} construct in which cause return nothing.
    380388        url = ''
    381389        try:
    382             url = reverse(self.view_name, args=args, kwargs=kwargs)
     390            url = reverse(view_name, args=args, kwargs=kwargs)
    383391        except NoReverseMatch:
    384392            project_name = settings.SETTINGS_MODULE.split('.')[0]
    385393            try:
    386                 url = reverse(project_name + '.' + self.view_name,
     394                url = reverse(project_name + '.' + view_name,
    387395                              args=args, kwargs=kwargs)
    388396            except NoReverseMatch:
    389397                if self.asvar is None:
    def url(parser, token):  
    11021110        raise TemplateSyntaxError("'%s' takes at least one argument"
    11031111                                  " (path to a view)" % bits[0])
    11041112    viewname = bits[1]
     1113    if ':' in viewname:
     1114        viewname = viewname.split(':')
     1115    else:
     1116        viewname = [viewname]
    11051117    args = []
    11061118    kwargs = {}
    11071119    asvar = None
  • docs/ref/contrib/admin.txt

    diff --git a/docs/ref/contrib/admin.txt b/docs/ref/contrib/admin.txt
    index 35bfde2..f15fe1c 100644
    a b It possible to add additional views to the admin site in the same way one can  
    11861186add them to ``ModelAdmins``.  This by using the ``get_urls()`` method on an
    11871187AdminSite in the same way as `described above`__
    11881188
     1189.. note::
     1190    Any view you render that uses the admin templates, or extends the base
     1191    admin template should include in it's context a variable named
     1192    ``admin_site`` that contains the ``AdminSite`` instances name, which for
     1193    ``AdminSite`` instances exists at ``self.name`` and for ``ModelAdmin``
     1194    instances exists at ``self.admin_site.name``.
     1195
    11891196__ `get_urls(self)`_
     1197
     1198
     1199Reversing Admin URLs
     1200====================
     1201
     1202.. versionadded :: 1.1
     1203    Before Django 1.1 it wasn't possible to reverse admin URLs.  In addition
     1204    for this to work you need to be using the new Django 1.1 ``include()``
     1205    syntax for setting up admin URLs.
     1206
     1207The following are the url names, and parameters for ``AdminSite`` urls, all
     1208url names are prefixed with the ``AdminSite`` instace's name, followed by an
     1209underscore, so if an ``AdminSite`` was named ``"user_admin"`` it's urls names
     1210would be prefixed with ``"user_admin_"``, the default ``AdminSite``'s name is
     1211``''`` however it's names *do not* have the trailing underscore:
     1212
     1213    ======================  =============================== =============
     1214    Page                    URL name                        Parameters
     1215    ======================  =============================== =============
     1216    Index                   ``admin_index``
     1217    Logout                  ``admin_logout``
     1218    Password change         ``admin_password_change``
     1219    Password change done    ``admin_password_change_done``
     1220    i18n javascript         ``admin_jsi18n``
     1221    Application index page  ``admin_app_list``              ``app_label``
     1222    ======================  =============================== =============
     1223
     1224The remaining urls are for ``ModelAdmin`` instances, these too are all prefixed
     1225with the ``AdminSite``'s name(with the same caviets as the ``Adminsite``, and
     1226``app_label`` and ``model_name`` are the lowercase versions of the
     1227application's name and the model's name:
     1228
     1229    ======================  =====================================================   =============
     1230    Page                    URL name                                                Parameters
     1231    ======================  =====================================================   =============
     1232    Changelist              ``admin_{{ app_label }}_{{ model_name }}_changelist``
     1233    Add                     ``admin_{{ app_label }}_{{ model_name }}_add``
     1234    History                 ``admin_{{ app_label }}_{{ model_name }}_history``      ``object_id``
     1235    Delete                  ``admin_{{ app_label }}_{{ model_name }}_delete``       ``object_id``
     1236    Change                  ``admin_{{ app_label }}_{{ model_name }}_change``       ``object_id``
     1237    ======================  =====================================================   =============
     1238
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index 39daf11..52ce268 100644
    a b class AdminViewBasicTest(TestCase):  
    172172        self.assertRedirects(response, '/test_admin/%s/admin_views/thing/?e=1' % self.urlbit)       
    173173        response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit, {'color__id__exact': 'StringNotInteger!'})
    174174        self.assertRedirects(response, '/test_admin/%s/admin_views/thing/?e=1' % self.urlbit)
     175   
     176    def testLogoutAndPasswordChangeURLs(self):
     177        response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit)
     178        self.failIf('<a href="/test_admin/%s/logout/">' % self.urlbit not in response.content)
     179        self.failIf('<a href="/test_admin/%s/password_change/">' % self.urlbit not in response.content)
    175180
    176181class CustomModelAdminTest(AdminViewBasicTest):
    177182    urlbit = "admin2"
  • tests/urls.py

    diff --git a/tests/urls.py b/tests/urls.py
    index 43806de..a192f16 100644
    a b urlpatterns = patterns('',  
    2020
    2121    # test urlconf for middleware tests
    2222    (r'^middleware/', include('regressiontests.middleware.urls')),
     23
     24    # admin widget tests
     25    (r'widget_admin/', include('regressiontests.admin_widgets.urls')),
    2326   
    2427    # admin view tests
    2528    (r'^test_admin/', include('regressiontests.admin_views.urls')),
    2629    (r'^generic_inline_admin/', include('regressiontests.generic_inline_admin.urls')),
    2730   
    28     # admin widget tests
    29     (r'widget_admin/', include('regressiontests.admin_widgets.urls')),
    3031
    3132    (r'^utils/', include('regressiontests.utils.urls')),
    3233
Back to Top