-
Notifications
You must be signed in to change notification settings - Fork 3
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
✨Family relationship resource #194
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,6 @@ | ||
from dataservice.api.family_relationship.resources import ( | ||
FamilyRelationshipAPI | ||
) | ||
from dataservice.api.family_relationship.resources import ( | ||
FamilyRelationshipListAPI | ||
) |
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,154 @@ | ||
from flask import abort, request | ||
from marshmallow import ValidationError | ||
|
||
from dataservice.extensions import db | ||
from dataservice.api.common.pagination import paginated, Pagination | ||
from dataservice.api.family_relationship.models import FamilyRelationship | ||
from dataservice.api.family_relationship.schemas import ( | ||
FamilyRelationshipSchema | ||
) | ||
from dataservice.api.common.views import CRUDView | ||
|
||
|
||
class FamilyRelationshipListAPI(CRUDView): | ||
""" | ||
FamilyRelationship REST API | ||
""" | ||
endpoint = 'family_relationships_list' | ||
rule = '/family-relationships' | ||
schemas = {'FamilyRelationship': FamilyRelationshipSchema} | ||
|
||
@paginated | ||
def get(self, after, limit): | ||
""" | ||
Get all family_relationships | ||
--- | ||
description: Get all family_relationships | ||
template: | ||
path: | ||
get_list.yml | ||
properties: | ||
resource: | ||
FamilyRelationship | ||
""" | ||
q = FamilyRelationship.query | ||
|
||
return (FamilyRelationshipSchema(many=True) | ||
.jsonify(Pagination(q, after, limit))) | ||
|
||
def post(self): | ||
""" | ||
Create a new family_relationship | ||
--- | ||
template: | ||
path: | ||
new_resource.yml | ||
properties: | ||
resource: | ||
FamilyRelationship | ||
""" | ||
|
||
body = request.json | ||
|
||
# Deserialize | ||
try: | ||
fr = FamilyRelationshipSchema(strict=True).load(body).data | ||
# Request body not valid | ||
except ValidationError as e: | ||
abort(400, 'could not create family_relationship: {}' | ||
.format(e.messages)) | ||
|
||
# Add to and save in database | ||
db.session.add(fr) | ||
db.session.commit() | ||
|
||
return FamilyRelationshipSchema(201, 'family_relationship {} created' | ||
.format(fr.kf_id)).jsonify(fr), 201 | ||
|
||
|
||
class FamilyRelationshipAPI(CRUDView): | ||
""" | ||
FamilyRelationship REST API | ||
""" | ||
endpoint = 'family_relationships' | ||
rule = '/family-relationships/<string:kf_id>' | ||
schemas = {'FamilyRelationship': FamilyRelationshipSchema} | ||
|
||
def get(self, kf_id): | ||
""" | ||
Get a family_relationship by id | ||
--- | ||
template: | ||
path: | ||
get_by_id.yml | ||
properties: | ||
resource: | ||
FamilyRelationship | ||
""" | ||
# Get one | ||
fr = FamilyRelationship.query.get(kf_id) | ||
if fr is None: | ||
abort(404, 'could not find {} `{}`' | ||
.format('family_relationship', kf_id)) | ||
return FamilyRelationshipSchema().jsonify(fr) | ||
|
||
def patch(self, kf_id): | ||
""" | ||
Update an existing family_relationship. | ||
|
||
Allows partial update of resource | ||
--- | ||
template: | ||
path: | ||
update_by_id.yml | ||
properties: | ||
resource: | ||
FamilyRelationship | ||
""" | ||
fr = FamilyRelationship.query.get(kf_id) | ||
if fr is None: | ||
abort(404, 'could not find {} `{}`' | ||
.format('family_relationship', kf_id)) | ||
|
||
# Partial update - validate but allow missing required fields | ||
body = request.json or {} | ||
try: | ||
fr = FamilyRelationshipSchema(strict=True).load(body, instance=fr, | ||
partial=True).data | ||
except ValidationError as err: | ||
abort(400, 'could not update family_relationship: {}' | ||
.format(err.messages)) | ||
|
||
db.session.add(fr) | ||
db.session.commit() | ||
|
||
return FamilyRelationshipSchema( | ||
200, 'family_relationship {} updated'.format(fr.kf_id) | ||
).jsonify(fr), 200 | ||
|
||
def delete(self, kf_id): | ||
""" | ||
Delete family_relationship by id | ||
|
||
Deletes a family_relationship given a Kids First id | ||
--- | ||
template: | ||
path: | ||
delete_by_id.yml | ||
properties: | ||
resource: | ||
FamilyRelationship | ||
""" | ||
|
||
# Check if family_relationship exists | ||
fr = FamilyRelationship.query.get(kf_id) | ||
if fr is None: | ||
abort(404, 'could not find {} `{}`' | ||
.format('family_relationship', kf_id)) | ||
|
||
# Save in database | ||
db.session.delete(fr) | ||
db.session.commit() | ||
|
||
return FamilyRelationshipSchema(200, 'family_relationship {} deleted' | ||
.format(fr.kf_id)).jsonify(fr), 200 |
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,27 @@ | ||
from marshmallow_sqlalchemy import field_for | ||
|
||
from dataservice.api.family_relationship.models import FamilyRelationship | ||
from dataservice.api.common.schemas import BaseSchema | ||
from dataservice.extensions import ma | ||
|
||
|
||
class FamilyRelationshipSchema(BaseSchema): | ||
participant_id = field_for(FamilyRelationship, 'participant_id', | ||
required=True, | ||
load_only=True, example='PT_B048J5') | ||
relative_id = field_for(FamilyRelationship, 'relative_id', | ||
required=True, | ||
load_only=True, example='PT_B048J6') | ||
|
||
class Meta(BaseSchema.Meta): | ||
model = FamilyRelationship | ||
resource_url = 'api.family_relationships' | ||
collection_url = 'api.family_relationships_list' | ||
exclude = ('relative', 'participant') | ||
|
||
_links = ma.Hyperlinks({ | ||
'self': ma.URLFor(Meta.resource_url, kf_id='<kf_id>'), | ||
'collection': ma.URLFor(Meta.collection_url), | ||
'participant': ma.URLFor('api.participants', kf_id='<participant_id>'), | ||
'relative': ma.URLFor('api.participants', kf_id='<relative_id>') | ||
}) |
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 |
---|---|---|
@@ -1,5 +1,15 @@ | ||
import pkg_resources | ||
from itertools import tee | ||
|
||
|
||
def _get_version(): | ||
return pkg_resources.get_distribution("kf-api-dataservice").version | ||
|
||
|
||
def iterate_pairwise(iterable): | ||
""" | ||
Iterate over an iterable in consecutive pairs | ||
""" | ||
a, b = tee(iterable) | ||
next(b, None) | ||
return zip(a, b) |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know we've discussed doing this in #104, but do we want to do it to all resources at once to stay consistent? How do we track?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Welp, I think I might have added these types of links for the other resources I did...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh well, we'll just need to make sure the others get done.