Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Update facility details API #1333

Merged
merged 1 commit into from May 13, 2021
Merged

Conversation

TaiWilkin
Copy link
Contributor

@TaiWilkin TaiWilkin commented May 11, 2021

Overview

The embedded map feature supports a contributor showing additional
fields they have added that are not visible on OAR. When a contributor
id and embed=1 are passed to the facility details API, an array of
additional fields, as defined by that contributor's embedded map
settings, are returned with the facility details.

Connects #1218

Demo

Screen Shot 2021-05-11 at 1 24 36 PM

Notes

We previously discussed the format of the fields being: { "label": "Test Field", "value": "1234" } This works well when the contributor has only submitted the facility once; however, in some cases contributors submit a facility multiple times, which can result in multiple available values for a field.

Currently, instead, all of the available values for the field are returned as an array { "label": "Test Field", "value": ["1234", "abc"] }. This seemed like the most honest presentation of the available data, but other options would be to only use the most recently submitted FacilityListItem for the facility (regardless of whether the submission includes all of the fields in question), or to use the most recently submitted FacilityListItem where the field is present for each field.

Testing Instructions

  • Run vagrant ssh and ./scripts/test.
    • All tests should pass.
  • Sign in and submit a facility by API with custom fields. Ensure that it has been matched or created.
  • Submit a facility via list.
  • Manually add the custom fields as EmbedFields for the account you're signed into, ensuring they are set to 'visible'.
  • GET the details for the facilities you submitted, passing embed=1 and your contributor id as query params.
    http://localhost:6543/api/facilities/{facility id}/?embed=1&contributor={id}
    • The contributor fields should be provided.
    • The fields should show the display name and the provided value(s).
    • The fields should be in the order defined by the EmbedFields.

Checklist

  • fixup! commits have been squashed
  • CI passes after rebase
  • CHANGELOG.md updated with summary of features or fixes, following Keep a Changelog guidelines

Copy link
Contributor

@jwalgran jwalgran left a comment

Choose a reason for hiding this comment

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

I was able to curl facilities with additional field values and splitting the data processing into a set of smaller helper functions helps with readability.

I made some suggestions around multiple submissions and dissociation that I think we want to handle but weren't fully described in the initial issue.

Comment on lines 699 to 701
list_items = FacilityListItem.objects.filter(
facility=facility,
source__contributor=contributor)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not captured in the original issue description is the need to allow contributors to dissociate from facilities they no longer do business with, which we allow for via the is_active flags. I expect that adding 2 additional filter statements to this call to .filter() should take care of this

source__is_active=True, 
facilitymatch__is_active=True

Although the same facility could be submitted multiple times and therefore have multiple line items matched to it, I think that we should assume that the most recent, active submission is the one from which we should pull the nonstandard field values. I expect that adding an order_by('-created_at') and then taking just the first matching row will take care of this.

@@ -6494,3 +6495,143 @@ def test_querydict_in_api_data(self):
# We plan to only store raw data as CSV or JSON in the future. Legacy
# data can be ignored
self.assertNotIn('extra_2', content)


class ContributorFieldsApiTest(APITestCase):
Copy link
Contributor

Choose a reason for hiding this comment

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

Love to see a new test case appear.

Comment on lines 537 to 556
def assign_contributor_field_values(list_items, fields):
contributor_fields = [{
'label': f.display_name, 'values': [], 'column_name': f.column_name
} for f in fields]

for item in list_items:
if item.source.source_type == 'SINGLE':
contributor_fields = get_single_contributor_field_values(
item, contributor_fields
)
else:
contributor_fields = get_list_contributor_field_values(
item, contributor_fields
)

return [{
'values': f['values'], 'label': f['label']
} for f in contributor_fields]
Copy link
Contributor

Choose a reason for hiding this comment

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

Although the same facility could be submitted multiple times and therefore have multiple line items matched to it, I think that we should assume that the most recent, active submission is the one from which we should pull the nonstandard field values. I mentioned this at the point in the code where the QuerySet is built and, as a result, we should be able to take a single list item as a parameter to this function and use a single 'value': None to initialize the return rather than 'values': []'.

@TaiWilkin
Copy link
Contributor Author

Ready for another look.

Copy link
Contributor

@jwalgran jwalgran left a comment

Choose a reason for hiding this comment

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

I verified the changes. Looks great. 👍

@jwalgran jwalgran removed their assignment May 13, 2021
@jwalgran jwalgran added the task 004 Enable contributors to embed a map displaying only the facilities with which they are associated in label May 13, 2021
The embedded map feature supports a contributor showing additional
fields they have added that are not visible on OAR. When a contributor
id and embed=1 are passed to the facility details API, an array of
additional fields, as defined by that contributor's embedded map
settings, are returned with the facility details.
@TaiWilkin TaiWilkin force-pushed the tw/update-facility-details-api branch from 5551bf2 to ffa7a81 Compare May 13, 2021 14:28
@TaiWilkin TaiWilkin merged commit 6b8e265 into develop May 13, 2021
@TaiWilkin TaiWilkin deleted the tw/update-facility-details-api branch May 13, 2021 14:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
task 004 Enable contributors to embed a map displaying only the facilities with which they are associated in
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants