mirror of
https://github.com/sartography/uva-covid19-testing-communicator.git
synced 2025-02-23 20:38:13 +00:00
switch from accepting bar codes to accepting a last modified date when querying samples.
This commit is contained in:
parent
dd17db1851
commit
67937535bb
@ -162,9 +162,9 @@ paths:
|
||||
- ApiKeyAuth: []
|
||||
parameters:
|
||||
- in: query
|
||||
name: bar_code
|
||||
name: last_modified
|
||||
required: false
|
||||
description: Return all samples AFTER this bar code.
|
||||
description: Return all samples modified AFTER the given date, provided as an ISO string (ex '2021-01-01T01:01:00')
|
||||
schema:
|
||||
type: string
|
||||
responses:
|
||||
|
@ -1,4 +1,5 @@
|
||||
import smtplib
|
||||
from datetime import datetime
|
||||
|
||||
from communicator import db, app, executor
|
||||
from communicator.models import Sample
|
||||
@ -35,20 +36,16 @@ def add_sample(body):
|
||||
SampleService().add_or_update_records([sample])
|
||||
|
||||
|
||||
def get_samples(bar_code=None):
|
||||
def get_samples(last_modified=None):
|
||||
query = db.session.query(Sample)
|
||||
if bar_code:
|
||||
last_sample = db.session.query(Sample).filter(Sample.barcode == bar_code).first()
|
||||
if not last_sample:
|
||||
app.logger.error(f'Someone queried for a barcode that does not exist: {bar_code} ', exc_info=True)
|
||||
raise Exception("No such bar code.")
|
||||
query = query.filter(Sample.date > last_sample.date)
|
||||
samples = query.order_by(Sample.date).limit(200).all()
|
||||
if last_modified:
|
||||
lm_date = datetime.fromisoformat(last_modified)
|
||||
query = query.filter(Sample.last_modified > lm_date)
|
||||
samples = query.order_by(Sample.last_modified).limit(200).all()
|
||||
response = SampleSchema(many=True).dump(samples)
|
||||
return response
|
||||
|
||||
|
||||
|
||||
def clear_samples():
|
||||
db.session.query(Notification).delete()
|
||||
db.session.query(Sample).delete()
|
||||
|
@ -128,18 +128,20 @@ class TestSampleEndpoint(BaseTest):
|
||||
|
||||
print(data)
|
||||
|
||||
def test_get_all_samples_after_barcode(self):
|
||||
d1_str = '202009091449'
|
||||
|
||||
def test_get_all_samples_by_last_modified(self):
|
||||
d1_str = '202012300101' # dec 30th 2020
|
||||
d1 = datetime.strptime(d1_str, '%Y%m%d%H%M')
|
||||
s1_bar_code = '000000111-'+ d1_str +'-4321'
|
||||
|
||||
d2_str = '202011010000'
|
||||
d2_str = '202101010101' # Jan 1st 2021
|
||||
d2 = datetime.strptime(d2_str, '%Y%m%d%H%M')
|
||||
s2_bar_code = '000000111-'+ d2_str +'-4321'
|
||||
|
||||
s1 = Sample(barcode=s1_bar_code,
|
||||
location="4321",
|
||||
date=d1,
|
||||
date= datetime.now(),
|
||||
last_modified=d1,
|
||||
student_id="000000111",
|
||||
email="daniel.h.funk@gmail.com",
|
||||
result_code="12345",
|
||||
@ -148,7 +150,8 @@ class TestSampleEndpoint(BaseTest):
|
||||
text_notified=True)
|
||||
s2 = Sample(barcode=s2_bar_code,
|
||||
location="4321",
|
||||
date=d2,
|
||||
date= datetime.now(),
|
||||
last_modified=d2,
|
||||
student_id="000000112",
|
||||
email="dan@gmail.com",
|
||||
result_code="12345",
|
||||
@ -158,9 +161,20 @@ class TestSampleEndpoint(BaseTest):
|
||||
db.session.add(s1)
|
||||
db.session.add(s2)
|
||||
|
||||
rv = self.app.get(f'/v1.0/sample?bar_code={s1_bar_code}', content_type="application/json",
|
||||
rv = self.app.get(f'/v1.0/sample', content_type="application/json",
|
||||
headers={'X-CR-API-KEY': app.config.get('API_TOKEN')})
|
||||
data = json.loads(rv.get_data(as_text=True))
|
||||
self.assertEqual(1, len(data))
|
||||
self.assertEqual(s2_bar_code, data[0]["barcode"])
|
||||
print(data)
|
||||
self.assertEquals(2, len(data))
|
||||
|
||||
last_modified_arg = d1.isoformat()
|
||||
rv = self.app.get(f'/v1.0/sample?last_modified={last_modified_arg}', content_type="application/json",
|
||||
headers={'X-CR-API-KEY': app.config.get('API_TOKEN')})
|
||||
data = json.loads(rv.get_data(as_text=True))
|
||||
self.assertEquals(1, len(data))
|
||||
self.assertEquals(s2.barcode, data[0]['barcode'])
|
||||
|
||||
last_modified_arg = d2.isoformat()
|
||||
rv = self.app.get(f'/v1.0/sample?last_modified={last_modified_arg}', content_type="application/json",
|
||||
headers={'X-CR-API-KEY': app.config.get('API_TOKEN')})
|
||||
data = json.loads(rv.get_data(as_text=True))
|
||||
self.assertEquals(0, len(data))
|
||||
|
Loading…
x
Reference in New Issue
Block a user