Commits

Mark Lavin committed 158ea93

Handle some moved functions in Python 3 and Django 1.5 with compatibility module.

Comments (0)

Files changed (7)

selectable/base.py

 from django.core.serializers.json import DjangoJSONEncoder
 from django.http import HttpResponse
 from django.db.models import Q
-from django.utils.encoding import smart_unicode
 from django.utils.html import conditional_escape
 from django.utils.translation import ugettext as _
 
+from selectable.compat import smart_text
 from selectable.forms import BaseLookupForm
 
 
         return []
 
     def get_item_label(self, item):
-        return smart_unicode(item)
+        return smart_text(item)
 
     def get_item_id(self, item):
-        return smart_unicode(item)
+        return smart_text(item)
 
     def get_item_value(self, item):
-        return smart_unicode(item)
+        return smart_text(item)
 
     def get_item(self, value):
         return value
     def paginate_results(self, results, options):
         "Return a django.core.paginator.Page of results."
         limit = options.get('limit', settings.SELECTABLE_MAX_LIMIT)
-        paginator = Paginator(results, limit)        
+        paginator = Paginator(results, limit)
         page = options.get('page', 1)
         try:
             results = paginator.page(page)

selectable/compat.py

+"Compatibility utilites for Python/Django versions."
+import sys
+
+try:
+    from urllib import parse as urlparse
+except ImportError:
+    from urlparse import urlparse
+
+try:
+    from django.utils.encoding import smart_text, force_text
+except ImportError:
+    from django.utils.encoding import smart_unicode as smart_text
+    from django.utils.encoding import force_unicode as force_text
+
+PY3 = sys.version_info[0] == 3
+
+if PY3:
+    string_types = str,
+else:
+    string_types = basestring,

selectable/forms/base.py

 from django.conf import settings
 
 
+from selectable.compat import string_types
+
+
 __all__ = (
     'BaseLookupForm',
     'import_lookup_class',
     Import lookup_class as a dotted base and ensure it extends LookupBase
     """
     from selectable.base import LookupBase
-    if isinstance(lookup_class, basestring):
+    if isinstance(lookup_class, string_types):
         mod_str, cls_str = lookup_class.rsplit('.', 1)
         mod = __import__(mod_str, fromlist=[cls_str])
         lookup_class = getattr(mod, cls_str)

selectable/forms/widgets.py

 from django.conf import settings
 from django.forms.util import flatatt
 from django.utils.http import urlencode
-from django.utils.encoding import force_unicode
 from django.utils.safestring import mark_safe
 
+from selectable.compat import force_text
 from selectable.forms.base import import_lookup_class
 
 __all__ = (
             if model and isinstance(v, model):
                 item = v
                 v = lookup.get_item_id(item)
-            input_attrs = dict(value=force_unicode(v), **final_attrs)
+            input_attrs = dict(value=force_text(v), **final_attrs)
             if id_:
                 # An ID attribute was given. Add a numeric index as a suffix
                 # so that the inputs don't all have the same ID attribute.

selectable/registry.py

 from __future__ import unicode_literals
 
-import warnings
-
-from django.utils.encoding import force_unicode
-
 from selectable.base import LookupBase, ModelLookup
+from selectable.compat import force_text
 from selectable.exceptions import (LookupAlreadyRegistered, LookupNotRegistered,
                                     LookupInvalid)
 
 
     def register(self, lookup):
         self.validate(lookup)
-        name = force_unicode(lookup.name())
+        name = force_text(lookup.name())
         if name in self._registry:
             raise LookupAlreadyRegistered('The name %s is already registered' % name)
         self._registry[name] = lookup
 
     def unregister(self, lookup):
         self.validate(lookup)
-        name = force_unicode(lookup.name())
+        name = force_text(lookup.name())
         if name not in self._registry:
            raise LookupNotRegistered('The name %s is not registered' % name)
         del self._registry[name]

selectable/tests/base.py

     urls = 'selectable.tests.urls'
 
     def get_random_string(self, length=10):
-        return ''.join(random.choice(string.ascii_letters) for x in xrange(length))
+        return ''.join(random.choice(string.ascii_letters) for x in range(length))
 
     def create_thing(self, data=None):
         data = data or {}

selectable/tests/widgets.py

 import json
-from urlparse import urlparse
 
 from django import forms
 from django.utils.http import urlencode
 
+from selectable.compat import urlparse
 from selectable.forms import widgets
 from selectable.tests import Thing, ThingLookup
 from selectable.tests.base import BaseSelectableTestCase, parsed_inputs
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.