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

Adjust Date Limits for Sample Collection Info #550

Merged
merged 5 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions microsetta_private_api/api/_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,13 @@ def update_sample_association(account_id, source_id, sample_id, body,
except ValueError:
raise BadRequest("Invalid sample_datetime")
curdate = datetime.now(sample_datetime.tzinfo)
lower_limit = curdate + relativedelta(years=-10)
lower_limit = curdate + relativedelta(years=-1)
upper_limit = curdate + relativedelta(months=+1)
if sample_datetime < lower_limit or sample_datetime > upper_limit:
is_admin = token_grants_admin_access(token_info)

# Allow admins to bypass the back-dating/forward-dating limits
if (sample_datetime < lower_limit or sample_datetime > upper_limit)\
and not is_admin:
raise BadRequest('Invalid sample date')
# sample_site will not be present if its environmental. this will
# default to None if the key is not present
Expand All @@ -151,7 +155,6 @@ def update_sample_association(account_id, source_id, sample_id, body,
body["sample_notes"]
)

is_admin = token_grants_admin_access(token_info)
sample_repo.update_info(account_id, source_id, sample_info,
override_locked=is_admin)

Expand Down
17 changes: 10 additions & 7 deletions microsetta_private_api/api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ def test_account_scrub_success(self):

sample_body = {'sample_site': 'Stool',
"sample_notes": "foobar",
'sample_datetime': "2017-07-21T17:32:28Z"}
'sample_datetime': datetime.datetime.utcnow()}

put_resp = self.client.put(
'%s?%s' % (sample_url, self.default_lang_querystring),
Expand Down Expand Up @@ -1016,10 +1016,13 @@ def test_account_scrub_success(self):
self.assertEqual(response_obj['sample_site'],
sample_body['sample_site'])

# strip the trailing "Z" which comes from the database... easier
# than loading into datetime
self.assertEqual(response_obj['sample_datetime'],
sample_body['sample_datetime'][:-1])
response_ts = datetime.datetime.strptime(
response_obj['sample_datetime'], "%Y-%m-%dT%H:%M:%S.%f"
)
self.assertEqual(
response_ts.strftime("%Y-%m-%d %H:%M:%S"),
sample_body['sample_datetime'].strftime("%Y-%m-%d %H:%M:%S")
)

# This test specifically verifies that the scenario in Private API
# issue #492 - where a user takes an external survey, deletes the source,
Expand Down Expand Up @@ -2353,7 +2356,7 @@ def test_update_sample_association_locked(self):
sample_url = "{0}/{1}".format(base_url, MOCK_SAMPLE_ID)

body = {'sample_site': 'Stool', "sample_notes": "",
'sample_datetime': "2017-07-21T17:32:28Z"}
'sample_datetime': datetime.datetime.utcnow()}

put_resp = self.client.put(
'%s?%s' % (sample_url, self.default_lang_querystring),
Expand All @@ -2376,7 +2379,7 @@ def test_update_sample_association_locked(self):

# attempt to modify the locked sample as the participant
body = {'sample_site': 'Saliva', "sample_notes": "",
'sample_datetime': "2017-07-21T17:32:28Z"}
'sample_datetime': datetime.datetime.utcnow()}

put_resp = self.client.put(
'%s?%s' % (sample_url, self.default_lang_querystring),
Expand Down
Loading