-
Notifications
You must be signed in to change notification settings - Fork 299
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 (#876)
Fixes #790.
- Loading branch information
1 parent
3833271
commit c503748
Showing
10 changed files
with
200 additions
and
8 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,74 @@ | ||
import pytest | ||
from django.conf.urls import re_path | ||
from rest_framework.routers import SimpleRouter | ||
|
||
from rest_framework_json_api.relations import HyperlinkedRelatedField | ||
from rest_framework_json_api.views import ModelViewSet, RelationshipView | ||
|
||
from .models import BasicModel | ||
|
||
|
||
@pytest.mark.urls(__name__) | ||
@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": f"/basic_models/{model.pk}/relationships/{expected_url_segment}/", | ||
"related": f"/basic_models/{model.pk}/{expected_url_segment}/", | ||
} | ||
|
||
actual = field.get_links(model) | ||
|
||
assert expected == actual | ||
|
||
|
||
# Routing setup | ||
|
||
|
||
class BasicModelViewSet(ModelViewSet): | ||
class Meta: | ||
model = BasicModel | ||
|
||
|
||
class BasicModelRelationshipView(RelationshipView): | ||
queryset = BasicModel.objects | ||
|
||
|
||
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
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,49 @@ | ||
import pytest | ||
|
||
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.parametrize( | ||
"format_links", | ||
[ | ||
None, | ||
"dasherize", | ||
"camelize", | ||
"capitalize", | ||
"underscore", | ||
], | ||
) | ||
def test_get_related_field_name_handles_formatted_link_segments(format_links, rf): | ||
url_segment = format_value(related_model_field_name, format_links) | ||
|
||
request = rf.get(f"/basic_models/1/{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 |