From 86e4e17fc685edd44c3097432d2511c1e26e6b56 Mon Sep 17 00:00:00 2001 From: Federico Ariza Date: Wed, 6 Aug 2014 15:05:15 -0400 Subject: [PATCH 1/4] port a python3.4 --- sliver/mixins.py | 10 +++++----- sliver/views.py | 15 ++++++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/sliver/mixins.py b/sliver/mixins.py index 0039e46..4cd2019 100644 --- a/sliver/mixins.py +++ b/sliver/mixins.py @@ -1,5 +1,5 @@ +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 @@ -127,7 +127,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 +136,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..a7b44bd 100644 --- a/sliver/views.py +++ b/sliver/views.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import +import six from django.views.generic import View from django.views.generic.detail import SingleObjectMixin from django.views.generic.list import MultipleObjectMixin @@ -7,7 +9,6 @@ import datetime -from types import NoneType from decimal import Decimal from django.http import HttpResponse @@ -15,7 +16,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 +36,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 +52,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 +101,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 @@ -217,7 +218,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() From 221ee45728651acfc2cae08aee6a7e369170941b Mon Sep 17 00:00:00 2001 From: Federico Ariza Date: Thu, 7 Aug 2014 09:28:27 -0400 Subject: [PATCH 2/4] using six alerady included in django --- sliver/mixins.py | 2 +- sliver/views.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/sliver/mixins.py b/sliver/mixins.py index 4cd2019..3b305c5 100644 --- a/sliver/mixins.py +++ b/sliver/mixins.py @@ -1,4 +1,4 @@ -import six +from django.utils import six import json, csv from django.core.exceptions import ImproperlyConfigured from django.core.urlresolvers import reverse diff --git a/sliver/views.py b/sliver/views.py index a7b44bd..5814b50 100644 --- a/sliver/views.py +++ b/sliver/views.py @@ -1,5 +1,6 @@ from __future__ import absolute_import -import six + +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 From c10d2879bed58a0f1a1866cdfb1c3ee102ab25cb Mon Sep 17 00:00:00 2001 From: Federico Ariza Date: Thu, 7 Aug 2014 11:00:20 -0400 Subject: [PATCH 3/4] json.load for bytes --- sliver/mixins.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sliver/mixins.py b/sliver/mixins.py index 3b305c5..41f25c7 100644 --- a/sliver/mixins.py +++ b/sliver/mixins.py @@ -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): From 798552c48c35e3606878a5e817fe42b28176918b Mon Sep 17 00:00:00 2001 From: Federico Ariza Date: Tue, 17 Mar 2015 09:13:33 -0400 Subject: [PATCH 4/4] mimetype replacement --- sliver/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sliver/views.py b/sliver/views.py index 5814b50..40cdb6d 100644 --- a/sliver/views.py +++ b/sliver/views.py @@ -185,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): """