Ticket #14497: 14497.admin-filewidget-readonly.diff

File 14497.admin-filewidget-readonly.diff, 3.7 KB (added by Julien Phalip, 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..3d70e3e 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 AdminSplitDateTimeWidgetTest(DjangoTestCase):  
    262263                )
    263264
    264265
    265 class AdminFileWidgetTest(DjangoTestCase):
     266class AdminFileWidgetTests(DjangoTestCase):
     267    fixtures = ['admin-widgets-users.xml']
     268    urls = "regressiontests.admin_widgets.urls"
     269
    266270    def test_render(self):
    267271        band = models.Band.objects.create(name='Linkin Park')
    268272        album = band.album_set.create(
    class AdminFileWidgetTest(DjangoTestCase):  
    280284            '<input type="file" name="test" />',
    281285        )
    282286
     287    def test_readonly(self):
     288        """File widgets should render as a link when they're marked Read Only
     289        """
     290
     291        band = models.Band.objects.create(name='Linkin Park')
     292        album = band.album_set.create(
     293            name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg'
     294        )
     295        client = Client()
     296        client.login(username='super', password='secret')
     297        resp = client.get('/ro-admin/admin_widgets/album/%s/' % album.id)
     298        self.assertContains(resp,
     299                            '<p><a href="%(STORAGE_URL)salbums/hybrid_theory.jpg">albums\hybrid_theory.jpg</a></p>' % { 'STORAGE_URL': default_storage.url('') },
     300                            html=True)
     301        self.assertNotContains(resp,
     302                               '<input type="file" name="cover_art" id="id_cover_art" />',
     303                               html=True)
     304
    283305
    284306class ForeignKeyRawIdWidgetTest(DjangoTestCase):
    285307    def test_render(self):
  • 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