def all_changes(pt, changes): LOGGER.debug("writing_changes to patient - %s" % str(changes)) if changes == []: LOGGER.warning( "write changes called, but no changes for patient %d!" % ( pt.serialno) ) return True success = True # set up some booleans to prevent multiple updates of the same data # example exemption AND exemption text have changed.. exemptionsHandled = False if pt.HIDDENNOTES != []: # hidden notes is # treatment codes... money, printing etc.. LOGGER.debug("saving hiddennotes") toNotes(pt.serialno, pt.HIDDENNOTES) pt.clearHiddenNotes() sqlcommands = {} patchanges, patvalues = [], [] static_changes, static_values = [], [] date_changes, date_values = [], [] nhs_changes, nhs_values = [], [] trtchanges, trtvalues = "", [] # money handled slightly differently. more complex query. money_changes, money_values = [], [] for change in changes: if change == "courseno": pass # these values should never get munged. elif change in patient_class.money_table_atts: money_changes.append(change) money_values.append(pt.__dict__[change]) elif change in patient_class.patientTableAtts: # patchanges += '%s = %%s,' % change patchanges.append(change) patvalues.append(pt.__dict__[change]) elif change in patient_class.date_table_atts: date_changes.append(change) date_values.append(pt.__dict__[change]) elif change in patient_class.static_table_atts: static_changes.append(change.rstrip("st")) static_values.append(pt.__dict__[change]) elif change in patient_class.nhs_table_atts: nhs_changes.append(change) nhs_values.append(pt.__dict__[change]) elif (change in patient_class.exemptionTableAtts and not exemptionsHandled): values = (pt.serialno, pt.exemption, pt.exempttext) sqlcommands['exemptions'] = ((EXMPT_INS_QUERY, values),) exemptionsHandled = True elif change == "bpe": values = (pt.serialno, pt.bpe[-1][0], pt.bpe[-1][1], pt.bpe[-1][1] ) sqlcommands['bpe'] = ((BPE_INS_QUERY, values),) elif change == "synopsis": values = (pt.serialno, pt.synopsis, localsettings.operator) sqlcommands['clinical_memos'] = ((SYNOPSIS_INS_QUERY, values),) elif change == "treatment_course": # patient.CURRTRT_ATTS: for trt_att in CURRTRT_ATTS: value = pt.treatment_course.__dict__[trt_att] existing = pt.dbstate.treatment_course.__dict__[trt_att] if pt.has_new_course or value != existing: trtchanges += '%s = %%s ,' % trt_att trtvalues.append(value) elif change == "appt_prefs": pt.appt_prefs.commit_changes() elif change == "estimates": pass # dealt with below if patchanges: query = "update new_patients SET %s where serialno=%%s" % \ ", ".join(["%s = %%s" % change for change in patchanges]) patvalues.append(pt.serialno) sqlcommands['patients'] = ((query, patvalues),) if static_changes: LOGGER.warning( "applying static_changes %s values %s", static_changes, static_values) query = '''insert into static_chart (pt_sno, %s) values (%%s, %s) on duplicate key update %s''' % ( ", ".join(static_changes), ", ".join(("%s",) * len(static_changes)), ", ".join(["%s = %%s" % change for change in static_changes]) ) values = [pt.serialno] + static_values * 2 sqlcommands['static'] = ((query, values),) if nhs_changes: LOGGER.warning( "applying nhs_changes %s values %s", nhs_changes, nhs_values) query = '''insert into patient_nhs (pt_sno, %s) values (%%s, %s) on duplicate key update %s''' % ( ", ".join(nhs_changes), ", ".join(("%s",) * len(nhs_changes)), ", ".join(["%s = %%s" % change for change in nhs_changes]) ) values = [pt.serialno] + nhs_values * 2 sqlcommands['nhs'] = ((query, values),) if date_changes: LOGGER.warning( "applying date_changes %s values %s", date_changes, date_values) query = '''insert into patient_dates (pt_sno, %s) values (%%s, %s) on duplicate key update %s''' % ( ", ".join(date_changes), ", ".join(("%s",) * len(date_changes)), ", ".join(["%s = %%s" % change for change in date_changes]) ) values = [pt.serialno] + date_values * 2 sqlcommands['patient_dates'] = ((query, values),) if money_changes: update_money_values = [] update_query = "update " for i, change in enumerate(money_changes): if change in ("money0", "money1"): diff = pt.__dict__[change] - pt.dbstate.__dict__[change] update_money_values.append(diff) update_query += "%s=%s +%%s, " % (change, change) else: update_money_values.append(money_values[i]) update_query += "%s=%%s, " % change LOGGER.warning( "applying money_changes %s values %s addition_values %s", money_changes, money_values, update_money_values) query = '''insert into patient_money (pt_sno, %s) values (%%s, %s) on duplicate key %s''' % ( ", ".join(money_changes), ", ".join(("%s",) * len(money_changes)), update_query.rstrip(", ") ) values = [pt.serialno] + money_values + update_money_values LOGGER.debug(query.replace("\n", " ")) LOGGER.debug(values) sqlcommands['patient_money'] = ((query, values),) if trtchanges != "": trtvalues.append(pt.serialno) trtvalues.append(pt.treatment_course.courseno) query = UPDATE_CURRTTMT2_QUERY % (trtchanges.strip(",")) sqlcommands['currtrtmt'] = ((query, trtvalues),) try: db = connect() db.autocommit = False if sqlcommands != {}: LOGGER.debug(sqlcommands) cursor = db.cursor() tables = list(sqlcommands.keys()) for table in tables: for query, values in sqlcommands[table]: try: cursor.execute(query, values) except Exception as exc: LOGGER.error("error executing query %s" % query) raise exc cursor.close() if "estimates" in changes: estimates.apply_changes(pt, pt.dbstate.estimates, pt.estimates) db.commit() except Exception as exc: LOGGER.exception("rolling back database") db.rollback() success = False raise exc finally: db.autocommit = True return success
def all_changes(pt, changes): LOGGER.debug("writing_changes to patient - %s" % str(changes)) if changes == []: LOGGER.warning("write changes called, but no changes for patient %d!" % (pt.serialno)) return True success = True # set up some booleans to prevent multiple updates of the same data # example exemption AND exemption text have changed.. exemptionsHandled = False if pt.HIDDENNOTES != []: # hidden notes is # treatment codes... money, printing etc.. LOGGER.debug("saving hiddennotes") toNotes(pt.serialno, pt.HIDDENNOTES) pt.clearHiddenNotes() sqlcommands = {} patchanges, patvalues = [], [] static_changes, static_values = [], [] date_changes, date_values = [], [] nhs_changes, nhs_values = [], [] trtchanges, trtvalues = "", [] # money handled slightly differently. more complex query. money_changes, money_values = [], [] for change in changes: if change == "courseno": pass # these values should never get munged. elif change in patient_class.money_table_atts: money_changes.append(change) money_values.append(pt.__dict__[change]) elif change in patient_class.patientTableAtts: # patchanges += '%s = %%s,' % change patchanges.append(change) patvalues.append(pt.__dict__[change]) elif change in patient_class.date_table_atts: date_changes.append(change) date_values.append(pt.__dict__[change]) elif change in patient_class.static_table_atts: static_changes.append(change.rstrip("st")) static_values.append(pt.__dict__[change]) elif change in patient_class.nhs_table_atts: nhs_changes.append(change) nhs_values.append(pt.__dict__[change]) elif (change in patient_class.exemptionTableAtts and not exemptionsHandled): values = (pt.serialno, pt.exemption, pt.exempttext) sqlcommands['exemptions'] = ((EXMPT_INS_QUERY, values), ) exemptionsHandled = True elif change == "bpe": values = (pt.serialno, pt.bpe[-1][0], pt.bpe[-1][1], pt.bpe[-1][1]) sqlcommands['bpe'] = ((BPE_INS_QUERY, values), ) elif change == "synopsis": values = (pt.serialno, pt.synopsis, localsettings.operator) sqlcommands['clinical_memos'] = ((SYNOPSIS_INS_QUERY, values), ) elif change == "treatment_course": # patient.CURRTRT_ATTS: for trt_att in CURRTRT_ATTS: value = pt.treatment_course.__dict__[trt_att] existing = pt.dbstate.treatment_course.__dict__[trt_att] if pt.has_new_course or value != existing: trtchanges += '%s = %%s ,' % trt_att trtvalues.append(value) elif change == "appt_prefs": pt.appt_prefs.commit_changes() elif change == "estimates": pass # dealt with below if patchanges: query = "update new_patients SET %s where serialno=%%s" % \ ", ".join(["%s = %%s" % change for change in patchanges]) patvalues.append(pt.serialno) sqlcommands['patients'] = ((query, patvalues), ) if static_changes: LOGGER.warning("applying static_changes %s values %s", static_changes, static_values) query = '''insert into static_chart (pt_sno, %s) values (%%s, %s) on duplicate key update %s''' % (", ".join(static_changes), ", ".join( ("%s", ) * len(static_changes)), ", ".join( ["%s = %%s" % change for change in static_changes])) values = [pt.serialno] + static_values * 2 sqlcommands['static'] = ((query, values), ) if nhs_changes: LOGGER.warning("applying nhs_changes %s values %s", nhs_changes, nhs_values) query = '''insert into patient_nhs (pt_sno, %s) values (%%s, %s) on duplicate key update %s''' % (", ".join(nhs_changes), ", ".join( ("%s", ) * len(nhs_changes)), ", ".join( ["%s = %%s" % change for change in nhs_changes])) values = [pt.serialno] + nhs_values * 2 sqlcommands['nhs'] = ((query, values), ) if date_changes: LOGGER.warning("applying date_changes %s values %s", date_changes, date_values) query = '''insert into patient_dates (pt_sno, %s) values (%%s, %s) on duplicate key update %s''' % (", ".join(date_changes), ", ".join( ("%s", ) * len(date_changes)), ", ".join( ["%s = %%s" % change for change in date_changes])) values = [pt.serialno] + date_values * 2 sqlcommands['patient_dates'] = ((query, values), ) if money_changes: update_money_values = [] update_query = "update " for i, change in enumerate(money_changes): if change in ("money0", "money1"): diff = pt.__dict__[change] - pt.dbstate.__dict__[change] update_money_values.append(diff) update_query += "%s=%s +%%s, " % (change, change) else: update_money_values.append(money_values[i]) update_query += "%s=%%s, " % change LOGGER.warning( "applying money_changes %s values %s addition_values %s", money_changes, money_values, update_money_values) query = '''insert into patient_money (pt_sno, %s) values (%%s, %s) on duplicate key %s''' % (", ".join(money_changes), ", ".join( ("%s", ) * len(money_changes)), update_query.rstrip(", ")) values = [pt.serialno] + money_values + update_money_values LOGGER.debug(query.replace("\n", " ")) LOGGER.debug(values) sqlcommands['patient_money'] = ((query, values), ) if trtchanges != "": trtvalues.append(pt.serialno) trtvalues.append(pt.treatment_course.courseno) query = UPDATE_CURRTTMT2_QUERY % (trtchanges.strip(",")) sqlcommands['currtrtmt'] = ((query, trtvalues), ) try: db = connect() db.autocommit = False if sqlcommands != {}: LOGGER.debug(sqlcommands) cursor = db.cursor() tables = list(sqlcommands.keys()) for table in tables: for query, values in sqlcommands[table]: try: cursor.execute(query, values) except Exception as exc: LOGGER.error("error executing query %s" % query) raise exc cursor.close() if "estimates" in changes: estimates.apply_changes(pt, pt.dbstate.estimates, pt.estimates) db.commit() except Exception as exc: LOGGER.exception("rolling back database") db.rollback() success = False raise exc finally: db.autocommit = True return success
def update_db(self): estimates.apply_changes( self.patient, self.orig_ests, self.patient.estimates)
def update_db(self): estimates.apply_changes(self.patient, self.orig_ests, self.patient.estimates)