Add a field to sample that contains the moment the sample was added to the database, and make it possible to retrieve records on this date.

This commit is contained in:
Dan 2021-01-19 15:47:41 -05:00
parent 7dad879c13
commit b3e4bfb3f5
4 changed files with 55 additions and 1 deletions

View File

@ -167,6 +167,12 @@ paths:
description: Return all samples modified AFTER the given date, provided as an ISO string (ex '2021-01-01T01:01:00')
schema:
type: string
- in: query
name: created_on
required: false
description: Return all samples added to the database AFTER the given date, provided as an ISO string (ex '2021-01-01T01:01:00')
schema:
type: string
responses:
'200':
description: a list of samples

View File

@ -36,11 +36,14 @@ def add_sample(body):
SampleService().add_or_update_records([sample])
def get_samples(last_modified=None):
def get_samples(last_modified=None, created_on=None):
query = db.session.query(Sample)
if last_modified:
lm_date = datetime.fromisoformat(last_modified)
query = query.filter(Sample.last_modified > lm_date)
if created_on:
co_date = datetime.fromisoformat(created_on)
query = query.filter(Sample.created_on > co_date)
samples = query.order_by(Sample.last_modified).limit(200).all()
response = SampleSchema(many=True).dump(samples)
return response

View File

@ -13,6 +13,7 @@ class Sample(db.Model):
student_id = db.Column(db.Integer)
computing_id = db.Column(db.String)
date = db.Column(db.DateTime)
created_on = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
last_modified = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.now, nullable=False)
location = db.Column(db.Integer)
station = db.Column(db.Integer)

View File

@ -178,3 +178,47 @@ class TestSampleEndpoint(BaseTest):
headers={'X-CR-API-KEY': app.config.get('API_TOKEN')})
data = json.loads(rv.get_data(as_text=True))
self.assertEqual(0, len(data))
def test_get_all_samples_by_created_on(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 = '202101010101' # Jan 1st 2021
d2 = datetime.strptime(d2_str, '%Y%m%d%H%M')
s2_bar_code = '000000111-' + d2_str + '-4321'
d3_str = '202101010101' # Jan 5th 2021
d3 = datetime.strptime(d3_str, '%Y%m%d%H%M')
s1 = Sample(barcode=s1_bar_code,
location="4321",
date=datetime.now(),
created_on=d1,
last_modified=d3, # Note Modified date is later than created date.
student_id="000000111",
email="daniel.h.funk@gmail.com",
result_code="12345",
ivy_file="xxx",
email_notified=True,
text_notified=True)
s2 = Sample(barcode=s2_bar_code,
location="4321",
date=datetime.now(),
created_on=d2,
last_modified=d2,
student_id="000000112",
email="dan@gmail.com",
result_code="12345",
ivy_file="yyy",
email_notified=False,
text_notified=False)
db.session.add(s1)
db.session.add(s2)
created_on_arg = d1.isoformat()
rv = self.app.get(f'/v1.0/sample?created_on={created_on_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.assertEqual(1, len(data)) # Even through s1 was modified on the 4th, it isn't returned.
self.assertEqual(s2.barcode, data[0]['barcode'])