Skip to content

Fixed naming that suggested settings were used to inflect relationship field names #226

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

Merged
merged 2 commits into from
Apr 15, 2016
Merged
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@

v2.0.0

* Renamed `JSON_API_FORMAT_RELATION_KEYS` to `JSON_API_FORMAT_TYPES` to match what it was actually doing
* Renamed `JSON_API_PLURALIZE_RELATION_TYPE` to `JSON_API_PLURALIZE_TYPES`
* Documented ResourceRelatedField and RelationshipView
* Added LimitOffsetPagination
* Support deeply nested `?includes=foo.bar.baz` without returning intermediate models (bar)
* Allow a view's serializer_class to be fetched at runtime via `get_serializer_class`
* Added support for `get_root_meta` on list serializers


v2.0.0-beta.2

* Added JSONAPIMeta class option to models for overriding `resource_name`. #197
Expand Down
15 changes: 6 additions & 9 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,20 @@ Example - With format conversion set to `dasherize`:
}
```

#### Relationship types
#### Types

A similar option to JSON\_API\_FORMAT\_RELATION\_KEYS can be set for the relationship names:
A similar option to JSON\_API\_FORMAT\_KEYS can be set for the types:

``` python
JSON_API_FORMAT_RELATION_KEYS = 'dasherize'
JSON_API_FORMAT_TYPES = 'dasherize'
```

Example without format conversion:

``` js
{
"data": [{
"type": "identities",
"type": "blog_identity",
"id": 3,
"attributes": {
...
Expand All @@ -191,7 +191,7 @@ When set to dasherize:
``` js
{
"data": [{
"type": "identities",
"type": "blog-identity",
"id": 3,
"attributes": {
...
Expand All @@ -210,7 +210,7 @@ When set to dasherize:
It is also possible to pluralize the types like so:

```python
JSON_API_PLURALIZE_RELATION_TYPE = True
JSON_API_PLURALIZE_TYPES = True
```
Example without pluralization:

Expand Down Expand Up @@ -257,9 +257,6 @@ When set to pluralize:
}
```

Both `JSON_API_PLURALIZE_RELATION_TYPE` and `JSON_API_FORMAT_RELATION_KEYS` can be combined to
achieve different results.

### Related fields

Because of the additional structure needed to represent relationships in JSON
Expand Down
2 changes: 1 addition & 1 deletion example/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
MIDDLEWARE_CLASSES = ()

JSON_API_FORMAT_KEYS = 'camelize'
JSON_API_FORMAT_RELATION_KEYS = 'camelize'
JSON_API_FORMAT_TYPES = 'camelize'
REST_FRAMEWORK = {
'PAGE_SIZE': 5,
'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
Expand Down
4 changes: 2 additions & 2 deletions example/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
ROOT_URLCONF = 'example.urls_test'

JSON_API_FORMAT_KEYS = 'camelize'
JSON_API_FORMAT_RELATION_KEYS = 'camelize'
JSON_API_PLURALIZE_RELATION_TYPE = True
JSON_API_FORMAT_TYPES = 'camelize'
JSON_API_PLURALIZE_TYPES = True
REST_FRAMEWORK.update({
'PAGE_SIZE': 1,
})
12 changes: 6 additions & 6 deletions example/tests/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from . import TestBase
from rest_framework_json_api.exceptions import Conflict
from rest_framework_json_api.utils import format_relation_name
from rest_framework_json_api.utils import format_resource_type
from example.models import Blog, Entry, Comment, Author
from example.serializers import CommentSerializer
from rest_framework_json_api.relations import ResourceRelatedField
Expand Down Expand Up @@ -42,7 +42,7 @@ def test_data_in_correct_format_when_instantiated_with_blog_object(self):
serializer = BlogFKSerializer(instance={'blog': self.blog})

expected_data = {
'type': format_relation_name('Blog'),
'type': format_resource_type('Blog'),
'id': str(self.blog.id)
}

Expand All @@ -54,7 +54,7 @@ def test_data_in_correct_format_when_instantiated_with_entry_object(self):
serializer = EntryFKSerializer(instance={'entry': self.entry})

expected_data = {
'type': format_relation_name('Entry'),
'type': format_resource_type('Entry'),
'id': str(self.entry.id)
}

Expand All @@ -65,7 +65,7 @@ def test_data_in_correct_format_when_instantiated_with_entry_object(self):
def test_deserialize_primitive_data_blog(self):
serializer = BlogFKSerializer(data={
'blog': {
'type': format_relation_name('Blog'),
'type': format_resource_type('Blog'),
'id': str(self.blog.id)
}
}
Expand All @@ -90,7 +90,7 @@ def test_validation_fails_for_wrong_type(self):
def test_serialize_many_to_many_relation(self):
serializer = EntryModelSerializer(instance=self.entry)

type_string = format_relation_name('Author')
type_string = format_resource_type('Author')
author_pks = Author.objects.values_list('pk', flat=True)
expected_data = [{'type': type_string, 'id': str(pk)} for pk in author_pks]

Expand All @@ -100,7 +100,7 @@ def test_serialize_many_to_many_relation(self):
)

def test_deserialize_many_to_many_relation(self):
type_string = format_relation_name('Author')
type_string = format_resource_type('Author')
author_pks = Author.objects.values_list('pk', flat=True)
authors = [{'type': type_string, 'id': pk} for pk in author_pks]

Expand Down
12 changes: 6 additions & 6 deletions example/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.test import TestCase
from django.utils import timezone

from rest_framework_json_api.utils import format_relation_name
from rest_framework_json_api.utils import format_resource_type
from rest_framework_json_api.serializers import ResourceIdentifierObjectSerializer

from example.models import Blog, Entry, Author
Expand Down Expand Up @@ -35,20 +35,20 @@ def setUp(self):
def test_data_in_correct_format_when_instantiated_with_blog_object(self):
serializer = ResourceIdentifierObjectSerializer(instance=self.blog)

expected_data = {'type': format_relation_name('Blog'), 'id': str(self.blog.id)}
expected_data = {'type': format_resource_type('Blog'), 'id': str(self.blog.id)}

assert serializer.data == expected_data

def test_data_in_correct_format_when_instantiated_with_entry_object(self):
serializer = ResourceIdentifierObjectSerializer(instance=self.entry)

expected_data = {'type': format_relation_name('Entry'), 'id': str(self.entry.id)}
expected_data = {'type': format_resource_type('Entry'), 'id': str(self.entry.id)}

assert serializer.data == expected_data

def test_deserialize_primitive_data_blog(self):
initial_data = {
'type': format_relation_name('Blog'),
'type': format_resource_type('Blog'),
'id': str(self.blog.id)
}
serializer = ResourceIdentifierObjectSerializer(data=initial_data, model_class=Blog)
Expand All @@ -60,14 +60,14 @@ def test_data_in_correct_format_when_instantiated_with_queryset(self):
qs = Author.objects.all()
serializer = ResourceIdentifierObjectSerializer(instance=qs, many=True)

type_string = format_relation_name('Author')
type_string = format_resource_type('Author')
author_pks = Author.objects.values_list('pk', flat=True)
expected_data = [{'type': type_string, 'id': str(pk)} for pk in author_pks]

assert serializer.data == expected_data

def test_deserialize_many(self):
type_string = format_relation_name('Author')
type_string = format_resource_type('Author')
author_pks = Author.objects.values_list('pk', flat=True)
initial_data = [{'type': type_string, 'id': str(pk)} for pk in author_pks]

Expand Down
28 changes: 14 additions & 14 deletions example/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from rest_framework.test import APITestCase

from rest_framework_json_api.utils import format_relation_name
from rest_framework_json_api.utils import format_resource_type
from example.models import Blog, Entry, Comment, Author


Expand Down Expand Up @@ -44,7 +44,7 @@ def setUp(self):
def test_get_entry_relationship_blog(self):
url = reverse('entry-relationships', kwargs={'pk': self.first_entry.id, 'related_field': 'blog'})
response = self.client.get(url)
expected_data = {'type': format_relation_name('Blog'), 'id': str(self.first_entry.blog.id)}
expected_data = {'type': format_resource_type('Blog'), 'id': str(self.first_entry.blog.id)}

assert response.data == expected_data

Expand All @@ -55,8 +55,8 @@ def test_get_entry_relationship_invalid_field(self):

def test_get_blog_relationship_entry_set(self):
response = self.client.get('/blogs/{}/relationships/entry_set'.format(self.blog.id))
expected_data = [{'type': format_relation_name('Entry'), 'id': str(self.first_entry.id)},
{'type': format_relation_name('Entry'), 'id': str(self.second_entry.id)}]
expected_data = [{'type': format_resource_type('Entry'), 'id': str(self.first_entry.id)},
{'type': format_resource_type('Entry'), 'id': str(self.second_entry.id)}]

assert response.data == expected_data

Expand Down Expand Up @@ -85,14 +85,14 @@ def test_get_to_many_relationship_self_link(self):
response = self.client.get(url)
expected_data = {
'links': {'self': 'http://testserver/authors/1/relationships/comment_set'},
'data': [{'id': str(self.second_comment.id), 'type': format_relation_name('Comment')}]
'data': [{'id': str(self.second_comment.id), 'type': format_resource_type('Comment')}]
}
assert json.loads(response.content.decode('utf-8')) == expected_data

def test_patch_to_one_relationship(self):
url = '/entries/{}/relationships/blog'.format(self.first_entry.id)
request_data = {
'data': {'type': format_relation_name('Blog'), 'id': str(self.other_blog.id)}
'data': {'type': format_resource_type('Blog'), 'id': str(self.other_blog.id)}
}
response = self.client.patch(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assert response.status_code == 200, response.content.decode()
Expand All @@ -103,7 +103,7 @@ def test_patch_to_one_relationship(self):
def test_patch_to_many_relationship(self):
url = '/blogs/{}/relationships/entry_set'.format(self.first_entry.id)
request_data = {
'data': [{'type': format_relation_name('Entry'), 'id': str(self.first_entry.id)}, ]
'data': [{'type': format_resource_type('Entry'), 'id': str(self.first_entry.id)}, ]
}
response = self.client.patch(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assert response.status_code == 200, response.content.decode()
Expand All @@ -114,23 +114,23 @@ def test_patch_to_many_relationship(self):
def test_post_to_one_relationship_should_fail(self):
url = '/entries/{}/relationships/blog'.format(self.first_entry.id)
request_data = {
'data': {'type': format_relation_name('Blog'), 'id': str(self.other_blog.id)}
'data': {'type': format_resource_type('Blog'), 'id': str(self.other_blog.id)}
}
response = self.client.post(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assert response.status_code == 405, response.content.decode()

def test_post_to_many_relationship_with_no_change(self):
url = '/entries/{}/relationships/comment_set'.format(self.first_entry.id)
request_data = {
'data': [{'type': format_relation_name('Comment'), 'id': str(self.first_comment.id)}, ]
'data': [{'type': format_resource_type('Comment'), 'id': str(self.first_comment.id)}, ]
}
response = self.client.post(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assert response.status_code == 204, response.content.decode()

def test_post_to_many_relationship_with_change(self):
url = '/entries/{}/relationships/comment_set'.format(self.first_entry.id)
request_data = {
'data': [{'type': format_relation_name('Comment'), 'id': str(self.second_comment.id)}, ]
'data': [{'type': format_resource_type('Comment'), 'id': str(self.second_comment.id)}, ]
}
response = self.client.post(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assert response.status_code == 200, response.content.decode()
Expand All @@ -140,7 +140,7 @@ def test_post_to_many_relationship_with_change(self):
def test_delete_to_one_relationship_should_fail(self):
url = '/entries/{}/relationships/blog'.format(self.first_entry.id)
request_data = {
'data': {'type': format_relation_name('Blog'), 'id': str(self.other_blog.id)}
'data': {'type': format_resource_type('Blog'), 'id': str(self.other_blog.id)}
}
response = self.client.delete(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assert response.status_code == 405, response.content.decode()
Expand All @@ -164,23 +164,23 @@ def test_delete_relationship_overriding_with_none(self):
def test_delete_to_many_relationship_with_no_change(self):
url = '/entries/{}/relationships/comment_set'.format(self.first_entry.id)
request_data = {
'data': [{'type': format_relation_name('Comment'), 'id': str(self.second_comment.id)}, ]
'data': [{'type': format_resource_type('Comment'), 'id': str(self.second_comment.id)}, ]
}
response = self.client.delete(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assert response.status_code == 204, response.content.decode()

def test_delete_one_to_many_relationship_with_not_null_constraint(self):
url = '/entries/{}/relationships/comment_set'.format(self.first_entry.id)
request_data = {
'data': [{'type': format_relation_name('Comment'), 'id': str(self.first_comment.id)}, ]
'data': [{'type': format_resource_type('Comment'), 'id': str(self.first_comment.id)}, ]
}
response = self.client.delete(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assert response.status_code == 409, response.content.decode()

def test_delete_to_many_relationship_with_change(self):
url = '/authors/{}/relationships/comment_set'.format(self.author.id)
request_data = {
'data': [{'type': format_relation_name('Comment'), 'id': str(self.second_comment.id)}, ]
'data': [{'type': format_resource_type('Comment'), 'id': str(self.second_comment.id)}, ]
}
response = self.client.delete(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assert response.status_code == 200, response.content.decode()
10 changes: 5 additions & 5 deletions example/tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class Meta:
def test_get_resource_name():
view = APIView()
context = {'view': view}
setattr(settings, 'JSON_API_FORMAT_RELATION_KEYS', None)
setattr(settings, 'JSON_API_FORMAT_TYPES', None)
assert 'APIViews' == utils.get_resource_name(context), 'not formatted'

context = {'view': view}
setattr(settings, 'JSON_API_FORMAT_RELATION_KEYS', 'dasherize')
setattr(settings, 'JSON_API_FORMAT_TYPES', 'dasherize')
assert 'api-views' == utils.get_resource_name(context), 'derived from view'

view.model = get_user_model()
Expand Down Expand Up @@ -91,9 +91,9 @@ def test_format_value():
assert utils.format_value('first-name', 'underscore') == 'first_name'


def test_format_relation_name():
assert utils.format_relation_name('first_name', 'capitalize') == 'FirstNames'
assert utils.format_relation_name('first_name', 'camelize') == 'firstNames'
def test_format_resource_type():
assert utils.format_resource_type('first_name', 'capitalize') == 'FirstNames'
assert utils.format_resource_type('first_name', 'camelize') == 'firstNames'


class SerializerWithIncludedSerializers(EntrySerializer):
Expand Down
4 changes: 2 additions & 2 deletions rest_framework_json_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def extract_relationships(fields, resource, resource_instance):

if isinstance(field, ModelSerializer):
relation_model = field.Meta.model
relation_type = utils.format_relation_name(relation_model.__name__)
relation_type = utils.format_type(relation_model.__name__)

data.update({
field_name: {
Expand Down Expand Up @@ -343,7 +343,7 @@ def extract_root_meta(serializer, resource):
if hasattr(serializer, 'child'):
many = True
serializer = serializer.child

data = {}
if getattr(serializer, 'get_root_meta', None):
json_api_meta = serializer.get_root_meta(resource, many)
Expand Down
Loading