Skip to content

Commit

Permalink
Merge branch 'release/0.6.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlov99 committed Feb 25, 2015
2 parents 3d1e826 + 1788fa1 commit 21f004f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion jsonapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""" JSON:API realization."""
__version = (0, 6, 9)
__version = (0, 6, 10)

__version__ = version = '.'.join(map(str, __version))
__project__ = PROJECT = __name__
12 changes: 8 additions & 4 deletions jsonapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Meta:
import logging
import json
from .exceptions import JSONAPIError
from .serializers import DatetimeDecimalEncoder

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -192,7 +193,8 @@ def handler_view_get(self, resource, **kwargs):
def handler_view_post(self, resource, **kwargs):
data = resource.post(**kwargs)
response = HttpResponse(
json.dumps(data), content_type=self.CONTENT_TYPE, status=201)
json.dumps(data, cls=DatetimeDecimalEncoder),
content_type=self.CONTENT_TYPE, status=201)

items = data[resource.Meta.name_plural]
items = items if isinstance(items, list) else [items]
Expand All @@ -208,9 +210,11 @@ def handler_view_put(self, resource, **kwargs):
return HttpResponse("Request SHOULD have resource ids", status=400)

try:
response = resource.put(**kwargs)
return HttpResponse(
response, content_type=self.CONTENT_TYPE, status=200)
data = resource.put(**kwargs)
response = HttpResponse(
json.dumps(data, cls=DatetimeDecimalEncoder),
content_type=self.CONTENT_TYPE, status=200)
return response
except JSONAPIError as e:
return HttpResponse(
e.message, content_type=self.CONTENT_TYPE, status=e.status_code)
Expand Down
5 changes: 1 addition & 4 deletions jsonapi/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Meta:
from .request_parser import RequestParser
from .model_inspector import ModelInspector
from .exceptions import JSONAPIError
from .import statuses
from . import statuses

__all__ = 'Resource',

Expand Down Expand Up @@ -334,8 +334,6 @@ def post(cls, request=None, **kwargs):

@classmethod
def put(cls, request=None, **kwargs):
# TODO: check ids for elements.
# TODO: check form is valid for elements.
jdata = request.body.decode('utf8')
data = ast.literal_eval(jdata)
items = data[cls.Meta.name_plural]
Expand Down Expand Up @@ -378,7 +376,6 @@ def put(cls, request=None, **kwargs):

@classmethod
def delete(cls, request=None, **kwargs):
# TODO: raise Error if there are no ids.
user = cls.authenticate(request)
queryset = cls.get_queryset(user=user, **kwargs)
queryset.filter(id__in=kwargs['ids']).delete()
Expand Down
18 changes: 18 additions & 0 deletions tests/testapp/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ def test_update_model(self):
author = Author.objects.get()
self.assertEqual(author.name, "author")

expected_data = {
"authors": {
"id": author.id,
"name": "author"
}
}
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(data, expected_data)

def test_update_models(self):
authors = mixer.cycle(2).blend("testapp.author", name="")
response = self.client.put(
Expand All @@ -289,6 +298,15 @@ def test_update_models(self):
for author in Author.objects.all():
self.assertEqual(author.name, "author")

expected_data = {
"authors": [{
"id": a.id,
"name": "author",
} for a in authors],
}
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(data, expected_data)

def test_update_model_missing_ids(self):
mixer.blend("testapp.author")
response = self.client.put(
Expand Down

0 comments on commit 21f004f

Please sign in to comment.