diff --git a/communicator/api.yml b/communicator/api.yml index d3b3097..26fd9a9 100644 --- a/communicator/api.yml +++ b/communicator/api.yml @@ -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 diff --git a/communicator/api/admin.py b/communicator/api/admin.py index 38d8161..45a0514 100644 --- a/communicator/api/admin.py +++ b/communicator/api/admin.py @@ -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 diff --git a/communicator/models/sample.py b/communicator/models/sample.py index 16ccad2..76a2d4e 100644 --- a/communicator/models/sample.py +++ b/communicator/models/sample.py @@ -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) diff --git a/tests/services/test_sample_endpoints.py b/tests/services/test_sample_endpoints.py index 1620b4c..21a5eb5 100644 --- a/tests/services/test_sample_endpoints.py +++ b/tests/services/test_sample_endpoints.py @@ -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'])