mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
newforms-admin: Fixed #6731 -- ManyToManyRawIdWidget no longer allows wrong data being sent to the database. Admin widgets tests are now in regressiontests.
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7262 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
bfc5660c47
commit
40d55238b8
@ -1,5 +0,0 @@
|
|||||||
from django.contrib.admin.tests.widgets import WIDGET_TESTS
|
|
||||||
|
|
||||||
__test__ = {
|
|
||||||
'WIDGET_TESTS': WIDGET_TESTS,
|
|
||||||
}
|
|
@ -1,47 +0,0 @@
|
|||||||
from django.conf import settings
|
|
||||||
|
|
||||||
WIDGET_TESTS = """
|
|
||||||
>>> from datetime import datetime
|
|
||||||
>>> from django.utils.html import escape, conditional_escape
|
|
||||||
>>> from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime
|
|
||||||
>>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget
|
|
||||||
>>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
|
|
||||||
>>> from django.contrib.admin.models import LogEntry
|
|
||||||
>>> from django.contrib.auth.models import User
|
|
||||||
|
|
||||||
Calling conditional_escape on the output of widget.render will simulate what
|
|
||||||
happens in the template. This is easier than setting up a template and context
|
|
||||||
for each test.
|
|
||||||
|
|
||||||
Make sure that the Admin widgets render properly, that is, without their extra
|
|
||||||
HTML escaped.
|
|
||||||
|
|
||||||
>>> w = FilteredSelectMultiple('test', False)
|
|
||||||
>>> print conditional_escape(w.render('test', 'test'))
|
|
||||||
<select multiple="multiple" name="test">
|
|
||||||
</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
|
|
||||||
<BLANKLINE>
|
|
||||||
|
|
||||||
>>> w = AdminSplitDateTime()
|
|
||||||
>>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
|
|
||||||
<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>
|
|
||||||
|
|
||||||
>>> w = AdminFileWidget()
|
|
||||||
>>> print conditional_escape(w.render('test', 'test'))
|
|
||||||
Currently: <a target="_blank" href="%(MEDIA_URL)stest">test</a> <br />Change: <input type="file" name="test" />
|
|
||||||
|
|
||||||
To test ForeignKeyRawIdWidget a user object must be created. Its pk is
|
|
||||||
explicitly set to 100 to avoid having to potentially overmatch in the test.
|
|
||||||
|
|
||||||
>>> user = User.objects.create(pk=100, username='jdoe')
|
|
||||||
>>> entry = LogEntry(action_flag=1, user=user)
|
|
||||||
>>> entry.save()
|
|
||||||
>>> rel = LogEntry._meta.get_field('user').rel
|
|
||||||
>>> w = ForeignKeyRawIdWidget(rel)
|
|
||||||
>>> print conditional_escape(w.render('test', entry.user.pk, attrs={}))
|
|
||||||
<input type="text" name="test" value="100" class="vForeignKeyRawIdAdminField" /><a href="../../../auth/user/" 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> <strong>jdoe</strong>
|
|
||||||
|
|
||||||
""" % {
|
|
||||||
'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
|
|
||||||
'MEDIA_URL': settings.MEDIA_URL
|
|
||||||
}
|
|
@ -102,9 +102,12 @@ class ForeignKeyRawIdWidget(forms.TextInput):
|
|||||||
(related_url, url, name))
|
(related_url, url, name))
|
||||||
output.append('<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>' % settings.ADMIN_MEDIA_PREFIX)
|
output.append('<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>' % settings.ADMIN_MEDIA_PREFIX)
|
||||||
if value:
|
if value:
|
||||||
output.append(' <strong>%s</strong>' % \
|
output.append(self.label_for_value(value))
|
||||||
truncate_words(self.rel.to.objects.get(pk=value), 14))
|
|
||||||
return mark_safe(u''.join(output))
|
return mark_safe(u''.join(output))
|
||||||
|
|
||||||
|
def label_for_value(self, value):
|
||||||
|
return ' <strong>%s</strong>' % \
|
||||||
|
truncate_words(self.rel.to.objects.get(pk=value), 14)
|
||||||
|
|
||||||
class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
|
class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
|
||||||
"""
|
"""
|
||||||
@ -121,6 +124,9 @@ class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
|
|||||||
else:
|
else:
|
||||||
value = ''
|
value = ''
|
||||||
return super(ManyToManyRawIdWidget, self).render(name, value, attrs)
|
return super(ManyToManyRawIdWidget, self).render(name, value, attrs)
|
||||||
|
|
||||||
|
def label_for_value(self, value):
|
||||||
|
return ''
|
||||||
|
|
||||||
def value_from_datadict(self, data, files, name):
|
def value_from_datadict(self, data, files, name):
|
||||||
value = data.get(name, None)
|
value = data.get(name, None)
|
||||||
|
0
tests/regressiontests/admin_widgets/__init__.py
Normal file
0
tests/regressiontests/admin_widgets/__init__.py
Normal file
73
tests/regressiontests/admin_widgets/models.py
Normal file
73
tests/regressiontests/admin_widgets/models.py
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
class Member(models.Model):
|
||||||
|
name = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
class Band(models.Model):
|
||||||
|
name = models.CharField(max_length=100)
|
||||||
|
members = models.ManyToManyField(Member)
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
class Album(models.Model):
|
||||||
|
band = models.ForeignKey(Band)
|
||||||
|
name = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
__test__ = {'WIDGETS_TESTS': """
|
||||||
|
>>> from datetime import datetime
|
||||||
|
>>> from django.utils.html import escape, conditional_escape
|
||||||
|
>>> from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime
|
||||||
|
>>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget, ManyToManyRawIdWidget
|
||||||
|
>>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
|
||||||
|
|
||||||
|
Calling conditional_escape on the output of widget.render will simulate what
|
||||||
|
happens in the template. This is easier than setting up a template and context
|
||||||
|
for each test.
|
||||||
|
|
||||||
|
Make sure that the Admin widgets render properly, that is, without their extra
|
||||||
|
HTML escaped.
|
||||||
|
|
||||||
|
>>> w = FilteredSelectMultiple('test', False)
|
||||||
|
>>> print conditional_escape(w.render('test', 'test'))
|
||||||
|
<select multiple="multiple" name="test">
|
||||||
|
</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
|
||||||
|
<BLANKLINE>
|
||||||
|
|
||||||
|
>>> w = AdminSplitDateTime()
|
||||||
|
>>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
|
||||||
|
<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>
|
||||||
|
|
||||||
|
>>> w = AdminFileWidget()
|
||||||
|
>>> print conditional_escape(w.render('test', 'test'))
|
||||||
|
Currently: <a target="_blank" href="%(MEDIA_URL)stest">test</a> <br />Change: <input type="file" name="test" />
|
||||||
|
|
||||||
|
>>> band = Band.objects.create(pk=1, name='Linkin Park')
|
||||||
|
>>> album = band.album_set.create(name='Hybrid Theory')
|
||||||
|
|
||||||
|
>>> rel = Album._meta.get_field('band').rel
|
||||||
|
>>> w = ForeignKeyRawIdWidget(rel)
|
||||||
|
>>> print conditional_escape(w.render('test', band.pk, attrs={}))
|
||||||
|
<input type="text" name="test" value="1" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/" 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> <strong>Linkin Park</strong>
|
||||||
|
|
||||||
|
>>> m1 = Member.objects.create(pk=1, name='Chester')
|
||||||
|
>>> m2 = Member.objects.create(pk=2, name='Mike')
|
||||||
|
>>> band.members.add(m1, m2)
|
||||||
|
|
||||||
|
>>> rel = Band._meta.get_field('members').rel
|
||||||
|
>>> w = ManyToManyRawIdWidget(rel)
|
||||||
|
>>> print conditional_escape(w.render('test', [m1.pk, m2.pk], attrs={}))
|
||||||
|
<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>
|
||||||
|
|
||||||
|
""" % {
|
||||||
|
'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
|
||||||
|
'MEDIA_URL': settings.MEDIA_URL,
|
||||||
|
}}
|
Loading…
x
Reference in New Issue
Block a user