Skip to content

Commit

Permalink
Fixed a unicode error that happens with Python 2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
epicserve committed Mar 29, 2017
1 parent d71d057 commit fb0174c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
35 changes: 13 additions & 22 deletions categories/editor/templatetags/admin_tree_list_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,18 @@ def items_for_tree_result(cl, result, form):
value = result.serializable_value(attr)
result_id = repr(force_text(value))[1:]
first = False
if django.VERSION[1] < 4: # versions 1.3 and smaller
yield mark_safe(
'<%s%s>%s<a href="%s"%s>%s</a></%s>' %
(table_tag, row_class, checkbox_value, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag))
elif django.VERSION[1] < 7: # versions 1.4 to 1.7
yield mark_safe(
'<%s%s><a href="%s"%s>%s</a></%s>' %
(table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag))
else: # versions 1.8 and greater
result_id = escapejs(value)
yield mark_safe(
result_id = escapejs(value)
yield mark_safe(
format_html(
smart_text('<{}{}><a href="{}"{}>{}</a></{}>'),
table_tag,
row_class,
url,
format_html(
'<{}{}><a href="{}"{}>{}</a></{}>',
table_tag,
row_class,
url,
format_html(
' onclick="opener.dismissRelatedLookupPopup(window, '
'&#39;{}&#39;); return false;"', result_id
) if cl.is_popup else '', result_repr, table_tag)
)
' onclick="opener.dismissRelatedLookupPopup(window, '
'&#39;{}&#39;); return false;"', result_id
) if cl.is_popup else '', result_repr, table_tag)
)

else:
# By default the fields come from ModelAdmin.list_editable, but if we pull
Expand All @@ -127,9 +118,9 @@ def items_for_tree_result(cl, result, form):
result_repr = mark_safe(force_text(bf.errors) + force_text(bf))
else:
result_repr = conditional_escape(result_repr)
yield mark_safe('<td%s>%s</td>' % (row_class, result_repr))
yield mark_safe(smart_text('<td%s>%s</td>' % (row_class, result_repr)))
if form and not form[cl.model._meta.pk.name].is_hidden:
yield mark_safe('<td>%s</td>' % force_text(form[cl.model._meta.pk.name]))
yield mark_safe(smart_text('<td>%s</td>' % force_text(form[cl.model._meta.pk.name])))


class TreeList(list):
Expand Down
16 changes: 12 additions & 4 deletions categories/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# -*- coding: utf-8 -*-

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import Client, TestCase
from django.utils.encoding import smart_text

from categories.models import Category

Expand All @@ -16,7 +19,7 @@ def test_adding_parent_and_child(self):
url = reverse('admin:categories_category_add')
data = {
'parent': '',
'name': "Parent",
'name': smart_text('Parent Catégory'),
'thumbnail': '',
'filename': '',
'active': 'on',
Expand All @@ -34,16 +37,16 @@ def test_adding_parent_and_child(self):
self.assertEqual(1, Category.objects.count())

# update parent
data.update({'name': 'Parent (Changed)'})
data.update({'name': smart_text('Parent Catégory (Changed)')})
resp = self.client.post(reverse('admin:categories_category_change', args=(1,)), data=data)
self.assertEqual(resp.status_code, 302)
self.assertEqual(1, Category.objects.count())

# add a child
data.update({
'parent': '1',
'name': 'Child',
'slug': 'child',
'name': smart_text('Child Catégory'),
'slug': smart_text('child-category'),
})
resp = self.client.post(url, data=data)
self.assertEqual(resp.status_code, 302)
Expand All @@ -54,3 +57,8 @@ def test_adding_parent_and_child(self):
resp = self.client.post(reverse('admin:categories_category_change', args=(2,)), data=data)
self.assertEqual(resp.status_code, 302)
self.assertEqual(2, Category.objects.count())

# test the admin list view
url = reverse('admin:categories_category_changelist')
resp = self.client.get(url)
self.assertEqual(resp.status_code, 200)

0 comments on commit fb0174c

Please sign in to comment.