mirror of
https://github.com/sartography/uva-covid19-testing-communicator.git
synced 2025-02-23 12:28:26 +00:00
All the pieces in place. Going to see if it works.
Text messages should only go out at reasonable hours. Adding the same sample a second time through the API should not create an error.
This commit is contained in:
parent
b786aba6d8
commit
fe565afda3
@ -17,15 +17,16 @@ def add_sample(body):
|
||||
student_id=body['student_id'],
|
||||
date=body['date'],
|
||||
location=body['location'])
|
||||
db.session.add(sample)
|
||||
db.session.commit()
|
||||
SampleService().add_or_update_records([sample])
|
||||
|
||||
def clear_samples():
|
||||
db.session.query(Sample).delete()
|
||||
db.session.commit()
|
||||
|
||||
def update_and_notify():
|
||||
print("updating and notifying")
|
||||
update_data()
|
||||
notify_by_email()
|
||||
notify_by_text()
|
||||
|
||||
def update_data():
|
||||
"""Updates the database based on local files placed by IVY. No longer attempts
|
||||
@ -48,7 +49,22 @@ def notify_by_email():
|
||||
except CommError as ce:
|
||||
print("Error")
|
||||
|
||||
|
||||
def notify_by_text():
|
||||
"""Sends out notifications via SMS Message, but only at reasonable times of day"""
|
||||
notifier = NotificationService(app)
|
||||
if not notifier.is_reasonable_hour_for_text_messages:
|
||||
print("Skipping text messages, it's not a good time to get one.")
|
||||
return
|
||||
|
||||
samples = db.session.query(Sample)\
|
||||
.filter(Sample.result_code != None)\
|
||||
.filter(Sample.text_notified == False).all()
|
||||
for sample in samples:
|
||||
try:
|
||||
notifier.send_result_sms(sample)
|
||||
sample.text_notified = True
|
||||
except CommError as ce:
|
||||
print("Error")
|
||||
|
||||
|
||||
|
||||
|
@ -1,11 +1,15 @@
|
||||
import smtplib
|
||||
import uuid
|
||||
from datetime import datetime, time, date
|
||||
from email.header import Header
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
import re
|
||||
|
||||
import dateutil
|
||||
import pytz
|
||||
from flask import render_template
|
||||
from pytz import timezone
|
||||
from twilio.rest import Client
|
||||
|
||||
from communicator import app, db
|
||||
@ -158,3 +162,11 @@ class NotificationService(object):
|
||||
app.logger.error('An exception happened in EmailService', exc_info=True)
|
||||
app.logger.error(str(e))
|
||||
raise CommError(5000, f"failed to send email to {', '.join(recipients)}", e)
|
||||
|
||||
def is_reasonable_hour_for_text_messages(self):
|
||||
"""Where 'reasaonable' is between 8am and 10pm. """
|
||||
tz = pytz.timezone('US/Eastern')
|
||||
now = (datetime.now(tz))
|
||||
eight_am = (datetime.now(tz).replace(hour=8, minute=0, second=0, microsecond=0))
|
||||
ten_pm = (datetime.now(tz).replace(hour=22, minute=0, second=0, microsecond=0))
|
||||
return eight_am <= now <= ten_pm
|
||||
|
@ -29,7 +29,8 @@ class SampleTable(Table):
|
||||
location = Col('Location')
|
||||
email_notified = BoolCol('Emailed?')
|
||||
text_notified = BoolCol('Texted?')
|
||||
|
||||
phone = Col('Phone')
|
||||
email = Col('Email')
|
||||
|
||||
class IvyFileTable(Table):
|
||||
def sort_url(self, col_id, reverse=False):
|
||||
|
@ -1,3 +1,7 @@
|
||||
from datetime import datetime
|
||||
|
||||
import pytz
|
||||
|
||||
from tests.base_test import BaseTest
|
||||
|
||||
|
||||
@ -16,3 +20,4 @@ class TestNotificationService(BaseTest):
|
||||
self.assertEqual(len(TEST_MESSAGES), message_count + 1)
|
||||
self.assertEqual("UVA: BE SAFE Notification", self.decode(TEST_MESSAGES[-1]['subject']))
|
||||
|
||||
|
||||
|
@ -7,12 +7,12 @@ from communicator import db
|
||||
|
||||
class TestSampleEndpoint(BaseTest):
|
||||
|
||||
def test_create_sample(self):
|
||||
sample_json = {"barcode": "000000111-202009091449-4321",
|
||||
"location": "4321",
|
||||
"date": "2020-09-09T14:49:00+0000",
|
||||
"student_id": "000000111"}
|
||||
|
||||
sample_json = {"barcode": "000000111-202009091449-4321",
|
||||
"location": "4321",
|
||||
"date": "2020-09-09T14:49:00+0000",
|
||||
"student_id": "000000111"}
|
||||
def test_create_sample(self):
|
||||
|
||||
# Test add sample
|
||||
samples = db.session.query(Sample).all()
|
||||
@ -20,8 +20,21 @@ class TestSampleEndpoint(BaseTest):
|
||||
|
||||
rv = self.app.post('/v1.0/sample',
|
||||
content_type="application/json",
|
||||
data=json.dumps(sample_json))
|
||||
data=json.dumps(self.sample_json))
|
||||
|
||||
samples = db.session.query(Sample).all()
|
||||
self.assertEquals(1, len(samples))
|
||||
|
||||
def test_create_duplicate_sample_does_not_raise_error(self):
|
||||
|
||||
# Test add sample
|
||||
samples = db.session.query(Sample).all()
|
||||
self.assertEquals(0, len(samples))
|
||||
|
||||
rv = self.app.post('/v1.0/sample',content_type="application/json", data=json.dumps(self.sample_json))
|
||||
rv = self.app.post('/v1.0/sample',content_type="application/json", data=json.dumps(self.sample_json))
|
||||
rv = self.app.post('/v1.0/sample',content_type="application/json", data=json.dumps(self.sample_json))
|
||||
rv = self.app.post('/v1.0/sample',content_type="application/json", data=json.dumps(self.sample_json))
|
||||
|
||||
samples = db.session.query(Sample).all()
|
||||
self.assertEquals(1, len(samples))
|
||||
|
Loading…
x
Reference in New Issue
Block a user