Skip to content

Commit c4bf6b1

Browse files
authored
Merge branch 'master' into pyup-scheduled-update-2020-12-21
2 parents 2e31f68 + cfca644 commit c4bf6b1

File tree

17 files changed

+299
-104
lines changed

17 files changed

+299
-104
lines changed

.github/workflows/tests.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Tests
2+
on: [push, pull_request]
3+
4+
jobs:
5+
test:
6+
name: Run test
7+
runs-on: ubuntu-latest
8+
continue-on-error: ${{ matrix.django-rest-framework == 'master' }}
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
python-version: ["3.6", "3.7", "3.8", "3.9"]
13+
django: ["2.2", "3.0", "3.1"]
14+
django-rest-framework: ["3.12", "master"]
15+
env:
16+
PYTHON: ${{ matrix.python-version }}
17+
DJANGO: ${{ matrix.django }}
18+
DJANGO_REST_FRAMEWORK: ${{ matrix.django-rest-framework }}
19+
steps:
20+
- uses: actions/checkout@v2
21+
- name: Set up Python ${{ matrix.python-version }}
22+
uses: actions/setup-python@v2
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
- name: Install dependencies
26+
run: |
27+
python -m pip install --upgrade pip
28+
pip install tox tox-gh-actions
29+
- name: Test with tox
30+
run: tox
31+
- name: Upload coverage report
32+
uses: codecov/codecov-action@v1
33+
with:
34+
env_vars: PYTHON,DJANGO,DJANGO_REST_FRAMEWORK
35+
check:
36+
name: Run check
37+
runs-on: ubuntu-latest
38+
strategy:
39+
fail-fast: false
40+
matrix:
41+
tox-env: ["black", "lint", "docs"]
42+
steps:
43+
- uses: actions/checkout@v2
44+
- name: Set up Python 3.6
45+
uses: actions/setup-python@v2
46+
with:
47+
python-version: 3.6
48+
- name: Install dependencies
49+
run: |
50+
python -m pip install --upgrade pip
51+
pip install tox
52+
- name: Run lint
53+
run: tox
54+
env:
55+
TOXENV: ${{ matrix.tox-env }}

.travis.yml

Lines changed: 0 additions & 88 deletions
This file was deleted.

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Jason Housley <[email protected]>
1515
Jerel Unruh <[email protected]>
1616
Jonathan Senecal <[email protected]>
1717
Joseba Mendivil <[email protected]>
18+
Kevin Partington <[email protected]>
1819
Kieran Evans <[email protected]>
1920
2021

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ any parts of the framework not mentioned in the documentation should generally b
1313
### Added
1414

1515
* Ability for the user to select `included_serializers` to apply when using `BrowsableAPI`, based on available `included_serializers` defined for the current endpoint.
16+
* Ability for the user to format serializer properties in URL segments using the `JSON_API_FORMAT_RELATED_LINKS` setting.
1617

1718
### Fixed
1819

README.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
JSON API and Django Rest Framework
33
==================================
44

5-
.. image:: https://travis-ci.com/django-json-api/django-rest-framework-json-api.svg?branch=master
6-
:target: https://travis-ci.com/django-json-api/django-rest-framework-json-api
5+
.. image:: hhttps://github.com/django-json-api/django-rest-framework-json-api/workflows/Tests/badge.svg
6+
:alt: Tests
7+
:target: https://github.com/django-json-api/django-rest-framework-json-api/actions
78

89
.. image:: https://readthedocs.org/projects/django-rest-framework-json-api/badge/?version=latest
910
:alt: Read the docs

docs/usage.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,44 @@ When set to pluralize:
477477
}
478478
```
479479

480+
#### Related URL segments
481+
482+
Serializer properties in relationship and related resource URLs may be infected using the `JSON_API_FORMAT_RELATED_LINKS` setting.
483+
484+
``` python
485+
JSON_API_FORMAT_RELATED_LINKS = 'dasherize'
486+
```
487+
488+
For example, with a serializer property `created_by` and with `'dasherize'` formatting:
489+
490+
```json
491+
{
492+
"data": {
493+
"type": "comments",
494+
"id": "1",
495+
"attributes": {
496+
"text": "Comments are fun!"
497+
},
498+
"links": {
499+
"self": "/comments/1"
500+
},
501+
"relationships": {
502+
"created_by": {
503+
"links": {
504+
"self": "/comments/1/relationships/created-by",
505+
"related": "/comments/1/created-by"
506+
}
507+
}
508+
}
509+
},
510+
"links": {
511+
"self": "/comments/1"
512+
}
513+
}
514+
```
515+
516+
The relationship name is formatted by the `JSON_API_FORMAT_FIELD_NAMES` setting, but the URL segments are formatted by the `JSON_API_FORMAT_RELATED_LINKS` setting.
517+
480518
### Related fields
481519

482520
#### ResourceRelatedField
@@ -703,6 +741,14 @@ class OrderSerializer(serializers.HyperlinkedModelSerializer):
703741
}
704742
```
705743

744+
<div class="warning">
745+
<strong>Note:</strong>
746+
Even though with related urls relations are served on different urls there are still served
747+
by the same view. This means that the object permission check is performed on the parent object.
748+
In other words when the parent object is accessible by the user the related object will be as well.
749+
</div>
750+
751+
706752
### RelationshipView
707753
`rest_framework_json_api.views.RelationshipView` is used to build
708754
relationship views (see the

example/settings/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
JSON_API_FORMAT_TYPES = "camelize"
1414
JSON_API_PLURALIZE_TYPES = True
1515

16-
REST_FRAMEWORK.update(
16+
REST_FRAMEWORK.update( # noqa: F405
1717
{ # noqa
1818
"PAGE_SIZE": 1,
1919
}

rest_framework_json_api/relations.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from rest_framework_json_api.exceptions import Conflict
1616
from rest_framework_json_api.utils import (
1717
Hyperlink,
18+
format_link_segment,
1819
get_included_serializers,
1920
get_resource_type_from_instance,
2021
get_resource_type_from_queryset,
@@ -112,14 +113,10 @@ def get_links(self, obj=None, lookup_field="pk"):
112113
else view.kwargs[lookup_field]
113114
}
114115

116+
field_name = self.field_name if self.field_name else self.parent.field_name
117+
115118
self_kwargs = kwargs.copy()
116-
self_kwargs.update(
117-
{
118-
"related_field": self.field_name
119-
if self.field_name
120-
else self.parent.field_name
121-
}
122-
)
119+
self_kwargs.update({"related_field": format_link_segment(field_name)})
123120
self_link = self.get_url("self", self.self_link_view_name, self_kwargs, request)
124121

125122
# Assuming RelatedField will be declared in two ways:

rest_framework_json_api/serializers.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1+
from collections import OrderedDict
2+
13
import inflection
24
from django.core.exceptions import ObjectDoesNotExist
35
from django.db.models.query import QuerySet
46
from django.utils.translation import gettext_lazy as _
57
from rest_framework.exceptions import ParseError
6-
from rest_framework.serializers import * # noqa: F403
8+
9+
# star import defined so `rest_framework_json_api.serializers` can be
10+
# a simple drop in for `rest_framework.serializers`
11+
from rest_framework.serializers import * # noqa: F401, F403
12+
from rest_framework.serializers import (
13+
BaseSerializer,
14+
HyperlinkedModelSerializer,
15+
ModelSerializer,
16+
Serializer,
17+
SerializerMetaclass,
18+
)
19+
from rest_framework.settings import api_settings
720

821
from rest_framework_json_api.exceptions import Conflict
922
from rest_framework_json_api.relations import ResourceRelatedField

rest_framework_json_api/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
DEFAULTS = {
1313
"FORMAT_FIELD_NAMES": False,
1414
"FORMAT_TYPES": False,
15+
"FORMAT_RELATED_LINKS": False,
1516
"PLURALIZE_TYPES": False,
1617
"UNIFORM_EXCEPTIONS": False,
1718
}

0 commit comments

Comments
 (0)