Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

port to python3.4 #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions sliver/mixins.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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)
Expand Down
18 changes: 10 additions & 8 deletions sliver/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,15 +10,14 @@

import datetime

from types import NoneType
from decimal import Decimal

from django.http import HttpResponse

from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt

import responses
from . import responses

class Resource(View):
"""
Expand All @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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()
Expand Down