forked from django-json-api/django-rest-framework-json-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support formatting URL segments via new FORMAT_LINKS setting
Fixes django-json-api#790.
- Loading branch information
1 parent
3833271
commit 2bf580e
Showing
12 changed files
with
210 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ Jason Housley <[email protected]> | |
Jerel Unruh <[email protected]> | ||
Jonathan Senecal <[email protected]> | ||
Joseba Mendivil <[email protected]> | ||
Kevin Partington <[email protected]> | ||
Kieran Evans <[email protected]> | ||
Léo S. <[email protected]> | ||
Luc Cary <[email protected]> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import pytest | ||
|
||
from rest_framework_json_api.relations import HyperlinkedRelatedField | ||
|
||
from .models import BasicModel | ||
|
||
|
||
@pytest.mark.urls("tests.urls") | ||
@pytest.mark.parametrize( | ||
"format_links,expected_url_segment", | ||
[ | ||
(None, "relatedField_name"), | ||
("dasherize", "related-field-name"), | ||
("camelize", "relatedFieldName"), | ||
("capitalize", "RelatedFieldName"), | ||
("underscore", "related_field_name"), | ||
], | ||
) | ||
def test_relationship_urls_respect_format_links( | ||
settings, format_links, expected_url_segment | ||
): | ||
settings.JSON_API_FORMAT_LINKS = format_links | ||
|
||
model = BasicModel(text="Some text") | ||
|
||
field = HyperlinkedRelatedField( | ||
self_link_view_name="basic-model-relationships", | ||
related_link_view_name="basic-model-related", | ||
read_only=True, | ||
) | ||
field.field_name = "relatedField_name" | ||
|
||
expected = { | ||
"self": "/basic_models/{}/relationships/{}/".format( | ||
model.pk, | ||
expected_url_segment, | ||
), | ||
"related": "/basic_models/{}/{}/".format( | ||
model.pk, | ||
expected_url_segment, | ||
), | ||
} | ||
|
||
actual = field.get_links(model) | ||
|
||
assert expected == actual |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import pytest | ||
from django.test import RequestFactory | ||
|
||
from rest_framework_json_api import serializers, views | ||
from rest_framework_json_api.relations import ResourceRelatedField | ||
from rest_framework_json_api.utils import format_value | ||
|
||
from .models import BasicModel | ||
|
||
related_model_field_name = "related_field_model" | ||
|
||
|
||
@pytest.mark.urls("tests.urls") | ||
@pytest.mark.parametrize( | ||
"format_links", | ||
[ | ||
None, | ||
"dasherize", | ||
"camelize", | ||
"capitalize", | ||
"underscore", | ||
], | ||
) | ||
def test_get_related_field_name_handles_formatted_link_segments(format_links): | ||
url_segment = format_value(related_model_field_name, format_links) | ||
|
||
request = RequestFactory().get("/basic_models/1/{}".format(url_segment)) | ||
|
||
view = BasicModelFakeViewSet() | ||
view.setup(request, related_field=url_segment) | ||
|
||
assert view.get_related_field_name() == related_model_field_name | ||
|
||
|
||
class BasicModelSerializer(serializers.ModelSerializer): | ||
related_model_field = ResourceRelatedField(queryset=BasicModel.objects) | ||
|
||
def __init__(self, *args, **kwargs): | ||
# Intentionally setting field_name property to something that matches no format | ||
self.related_model_field.field_name = related_model_field_name | ||
super(BasicModelSerializer, self).__init(*args, **kwargs) | ||
|
||
class Meta: | ||
model = BasicModel | ||
|
||
|
||
class BasicModelFakeViewSet(views.ModelViewSet): | ||
serializer_class = BasicModelSerializer | ||
|
||
def retrieve(self, request, *args, **kwargs): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django.conf.urls import re_path | ||
from rest_framework.routers import SimpleRouter | ||
|
||
from .views import BasicModelRelationshipView, BasicModelViewSet | ||
|
||
router = SimpleRouter() | ||
router.register(r"basic_models", BasicModelViewSet, basename="basic-model") | ||
|
||
urlpatterns = [ | ||
re_path( | ||
r"^basic_models/(?P<pk>[^/.]+)/(?P<related_field>[^/.]+)/$", | ||
BasicModelViewSet.as_view({"get": "retrieve_related"}), | ||
name="basic-model-related", | ||
), | ||
re_path( | ||
r"^basic_models/(?P<pk>[^/.]+)/relationships/(?P<related_field>[^/.]+)/$", | ||
BasicModelRelationshipView.as_view(), | ||
name="basic-model-relationships", | ||
), | ||
] | ||
|
||
urlpatterns += router.urls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from rest_framework_json_api.views import ModelViewSet, RelationshipView | ||
|
||
from .models import BasicModel | ||
|
||
|
||
class BasicModelViewSet(ModelViewSet): | ||
class Meta: | ||
model = BasicModel | ||
|
||
|
||
class BasicModelRelationshipView(RelationshipView): | ||
queryset = BasicModel.objects |