Skip to content

Commit

Permalink
Remove traces of StrAndUnicode
Browse files Browse the repository at this point in the history
This shift brings us closer to a Python3-first code base.
  • Loading branch information
tiliv committed Jul 23, 2014
1 parent 3095b69 commit d4bd4a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
19 changes: 19 additions & 0 deletions datatableview/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
""" Backports of code left behind by new versions of Django. """

import six

# Django 1.7 removed StrAndUnicode, so it has been purged from this project as well. To bridge the
# gap, we will rely on this utility directly, instead of trying to generate our own replacement
# StrAndUnicode class.
def python_2_unicode_compatible(klass):
"""
A decorator that defines __unicode__ and __str__ methods under Python 2.
Under Python 3 it does nothing.
To support Python 2 and 3 with a single code base, define a __str__ method
returning text and apply this decorator to the class.
"""
if not six.PY3:
klass.__unicode__ = klass.__str__
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
return klass
16 changes: 6 additions & 10 deletions datatableview/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,12 @@

from django.db.models.fields import FieldDoesNotExist
from django.template.loader import render_to_string
from django.forms.util import flatatt

try:
from django.utils.encoding import StrAndUnicode
except ImportError:
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class StrAndUnicode:
def __str__(self):
return self.code

from django.forms.util import flatatt
except ImportError:
from .compat import python_2_unicode_compatible

import six

Expand Down Expand Up @@ -155,7 +150,8 @@ def get_field_definition(field_definition):
return FieldDefinitionTuple(*field)


class DatatableStructure(StrAndUnicode):
@python_2_unicode_compatible
class DatatableStructure(object):
"""
A class designed to be echoed directly to into template HTML to represent a skeleton table
structure that datatables.js can use.
Expand All @@ -177,7 +173,7 @@ def __init__(self, ajax_url, options, model=None):
sort_direction = 'desc' if name[0] == '-' else 'asc'
self.ordering[plain_name] = ColumnOrderingTuple(i, index, sort_direction)

def __unicode__(self):
def __str__(self):
return render_to_string(self.options['structure_template'], {
'url': self.url,
'result_counter_id': self.options['result_counter_id'],
Expand Down

0 comments on commit d4bd4a0

Please sign in to comment.