Ticket #13163: admin_url.diff

File admin_url.diff, 8.2 KB (added by Simon Meers, 15 years ago)

InlineAdminForm changes, stacked/tabular template updates and regression tests

  • django/contrib/admin/helpers.py

     
    11from django import forms
    22from django.conf import settings
    3 from django.contrib.admin.util import flatten_fieldsets, lookup_field
     3from django.contrib.admin.util import flatten_fieldsets, lookup_field, quote
    44from django.contrib.admin.util import display_for_field, label_for_field
    55from django.contrib.contenttypes.models import ContentType
    66from django.core.exceptions import ObjectDoesNotExist
     
    1111from django.utils.html import escape, conditional_escape
    1212from django.utils.safestring import mark_safe
    1313from django.utils.translation import ugettext_lazy as _
     14from django.core.urlresolvers import reverse, NoReverseMatch
    1415
    15 
    1616ACTION_CHECKBOX_NAME = '_selected_action'
    1717
    1818class ActionForm(forms.Form):
     
    231231        if original is not None:
    232232            self.original_content_type_id = ContentType.objects.get_for_model(original).pk
    233233        self.show_url = original and hasattr(original, 'get_absolute_url')
     234        if original is not None and model_admin is not None:
     235            if original.__class__ in model_admin.admin_site._registry:
     236                try:
     237                    view_name = '%s:%s_%s_change' % (
     238                        self.model_admin.admin_site.name,
     239                        original._meta.app_label,
     240                        original._meta.object_name.lower())
     241                    self.admin_url = reverse(
     242                        view_name, None, (quote(original._get_pk_val()),))
     243                except NoReverseMatch:
     244                    # should not happen, but fail silently just in case
     245                    self.admin_url = None
    234246        super(InlineAdminForm, self).__init__(form, fieldsets, prepopulated_fields,
    235247            readonly_fields)
    236248
  • django/contrib/admin/templates/admin/edit_inline/stacked.html

     
    66
    77{% for inline_admin_form in inline_admin_formset %}<div class="inline-related{% if forloop.last %} empty-form last-related{% endif %}" id="{{ inline_admin_formset.formset.prefix }}{% if not forloop.last %}{{ forloop.counter }}{% else %}-empty{% endif %}">
    88  <h3><b>{{ inline_admin_formset.opts.verbose_name|title }}:</b>&nbsp;<span class="inline_label">{% if inline_admin_form.original %}{{ inline_admin_form.original }}{% else %}#{{ forloop.counter }}{% endif %}</span>
    9     {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}
     9    {% if inline_admin_form.admin_url %}<a href="{{ inline_admin_form.admin_url }}">[ {% trans "Edit" %} ]</a>{% endif %}
     10    {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">[ {% trans "View on site" %} ]</a>{% endif %}
    1011    {% if inline_admin_formset.formset.can_delete and inline_admin_form.original %}<span class="delete">{{ inline_admin_form.deletion_field.field }} {{ inline_admin_form.deletion_field.label_tag }}</span>{% endif %}
    1112  </h3>
    1213  {% if inline_admin_form.form.non_field_errors %}{{ inline_admin_form.form.non_field_errors }}{% endif %}
  • django/contrib/admin/templates/admin/edit_inline/tabular.html

     
    2525        <td class="original">
    2626          {% if inline_admin_form.original or inline_admin_form.show_url %}<p>
    2727          {% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}
    28           {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}
     28          {% if inline_admin_form.admin_url %}<a href="{{ inline_admin_form.admin_url }}">[ {% trans "Edit" %} ]</a>{% endif %}
     29          {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">[ {% trans "View on site" %} ]</a>{% endif %}
    2930            </p>{% endif %}
    3031          {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
    3132          {{ inline_admin_form.fk_field.field }}
  • tests/regressiontests/admin_inlines/tests.py

     
    22
    33# local test models
    44from models import Holder, Inner, InnerInline
     5from models import Household, Person, Phone
    56
    67class TestInline(TestCase):
    78    fixtures = ['admin-views-users.xml']
     
    2829        actual = inner_formset.can_delete
    2930        self.assertEqual(expected, actual, 'can_delete must be equal')
    3031
     32       
     33class TestAdminURL(TestCase):
     34    fixtures = ['admin-views-users.xml']
     35   
     36    def get_admin_url(self, obj):
     37        return '/test_admin/admin/admin_inlines/%s/%i/' % (
     38            obj.__class__.__name__.lower(), obj.id)
     39       
     40    def setUp(self):
     41        self.household = Household.objects.create()
     42        self.person = Person.objects.create(household=self.household)
     43        self.phone = Phone.objects.create(household=self.household)
     44
     45        result = self.client.login(username='super', password='secret')
     46        self.failUnlessEqual(result, True)
     47       
     48    def tearDown(self):
     49        self.client.logout()
     50
     51    def test_admin_url(self):
     52        """
     53        admin_url should be set for admin-registered inline models only.
     54
     55        Also check to ensure URLs look correct and only set on bound forms.
     56        """
     57        response = self.client.get(self.get_admin_url(self.household))
     58        for inline_admin_fset in response.context[-1]['inline_admin_formsets']:
     59            for inline_admin_form in inline_admin_fset:
     60                if inline_admin_form.form._meta.model != Person:
     61                    self.assertFalse(hasattr(inline_admin_form, 'admin_url'),
     62                                     'admin_url set with unregistered model')
     63                elif not inline_admin_form.original:
     64                    self.assertFalse(hasattr(inline_admin_form, 'admin_url'),
     65                                     'admin_url set on unbound form!')
     66                else:
     67                    self.assertTrue(hasattr(inline_admin_form, 'admin_url'),
     68                                    'admin_url not set')
     69                    self.assertEqual(inline_admin_form.original, self.person,
     70                                     'original is not expected object')
     71                    self.assertEqual(
     72                        inline_admin_form.admin_url,
     73                        self.get_admin_url(inline_admin_form.original),
     74                        'admin_url appears incorrect')
     75
  • tests/regressiontests/admin_inlines/models.py

     
    4848# Test bug #12561
    4949admin.site.register(Holder, inlines=[InnerInline])
    5050
     51
     52# Test InlineAdminForm.admin_url:
     53
     54class Household(models.Model):
     55    pass
     56
     57class Person(models.Model):
     58    household = models.ForeignKey(Household)
     59
     60class Phone(models.Model):
     61    household = models.ForeignKey(Household)
     62
     63class PersonInline(admin.StackedInline):
     64    model = Person
     65    extra = 1
     66
     67class PhoneInline(admin.StackedInline):
     68    model = Phone
     69    extra = 1
     70   
     71admin.site.register(Household, inlines=[PersonInline, PhoneInline])
     72admin.site.register(Person)
     73# (Phone not registered)
     74
    5175__test__ = {'API_TESTS': """
    5276
    5377# Regression test for #9362
Back to Top