diff --git a/sliver/mixins.py b/sliver/mixins.py index 0039e46..41f25c7 100644 --- a/sliver/mixins.py +++ b/sliver/mixins.py @@ -1,5 +1,5 @@ +from django.utils import six import json, csv -from StringIO import StringIO from django.core.exceptions import ImproperlyConfigured from django.core.urlresolvers import reverse @@ -24,7 +24,7 @@ def get_filters(self): filters = {} - for key, value in self.request.GET.items(): + for key, value in list(self.request.GET.items()): if key not in self.filters: continue @@ -119,6 +119,8 @@ class JSONMixin(object): mimetype = 'application/json' def parse(self, data): + if isinstance(data, bytes): + return json.loads(data.decode('utf-8')) return json.loads(data) def render(self, context): @@ -127,7 +129,7 @@ def render(self, context): class FlatFileMixin(object): def parse(self, data): - input = StringIO(data) + input = six.StringIO(data) reader = csv.DictReader(input, delimiter=self.get_delimiter()) data = list(reader) @@ -136,8 +138,8 @@ def parse(self, data): def render(self, context): - output = StringIO() - writer = csv.DictWriter(output, context.keys(), extrasaction='ignore', delimiter=self.get_delimiter()) + output = six.StringIO() + writer = csv.DictWriter(output, list(context.keys()), extrasaction='ignore', delimiter=self.get_delimiter()) writer.writeheader() writer.writerows(context) diff --git a/sliver/views.py b/sliver/views.py index 6cbbafd..40cdb6d 100644 --- a/sliver/views.py +++ b/sliver/views.py @@ -1,3 +1,6 @@ +from __future__ import absolute_import + +from django.utils import six from django.views.generic import View from django.views.generic.detail import SingleObjectMixin from django.views.generic.list import MultipleObjectMixin @@ -7,7 +10,6 @@ import datetime -from types import NoneType from decimal import Decimal from django.http import HttpResponse @@ -15,7 +17,7 @@ from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt -import responses +from . import responses class Resource(View): """ @@ -35,7 +37,7 @@ def dispatch(self, request, *args, **kwargs): #if a response exception is raised, grab it and return it to django try: return super(Resource, self).dispatch(request, *args, **kwargs) - except responses.SliverResponse, r: + except responses.SliverResponse as r: return r.response() def get_model_class(self): @@ -51,7 +53,7 @@ def hydrate(self): model_class = self.get_model_class() - for key, val in data.items(): + for key, val in list(data.items()): #make sure it's allowed if (self.fields and key not in self.fields) or key in self.exclude: continue @@ -100,8 +102,8 @@ def dehydrate_value(self, model, field): val = float(val) #if it's something we don't know about, just convert to string - elif not isinstance(val, (int, str, bool, NoneType)): - val = unicode(val) + elif not isinstance(val, (int, str, bool, type(None))): + val = six.text_type(val) return val @@ -183,7 +185,7 @@ def render_to_response(self, context, status=200): """ Out to the tubes... """ - return HttpResponse(self.render(context), mimetype=self.get_mimetype(), status=status) + return HttpResponse(self.render(context), content_type=self.get_mimetype(), status=status) def get_mimetype(self): """ @@ -217,7 +219,7 @@ def put(self, request, *args, **kwargs): self.object = self.get_object() - for key, val in self.hydrate().items(): + for key, val in list(self.hydrate().items()): setattr(self.object, key, val) self.object.save()