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

File 14497-admin-filewidget-readonly.diff, 4.0 KB (added by rm_, 11 years ago)

Updated to apply to a recent git master, also simplified a bit by reusing current widgetadmin instead of creating a new one

  • django/contrib/admin/utils.py

    diff --git a/django/contrib/admin/utils.py b/django/contrib/admin/utils.py
    index 0c8ac1c..95c80aa 100644
    a b from django.utils.html import format_html  
    1313from django.utils.text import capfirst
    1414from django.utils import timezone
    1515from django.utils.encoding import force_str, force_text, smart_text
     16from django.utils.safestring import mark_safe
    1617from django.utils import six
    1718from django.utils.translation import ungettext
    1819from django.core.urlresolvers import reverse, NoReverseMatch
    def display_for_field(value, field):  
    375376        return formats.number_format(value, field.decimal_places)
    376377    elif isinstance(field, models.FloatField):
    377378        return formats.number_format(value)
     379    elif isinstance(field, models.FileField):
     380        return mark_safe('<a href="%s">%s</a>' % (value.url, value))
    378381    else:
    379382        return smart_text(value)
    380383
  • tests/admin_widgets/tests.py

    diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py
    index 86fb611..59cfaec 100644
    a b class AdminURLWidgetTest(DjangoTestCase):  
    370370        )
    371371
    372372
    373 class AdminFileWidgetTest(DjangoTestCase):
    374     def test_render(self):
     373@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
     374class AdminFileWidgetTests(DjangoTestCase):
     375    fixtures = ['admin-widgets-users.xml']
     376    urls = "admin_widgets.urls"
     377
     378    def setUp(self):
    375379        band = models.Band.objects.create(name='Linkin Park')
    376         album = band.album_set.create(
     380        self.album = band.album_set.create(
    377381            name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg'
    378382        )
    379383
     384    def test_render(self):
    380385        w = widgets.AdminFileWidget()
    381386        self.assertHTMLEqual(
    382             w.render('test', album.cover_art),
     387            w.render('test', self.album.cover_art),
    383388            '<p class="file-upload">Currently: <a href="%(STORAGE_URL)salbums/hybrid_theory.jpg">albums\hybrid_theory.jpg</a> <span class="clearable-file-input"><input type="checkbox" name="test-clear" id="test-clear_id" /> <label for="test-clear_id">Clear</label></span><br />Change: <input type="file" name="test" /></p>' % {
    384389                'STORAGE_URL': default_storage.url('')
    385390            },
    class AdminFileWidgetTest(DjangoTestCase):  
    390395            '<input type="file" name="test" />',
    391396        )
    392397
     398    def test_readonly(self):
     399        """
     400        File widgets should render as a link when they're marked Read Only
     401        """
     402
     403        self.client.login(username="super", password="secret")
     404        response = self.client.get('/admin_widgets/album/%s/' % self.album.id)
     405        self.assertContains(response,
     406                            '<p><a href="%(STORAGE_URL)salbums/hybrid_theory.jpg">albums\hybrid_theory.jpg</a></p>' % { 'STORAGE_URL': default_storage.url('') },
     407                            html=True)
     408        self.assertNotContains(response,
     409                               '<input type="file" name="cover_art" id="id_cover_art" />',
     410                               html=True)
     411
    393412
    394413class ForeignKeyRawIdWidgetTest(DjangoTestCase):
    395414    urls = 'admin_widgets.urls'
  • tests/admin_widgets/widgetadmin.py

    diff --git a/tests/admin_widgets/widgetadmin.py b/tests/admin_widgets/widgetadmin.py
    index fa46530..685daca 100644
    a b class CarTireAdmin(admin.ModelAdmin):  
    2323class EventAdmin(admin.ModelAdmin):
    2424    raw_id_fields = ['main_band', 'supporting_bands']
    2525
     26class AlbumAdmin(admin.ModelAdmin):
     27    fields = ('name', 'cover_art',)
     28    readonly_fields = ('cover_art',)
    2629
    2730class SchoolAdmin(admin.ModelAdmin):
    2831    filter_vertical = ('students',)
    site.register(models.CarTire, CarTireAdmin)  
    3740site.register(models.Member)
    3841site.register(models.Band)
    3942site.register(models.Event, EventAdmin)
    40 site.register(models.Album)
     43site.register(models.Album, AlbumAdmin)
    4144
    4245site.register(models.Inventory)
    4346
Back to Top