mirror of
https://github.com/sartography/uva-covid19-testing-communicator.git
synced 2025-02-23 12:28:26 +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: []
|
- ApiKeyAuth: []
|
||||||
parameters:
|
parameters:
|
||||||
- in: query
|
- in: query
|
||||||
name: bar_code
|
name: last_modified
|
||||||
required: false
|
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:
|
schema:
|
||||||
type: string
|
type: string
|
||||||
responses:
|
responses:
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import smtplib
|
import smtplib
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from communicator import db, app, executor
|
from communicator import db, app, executor
|
||||||
from communicator.models import Sample
|
from communicator.models import Sample
|
||||||
@ -35,20 +36,16 @@ def add_sample(body):
|
|||||||
SampleService().add_or_update_records([sample])
|
SampleService().add_or_update_records([sample])
|
||||||
|
|
||||||
|
|
||||||
def get_samples(bar_code=None):
|
def get_samples(last_modified=None):
|
||||||
query = db.session.query(Sample)
|
query = db.session.query(Sample)
|
||||||
if bar_code:
|
if last_modified:
|
||||||
last_sample = db.session.query(Sample).filter(Sample.barcode == bar_code).first()
|
lm_date = datetime.fromisoformat(last_modified)
|
||||||
if not last_sample:
|
query = query.filter(Sample.last_modified > lm_date)
|
||||||
app.logger.error(f'Someone queried for a barcode that does not exist: {bar_code} ', exc_info=True)
|
samples = query.order_by(Sample.last_modified).limit(200).all()
|
||||||
raise Exception("No such bar code.")
|
|
||||||
query = query.filter(Sample.date > last_sample.date)
|
|
||||||
samples = query.order_by(Sample.date).limit(200).all()
|
|
||||||
response = SampleSchema(many=True).dump(samples)
|
response = SampleSchema(many=True).dump(samples)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def clear_samples():
|
def clear_samples():
|
||||||
db.session.query(Notification).delete()
|
db.session.query(Notification).delete()
|
||||||
db.session.query(Sample).delete()
|
db.session.query(Sample).delete()
|
||||||
|
@ -128,18 +128,20 @@ class TestSampleEndpoint(BaseTest):
|
|||||||
|
|
||||||
print(data)
|
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')
|
d1 = datetime.strptime(d1_str, '%Y%m%d%H%M')
|
||||||
s1_bar_code = '000000111-'+ d1_str +'-4321'
|
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')
|
d2 = datetime.strptime(d2_str, '%Y%m%d%H%M')
|
||||||
s2_bar_code = '000000111-'+ d2_str +'-4321'
|
s2_bar_code = '000000111-'+ d2_str +'-4321'
|
||||||
|
|
||||||
s1 = Sample(barcode=s1_bar_code,
|
s1 = Sample(barcode=s1_bar_code,
|
||||||
location="4321",
|
location="4321",
|
||||||
date=d1,
|
date= datetime.now(),
|
||||||
|
last_modified=d1,
|
||||||
student_id="000000111",
|
student_id="000000111",
|
||||||
email="daniel.h.funk@gmail.com",
|
email="daniel.h.funk@gmail.com",
|
||||||
result_code="12345",
|
result_code="12345",
|
||||||
@ -148,7 +150,8 @@ class TestSampleEndpoint(BaseTest):
|
|||||||
text_notified=True)
|
text_notified=True)
|
||||||
s2 = Sample(barcode=s2_bar_code,
|
s2 = Sample(barcode=s2_bar_code,
|
||||||
location="4321",
|
location="4321",
|
||||||
date=d2,
|
date= datetime.now(),
|
||||||
|
last_modified=d2,
|
||||||
student_id="000000112",
|
student_id="000000112",
|
||||||
email="dan@gmail.com",
|
email="dan@gmail.com",
|
||||||
result_code="12345",
|
result_code="12345",
|
||||||
@ -158,9 +161,20 @@ class TestSampleEndpoint(BaseTest):
|
|||||||
db.session.add(s1)
|
db.session.add(s1)
|
||||||
db.session.add(s2)
|
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')})
|
headers={'X-CR-API-KEY': app.config.get('API_TOKEN')})
|
||||||
data = json.loads(rv.get_data(as_text=True))
|
data = json.loads(rv.get_data(as_text=True))
|
||||||
self.assertEqual(1, len(data))
|
self.assertEquals(2, len(data))
|
||||||
self.assertEqual(s2_bar_code, data[0]["barcode"])
|
|
||||||
print(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