def add_checkin(nhi, data): c = CheckIn(**data) c.patient_nhi = nhi db.add(c) db.commit() if c.patient.latest_checkin_time is None or c.checkin_time > c.patient.latest_checkin_time: c.patient.latest_checkin_time = c.checkin_time db.commit() return c
def populate_database(num_patients, min_checkins, max_checkins): """ Generates a number of Patients and a number of CheckIns per patient and stores them in the database. Arguments num_patients -- the number of patients to generate min_checkins -- the minimum number of CheckIns to generate per Patient max_checkins -- the maximum number of CheckIns to generate per Patient """ departments = [ Department(department_name="Cardiology"), Department(department_name="Emergency"), Department(department_name="Gynecology"), Department(department_name="Pediatrics"), Department(department_name="Obstetrics"), Department(department_name="Oncology"), Department(department_name="Orthopedics"), Department(department_name="Neurology") ] for i in xrange(num_patients): patient = Patient(**generate_patient()) patient.departments.append(choice(departments)) db.add(patient) for j in xrange(randrange(min_checkins, max_checkins)): checkin = CheckIn(**generate_checkin()) checkin.patient_nhi = patient.nhi lci = patient.latest_checkin_time vid = checkin.checkin_time lci = vid if lci is None or vid > lci else lci patient.latest_checkin_time = lci db.add(checkin) for k in xrange(randrange(0, 3)): appointment = Appointment(**generate_appointment()) appointment.patient_nhi = patient.nhi db.add(appointment) db.commit()