diff --git a/django_elasticsearch/contrib/restframework/restframework2.py b/django_elasticsearch/contrib/restframework/restframework2.py index e17b7a2..a6058e9 100644 --- a/django_elasticsearch/contrib/restframework/restframework2.py +++ b/django_elasticsearch/contrib/restframework/restframework2.py @@ -10,6 +10,7 @@ from rest_framework.filters import DjangoFilterBackend from django_elasticsearch.models import EsIndexable +from django.utils import six from elasticsearch import NotFoundError @@ -75,7 +76,7 @@ def filter_queryset(self, request, queryset, view): filterable = getattr(view, 'filter_fields', []) filters = dict([(k, v) - for k, v in request.GET.iteritems() + for k, v in six.iteritems(request.GET) if k in filterable]) q = queryset.query(query).filter(**filters) @@ -147,7 +148,7 @@ def list(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs): try: r = super(IndexableModelMixin, self).dispatch(request, *args, **kwargs) - except (ConnectionError, TransportError), e: + except (ConnectionError, TransportError) as e: # reset object list self.queryset = None self.es_failed = True diff --git a/django_elasticsearch/contrib/restframework/restframework3.py b/django_elasticsearch/contrib/restframework/restframework3.py index 090cc1e..4c311ad 100644 --- a/django_elasticsearch/contrib/restframework/restframework3.py +++ b/django_elasticsearch/contrib/restframework/restframework3.py @@ -8,6 +8,7 @@ from rest_framework.filters import DjangoFilterBackend from django_elasticsearch.models import EsIndexable +from django.utils import six try: @@ -38,7 +39,7 @@ def filter_queryset(self, request, queryset, view): filterable = getattr(view, 'filter_fields', []) filters = dict([(k, v) - for k, v in request.GET.iteritems() + for k, v in six.iteritems(request.GET) if k in filterable]) q = queryset.query(query).filter(**filters) @@ -87,7 +88,7 @@ def filter_queryset(self, queryset): def dispatch(self, request, *args, **kwargs): try: r = super(IndexableModelMixin, self).dispatch(request, *args, **kwargs) - except (ConnectionError, TransportError), e: + except (ConnectionError, TransportError) as e: # reset object list self.queryset = None self.es_failed = True diff --git a/django_elasticsearch/managers.py b/django_elasticsearch/managers.py index edf2718..9cbfce5 100644 --- a/django_elasticsearch/managers.py +++ b/django_elasticsearch/managers.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import json +from django.utils import six try: import importlib except ImportError: # python < 2.7 @@ -87,7 +88,7 @@ def check_cluster(self): def get_serializer(self, **kwargs): serializer = self.model.Elasticsearch.serializer_class - if isinstance(serializer, basestring): + if isinstance(serializer, basestring if six.PY2 else str): module, kls = self.model.Elasticsearch.serializer_class.rsplit(".", 1) mod = importlib.import_module(module) return getattr(mod, kls)(self.model, **kwargs) diff --git a/django_elasticsearch/serializers.py b/django_elasticsearch/serializers.py index 474e64e..049d32a 100644 --- a/django_elasticsearch/serializers.py +++ b/django_elasticsearch/serializers.py @@ -3,7 +3,7 @@ from django.db.models import FieldDoesNotExist from django.db.models.fields.related import ManyToManyField - +from django.utils import six class EsSerializer(object): def serialize(self, instance): @@ -81,7 +81,7 @@ def deserialize(self, source): Returns a model instance """ attrs = {} - for k, v in source.iteritems(): + for k, v in six.iteritems(source): try: attrs[k] = self.deserialize_field(source, k) except (AttributeError, FieldDoesNotExist): @@ -145,7 +145,7 @@ def nested_serialize(self, rel): return obj # Fallback on a dict with id + __unicode__ value of the related model instance. - return dict(id=rel.pk, value=unicode(rel)) + return dict(id=rel.pk, value=six.text_type(rel)) def format(self, instance): # from a model instance to a dict diff --git a/django_elasticsearch/utils.py b/django_elasticsearch/utils.py index 1b05ad9..221c31b 100644 --- a/django_elasticsearch/utils.py +++ b/django_elasticsearch/utils.py @@ -2,7 +2,7 @@ def nested_update(d, u): - for k, v in u.iteritems(): + for k, v in u.items(): if isinstance(v, collections.Mapping): r = nested_update(d.get(k, {}), v) d[k] = r @@ -20,4 +20,4 @@ def dict_depth(d, depth=0): if not isinstance(d, dict) or not d: return depth return max(dict_depth(v, depth + 1) - for k, v in d.iteritems()) + for k, v in d.items())