Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
jerel committed Dec 17, 2014
2 parents baa0815 + 0d9a4cf commit 3732dc1
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 106 deletions.
63 changes: 58 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Rest Framework.
Settings
^^^^^^^^

One can either add ``rest_framework_ember.parsers.EmberJSONParser`` and
One can either add ``rest_framework_ember.parsers.JSONParser`` and
``rest_framework_ember.renderers.JSONRenderer`` to each ``ViewSet`` class, or
override ``settings.REST_FRAMEWORK``::

Expand All @@ -104,9 +104,9 @@ override ``settings.REST_FRAMEWORK``::
'PAGINATE_BY_PARAM': 'page_size',
'MAX_PAGINATE_BY': 100,
'DEFAULT_PAGINATION_SERIALIZER_CLASS':
'rest_framework_ember.pagination.EmberPaginationSerializer',
'rest_framework_ember.pagination.PaginationSerializer',
'DEFAULT_PARSER_CLASSES': (
'rest_framework_ember.parsers.EmberJSONParser',
'rest_framework_ember.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
),
Expand All @@ -116,8 +116,6 @@ override ``settings.REST_FRAMEWORK``::
),
}



If ``PAGINATE_BY`` is set the renderer will return a ``meta`` object with
record count and the next and previous links. Django Rest Framework looks
for the ``page`` GET parameter by default allowing you to make requests for
Expand All @@ -141,6 +139,61 @@ the ``resource_name`` property is required on the class::
permission_classes = (permissions.IsAuthenticated, )


Ember Data <-> Rest Framework Format Conversion
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
*(camelization/underscore/pluralize)*

This package includes the optional ability to automatically convert json requests
and responses from the Ember Data camelCase to python/rest_framework's preferred
underscore. Additionally resource names can be pluralized if more than one object
is included in a serialized response as Ember Data expects. To hook this up,
include the following in your project settings::

REST_EMBER_FORMAT_KEYS = True
REST_EMBER_PLURALIZE_KEYS = True


Example - Without format conversion::

{
"identity": [
{
"id": 1,
"username": "john",
"first_name": "John",
"last_name": "Coltrane"
},
{
"id": 2,
"username": "frodo",
"first_name": "Bilbo",
"last_name": "Baggins"
},
],
...
}

Example - With format conversion::

{
"identities": [
{
"id": 1,
"username": "john",
"firstName": "John",
"lastName": "Coltrane"
},
{
"id": 2,
"username": "frodo",
"firstName": "Bilbo",
"lastName": "Baggins"
},
],
...
}


Managing the trailing slash
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
6 changes: 3 additions & 3 deletions example/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ class UserEmber(User):
resource_name = 'data'

renderer_classes = (renderers.JSONRenderer, )
parser_classes = (parsers.EmberJSONParser, )
parser_classes = (parsers.JSONParser, )


class EmberUserModelViewSet(viewsets.ModelViewSet):
model = auth_models.User
queryset = auth_models.User.objects.all()
serializer_class = IdentitySerializer
allowed_methods = ['GET', 'POST', 'PUT', ]
renderer_classes = (renderers.JSONRenderer, )
parser_classes = (parsers.EmberJSONParser, )
parser_classes = (parsers.JSONParser, )


class MultipleIDMixinUserModelViewSet(mixins.MultipleIDMixin,
Expand Down
5 changes: 2 additions & 3 deletions example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
'PAGINATE_BY_PARAM': 'page_size',
'MAX_PAGINATE_BY': 100,
'DEFAULT_PAGINATION_SERIALIZER_CLASS':
'rest_framework_ember.pagination.EmberPaginationSerializer',
'rest_framework_ember.pagination.PaginationSerializer',
'DEFAULT_PARSER_CLASSES': (
# 'rest_framework_ember.parsers.EmberJSONParser',
# 'rest_framework_ember.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
),
Expand All @@ -49,7 +49,6 @@
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
),
'DATETIME_FORMAT': '%Y-%m-%d %H:%M:%S',
}


Expand Down
76 changes: 76 additions & 0 deletions example/tests/test_format_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import json

from example.tests import TestBase

from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse, reverse_lazy
from django.conf import settings


class FormatKeysSetTests(TestBase):
"""
Test that camelization and underscoring of key names works if they are activated.
"""
list_url = reverse_lazy('user-list')

def setUp(self):
super(FormatKeysSetTests, self).setUp()
self.detail_url = reverse('user-detail', kwargs={'pk': self.miles.pk})

# Set the format keys settings.
setattr(settings, 'REST_EMBER_FORMAT_KEYS', True)
setattr(settings, 'REST_EMBER_PLURALIZE_KEYS', True)

def tearDown(self):
# Remove the format keys settings.
delattr(settings, 'REST_EMBER_FORMAT_KEYS')
delattr(settings, 'REST_EMBER_PLURALIZE_KEYS')


def test_camelization(self):
"""
Test that camelization works.
"""
response = self.client.get(self.list_url)
self.assertEqual(response.status_code, 200)

user = get_user_model().objects.all()[0]
expected = {
u'user': [{
u'id': user.pk,
u'firstName': user.first_name,
u'lastName': user.last_name,
u'email': user.email
}]
}

json_content = json.loads(response.content)
meta = json_content.get('meta')

self.assertEquals(expected.get('user'), json_content.get('user'))
self.assertEqual('http://testserver/user-viewset/?page=2', meta.get('nextLink'))

def test_pluralization(self):
"""
Test that the key name is pluralized.
"""
response = self.client.get(self.list_url, {'page_size': 2})
self.assertEqual(response.status_code, 200)

users = get_user_model().objects.all()
expected = {
u'users': [{
u'id': users[0].pk,
u'firstName': users[0].first_name,
u'lastName': users[0].last_name,
u'email': users[0].email
},{
u'id': users[1].pk,
u'firstName': users[1].first_name,
u'lastName': users[1].last_name,
u'email': users[1].email
}]
}

json_content = json.loads(response.content)
self.assertEquals(expected.get('users'), json_content.get('users'))
2 changes: 1 addition & 1 deletion example/tests/test_generic_viewset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json
from example.tests import TestBase
from django.core.urlresolvers import reverse
Expand Down Expand Up @@ -49,3 +48,4 @@ def test_ember_expected_renderer(self):
}
)


Loading

0 comments on commit 3732dc1

Please sign in to comment.