-
Notifications
You must be signed in to change notification settings - Fork 300
Support formatting URL segments via new FORMAT_LINKS setting #876
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
sliverc
merged 6 commits into
django-json-api:master
from
platinumazure:related-url-formatting
Dec 27, 2020
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4776e2b
Support formatting URL segments via new FORMAT_LINKS setting
platinumazure 81a6c7d
Fixup: Use utils.format_link_segment
platinumazure f67b463
Fixup: Undo changes to field_name_mapping dict
platinumazure 33f9ea7
Fixup: Use f-strings and pytest-django RequestFactory
platinumazure 6343e8a
Fixup: Remove unnecessary pytest urls mark call
platinumazure 8a2656d
Fixup: Move urlconf fixture setup into test_relations
platinumazure File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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") | ||
platinumazure marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@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)) | ||
platinumazure marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 hidden or 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 = [ | ||
platinumazure marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 hidden or 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): | ||
platinumazure marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class Meta: | ||
model = BasicModel | ||
|
||
|
||
class BasicModelRelationshipView(RelationshipView): | ||
queryset = BasicModel.objects |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.