예제 #1
0
    def serialize_data(self):
        """Serializes appointment data. Returns list of AppointmentData objects."""
        LOGGER.info('------------------------------')
        LOGGER.info('|      Serialize Data        |')
        LOGGER.info('------------------------------')
        
        self.serialized_appointments = []
        i = 1
        total = len(self.appointments_data)
        LOGGER.info('Serializing appointments data. Total count: %i', total)
        for appt in self.appointments_data:
            LOGGER.info('Serializing appointment (%i of %i)', i, total)
            i = i + 1
            appointment = AppointmentData()
            LOGGER.debug('\tAppointment Id: %s - Raw Data: \n%s', appt.get('altAppointmentId'), ET.tostring(appt))
            LOGGER.info('\tAlt appointment id: %s', appt.get('altAppointmentId'))
            
            # Customer ID
            appointment.customer_id = self.customer_id
            
            # ================
            # Appointment data
            # ================
            appointment.altAppointmentId = appt.get('altAppointmentId')
            appt_date_time = appt.get('apptDateTime')
            if (appt_date_time):
                date_time = datetime.strptime(appt_date_time, "%Y-%m-%dT%H:%M:%S") # Convert string(2012-09-26T10:00:00) to datetime object
                appointment.apptDate = date_time.strftime("%Y-%m-%d")
                appointment.apptTime = date_time.strftime("%Y-%m-%d %H:%M:%S")
            
            appointment.apptTypeName = appt.get('apptTypeName').title()
            #TODO: confirm if apptType="RefReason" can be used
            appointment.altApptTypeId = appt.get('apptTypeName').title()
            (status_id, conformation_id, appt_kept, reschdeuled) = AMDStatusLookup.get_appointment_status_info(appt.get('apptStatusId'))
            appointment.apptStatusId = status_id
            appointment.apptConformationId = conformation_id
            appointment.apptKept = appt_kept
            appointment.reschedule = reschdeuled
            
            # =============
            # Facility data
            # =============
            appointment.altFacilityId = appt.get('altFacilityId')
            appointment.facilityName = appt.get('facilityName').title()
            #TODO: Facility id is empty for Advanced MD, confirm
            appointment.facilityAddr = ""
            
            # ============
            # Patient data
            # ============
            appointment.altPatientId = appt.get('altPatientId')
            appt_patient = appt.find('patientlist/patient')
            appointment.fname = appt_patient.get('fname').title()
            appointment.lname = appt_patient.get('lname').title()
            if (is_email_valid(appt_patient.get('email'))): # Validate email

                appointment.email = appt_patient.get('email').lower()
            if (AMDStatusLookup.is_cell_phone(appt_patient.get('otherPhoneType'))):
                appointment.cellPhone = appt_patient.get('cellPhone')
                #TODO: Handle empty workPhone and homePhone
            appointment.homePhone = appt_patient.get('homePhone')
            appointment.workPhone = appt_patient.get('workPhone')
            appointment.workPhoneExt = appt_patient.get('workPhoneExt')
            appointment.address = (appt_patient.get('address1') + ' ' + appt_patient.get('address2')).title()
            appointment.city = appt_patient.get('city').title()
            appointment.state = appt_patient.get('state').upper()
            appointment.zip = appt_patient.get('zip')
            appointment.language = appt_patient.get('language').strip()
            if appointment.language in NULLS:
                appointment.language = "ENG"
            if(appt_patient.get('dateOfBirth')):
                dob = datetime.strptime(appt_patient.get('dateOfBirth'), "%Y-%m-%dT%H:%M:%S")
                appointment.dateOfBirth = dob.strftime("%Y-%m-%d")
             
            # ==============   
            # Guarantor data
            # ==============
            appointment.guarantorID = appt_patient.get('altGuarantorId')
            appointment.gFname = appt_patient.get('rpFname').title()
            appointment.gLname = appt_patient.get('rpLname').title()
            appointment.gAddress = (appt_patient.get('rpAddress1') + ' ' + appt_patient.get('rpAddress2')).title()
            appointment.gZip = appt_patient.get('rpZip')
            appointment.gCity = appt_patient.get('rpCity').title()
            appointment.gState = appt_patient.get('rpState').upper()
            appointment.gWorkPhone = appt_patient.get('rpWorkPhone')
            appointment.gHomePhone = appt_patient.get('rpHomePhone')
            if (AMDStatusLookup.is_cell_phone(appt_patient.get('rpOtherPhoneType'))):
                appointment.gCellPhone = appt_patient.get('rpCellPhone')
                #TODO: Handle empty workPhone and homePhone
            if (is_email_valid(appt_patient.get('rpEmail'))): # Validate email
                appointment.gEmail = appt_patient.get('rpEmail').lower()
            if(appt_patient.get('rpDob')): # "1945-11-14T00:00:00"
                dob = datetime.strptime(appt_patient.get('rpDob'), "%Y-%m-%dT%H:%M:%S")
                appointment.gDob = dob.strftime("%Y-%m-%d")
            
            # ===========
            # Doctor data
            # ===========
            appointment.altDoctorId = appt_patient.get('altDoctorId')
            appointment.doctorName = (appt_patient.get('drFname') + ' ' + appt_patient.get('drLname')).title()
            # Get doctor titled name
            appointment.doctorName = AMDStatusLookup.get_dr_titled_name(appt_patient.get('drTitle'), appointment.doctorName)
            
            appointment.normalize()
            LOGGER.debug('Appointment ID: %s, serialized result:\n%s', appt.get('altAppointmentId'), json.dumps(appointment.__dict__))
            self.serialized_appointments.append(appointment)
            
        return self.serialized_appointments
    def serialize_data(self):
        """
            Mapping data from row data to appointment class
            
        """
        LOGGER.info("------------------------------")
        LOGGER.info("|      Serialize Data        |")
        LOGGER.info("------------------------------")
        self.serialized_appointments = []
        if self.appointments_data:
            number = 1
            total_count = len(self.appointments_data["SitesScheduledAppointmentsGetResult"]["ScheduledAppointments"])
            LOGGER.info("Serializing appointments data. Total count: %s" % (total_count))
            for appt in self.appointments_data["SitesScheduledAppointmentsGetResult"]["ScheduledAppointments"]:
                LOGGER.debug("Serializing appointment (%s of %s)" % (number, total_count))
                number += 1
                LOGGER.debug("Appointment Id: %s - Raw Data: \n%s" % (appt["AppointmentID"], appt))
                appointment = AppointmentData()

                # Customer ID
                appointment.customer_id = self.customer_id

                # Appointment data
                appointment.altAppointmentId = appt["AppointmentID"]
                if appt["ScheduleDate"]:
                    appt_date = time.strptime(appt["ScheduleDate"], "%m/%d/%Y")
                    appointment.apptDate = time.strftime("%Y-%m-%d", appt_date)
                if appt["StartTime"]:
                    appt_time = time.strptime(appt["ScheduleDate"] + appt["StartTime"], "%m/%d/%Y%I:%M:%S %p")
                    appointment.apptTime = time.strftime("%Y-%m-%d %H:%M:%S", appt_time)
                appointment.apptTypeName = appt["Type"]["Name"]
                appointment.altApptTypeId = appt["Type"]["AppointmentTypeID"]

                # Patient data
                appointment.altPatientId = appt["Patient"]["PatientID"]
                if appt["Patient"]["FullName"] not in NULLS:
                    name = appt["Patient"]["FullName"].split(",")
                    appointment.fname = name[1]
                    appointment.lname = name[0]
                elif appt["Patient"]["Firstname"] not in NULLS:
                    appointment.fname = appt["Patient"]["Firstname"]
                elif appt["Patient"]["LastName"] not in NULLS:
                    appointment.lname = appt["Patient"]["LastName"]
                if appt["Patient"]["EmailAddress1"] not in NULLS:
                    appointment.email = appt["Patient"]["EmailAddress1"]
                elif appt["Patient"]["EmailAddress2"] not in NULLS:
                    appointment.email = appt["Patient"]["EmailAddress2"]
                # Patient's email validating
                if not is_email_valid(appointment.email):
                    appointment.email = ""
                # Patient's cell phone is added
                if appt["Patient"]["PrimaryPhoneNumber"] not in NULLS:
                    appointment.cellPhone = appt["Patient"]["PrimaryPhoneNumber"]
                elif appt["Patient"]["CellPhoneNumber1"] not in NULLS:
                    appointment.cellPhone = appt["Patient"]["CellPhoneNumber1"]
                elif appt["Patient"]["CellPhoneNumber2"] not in NULLS:
                    appointment.cellPhone = appt["Patient"]["CellPhoneNumber2"]
                # Patient's home phone is added
                if appt["Patient"]["PhoneNumber1"] not in NULLS:
                    appointment.homePhone = appt["Patient"]["PhoneNumber1"]
                elif appt["Patient"]["PhoneNumber2"] not in NULLS:
                    appointment.homePhone = appt["Patient"]["PhoneNumber2"]
                # Patient's work phone is added
                if appt["Patient"]["PrimaryWorkPhone"] not in NULLS:
                    appointment.workPhone = appt["Patient"]["PrimaryWorkPhone"]
                appointment.pagerNo = appt["Patient"]["PagerNumber"]
                # Patient's Address mapping.
                if appt["Patient"]["ResidentialAddress"] not in NULLS:
                    if appt["Patient"]["ResidentialAddress"]["AddressLine1"] not in NULLS:
                        appointment.address = appt["Patient"]["ResidentialAddress"]["AddressLine1"]
                    elif appt["Patient"]["ResidentialAddress"]["AddressLine2"] not in NULLS:
                        appointment.address += " " + appt["Patient"]["ResidentialAddress"]["AddressLine2"]
                    if appt["Patient"]["ResidentialAddress"]["City"] not in NULLS:
                        appointment.city = appt["Patient"]["ResidentialAddress"]["City"]
                    if appt["Patient"]["ResidentialAddress"]["State"] not in NULLS:
                        appointment.state = appt["Patient"]["ResidentialAddress"]["State"]
                    if appt["Patient"]["ResidentialAddress"]["PostalCode"] not in NULLS:
                        appointment.zip = appt["Patient"]["ResidentialAddress"]["PostalCode"]
                if appt["Patient"]["PrimaryLanguage"] not in NULLS:
                    appointment.language = appt["Patient"]["PrimaryLanguage"]
                if appt["Patient"]["DateOfBirth"] not in NULLS:
                    dob = re.findall(r"\/\w+\S(\-?\d+)", appt["Patient"]["DateOfBirth"])[
                        0
                    ]  # trying to parse '/Date(223432432432-4400)/ and /Date(-223432432432-4400)/'
                    if dob[0] == "-":
                        dob = dob[1:]
                    dob = time.gmtime(long(dob) / 1000)
                    appointment.dateOfBirth = time.strftime("%Y-%m-%d", dob)
                appointment.guarantorID = appt["Patient"]["GuarantorID"]

                # Doctor data
                appointment.altDoctorId = appt["Resource"]["OwnerCareProviderID"]
                if appt["Resource"]["Name"]:
                    appointment.doctorName = appt["Resource"]["Name"]
                    if appointment.doctorName.find("Dr") == -1:
                        appointment.doctorName = "Dr. " + appointment.doctorName
                else:
                    appointment.doctorName = "the staff"

                # Facility data
                appointment.altFacilityId = appt["ScheduleLocation"]["ScheduleLocationID"]
                appointment.facilityName = appt["ScheduleLocation"]["Name"]
                appointment.facilityAddr = appt["ScheduleLocation"]["CareProviderLocation"]["Address"]

                appointment.normalize()
                LOGGER.debug(
                    "Appointment ID: %s, serialized result: \n%s" % (appointment.altAppointmentId, appointment.__dict__)
                )
                self.serialized_appointments.append(appointment)

        return self.serialized_appointments