From: Bradley Ayers Date: Sat, 14 May 2011 11:05:22 +0000 (+1000) Subject: * Fixed bug with LinkColumn when using unicode. Thanks kwevej X-Git-Tag: v0.4.3^2 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=17d506916a8b2f5aabfb3968ae0d142eca831ae4;p=django-tables2.git * Fixed bug with LinkColumn when using unicode. Thanks kwevej * Bumped version to 0.4.3 --- diff --git a/django_tables/columns.py b/django_tables/columns.py index bd7ce92..b546ba8 100644 --- a/django_tables/columns.py +++ b/django_tables/columns.py @@ -52,7 +52,7 @@ class Column(object): visible=True, sortable=None): if not (accessor is None or isinstance(accessor, basestring) or callable(accessor)): - raise TypeError('accessor must be a string or callable, not %s' % + raise TypeError(u'accessor must be a string or callable, not %s' % accessor.__class__.__name__) if callable(accessor) and default is not None: raise TypeError('accessor must be string when default is used, not' @@ -151,7 +151,7 @@ class CheckBoxColumn(Column): 'type': 'checkbox', }) attrs.update(self.header_attrs) - return mark_safe('' % attrs.as_html()) + return mark_safe(u'' % attrs.as_html()) def render(self, value, bound_column): attrs = AttributeDict({ @@ -160,7 +160,7 @@ class CheckBoxColumn(Column): 'value': value }) attrs.update(self.attrs) - return mark_safe('' % attrs.as_html()) + return mark_safe(u'' % attrs.as_html()) class LinkColumn(Column): @@ -221,7 +221,9 @@ class LinkColumn(Column): self.attrs = attrs or {} def render(self, value, record, bound_column): - params = {} # args for reverse() + # The following params + if statements create the arguments required to + # pass to Django's reverse() function. + params = {} if self.viewname: params['viewname'] = (self.viewname.resolve(record) if isinstance(self.viewname, A) @@ -244,7 +246,7 @@ class LinkColumn(Column): if isinstance(value, A): params['current_app'][key] = value.resolve(record) url = reverse(**params) - html = '{value}'.format( + html = u'{value}'.format( url=reverse(**params), attrs=AttributeDict(self.attrs).as_html(), value=value @@ -537,5 +539,5 @@ class BoundColumns(object): elif isinstance(index, basestring): return self._columns[index] else: - raise TypeError('row indices must be integers or str, not %s' % + raise TypeError(u'row indices must be integers or str, not %s' % index.__class__.__name__) diff --git a/django_tables/templatetags/django_tables.py b/django_tables/templatetags/django_tables.py index 533cc26..b455eae 100644 --- a/django_tables/templatetags/django_tables.py +++ b/django_tables/templatetags/django_tables.py @@ -109,5 +109,6 @@ def render_table(parser, token): _, table_var_name = token.contents.split() except ValueError: raise (template.TemplateSyntaxError, - '%r tag requires a single argument' % token.contents.split()[0]) + u'%r tag requires a single argument' + % token.contents.split()[0]) return RenderTableNode(table_var_name) diff --git a/django_tables/utils.py b/django_tables/utils.py index 405f447..93107dd 100644 --- a/django_tables/utils.py +++ b/django_tables/utils.py @@ -270,5 +270,5 @@ class AttributeDict(dict): :rtype: :class:`~django.utils.safestring.SafeUnicode` object """ - return mark_safe(' '.join(['%s="%s"' % (k, escape(v)) + return mark_safe(' '.join([u'%s="%s"' % (k, escape(v)) for k, v in self.iteritems()])) diff --git a/docs/conf.py b/docs/conf.py index a953d49..e83fc4f 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -50,9 +50,9 @@ project = u'django-tables' # built documents. # # The short X.Y version. -version = '0.4.2' +version = '0.4.3' # The full version, including alpha/beta/rc tags. -release = '0.4.2' +release = '0.4.3' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/setup.py b/setup.py index 5e7d230..d6f4170 100755 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup, find_packages setup( name='django-tables', - version='0.4.2', + version='0.4.3', description='Table framework for Django', author='Bradley Ayers', diff --git a/tests/__init__.py b/tests/__init__.py index 0774bec..4f2c71b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -18,7 +18,8 @@ settings.configure( INSTALLED_APPS = [ 'tests.testapp', 'django_tables', - ] + ], + ROOT_URLCONF = 'tests.testapp.urls', ) diff --git a/tests/columns.py b/tests/columns.py index d429ec9..ba87e6e 100644 --- a/tests/columns.py +++ b/tests/columns.py @@ -1,7 +1,10 @@ +# -*- coding: utf-8 -*- """Test the core table functionality.""" from attest import Tests, Assert +from django.test.client import RequestFactory +from django.template import Context, Template import django_tables as tables -from django_tables import utils +from django_tables import utils, A columns = Tests() @@ -15,7 +18,7 @@ def sortable(): class SimpleTable(tables.Table): name = tables.Column() - + class Meta: sortable = False Assert(SimpleTable([]).columns['name'].sortable) is False @@ -28,3 +31,27 @@ def sortable(): Assert(SimpleTable([]).columns['name'].sortable) is True +@columns.test +def link_column(): + """Test LinkColumn""" + # test unicode values + headings + class UnicodeTable(tables.Table): + first_name = tables.LinkColumn('person', args=[A('pk')]) + last_name = tables.LinkColumn('person', args=[A('pk')], verbose_name=u'äÚ¨´ˆÁ˜¨ˆ˜˘Ú…Ò˚ˆπ∆ˆ´') + + dataset = [ + {'pk': 1, 'first_name': u'Brädley', 'last_name': u'∆yers'}, + {'pk': 2, 'first_name': u'Chr…s', 'last_name': u'DÒble'}, + ] + + table = UnicodeTable(dataset) + request = RequestFactory().get('/some-url/') + template = Template('{% load django_tables %}{% render_table table %}') + html = template.render(Context({'request': request, 'table': table})) + + Assert(u'Brädley' in html) + Assert(u'∆yers' in html) + Assert(u'Chr…s' in html) + Assert(u'DÒble' in html) + + diff --git a/tests/testapp/urls.py b/tests/testapp/urls.py new file mode 100644 index 0000000..99fec2c --- /dev/null +++ b/tests/testapp/urls.py @@ -0,0 +1,8 @@ +from django.conf.urls.defaults import patterns, include, url +from . import views + + +urlpatterns = patterns('', + url(r'^people/(\d+)/$', views.person, name='person'), + url(r'^occupations/(\d+)/$', views.occupation, name='occupation'), +) diff --git a/tests/testapp/views.py b/tests/testapp/views.py new file mode 100644 index 0000000..036b05e --- /dev/null +++ b/tests/testapp/views.py @@ -0,0 +1,17 @@ +from django.shortcuts import get_object_or_404 +from django.http import HttpResponse +from .models import Person, Occupation + + +def person(request, pk): + """A really simple view to provide an endpoint for the 'person' URL.""" + person = get_object_or_404(Person, pk=pk) + return HttpResponse('Person: %s' % person) + + +def occupation(request, pk): + """ + Another really simple view to provide an endpoint for the 'occupation' URL. + """ + occupation = get_object_or_404(Occupation, pk=pk) + return HttpResponse('Occupation: %s' % occupation)