Ticket #14454: admin_widgets.diff

File admin_widgets.diff, 16.9 KB (added by Preston Timmons, 14 years ago)
  • tests/regressiontests/admin_widgets/models.py

    diff --git a/tests/regressiontests/admin_widgets/models.py b/tests/regressiontests/admin_widgets/models.py
    index 8945054..6d51746 100644
    a b  
    1 
    2 from django.conf import settings
    31from django.db import models
    4 from django.core.files.storage import default_storage
    52from django.contrib.auth.models import User
    63
    74class MyFileField(models.FileField):
    class CarTire(models.Model):  
    6966    A single car tire. This to test that a user can only select their own cars.
    7067    """
    7168    car = models.ForeignKey(Car)
    72 
    73 __test__ = {'WIDGETS_TESTS': """
    74 >>> from datetime import datetime
    75 >>> from django.utils.html import escape, conditional_escape
    76 >>> from django.core.files.uploadedfile import SimpleUploadedFile
    77 >>> from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime
    78 >>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget, ManyToManyRawIdWidget
    79 >>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
    80 >>> from django.utils.translation import activate, deactivate
    81 >>> from django.conf import settings
    82 
    83 
    84 Calling conditional_escape on the output of widget.render will simulate what
    85 happens in the template. This is easier than setting up a template and context
    86 for each test.
    87 
    88 Make sure that the Admin widgets render properly, that is, without their extra
    89 HTML escaped.
    90 
    91 >>> w = FilteredSelectMultiple('test', False)
    92 >>> print conditional_escape(w.render('test', 'test'))
    93 <select multiple="multiple" name="test" class="selectfilter">
    94 </select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
    95 <BLANKLINE>
    96 
    97 >>> w = FilteredSelectMultiple('test', True)
    98 >>> print conditional_escape(w.render('test', 'test'))
    99 <select multiple="multiple" name="test" class="selectfilterstacked">
    100 </select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
    101 <BLANKLINE>
    102 
    103 >>> w = AdminSplitDateTime()
    104 >>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
    105 <p class="datetime">Date: <input value="2007-12-01" type="text" class="vDateField" name="test_0" size="10" /><br />Time: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>
    106 >>> activate('de-at')
    107 >>> settings.USE_L10N = True
    108 >>> w.is_localized = True
    109 >>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
    110 <p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>
    111 >>> deactivate()
    112 >>> settings.USE_L10N = False
    113 
    114 >>> band = Band.objects.create(pk=1, name='Linkin Park')
    115 >>> album = band.album_set.create(name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg')
    116 
    117 >>> w = AdminFileWidget()
    118 >>> print conditional_escape(w.render('test', album.cover_art))
    119 <p class="file-upload">Currently: <a target="_blank" 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>
    120 >>> print conditional_escape(w.render('test', SimpleUploadedFile('test', 'content')))
    121 <input type="file" name="test" />
    122 
    123 >>> rel = Album._meta.get_field('band').rel
    124 >>> w = ForeignKeyRawIdWidget(rel)
    125 >>> print conditional_escape(w.render('test', band.pk, attrs={}))
    126 <input type="text" name="test" value="1" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Linkin Park</strong>
    127 
    128 >>> m1 = Member.objects.create(pk=1, name='Chester')
    129 >>> m2 = Member.objects.create(pk=2, name='Mike')
    130 >>> band.members.add(m1, m2)
    131 
    132 >>> rel = Band._meta.get_field('members').rel
    133 >>> w = ManyToManyRawIdWidget(rel)
    134 >>> print conditional_escape(w.render('test', [m1.pk, m2.pk], attrs={}))
    135 <input type="text" name="test" value="1,2" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>
    136 >>> print conditional_escape(w.render('test', [m1.pk]))
    137 <input type="text" name="test" value="1" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>
    138 >>> w._has_changed(None, None)
    139 False
    140 >>> w._has_changed([], None)
    141 False
    142 >>> w._has_changed(None, [u'1'])
    143 True
    144 >>> w._has_changed([1, 2], [u'1', u'2'])
    145 False
    146 >>> w._has_changed([1, 2], [u'1'])
    147 True
    148 >>> w._has_changed([1, 2], [u'1', u'3'])
    149 True
    150 
    151 # Check that ForeignKeyRawIdWidget works with fields which aren't related to
    152 # the model's primary key.
    153 >>> apple = Inventory.objects.create(barcode=86, name='Apple')
    154 >>> pear = Inventory.objects.create(barcode=22, name='Pear')
    155 >>> core = Inventory.objects.create(barcode=87, name='Core', parent=apple)
    156 >>> rel = Inventory._meta.get_field('parent').rel
    157 >>> w = ForeignKeyRawIdWidget(rel)
    158 >>> print w.render('test', core.parent_id, attrs={})
    159 <input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>
    160 
    161 # see #9258
    162 >>> hidden = Inventory.objects.create(barcode=93, name='Hidden', hidden=True)
    163 >>> child_of_hidden = Inventory.objects.create(barcode=94, name='Child of hidden', parent=hidden)
    164 >>> print w.render('test', child_of_hidden.parent_id, attrs={})
    165 <input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Hidden</strong>
    166 """ % {
    167     'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
    168     'STORAGE_URL': default_storage.url(''),
    169 }}
  • tests/regressiontests/admin_widgets/tests.py

    diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py
    index c445644..5340aa2 100644
    a b  
    11# encoding: utf-8
    22
     3from datetime import datetime
     4from unittest import TestCase
     5
    36from django import forms
     7from django.conf import settings
    48from django.contrib import admin
    59from django.contrib.admin import widgets
    6 from unittest import TestCase
    7 from django.test import TestCase as DjangoTestCase
     10from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime
     11from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget, ManyToManyRawIdWidget
     12from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
     13from django.core.files.storage import default_storage
     14from django.core.files.uploadedfile import SimpleUploadedFile
    815from django.db.models import DateField
     16from django.utils.html import conditional_escape
     17from django.utils.translation import activate, deactivate
     18from django.test import TestCase as DjangoTestCase
     19
    920import models
    1021
     22
    1123class AdminFormfieldForDBFieldTests(TestCase):
    1224    """
    1325    Tests for correct behavior of ModelAdmin.formfield_for_dbfield
    class AdminFormfieldForDBFieldTests(TestCase):  
    104116    def testInheritance(self):
    105117        self.assertFormfield(models.Album, 'backside_art', widgets.AdminFileWidget)
    106118
     119
    107120class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase):
    108121    fixtures = ["admin-widgets-users.xml"]
    109122
    class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase):  
    116129        self.assert_("BMW M3" not in response.content)
    117130        self.assert_("Volkswagon Passat" in response.content)
    118131
     132
    119133class AdminForeignKeyWidgetChangeList(DjangoTestCase):
    120134    fixtures = ["admin-widgets-users.xml"]
    121135    admin_root = '/widget_admin'
    class AdminForeignKeyWidgetChangeList(DjangoTestCase):  
    130144        response = self.client.get('%s/admin_widgets/car/' % self.admin_root)
    131145        self.failUnless('%s/auth/user/add/' % self.admin_root in response.content)
    132146
     147
    133148class AdminForeignKeyRawIdWidget(DjangoTestCase):
    134149    fixtures = ["admin-widgets-users.xml"]
    135150    admin_root = '/widget_admin'
    class AdminForeignKeyRawIdWidget(DjangoTestCase):  
    163178
    164179            self.assertContains(response,
    165180                'Select a valid choice. That choice is not one of the available choices.')
     181
     182
     183class FilteredSelectMultipleWidgetTest(DjangoTestCase):
     184
     185    def test_render(self):
     186        w = FilteredSelectMultiple('test', False)
     187        self.assertEqual(conditional_escape(w.render('test', 'test')),
     188            '<select multiple="multiple" name="test" class="selectfilter">\n</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>\n' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX })
     189
     190    def test_stacked_render(self):
     191        w = FilteredSelectMultiple('test', True)
     192        self.assertEqual(conditional_escape(w.render('test', 'test')),
     193            '<select multiple="multiple" name="test" class="selectfilterstacked">\n</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_MEDIA_PREFIX)s"); });</script>\n' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX })
     194
     195
     196class AdminSplitDateTimeWidgetTest(DjangoTestCase):
     197
     198    def test_render(self):
     199        w = AdminSplitDateTime()
     200        self.assertEqual(
     201            conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
     202            '<p class="datetime">Date: <input value="2007-12-01" type="text" class="vDateField" name="test_0" size="10" /><br />Time: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>',
     203        )
     204
     205    def test_localization(self):
     206        w = AdminSplitDateTime()
     207
     208        activate('de-at')
     209        settings.USE_L10N = True
     210        w.is_localized = True
     211        self.assertEqual(
     212            conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
     213            '<p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>',
     214        )
     215        deactivate()
     216        settings.USE_L10N = False
     217
     218
     219class AdminFileWidgetTest(DjangoTestCase):
     220
     221    def test_render(self):
     222        band = models.Band.objects.create(name='Linkin Park')
     223        album = band.album_set.create(name='Hybrid Theory',
     224            cover_art=r'albums\hybrid_theory.jpg')
     225
     226        w = AdminFileWidget()
     227        self.assertEqual(
     228            conditional_escape(w.render('test', album.cover_art)),
     229            '<p class="file-upload">Currently: <a target="_blank" 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>' % { 'STORAGE_URL': default_storage.url('') },
     230        )
     231
     232        self.assertEqual(
     233            conditional_escape(w.render('test', SimpleUploadedFile('test', 'content'))),
     234            '<input type="file" name="test" />',
     235        )
     236
     237
     238class ForeignKeyRawIdWidgetTest(DjangoTestCase):
     239
     240    def test_render(self):
     241        band = models.Band.objects.create(name='Linkin Park')
     242        album = band.album_set.create(name='Hybrid Theory',
     243            cover_art=r'albums\hybrid_theory.jpg')
     244        rel = models.Album._meta.get_field('band').rel
     245
     246        w = ForeignKeyRawIdWidget(rel)
     247        self.assertEqual(
     248            conditional_escape(w.render('test', band.pk, attrs={})),
     249            '<input type="text" name="test" value="%(bandpk)s" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Linkin Park</strong>' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX, "bandpk": band.pk },
     250        )
     251
     252    def test_relations_to_non_primary_key(self):
     253        # Check that ForeignKeyRawIdWidget works with fields which aren't
     254        # related to the model's primary key.
     255        apple = models.Inventory.objects.create(barcode=86, name='Apple')
     256        pear = models.Inventory.objects.create(barcode=22, name='Pear')
     257        core = models.Inventory.objects.create(
     258            barcode=87, name='Core', parent=apple)
     259        rel = models.Inventory._meta.get_field('parent').rel
     260        w = ForeignKeyRawIdWidget(rel)
     261        self.assertEqual(w.render('test', core.parent_id, attrs={}),
     262            '<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX },
     263        )
     264
     265
     266    def test_proper_manager_for_label_lookup(self):
     267        # see #9258
     268        rel = models.Inventory._meta.get_field('parent').rel
     269        w = ForeignKeyRawIdWidget(rel)
     270
     271        hidden = models.Inventory.objects.create(
     272            barcode=93, name='Hidden', hidden=True)
     273        child_of_hidden = models.Inventory.objects.create(
     274            barcode=94, name='Child of hidden', parent=hidden)
     275        self.assertEqual(
     276            w.render('test', child_of_hidden.parent_id, attrs={}),
     277            '<input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Hidden</strong>' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX },
     278        )
     279
     280       
     281class ManyToManyRawIdWidgetTest(DjangoTestCase):
     282
     283    def test_render(self):
     284        band = models.Band.objects.create(name='Linkin Park')
     285        album = band.album_set.create(name='Hybrid Theory',
     286            cover_art=r'albums\hybrid_theory.jpg')
     287
     288        m1 = models.Member.objects.create(name='Chester')
     289        m2 = models.Member.objects.create(name='Mike')
     290        band.members.add(m1, m2)
     291        rel = models.Band._meta.get_field('members').rel
     292
     293        w = ManyToManyRawIdWidget(rel)
     294        self.assertEqual(
     295            conditional_escape(w.render('test', [m1.pk, m2.pk], attrs={})),
     296            '<input type="text" name="test" value="%(m1pk)s,%(m2pk)s" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX, "m1pk": m1.pk, "m2pk": m2.pk },
     297        )
     298
     299        self.assertEqual(conditional_escape(w.render('test', [m1.pk])),
     300            '<input type="text" name="test" value="%(m1pk)s" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX, "m1pk": m1.pk },
     301        )
     302
     303        self.assertEqual(w._has_changed(None, None), False)
     304        self.assertEqual(w._has_changed([], None), False)
     305        self.assertEqual(w._has_changed(None, [u'1']), True)
     306        self.assertEqual(w._has_changed([1, 2], [u'1', u'2']), False)
     307        self.assertEqual(w._has_changed([1, 2], [u'1']), True)
     308        self.assertEqual(w._has_changed([1, 2], [u'1', u'3']), True)
Back to Top