Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.0.X] Fixed #9859 -- Added another missing force_unicode needed in …

…admin when running on Python 2.3. Many thanks for report & patch with test to nfg.

Also enhanced the new test to hopefully exercise all paths through the get_deleted_objects function, and fixed the errors pointed out by the beefed-up test.

r9656 and r9657 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9658 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ae5c9b33d0e7b2de6202b1bf841276f0a0d3c623 1 parent 75ea7a4
@kmtracey kmtracey authored
View
23 django/contrib/admin/util.py
@@ -88,14 +88,16 @@ def get_deleted_objects(deleted_objects, perms_needed, user, obj, opts, current_
if not has_admin:
# Don't display link to edit, because it either has no
# admin or is edited inline.
- nh(deleted_objects, current_depth, [u'%s: %s' % (force_unicode(capfirst(related.opts.verbose_name)), sub_obj), []])
+ nh(deleted_objects, current_depth,
+ [u'%s: %s' % (capfirst(related.opts.verbose_name), force_unicode(sub_obj)), []])
else:
# Display a link to the admin page.
nh(deleted_objects, current_depth, [mark_safe(u'%s: <a href="../../../../%s/%s/%s/">%s</a>' %
- (escape(force_unicode(capfirst(related.opts.verbose_name))),
- related.opts.app_label,
- related.opts.object_name.lower(),
- sub_obj._get_pk_val(), sub_obj)), []])
+ (escape(capfirst(related.opts.verbose_name)),
+ related.opts.app_label,
+ related.opts.object_name.lower(),
+ sub_obj._get_pk_val(),
+ escape(sub_obj))), []])
get_deleted_objects(deleted_objects, perms_needed, user, sub_obj, related.opts, current_depth+2, admin_site)
else:
has_related_objs = False
@@ -104,11 +106,16 @@ def get_deleted_objects(deleted_objects, perms_needed, user, obj, opts, current_
if not has_admin:
# Don't display link to edit, because it either has no
# admin or is edited inline.
- nh(deleted_objects, current_depth, [u'%s: %s' % (force_unicode(capfirst(related.opts.verbose_name)), sub_obj), []])
+ nh(deleted_objects, current_depth,
+ [u'%s: %s' % (capfirst(related.opts.verbose_name), force_unicode(sub_obj)), []])
else:
# Display a link to the admin page.
- nh(deleted_objects, current_depth, [mark_safe(u'%s: <a href="../../../../%s/%s/%s/">%s</a>' % \
- (escape(force_unicode(capfirst(related.opts.verbose_name))), related.opts.app_label, related.opts.object_name.lower(), sub_obj._get_pk_val(), escape(sub_obj))), []])
+ nh(deleted_objects, current_depth, [mark_safe(u'%s: <a href="../../../../%s/%s/%s/">%s</a>' %
+ (escape(capfirst(related.opts.verbose_name)),
+ related.opts.app_label,
+ related.opts.object_name.lower(),
+ sub_obj._get_pk_val(),
+ escape(sub_obj))), []])
get_deleted_objects(deleted_objects, perms_needed, user, sub_obj, related.opts, current_depth+2, admin_site)
# If there were related objects, and the user doesn't have
# permission to delete them, add the missing perm to perms_needed.
View
63 tests/regressiontests/admin_views/fixtures/admin-views-unicode.xml
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="utf-8"?>
+<django-objects version="1.0">
+ <object pk="100" model="auth.user">
+ <field type="CharField" name="username">super</field>
+ <field type="CharField" name="first_name">Super</field>
+ <field type="CharField" name="last_name">User</field>
+ <field type="CharField" name="email">super@example.com</field>
+ <field type="CharField" name="password">sha1$995a3$6011485ea3834267d719b4c801409b8b1ddd0158</field>
+ <field type="BooleanField" name="is_staff">True</field>
+ <field type="BooleanField" name="is_active">True</field>
+ <field type="BooleanField" name="is_superuser">True</field>
+ <field type="DateTimeField" name="last_login">2007-05-30 13:20:10</field>
+ <field type="DateTimeField" name="date_joined">2007-05-30 13:20:10</field>
+ <field to="auth.group" name="groups" rel="ManyToManyRel"></field>
+ <field to="auth.permission" name="user_permissions" rel="ManyToManyRel"></field>
+ </object>
+ <object pk="1" model="admin_views.book">
+ <field type="CharField" name="name">Lærdommer</field>
+ </object>
+ <object pk="1" model="admin_views.promo">
+ <field type="CharField" name="name">&lt;Promo for Lærdommer&gt;</field>
+ <field to="admin_views.book" name="book" rel="ManyToOneRel">1</field>
+ </object>
+ <object pk="1" model="admin_views.chapter">
+ <field type="CharField" name="title">Norske bostaver æøå skaper problemer</field>
+ <field type="TextField" name="content">&lt;p&gt;Svært frustrerende med UnicodeDecodeErro&lt;/p&gt;</field>
+ <field to="admin_views.book" name="book" rel="ManyToOneRel">1</field>
+ </object>
+ <object pk="2" model="admin_views.chapter">
+ <field type="CharField" name="title">Kjærlighet</field>
+ <field type="TextField" name="content">&lt;p&gt;La kjærligheten til de lidende seire.&lt;/p&gt;</field>
+ <field to="admin_views.book" name="book" rel="ManyToOneRel">1</field>
+ </object>
+ <object pk="3" model="admin_views.chapter">
+ <field type="CharField" name="title">Kjærlighet</field>
+ <field type="TextField" name="content">&lt;p&gt;Noe innhold&lt;/p&gt;</field>
+ <field to="admin_views.book" name="book" rel="ManyToOneRel">1</field>
+ </object>
+ <object pk="1" model="admin_views.chapterxtra1">
+ <field type="CharField" name="xtra">&lt;Xtra(1) Norske bostaver æøå skaper problemer&gt;</field>
+ <field to="admin_views.chapter" name="chap" rel="OneToOneRel">1</field>
+ </object>
+ <object pk="2" model="admin_views.chapterxtra1">
+ <field type="CharField" name="xtra">&lt;Xtra(1) Kjærlighet&gt;</field>
+ <field to="admin_views.chapter" name="chap" rel="OneToOneRel">2</field>
+ </object>
+ <object pk="3" model="admin_views.chapterxtra1">
+ <field type="CharField" name="xtra">&lt;Xtra(1) Kjærlighet&gt;</field>
+ <field to="admin_views.chapter" name="chap" rel="OneToOneRel">3</field>
+ </object>
+ <object pk="1" model="admin_views.chapterxtra2">
+ <field type="CharField" name="xtra">&lt;Xtra(2) Norske bostaver æøå skaper problemer&gt;</field>
+ <field to="admin_views.chapter" name="chap" rel="OneToOneRel">1</field>
+ </object>
+ <object pk="2" model="admin_views.chapterxtra2">
+ <field type="CharField" name="xtra">&lt;Xtra(2) Kjærlighet&gt;</field>
+ <field to="admin_views.chapter" name="chap" rel="OneToOneRel">2</field>
+ </object>
+ <object pk="3" model="admin_views.chapterxtra2">
+ <field type="CharField" name="xtra">&lt;Xtra(2) Kjærlighet&gt;</field>
+ <field to="admin_views.chapter" name="chap" rel="OneToOneRel">3</field>
+ </object>
+</django-objects>
View
58 tests/regressiontests/admin_views/models.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
from django.db import models
from django.contrib import admin
@@ -24,6 +25,47 @@ def model_year(self):
return self.date.year
model_year.admin_order_field = 'date'
+class Book(models.Model):
+ """
+ A simple book that has chapters.
+ """
+ name = models.CharField(max_length=100, verbose_name=u'¿Name?')
+
+ def __unicode__(self):
+ return self.name
+
+class Promo(models.Model):
+ name = models.CharField(max_length=100, verbose_name=u'¿Name?')
+ book = models.ForeignKey(Book)
+
+ def __unicode__(self):
+ return self.name
+
+class Chapter(models.Model):
+ title = models.CharField(max_length=100, verbose_name=u'¿Title?')
+ content = models.TextField()
+ book = models.ForeignKey(Book)
+
+ def __unicode__(self):
+ return self.title
+
+ class Meta:
+ verbose_name = u'¿Chapter?'
+
+class ChapterXtra1(models.Model):
+ chap = models.OneToOneField(Chapter, verbose_name=u'¿Chap?')
+ xtra = models.CharField(max_length=100, verbose_name=u'¿Xtra?')
+
+ def __unicode__(self):
+ return u'¿Xtra1: %s' % self.xtra
+
+class ChapterXtra2(models.Model):
+ chap = models.OneToOneField(Chapter, verbose_name=u'¿Chap?')
+ xtra = models.CharField(max_length=100, verbose_name=u'¿Xtra?')
+
+ def __unicode__(self):
+ return u'¿Xtra2: %s' % self.xtra
+
def callable_year(dt_value):
return dt_value.year
callable_year.admin_order_field = 'date'
@@ -31,6 +73,9 @@ def callable_year(dt_value):
class ArticleInline(admin.TabularInline):
model = Article
+class ChapterInline(admin.TabularInline):
+ model = Chapter
+
class ArticleAdmin(admin.ModelAdmin):
list_display = ('content', 'date', callable_year, 'model_year', 'modeladmin_year')
list_filter = ('date',)
@@ -95,3 +140,16 @@ class ThingAdmin(admin.ModelAdmin):
admin.site.register(ModelWithStringPrimaryKey)
admin.site.register(Color)
admin.site.register(Thing, ThingAdmin)
+
+# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
+# That way we cover all four cases:
+# related ForeignKey object registered in admin
+# related ForeignKey object not registered in admin
+# related OneToOne object registered in admin
+# related OneToOne object not registered in admin
+# when deleting Book so as exercise all four troublesome (w.r.t escaping
+# and calling force_unicode to avoid problems on Python 2.3) paths through
+# contrib.admin.util's get_deleted_objects function.
+admin.site.register(Book, inlines=[ChapterInline])
+admin.site.register(Promo)
+admin.site.register(ChapterXtra1)
View
51 tests/regressiontests/admin_views/tests.py
@@ -684,3 +684,54 @@ def test_staff_member_required_decorator_works_as_per_admin_login(self):
self.client.post('/test_admin/admin/secure-view/', self.super_login)
# make sure the view removes test cookie
self.failUnlessEqual(self.client.session.test_cookie_worked(), False)
+
+class AdminViewUnicodeTest(TestCase):
+ fixtures = ['admin-views-unicode.xml']
+
+ def setUp(self):
+ self.client.login(username='super', password='secret')
+
+ def tearDown(self):
+ self.client.logout()
+
+ def testUnicodeEdit(self):
+ """
+ A test to ensure that POST on edit_view handles non-ascii characters.
+ """
+ post_data = {
+ "name": u"Test lærdommer",
+ # inline data
+ "chapter_set-TOTAL_FORMS": u"6",
+ "chapter_set-INITIAL_FORMS": u"3",
+ "chapter_set-0-id": u"1",
+ "chapter_set-0-title": u"Norske bostaver æøå skaper problemer",
+ "chapter_set-0-content": u"&lt;p&gt;Svært frustrerende med UnicodeDecodeError&lt;/p&gt;",
+ "chapter_set-1-id": u"2",
+ "chapter_set-1-title": u"Kjærlighet.",
+ "chapter_set-1-content": u"&lt;p&gt;La kjærligheten til de lidende seire.&lt;/p&gt;",
+ "chapter_set-2-id": u"3",
+ "chapter_set-2-title": u"Need a title.",
+ "chapter_set-2-content": u"&lt;p&gt;Newest content&lt;/p&gt;",
+ "chapter_set-3-id": u"",
+ "chapter_set-3-title": u"",
+ "chapter_set-3-content": u"",
+ "chapter_set-4-id": u"",
+ "chapter_set-4-title": u"",
+ "chapter_set-4-content": u"",
+ "chapter_set-5-id": u"",
+ "chapter_set-5-title": u"",
+ "chapter_set-5-content": u"",
+ }
+
+ response = self.client.post('/test_admin/admin/admin_views/book/1/', post_data)
+ self.failUnlessEqual(response.status_code, 302) # redirect somewhere
+
+ def testUnicodeDelete(self):
+ """
+ Ensure that the delete_view handles non-ascii characters
+ """
+ delete_dict = {'post': 'yes'}
+ response = self.client.get('/test_admin/admin/admin_views/book/1/delete/')
+ self.failUnlessEqual(response.status_code, 200)
+ response = self.client.post('/test_admin/admin/admin_views/book/1/delete/', delete_dict)
+ self.assertRedirects(response, '/test_admin/admin/admin_views/book/')
Please sign in to comment.
Something went wrong with that request. Please try again.