2020-10-15 17:21:20 -04:00
|
|
|
from communicator import db, app
|
2020-09-14 13:09:07 -04:00
|
|
|
from communicator.models.sample import Sample
|
2020-12-30 09:06:46 -05:00
|
|
|
import random
|
2020-09-14 13:09:07 -04:00
|
|
|
|
|
|
|
class SampleService(object):
|
|
|
|
"""Handles the collection and syncing of data from various sources. """
|
|
|
|
|
|
|
|
def add_or_update_records(self, samples):
|
|
|
|
for sample in samples:
|
|
|
|
existing = db.session.query(Sample).filter(Sample.barcode == sample.barcode).first()
|
|
|
|
if existing is not None:
|
|
|
|
existing.merge(sample)
|
2020-10-15 16:10:48 -04:00
|
|
|
db.session.add(existing)
|
2020-09-14 13:09:07 -04:00
|
|
|
else:
|
|
|
|
db.session.add(sample)
|
|
|
|
db.session.commit()
|
2020-10-07 12:58:44 -04:00
|
|
|
|
2020-12-30 09:06:46 -05:00
|
|
|
def split_location_column(self):
|
|
|
|
samples = db.session.query(Sample).filter(Sample.station == None).all()
|
|
|
|
for sample in samples:
|
|
|
|
loc_code = str(sample.location)
|
|
|
|
if len(loc_code) == 4:
|
|
|
|
location, station = int(loc_code[:2]), int(loc_code[2:])
|
|
|
|
sample.location, sample.station = location, station
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-10-07 12:58:44 -04:00
|
|
|
def merge_similar_records(self):
|
2020-12-30 09:06:46 -05:00
|
|
|
""" We have samples that are duplicates of each other because of the way the data was coming in
|
2020-10-07 12:58:44 -04:00
|
|
|
earlier on. This is a onetime fix that will compare all records based on the studient id, location
|
|
|
|
and date, and merge them together using the new and correct bar code."""
|
|
|
|
|
|
|
|
# Get all samples that do not contain an email (these were added via the api call)
|
|
|
|
samples = db.session.query(Sample).filter(Sample.email == None).all()
|
|
|
|
for sample in samples:
|
|
|
|
sample2 = db.session.query(Sample).\
|
|
|
|
filter(Sample.email != None).\
|
|
|
|
filter(Sample.student_id == sample.student_id).\
|
|
|
|
filter(Sample.date == sample.date).\
|
|
|
|
filter(Sample.location == sample.location).\
|
|
|
|
first()
|
|
|
|
if sample2:
|
|
|
|
sample.merge(sample2)
|
2020-10-15 15:29:40 -04:00
|
|
|
# Move notifications over as well.
|
|
|
|
notifications = sample2.notifications
|
|
|
|
sample.notifications = notifications
|
|
|
|
sample2.notifications = []
|
2020-10-07 12:58:44 -04:00
|
|
|
db.session.add(sample)
|
|
|
|
db.session.delete(sample2)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|