Ticket #10223: 10223-charfield_pk_with_slashes_view_on_site.diff

File 10223-charfield_pk_with_slashes_view_on_site.diff, 3.0 KB (added by Ramiro Morales, 16 years ago)

Patch from HuCy? and tests from mmarshall, modified to actually test things with the legacy AdminSite?.root view

  • django/contrib/admin/sites.py

    diff -r d656e3f8e358 django/contrib/admin/sites.py
    a b  
    474474        # URLs starting with 'r/' are for the "View on site" links.
    475475        elif url.startswith('r/'):
    476476            from django.contrib.contenttypes.views import shortcut
    477             return shortcut(request, *url.split('/')[1:])
     477            return shortcut(request, *url.split('/', 2)[1:])
    478478        else:
    479479            if '/' in url:
    480480                return self.model_page(request, *url.split('/', 2))
  • tests/regressiontests/admin_views/models.py

    diff -r d656e3f8e358 tests/regressiontests/admin_views/models.py
    a b  
    122122
    123123    def __unicode__(self):
    124124        return self.id
     125
     126    def get_absolute_url(self):
     127        return "/" + self.id
    125128
    126129class Color(models.Model):
    127130    value = models.CharField(max_length=10)
  • tests/regressiontests/admin_views/tests.py

    diff -r d656e3f8e358 tests/regressiontests/admin_views/tests.py
    a b  
    2222    set
    2323except NameError:
    2424    from sets import Set as set
     25
     26class LegacyAdminViewBasicTest(TestCase):
     27    """This test can be removed when we deprecate the root() AdminSite method in Django release 1.1 + 2."""
     28
     29    fixtures = ['admin-views-users.xml']
     30    urlbit = 'legacy_admin'
     31
     32    def setUp(self):
     33        self.client.login(username='super', password='secret')
     34
     35    def tearDown(self):
     36        self.client.logout()
     37
     38    def testShortcutWithSlash(self):
     39        """Ensures the 'view on Site' links work for models with string PKs values that contain slashes."""
     40        obj = ModelWithStringPrimaryKey(id="pk/with/slashes")
     41        obj.save()
     42        content_type_pk = ContentType.objects.get_for_model(ModelWithStringPrimaryKey).pk
     43        short_url = '/test_admin/%s/r/%s/%s/' % (self.urlbit, content_type_pk, obj.pk)
     44        response = self.client.get(short_url)
     45        self.assertRedirects(response, 'http://example.com%s' % obj.get_absolute_url(),
     46                             status_code=302, target_status_code=404)
     47
    2548
    2649class AdminViewBasicTest(TestCase):
    2750    fixtures = ['admin-views-users.xml', 'admin-views-colors.xml', 'admin-views-fabrics.xml']
  • tests/regressiontests/admin_views/urls.py

    diff -r d656e3f8e358 tests/regressiontests/admin_views/urls.py
    a b  
    22from django.contrib import admin
    33import views
    44import customadmin
     5import legacyadmin
    56
    67urlpatterns = patterns('',
    78    (r'^admin/doc/', include('django.contrib.admindocs.urls')),
    89    (r'^admin/secure-view/$', views.secure_view),
    910    (r'^admin/', include(admin.site.urls)),
    1011    (r'^admin2/', include(customadmin.site.urls)),
     12    (r'^legacy_admin/(.*)', legacyadmin.site.root),
    1113)
Back to Top