Ticket #14497: admin-filefield-readonly-display_with_tests.diff

File admin-filefield-readonly-display_with_tests.diff, 3.4 KB (added by Paul Collins, 13 years ago)
  • django/contrib/admin/util.py

    diff --git a/django/contrib/admin/util.py b/django/contrib/admin/util.py
    index 61182a6..ba94280 100644
    a b def display_for_field(value, field):  
    329329        return formats.number_format(value, field.decimal_places)
    330330    elif isinstance(field, models.FloatField):
    331331        return formats.number_format(value)
     332    elif isinstance(field, models.FileField):
     333        return mark_safe(u'<a href="%s">%s</a>'
     334                         % (escape(value.url),
     335                            escape(force_unicode(value))))
    332336    else:
    333337        return smart_unicode(value)
    334338
  • new file tests/regressiontests/admin_widgets/readonly_widgetadmin.py

    diff --git a/tests/regressiontests/admin_widgets/readonly_widgetadmin.py b/tests/regressiontests/admin_widgets/readonly_widgetadmin.py
    new file mode 100644
    index 0000000..83d24a6
    - +  
     1"""
     2
     3"""
     4from __future__ import absolute_import
     5
     6from django.contrib import admin
     7
     8from . import models
     9
     10
     11class WidgetAdmin(admin.AdminSite):
     12    pass
     13
     14class AlbumAdmin(admin.ModelAdmin):
     15    readonly_fields = ('cover_art',)
     16
     17site = WidgetAdmin(name='rowidget-admin')
     18
     19site.register(models.Member)
     20site.register(models.Band)
     21site.register(models.Album, AlbumAdmin)
  • tests/regressiontests/admin_widgets/tests.py

    diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py
    index 87e0309..8dc3aae 100644
    a b from django.utils.unittest import TestCase  
    1818
    1919from . import models
    2020from .widgetadmin import site as widget_admin_site
     21from django.test.client import Client
    2122
    2223
    2324admin_media_prefix = lambda: {
    class AdminFileWidgetTest(DjangoTestCase):  
    281282        )
    282283
    283284
     285class ReadOnlyAdminFileWidget(DjangoTestCase):
     286    fixtures = ['admin-widgets-users.xml']
     287    urls = "regressiontests.admin_widgets.urls"
     288
     289    def test_fileupload(self):
     290        """File widgets should render as a link when they're marked Read Only
     291        """
     292
     293        band = models.Band.objects.create(name='Linkin Park')
     294        album = band.album_set.create(
     295            name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg'
     296        )
     297        client = Client()
     298        client.login(username='super', password='secret')
     299        resp = client.get('/ro-admin/admin_widgets/album/%s/' % album.id)
     300        self.assertContains(resp,
     301                            '<a href="/media/albums/hybrid_theory.jpg">albums\hybrid_theory.jpg</a>',
     302                            html=True)
     303        self.assertNotContains(resp,
     304                               '<input type="file" name="cover_art" id="id_cover_art" />',
     305                               html=True)
     306
     307
    284308class ForeignKeyRawIdWidgetTest(DjangoTestCase):
    285309    def test_render(self):
    286310        band = models.Band.objects.create(name='Linkin Park')
  • tests/regressiontests/admin_widgets/urls.py

    diff --git a/tests/regressiontests/admin_widgets/urls.py b/tests/regressiontests/admin_widgets/urls.py
    index aecee90..73b1981 100644
    a b from __future__ import absolute_import  
    22
    33from django.conf.urls import patterns, include
    44
    5 from . import  widgetadmin
     5from . import  widgetadmin, readonly_widgetadmin
    66
    77
    88urlpatterns = patterns('',
    99    (r'^', include(widgetadmin.site.urls)),
     10    (r'^ro-admin/', include(readonly_widgetadmin.site.urls)),
    1011)
Back to Top