Skip to content

Commit

Permalink
Merge branch 'release/0.6.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlov99 committed Feb 7, 2015
2 parents 4d81ad7 + d0d5295 commit 2e8af9f
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 177 deletions.
4 changes: 4 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Docs
- Authentication
- Authorization

Examples
========
curl -v -H "Content-Type: application/vnd.api+json" 127.0.0.1:8000/api/author

Indices and tables
==================

Expand Down
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, 0)
__version = (0, 6, 1)

__version__ = version = '.'.join(map(str, __version))
__project__ = PROJECT = __name__
32 changes: 18 additions & 14 deletions jsonapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Meta:
import logging
import json

from .utils import Choices
from .model_inspector import ModelInspector

logger = logging.getLogger(__name__)
Expand All @@ -36,12 +35,6 @@ class API(object):

""" API handler."""

HTTP_METHODS = Choices(
('GET', 'get'),
('POST', 'create'),
('PUT', 'update'),
('DELETE', 'delete'),
)
CONTENT_TYPE = "application/vnd.api+json"

def __init__(self):
Expand Down Expand Up @@ -200,18 +193,30 @@ def handler_view_get(self, resource, **kwargs):
return HttpResponse(items, content_type=self.CONTENT_TYPE)

def handler_view_post(self, resource, **kwargs):
response = resource.post(**kwargs)
return HttpResponse(
response, content_type=self.CONTENT_TYPE, status=201)
data = resource.post(**kwargs)
response = HttpResponse(
json.dumps(data), content_type=self.CONTENT_TYPE, status=201)

items = data[resource.Meta.name_plural]
items = items if isinstance(items, list) else [items]

response["Location"] = "{}/{}".format(
resource.Meta.name,
",".join([str(x["id"]) for x in items])
)
return response

def handler_view_put(self, resource, **kwargs):
if 'ids' not in kwargs:
return HttpResponse("Request SHOULD have resource ids", status=400)

response = resource.put(**kwargs)
return HttpResponse(
response, content_type=self.CONTENT_TYPE, status=200)

def handler_view_delete(self, resource, **kwargs):
if 'ids' not in kwargs:
return HttpResponse("Resource ids not specified", status=404)
return HttpResponse("Request SHOULD have resource ids", status=400)

response = resource.delete(**kwargs)
return HttpResponse(
Expand All @@ -229,16 +234,15 @@ def handler_view(self, request, resource_name, ids=None):
self.update_urls(request, resource_name=resource_name, ids=ids)
resource = self.resource_map[resource_name]

allowed_http_methods = {
getattr(API.HTTP_METHODS, x) for x in resource.Meta.allowed_methods}
allowed_http_methods = resource.Meta.allowed_methods
if request.method not in allowed_http_methods:
return HttpResponseNotAllowed(
permitted_methods=allowed_http_methods)

if resource.Meta.authenticators:
user = resource.authenticate(request)
if user is None or not user.is_authenticated():
return HttpResponse("Not Authenticated", status=404)
return HttpResponse("Not Authenticated", status=401)

kwargs = dict(request=request)
if ids is not None:
Expand Down
29 changes: 24 additions & 5 deletions jsonapi/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Meta:
* fieldnames_include = None
* fieldnames_exclude = None
* page_size = None
* allowed_methods = ('get',)
* allowed_methods = ('GET',)
Properties:
Expand Down Expand Up @@ -155,7 +155,7 @@ class Meta:
fieldnames_include = None
fieldnames_exclude = None
page_size = None
allowed_methods = 'get',
allowed_methods = 'GET',

@classproperty
def name_plural(cls):
Expand All @@ -173,9 +173,8 @@ def get_queryset(cls, user=None, **kwargs):
* Select related objects (or prefetch them) based on requested
requested objects to include
NOTE: if you need to get user in this method, use
cls.authenticate(request), because user could be authenticated using
HTTP_BASIC method, not only session.
NOTE: use user from parameters, it could be authenticated not with
session, so request.user might not work
"""
queryset = cls.Meta.model.objects
Expand All @@ -191,6 +190,26 @@ def get_queryset(cls, user=None, **kwargs):

return queryset

@classmethod
def update_get_queryset(cls, queryset, **kwargs):
""" Update permission queryset for GET operations."""
return queryset

@classmethod
def update_post_queryset(cls, queryset, **kwargs):
""" Update permission queryset for POST operations."""
return queryset

@classmethod
def update_put_queryset(cls, queryset, **kwargs):
""" Update permission queryset for PUT operations."""
return queryset

@classmethod
def update_delete_queryset(cls, queryset, **kwargs):
""" Update permission queryset for delete operations."""
return queryset

@classmethod
def get_form(cls, fields=None):
""" Create Partial Form based on given fields.
Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Meta:
class AuthorResource(Resource):
class Meta:
model = 'testapp.Author'
allowed_methods = 'get', 'create', 'update', 'delete'
allowed_methods = 'GET', 'POST', 'PUT', 'DELETE'


@api.register
Expand Down
4 changes: 0 additions & 4 deletions tests/testapp/settings/test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
from .base import *

INSTALLED_APPS += (
'tests.testapp.test_api',
)

if django.VERSION[:2] < (1, 6):
TEST_RUNNER = 'discover_runner.DiscoverRunner'

Expand Down
Empty file removed tests/testapp/test_api/__init__.py
Empty file.
148 changes: 0 additions & 148 deletions tests/testapp/test_api/tests.py

This file was deleted.

0 comments on commit 2e8af9f

Please sign in to comment.