diff options
| author | Xavier Ordoquy | 2014-02-18 11:42:17 +0100 | 
|---|---|---|
| committer | Xavier Ordoquy | 2014-02-18 11:42:17 +0100 | 
| commit | 5ae94547bc08ade94c3f1df2223c0b8261cae59f (patch) | |
| tree | 74ad22e10a5b64fa237c66c578f0d9ba16a1c6ea /rest_framework | |
| parent | d49846025311d5b1679f656414eb685bede132c0 (diff) | |
| download | django-rest-framework-5ae94547bc08ade94c3f1df2223c0b8261cae59f.tar.bz2 | |
Moved the python_2_unicode_compatible into compat module.
Diffstat (limited to 'rest_framework')
| -rw-r--r-- | rest_framework/compat.py | 20 | ||||
| -rw-r--r-- | rest_framework/tests/test_genericrelations.py | 20 | 
2 files changed, 21 insertions, 19 deletions
| diff --git a/rest_framework/compat.py b/rest_framework/compat.py index b69749fe..36f5653a 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -584,3 +584,23 @@ if six.PY3:  else:      def is_non_str_iterable(obj):          return hasattr(obj, '__iter__') + + +try: +    from django.utils.encoding import python_2_unicode_compatible +except ImportError: +    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 '__str__' not in klass.__dict__: +            raise ValueError("@python_2_unicode_compatible cannot be applied " +                             "to %s because it doesn't define __str__()." % +                             klass.__name__) +        klass.__unicode__ = klass.__str__ +        klass.__str__ = lambda self: self.__unicode__().encode('utf-8') +        return klass diff --git a/rest_framework/tests/test_genericrelations.py b/rest_framework/tests/test_genericrelations.py index 95810de7..fa09c9e6 100644 --- a/rest_framework/tests/test_genericrelations.py +++ b/rest_framework/tests/test_genericrelations.py @@ -4,25 +4,7 @@ from django.contrib.contenttypes.generic import GenericRelation, GenericForeignK  from django.db import models  from django.test import TestCase  from rest_framework import serializers - -try: -    from django.utils.encoding import python_2_unicode_compatible -except ImportError: -    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 '__str__' not in klass.__dict__: -            raise ValueError("@python_2_unicode_compatible cannot be applied " -                             "to %s because it doesn't define __str__()." % -                             klass.__name__) -        klass.__unicode__ = klass.__str__ -        klass.__str__ = lambda self: self.__unicode__().encode('utf-8') -        return klass +from rest_framework.compat import python_2_unicode_compatible  @python_2_unicode_compatible | 
