Skip to content

Commit

Permalink
fix(package): PPM-HOTFIX-072920 - Set line-length to 120; initial set…
Browse files Browse the repository at this point in the history
…up for tox; requirements refactor
  • Loading branch information
b32147 committed Jul 29, 2020
1 parent 39f85d6 commit 503dc84
Show file tree
Hide file tree
Showing 13 changed files with 553 additions and 2,008 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test and Deploy Prerelease
name: Deploy

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/prerelease-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test and Deploy Prerelease
name: Prerelease Deploy

on:
push:
Expand Down
6 changes: 2 additions & 4 deletions ppmutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
"""

__title__ = "PPM Utils"
__version__ = '0.7.0-alpha.3'
__version__ = "0.7.0-alpha.3"
__author__ = "Bryan Larson"
__license__ = "BSD 2-Clause"
__copyright__ = (
"Copyright 2011-2020 Harvard Medical School Department of Biomedical Informatics"
)
__copyright__ = "Copyright 2011-2020 Harvard Medical School Department of Biomedical Informatics"

# Version synonym
VERSION = __version__
Expand Down
1,434 changes: 308 additions & 1,126 deletions ppmutils/fhir.py

Large diffs are not rendered by default.

129 changes: 27 additions & 102 deletions ppmutils/p2md.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class P2MD(PPM.Service):

# Set identifier systems
p2md_identifier_system = "https://peoplepoweredmedicine.org/fhir/p2md/operation"
fileservice_identifier_system = (
"https://peoplepoweredmedicine.org/fhir/fileservice/file"
)
fileservice_identifier_system = "https://peoplepoweredmedicine.org/fhir/fileservice/file"

class ExportProviders(Enum):
Participant = "ppm-participant"
Expand All @@ -43,9 +41,7 @@ def default_url_for_env(cls, environment):
elif "prod" in environment:
return "https://p2m2.dbmi.hms.harvard.edu"
else:
logger.error(
f"Could not return a default URL for environment: {environment}"
)
logger.error(f"Could not return a default URL for environment: {environment}")

return None

Expand Down Expand Up @@ -151,9 +147,7 @@ def get_smart_authorizations(cls, request, ppm_id):
auths = next(p["authorizations"] for p in data if p["ppm_id"] == ppm_id)

# Get list of SMART providers and filter the user's auths list
smart_providers = [
p["provider"] for p in P2MD.get_smart_endpoints(request)["smart_endpoints"]
]
smart_providers = [p["provider"] for p in P2MD.get_smart_endpoints(request)["smart_endpoints"]]

return [auth for auth in auths if auth in smart_providers]

Expand All @@ -170,9 +164,7 @@ def get_twitter_data(cls, request, ppm_id, handle):
"""
Make a request to P2MD to fetch Twitter data and store it in PPM.
"""
response = cls.post(
request, f"/sources/api/twitter/{ppm_id}", {"handle": handle}, raw=True
)
response = cls.post(request, f"/sources/api/twitter/{ppm_id}", {"handle": handle}, raw=True)

# Return True if no errors
return response.ok
Expand Down Expand Up @@ -222,12 +214,7 @@ def get_gencove_data(cls, request, ppm_id, gencove_id):
"""
Make a request to P2MD to fetch Gencove data and store it in PPM.
"""
response = cls.post(
request,
f"/sources/api/gencove/{ppm_id}",
data={"gencove_id": gencove_id},
raw=True,
)
response = cls.post(request, f"/sources/api/gencove/{ppm_id}", data={"gencove_id": gencove_id}, raw=True,)

# Return True if no errors
return response.ok
Expand All @@ -252,9 +239,7 @@ def get_facebook_data(cls, request, ppm_id):
"""
Make a request to P2MD to fetch Facebook data and store it in PPM.
"""
response = cls.post(
request, f"/sources/api/facebook/{ppm_id}", data={}, raw=True
)
response = cls.post(request, f"/sources/api/facebook/{ppm_id}", data={}, raw=True)

# Return True if no errors
return response.ok
Expand All @@ -268,9 +253,7 @@ def download_facebook_data(cls, request, ppm_id):
:return: The requested dataset
"""
# Make the request
response = cls.get(
request, f"/sources/api/facebook/{ppm_id}/download", raw=True
)
response = cls.get(request, f"/sources/api/facebook/{ppm_id}/download", raw=True)
if response:
return response.content

Expand All @@ -281,9 +264,7 @@ def get_smart_data(cls, request, ppm_id, provider):
"""
Make a request to P2MD to fetch SMART on FHIR EHR data and store it in PPM.
"""
response = cls.post(
request, f"/sources/api/smart/{provider}/{ppm_id}", data={}, raw=True
)
response = cls.post(request, f"/sources/api/smart/{provider}/{ppm_id}", data={}, raw=True)

# Return True if no errors
return response.ok
Expand All @@ -298,9 +279,7 @@ def download_smart_data(cls, request, ppm_id, provider):
:return: The requested entire dataset
"""
# Make the request
response = cls.get(
request, f"/sources/api/smart/{provider}/{ppm_id}/download", raw=True
)
response = cls.get(request, f"/sources/api/smart/{provider}/{ppm_id}/download", raw=True)
if response:
return response.content

Expand Down Expand Up @@ -422,11 +401,7 @@ def check_qualtrics_survey(cls, request, study, ppm_id, survey_id):
:return: bool
"""
# Make the request
response = cls.head(
request,
f"/sources/api/qualtrics/survey/{study}/{ppm_id}/{survey_id}/",
raw=True,
)
response = cls.head(request, f"/sources/api/qualtrics/survey/{study}/{ppm_id}/{survey_id}/", raw=True,)
if response:
return response.ok

Expand All @@ -438,9 +413,7 @@ def get_qualtrics_survey_url(cls, study, ppm_id, survey_id):
Return the URL to send the participant to for taking the survey
"""
# Return True if no errors
url = cls._build_url(
path=f"/sources/api/qualtrics/survey/{study}/{ppm_id}/{survey_id}/"
)
url = cls._build_url(path=f"/sources/api/qualtrics/survey/{study}/{ppm_id}/{survey_id}/")

return url

Expand All @@ -450,25 +423,17 @@ def get_qualtrics_survey_data_url(cls, study, ppm_id, survey_id):
Return the URL to manage survey data
"""
# Return True if no errors
url = cls._build_url(
path=f"/sources/api/qualtrics/{study}/{ppm_id}/{survey_id}/"
)
url = cls._build_url(path=f"/sources/api/qualtrics/{study}/{ppm_id}/{survey_id}/")

return url

@classmethod
def get_qualtrics_survey_data(
cls, request, study, ppm_id, survey_id, response_id=None, older_than=None
):
def get_qualtrics_survey_data(cls, request, study, ppm_id, survey_id, response_id=None, older_than=None):
"""
Make a call to P2MD to look for a survey response
"""
# Return True if no errors
url = furl(
cls.get_qualtrics_survey_data_url(
study=study, ppm_id=ppm_id, survey_id=survey_id
)
)
url = furl(cls.get_qualtrics_survey_data_url(study=study, ppm_id=ppm_id, survey_id=survey_id))

data = {}
if response_id:
Expand All @@ -495,14 +460,7 @@ def get_file_proxy_url(cls, ppm_id, uuid):

@classmethod
def uploaded_file(
cls,
request,
study,
ppm_id,
document_type,
uuid,
location,
content_type="application/octect-stream",
cls, request, study, ppm_id, document_type, uuid, location, content_type="application/octect-stream",
):
"""
Make a request to P2MD to create a file upload
Expand Down Expand Up @@ -550,11 +508,7 @@ def get_smart_endpoint_urls(cls, request, ppm_id, return_url):
organization = endpoint.get("organization")
provider = endpoint.get("provider")
if not organization or not provider:
logger.error(
"Missing properties for SMART endpoint: {} - {}".format(
organization, provider
)
)
logger.error("Missing properties for SMART endpoint: {} - {}".format(organization, provider))
continue

# Build the URL
Expand Down Expand Up @@ -616,16 +570,10 @@ def download_participant_data(cls, request, ppm_id, filename=None, providers=Non
:return: The user's entire dataset
"""
# Build the URL
url = furl(
P2MD.get_participant_data_url(
ppm_id=ppm_id, filename=filename, providers=providers
)
)
url = furl(P2MD.get_participant_data_url(ppm_id=ppm_id, filename=filename, providers=providers))

# Make the request
response = cls.get(
request=request, path=url.pathstr, data=url.querystr, raw=True
)
response = cls.get(request=request, path=url.pathstr, data=url.querystr, raw=True)
if response:
return response.content

Expand Down Expand Up @@ -655,13 +603,8 @@ def get_data_document_references(cls, ppm_id, provider=None):
:rtype: list
"""
# Gather data-related DocumentReferences
document_references = FHIR.query_data_document_references(
patient=ppm_id, provider=provider
)
logger.debug(
f"{ppm_id}: Found {len(document_references)} DocumentReferences "
f"for: {provider}"
)
document_references = FHIR.query_data_document_references(patient=ppm_id, provider=provider)
logger.debug(f"{ppm_id}: Found {len(document_references)} DocumentReferences " f"for: {provider}")

# Flatten resources and pick out relevant identifiers
flats = []
Expand Down Expand Up @@ -702,30 +645,23 @@ def get_data_document_references_for_providers(cls, ppm_id, providers=None):
"""
# Get all flattened data document references
document_references = []
for document_reference in P2MD.get_data_document_references(
ppm_id, provider=None
):
for document_reference in P2MD.get_data_document_references(ppm_id, provider=None):

# Check type and filter out non-requested provider documents
if not providers or document_reference["type"] not in providers:
continue

document_references.append(document_reference)

logger.debug(
f"{ppm_id}: Found {len(document_references)} DocumentReferences "
f'for: {", ".join(providers)}'
)
logger.debug(f"{ppm_id}: Found {len(document_references)} DocumentReferences " f'for: {", ".join(providers)}')
return document_references

#
# Deprecated
#

@classmethod
def check_export(
cls, request, ppm_id, provider=ExportProviders.Participant, age=24
):
def check_export(cls, request, ppm_id, provider=ExportProviders.Participant, age=24):
"""
Checks the presence of the PPM dataset for the passed user
:param request: The original Django request object
Expand All @@ -736,12 +672,7 @@ def check_export(
:return: The age of the current dataset in hours, if any
"""
# Make the request
response = cls.head(
request,
f"/sources/api/ppm/{provider.value}/{ppm_id}/export",
{"age": age},
raw=True,
)
response = cls.head(request, f"/sources/api/ppm/{provider.value}/{ppm_id}/export", {"age": age}, raw=True,)
if response:
return response.ok

Expand Down Expand Up @@ -770,9 +701,7 @@ def download_export(cls, request, ppm_id, provider=ExportProviders.Participant):
:return: The user's entire dataset
"""
# Make the request
response = cls.get(
request, f"/sources/api/ppm/{provider.value}/{ppm_id}/export", raw=True
)
response = cls.get(request, f"/sources/api/ppm/{provider.value}/{ppm_id}/export", raw=True)
if response:
return response.content

Expand All @@ -788,18 +717,14 @@ def download_data(cls, request, ppm_id, provider=ExportProviders.Participant):
:return: The user's entire dataset
"""
# Make the request
response = cls.get(
request, f"/sources/api/ppm/{provider.value}/{ppm_id}/download", raw=True
)
response = cls.get(request, f"/sources/api/ppm/{provider.value}/{ppm_id}/download", raw=True)
if response:
return response.content

return None

@classmethod
def download_data_notify(
cls, request, ppm_id, recipients, provider=ExportProviders.Participant
):
def download_data_notify(cls, request, ppm_id, recipients, provider=ExportProviders.Participant):
"""
Downloads the PPM dataset for the passed user
:param request: The original Django request object
Expand Down
Loading

0 comments on commit 503dc84

Please sign in to comment.