Skip to content
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

✨ Study resource #175

Merged
merged 2 commits into from
Mar 16, 2018
Merged

✨ Study resource #175

merged 2 commits into from
Mar 16, 2018

Conversation

dankolbman
Copy link
Contributor

Adds endpoints for the study resource.

Addresses #161

@dankolbman dankolbman added the feature New functionality label Mar 8, 2018
@dankolbman dankolbman self-assigned this Mar 8, 2018
@dankolbman dankolbman requested review from znatty22 and parimalak March 8, 2018 20:35
@dankolbman dankolbman force-pushed the study-resource branch 5 times, most recently from f76e73f to f1e7ed7 Compare March 8, 2018 22:06
abort(404, 'could not find {} `{}`'
.format('Study', kf_id))

st = StudySchema(strict=True).load(body, instance=st).data
Copy link
Member

Choose a reason for hiding this comment

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

Couple requested changes here..

  1. Add the partial=True kw arg to the load so that marshmallow allows you to deserialize without all required fields. With PATCH we want to allow someone to update one or more fields without having to include all required fields.
  2. Surround the load with a try/except ValidationError.

This is what i did for participant:

       # Partial update - validate but allow missing required fields
        try:
            p = ParticipantSchema(strict=True).load(body, instance=p,
                                                    partial=True).data
        except ValidationError as err:
            abort(400, 'could not update participant: {}'.format(err.messages))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm I thought we had a general ValidationError handler, maybe we should add one?


def patch(self, kf_id):
"""
Update an existing study
Copy link
Member

Choose a reason for hiding this comment

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

Can we update the comment to something like:

Update an existing study. Allows for partial update of resource.

study = resp['results']
self.assertEqual(kf_id, study['kf_id'])

def test_patch_study(self):
Copy link
Member

@znatty22 znatty22 Mar 9, 2018

Choose a reason for hiding this comment

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

Probably a good idea to check the database as well. Make sure that only the fields in the body were modified and others remained the same.

Study
"""
try:
st = Study.query.filter_by(kf_id=kf_id).one()
Copy link
Member

@znatty22 znatty22 Mar 9, 2018

Choose a reason for hiding this comment

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

Should we change this to use .get now that kf_id is the primary key? If we decide to make that change, we'll also have to change the try/except NoResultFound to a if else statement to check if st is None. I think get() returns None if obj is not found

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, I'd like to use get_or_404(), but you're not able to pass a custom message to it. If we could do that, these 5 lines would be reduced down to one tidy line. We could consider hacking get_or_404() to take a message arg or not having a specific entity type returned in the not found response.

"""
body = request.json
try:
st = Study.query.filter_by(kf_id=kf_id).one()
Copy link
Member

Choose a reason for hiding this comment

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

Similar to above comment, should we change this to use .get now that kf_id is the primary key?

Study
"""
try:
st = Study.query.filter_by(kf_id=kf_id).one()
Copy link
Member

Choose a reason for hiding this comment

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

Similar to above comment, should we change this to use .get now that kf_id is the primary key?

@@ -85,7 +94,8 @@ def test_read_only(self, client, entities, endpoint, field):

@pytest.mark.parametrize('endpoint,field', [
('/participants', 'blah'),
('/samples', 'blah')
('/samples', 'blah'),
('/studies', 'blah'),
])
def test_unknown_field(self, client, entities, endpoint, field):
Copy link
Member

@znatty22 znatty22 Mar 9, 2018

Choose a reason for hiding this comment

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

I changed this method in #173 to provide an HTTP method as input so that we could perform this test for both POST and PATCH (and later PUT). We may want to merge #173 before this one and then you can update this test for study

st = StudySchema(strict=True).load(body, instance=st).data
db.session.commit()

return StudySchema(
Copy link
Member

Choose a reason for hiding this comment

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

Should return 200 instead of 201 since we are not creating a new resource

kf_id=kf_id),
headers=self._api_headers(),
data=json.dumps(body))
self.assertEqual(response.status_code, 201)
Copy link
Member

Choose a reason for hiding this comment

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

Rember to change to check for 200 after updating patch method to return 200 instead of 201

st = Study.query.get(kf_id)
if st is None:
abort(404, 'could not find {} `{}`'
.format('Study', kf_id))
Copy link
Member

Choose a reason for hiding this comment

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

Change 'Study' to 'study' to keep all error msgs standard

st = Study.query.get(kf_id)
if st is None:
abort(404, 'could not find {} `{}`'
.format('Study', kf_id))
Copy link
Member

Choose a reason for hiding this comment

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

Change Study to study to keep all error msgs standard

"""
st = Study.query.get(kf_id)
if st is None:
abort(404, 'could not find {} `{}`'.format('Study', kf_id))
Copy link
Member

Choose a reason for hiding this comment

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

Change Study to study to keep all error msgs standard

('/participants/123', 'DELETE', 'could not find Participant `123`')
('/participants/123', 'DELETE', 'could not find Participant `123`'),
('/studies', 'GET', 'success'),
('/studies/123', 'GET', 'could not find Study `123`'),
Copy link
Member

Choose a reason for hiding this comment

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

Remember to change from Study to study after updating resource class

@dankolbman dankolbman force-pushed the study-resource branch 5 times, most recently from 868634b to c70cd06 Compare March 16, 2018 13:39
data=json.dumps(body))
return response

def _test_response_content(self, resp, status_code):
Copy link
Member

Choose a reason for hiding this comment

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

Remove method and calls to it


from flask import url_for

from dataservice.extensions import db
Copy link
Member

@znatty22 znatty22 Mar 16, 2018

Choose a reason for hiding this comment

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

Remove unused import: dataservice.extensions.db

st = (StudySchema(strict=True).load(body, instance=st,
partial=True).data)
except ValidationError as err:
abort(400, 'could not update participant: {}'.format(err.messages))
Copy link
Member

Choose a reason for hiding this comment

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

Prob copy/paste error, change could not update participant to could not update study


def patch(self, kf_id):
"""
Update fields in a study and preserve existing fields.
Copy link
Member

Choose a reason for hiding this comment

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

Nitpicking but can we be consistent w the docstrings for crud resources? All the other resources have patch docstring like: Update an existing demographic. Allows partial update of resource. If you don't like above, then lets at least create an issue to standardize docstrings for the resource crud methods

@dankolbman dankolbman force-pushed the study-resource branch 2 times, most recently from 5a69450 to 3988371 Compare March 16, 2018 14:41
Copy link
Member

@znatty22 znatty22 left a comment

Choose a reason for hiding this comment

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

👍

@dankolbman dankolbman merged commit 33550a0 into master Mar 16, 2018
@dankolbman dankolbman deleted the study-resource branch March 16, 2018 15:48
@dankolbman dankolbman mentioned this pull request Apr 2, 2018
@dankolbman dankolbman removed the ready-for-review This PR needs a review label May 7, 2018
alubneuski pushed a commit that referenced this pull request Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants