2020-09-17 15:16:41 +00:00
|
|
|
from communicator import db, app
|
|
|
|
from communicator.models import Sample
|
2020-09-25 15:56:08 +00:00
|
|
|
from communicator.models.invitation import Invitation
|
2020-09-25 15:33:20 +00:00
|
|
|
from communicator.models.notification import Notification, EMAIL_TYPE, TEXT_TYPE
|
2020-09-17 15:16:41 +00:00
|
|
|
from communicator.services.ivy_service import IvyService
|
|
|
|
from communicator.services.notification_service import NotificationService
|
|
|
|
from communicator.services.sample_service import SampleService
|
2020-09-25 15:51:17 +00:00
|
|
|
from time import sleep
|
2020-09-17 15:16:41 +00:00
|
|
|
|
2020-09-10 15:28:58 +00:00
|
|
|
|
|
|
|
def status():
|
|
|
|
return {"status":"good"}
|
2020-09-17 15:16:41 +00:00
|
|
|
|
2020-09-23 16:43:58 +00:00
|
|
|
def add_sample(body):
|
|
|
|
sample = Sample(barcode=body['barcode'],
|
|
|
|
student_id=body['student_id'],
|
|
|
|
date=body['date'],
|
|
|
|
location=body['location'])
|
2020-09-24 20:51:49 +00:00
|
|
|
SampleService().add_or_update_records([sample])
|
2020-09-17 15:16:41 +00:00
|
|
|
|
2020-09-23 19:24:09 +00:00
|
|
|
def clear_samples():
|
2020-09-25 15:33:20 +00:00
|
|
|
db.session.query(Notification).delete()
|
2020-09-23 19:24:09 +00:00
|
|
|
db.session.query(Sample).delete()
|
2020-09-25 15:56:08 +00:00
|
|
|
db.session.query(Invitation).delete()
|
2020-09-23 19:24:09 +00:00
|
|
|
db.session.commit()
|
|
|
|
|
2020-09-25 15:33:20 +00:00
|
|
|
|
2020-09-24 16:25:18 +00:00
|
|
|
def update_and_notify():
|
2020-09-24 20:51:49 +00:00
|
|
|
update_data()
|
|
|
|
notify_by_email()
|
|
|
|
notify_by_text()
|
2020-09-24 16:25:18 +00:00
|
|
|
|
2020-09-17 15:16:41 +00:00
|
|
|
def update_data():
|
2020-09-23 19:24:09 +00:00
|
|
|
"""Updates the database based on local files placed by IVY. No longer attempts
|
|
|
|
to pull files from the Firebase service."""
|
2020-09-17 15:16:41 +00:00
|
|
|
ivy_service = IvyService()
|
2020-09-30 19:35:59 +00:00
|
|
|
ivy_service.request_transfer()
|
2020-09-23 19:24:09 +00:00
|
|
|
samples = ivy_service.load_directory()
|
2020-09-17 15:16:41 +00:00
|
|
|
SampleService().add_or_update_records(samples)
|
2020-09-22 20:22:15 +00:00
|
|
|
db.session.commit()
|
2020-09-17 15:16:41 +00:00
|
|
|
|
2020-10-07 16:58:44 +00:00
|
|
|
def merge_similar_records():
|
|
|
|
sample_service = SampleService()
|
|
|
|
sample_service.merge_similar_records()
|
|
|
|
|
2020-09-25 15:33:20 +00:00
|
|
|
|
2020-09-17 15:16:41 +00:00
|
|
|
def notify_by_email():
|
|
|
|
"""Sends out notifications via email"""
|
|
|
|
samples = db.session.query(Sample)\
|
|
|
|
.filter(Sample.result_code != None)\
|
|
|
|
.filter(Sample.email_notified == False).all()
|
2020-09-25 15:33:20 +00:00
|
|
|
with NotificationService(app) as notifier:
|
|
|
|
for sample in samples:
|
|
|
|
last_failure = sample.last_failure_by_type(EMAIL_TYPE)
|
|
|
|
if last_failure: continue
|
|
|
|
try:
|
|
|
|
notifier.send_result_email(sample)
|
|
|
|
sample.email_notified = True
|
|
|
|
db.session.add(Notification(type=EMAIL_TYPE, sample=sample, successful=True))
|
|
|
|
except Exception as e:
|
|
|
|
db.session.add(Notification(type=EMAIL_TYPE, sample=sample, successful=False,
|
|
|
|
error_message=str(e)))
|
2020-09-30 19:39:21 +00:00
|
|
|
db.session.commit()
|
2020-09-25 15:51:17 +00:00
|
|
|
sleep(0.5)
|
2020-09-17 15:16:41 +00:00
|
|
|
|
2020-09-30 19:39:21 +00:00
|
|
|
|
2020-09-24 20:51:49 +00:00
|
|
|
def notify_by_text():
|
|
|
|
"""Sends out notifications via SMS Message, but only at reasonable times of day"""
|
2020-09-25 15:33:20 +00:00
|
|
|
with NotificationService(app) as notifier:
|
|
|
|
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:
|
|
|
|
last_failure = sample.last_failure_by_type(TEXT_TYPE)
|
|
|
|
if last_failure: continue
|
|
|
|
try:
|
|
|
|
notifier.send_result_sms(sample)
|
|
|
|
sample.text_notified = True
|
|
|
|
db.session.add(Notification(type=TEXT_TYPE, sample=sample, successful=True))
|
|
|
|
except Exception as e:
|
|
|
|
db.session.add(Notification(type=TEXT_TYPE, sample=sample, successful=False,
|
|
|
|
error_message=str(e)))
|
2020-09-30 19:39:21 +00:00
|
|
|
db.session.commit()
|
2020-09-25 15:51:17 +00:00
|
|
|
sleep(0.5)
|
2020-09-17 15:16:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|