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
|
13 | 13 | from django.utils.text import capfirst |
14 | 14 | from django.utils import timezone |
15 | 15 | from django.utils.encoding import force_str, force_text, smart_text |
| 16 | from django.utils.safestring import mark_safe |
16 | 17 | from django.utils import six |
17 | 18 | from django.utils.translation import ungettext |
18 | 19 | from django.core.urlresolvers import reverse, NoReverseMatch |
… |
… |
def display_for_field(value, field):
|
375 | 376 | return formats.number_format(value, field.decimal_places) |
376 | 377 | elif isinstance(field, models.FloatField): |
377 | 378 | return formats.number_format(value) |
| 379 | elif isinstance(field, models.FileField): |
| 380 | return mark_safe('<a href="%s">%s</a>' % (value.url, value)) |
378 | 381 | else: |
379 | 382 | return smart_text(value) |
380 | 383 | |
diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py
index 86fb611..59cfaec 100644
a
|
b
|
class AdminURLWidgetTest(DjangoTestCase):
|
370 | 370 | ) |
371 | 371 | |
372 | 372 | |
373 | | class AdminFileWidgetTest(DjangoTestCase): |
374 | | def test_render(self): |
| 373 | @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',)) |
| 374 | class AdminFileWidgetTests(DjangoTestCase): |
| 375 | fixtures = ['admin-widgets-users.xml'] |
| 376 | urls = "admin_widgets.urls" |
| 377 | |
| 378 | def setUp(self): |
375 | 379 | band = models.Band.objects.create(name='Linkin Park') |
376 | | album = band.album_set.create( |
| 380 | self.album = band.album_set.create( |
377 | 381 | name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg' |
378 | 382 | ) |
379 | 383 | |
| 384 | def test_render(self): |
380 | 385 | w = widgets.AdminFileWidget() |
381 | 386 | self.assertHTMLEqual( |
382 | | w.render('test', album.cover_art), |
| 387 | w.render('test', self.album.cover_art), |
383 | 388 | '<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>' % { |
384 | 389 | 'STORAGE_URL': default_storage.url('') |
385 | 390 | }, |
… |
… |
class AdminFileWidgetTest(DjangoTestCase):
|
390 | 395 | '<input type="file" name="test" />', |
391 | 396 | ) |
392 | 397 | |
| 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 | |
393 | 412 | |
394 | 413 | class ForeignKeyRawIdWidgetTest(DjangoTestCase): |
395 | 414 | urls = 'admin_widgets.urls' |
diff --git a/tests/admin_widgets/widgetadmin.py b/tests/admin_widgets/widgetadmin.py
index fa46530..685daca 100644
a
|
b
|
class CarTireAdmin(admin.ModelAdmin):
|
23 | 23 | class EventAdmin(admin.ModelAdmin): |
24 | 24 | raw_id_fields = ['main_band', 'supporting_bands'] |
25 | 25 | |
| 26 | class AlbumAdmin(admin.ModelAdmin): |
| 27 | fields = ('name', 'cover_art',) |
| 28 | readonly_fields = ('cover_art',) |
26 | 29 | |
27 | 30 | class SchoolAdmin(admin.ModelAdmin): |
28 | 31 | filter_vertical = ('students',) |
… |
… |
site.register(models.CarTire, CarTireAdmin)
|
37 | 40 | site.register(models.Member) |
38 | 41 | site.register(models.Band) |
39 | 42 | site.register(models.Event, EventAdmin) |
40 | | site.register(models.Album) |
| 43 | site.register(models.Album, AlbumAdmin) |
41 | 44 | |
42 | 45 | site.register(models.Inventory) |
43 | 46 | |