Ticket #13163: admin_url.diff
File admin_url.diff, 8.2 KB (added by , 15 years ago) |
---|
-
django/contrib/admin/helpers.py
1 1 from django import forms 2 2 from django.conf import settings 3 from django.contrib.admin.util import flatten_fieldsets, lookup_field 3 from django.contrib.admin.util import flatten_fieldsets, lookup_field, quote 4 4 from django.contrib.admin.util import display_for_field, label_for_field 5 5 from django.contrib.contenttypes.models import ContentType 6 6 from django.core.exceptions import ObjectDoesNotExist … … 11 11 from django.utils.html import escape, conditional_escape 12 12 from django.utils.safestring import mark_safe 13 13 from django.utils.translation import ugettext_lazy as _ 14 from django.core.urlresolvers import reverse, NoReverseMatch 14 15 15 16 16 ACTION_CHECKBOX_NAME = '_selected_action' 17 17 18 18 class ActionForm(forms.Form): … … 231 231 if original is not None: 232 232 self.original_content_type_id = ContentType.objects.get_for_model(original).pk 233 233 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 234 246 super(InlineAdminForm, self).__init__(form, fieldsets, prepopulated_fields, 235 247 readonly_fields) 236 248 -
django/contrib/admin/templates/admin/edit_inline/stacked.html
6 6 7 7 {% 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 %}"> 8 8 <h3><b>{{ inline_admin_formset.opts.verbose_name|title }}:</b> <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 %} 10 11 {% 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 %} 11 12 </h3> 12 13 {% 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
25 25 <td class="original"> 26 26 {% if inline_admin_form.original or inline_admin_form.show_url %}<p> 27 27 {% 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 %} 29 30 </p>{% endif %} 30 31 {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %} 31 32 {{ inline_admin_form.fk_field.field }} -
tests/regressiontests/admin_inlines/tests.py
2 2 3 3 # local test models 4 4 from models import Holder, Inner, InnerInline 5 from models import Household, Person, Phone 5 6 6 7 class TestInline(TestCase): 7 8 fixtures = ['admin-views-users.xml'] … … 28 29 actual = inner_formset.can_delete 29 30 self.assertEqual(expected, actual, 'can_delete must be equal') 30 31 32 33 class 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
48 48 # Test bug #12561 49 49 admin.site.register(Holder, inlines=[InnerInline]) 50 50 51 52 # Test InlineAdminForm.admin_url: 53 54 class Household(models.Model): 55 pass 56 57 class Person(models.Model): 58 household = models.ForeignKey(Household) 59 60 class Phone(models.Model): 61 household = models.ForeignKey(Household) 62 63 class PersonInline(admin.StackedInline): 64 model = Person 65 extra = 1 66 67 class PhoneInline(admin.StackedInline): 68 model = Phone 69 extra = 1 70 71 admin.site.register(Household, inlines=[PersonInline, PhoneInline]) 72 admin.site.register(Person) 73 # (Phone not registered) 74 51 75 __test__ = {'API_TESTS': """ 52 76 53 77 # Regression test for #9362