Skip to content

JSONAPIOrderingFilter #459

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 6 commits into from
Aug 22, 2018
Merged

JSONAPIOrderingFilter #459

merged 6 commits into from
Aug 22, 2018

Conversation

n2ygk
Copy link
Contributor

@n2ygk n2ygk commented Aug 20, 2018

Fixes #432

Description of the Change

Partially addresses #432 by adding JSONAPIOrderingFilter backend. Broken up into smaller PR's per @sliverc.

Checklist

  • PR only contains one change (considered splitting up PR)
  • unit-test added
  • documentation updated
  • changelog entry added to CHANGELOG.md
  • author name in AUTHORS

Sorry, something went wrong.

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
@n2ygk n2ygk requested a review from sliverc August 20, 2018 22:02
n2ygk added 2 commits August 20, 2018 18:12

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
@codecov-io
Copy link

codecov-io commented Aug 20, 2018

Codecov Report

Merging #459 into master will increase coverage by 0.08%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #459      +/-   ##
==========================================
+ Coverage   93.24%   93.33%   +0.08%     
==========================================
  Files          54       56       +2     
  Lines        3123     3164      +41     
==========================================
+ Hits         2912     2953      +41     
  Misses        211      211
Impacted Files Coverage Δ
example/settings/dev.py 100% <ø> (ø) ⬆️
rest_framework_json_api/backends.py 100% <100%> (ø)
example/tests/test_backends.py 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 95e6d8d...779aed9. Read the comment docs.

Copy link
Member

@sliverc sliverc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This looks good. Just a few small changes and this should be ready for merging.

@@ -0,0 +1,60 @@
- model: example.blog
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's also do this in JSON as in drf_example

"""
test sort
"""
response = self.client.get(ENTRIES + '?sort=headline')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parameters can be passed on as data e.g. in this case data={'sort': 'headline'}. This way there is no need for string concats

response = self.client.get(ENTRIES + '?sort=headline')
self.assertEqual(response.status_code, 200,
msg=response.content.decode("utf-8"))
j = json.loads(response.content.decode("utf-8"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to get json result from response you should be able to simply do result = response.json() I would also prefer not to use single letter variables as python is not strongly typed so the name should tell what it which makes the code more readable (e.g. in this example result instead j or similar)

The same counts to the tests below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen cases where response.json() doesn't work. Taking a look if this is one of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this is not one of them.


from ..models import Blog, Entry

ENTRIES = "/nopage-entries"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't build routes manually but instead use django reverse resolver

In this case this would be reverse('nopage-entry'). It will be then a full path of the testserver url.

This needs to be done in setUp or what I usually do in each single test as it is more implicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

django.urls.exceptions.NoReverseMatch: Reverse for 'nopage-entry' not found. 'nopage-entry' is not a valid view function or pattern name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe nopage-entry-list

n2ygk added 3 commits August 21, 2018 17:42

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
- use json instead of yaml fixture to remove dependency on PyYAML
- use reverse()
- request.data dict instead of string concat to the url
- response.json() instead of json.loads(response.content.decode(...)

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
@n2ygk n2ygk merged commit 81d2236 into django-json-api:master Aug 22, 2018
@n2ygk n2ygk deleted the OrderingFilter branch August 22, 2018 01:21
@sliverc
Copy link
Member

sliverc commented Aug 22, 2018

I am bit confused that you merged this before I had the chance to look at the changes you did again.

I think it is important that we only merge once changes are approved.

Reasons for this:

  1. Requested changes need to be reviewed again whether there are done as expected.
  2. sometimes because of complexity of code errors might only be found at a second glance

In this case the latter is actually the case. Just when looking at the code I have the feeling that when we pass on a camel cased variable e.g. blogEntry in case blog_entry sort field exists no error would be throw but the parameter be removed and no sorting will happen because parent class with remove_invalid_fields is called.

It would be great if you could verify this (best in a test) and open up another PR.

Also another thing I noticed is I think we should stick to the same naming of modules as Django REST Framework does meaning the module should be named filters.py instead of backends.py. This seems to be more consistent to me.

@n2ygk
Copy link
Contributor Author

n2ygk commented Aug 22, 2018 via email

@n2ygk
Copy link
Contributor Author

n2ygk commented Aug 22, 2018

@sliverc wrote:

In this case the latter is actually the case. Just when looking at the code I have the feeling that when we pass on a camel cased variable e.g. blogEntry in case blog_entry sort field exists no error would be throw but the parameter be removed and no sorting will happen because parent class with remove_invalid_fields is called.

I override remove_invalid_fields and use format_value here

if format_value(term.lstrip('-'), "underscore") not in valid_fields

However, I think I see a fundamental problem with the information-losing reversibility of camel case, etc. Namely, if the model uses camel case rather than the "preferred" underscore for field names, the format_value(...'underscore') will do the wrong thing. I guess perhaps we just need to make the documentation a bit stronger about that requirement?

I'll take a look at more explicit testing and submit a PR shortly.

@n2ygk
Copy link
Contributor Author

n2ygk commented Aug 22, 2018

Of course you were correct, the sort is broken. Fixing.

@n2ygk n2ygk mentioned this pull request Aug 22, 2018
5 tasks
@n2ygk n2ygk added this to the 2.6.0 milestone Sep 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants