def treatment_plan_delete(request):
    try:
        msg = ''
        data = {}
        id = int(request.POST['id'])
        if id <= 0:
            msg = 'Illegal row id (' + str(id) + ') passed to delete routine'
            return
        print "Deleting TreatmentPlan with id = %d" % (id)
        treatmentPlan = DBSession.query(TreatmentPlan).get(id)
        DBSession.delete(treatmentPlan)
        DBSession.flush()
        # DBSession.commit() # Not needed since Pyramid uses the Zope transaction manager
        print "Delete successful"
    except exc.SQLAlchemyError as e:
        print "In treatment_plan_delete caught exception of type: "
        print type(e)
        msg = str(e)
        print msg
        DBSession.rollback()
    except Exception as e:
        print "In treatment_plan_delete caught exception of type: "
        print type(e)
        msg = str(e)
        DBSession.rollback()
    finally:
        return {'msg': msg, 'id': str(id)}
    return {'msg': msg, 'id': str(id)}
def nutritional_status_post(request):
    try:
        msg = ''
        data = {}
        id = int(request.POST['id'])
        if id > 0:
            nutritionalStatus = DBSession.query(NutritionalStatus).get(id)
        else:
            nutritionalStatus = NutritionalStatus()
        data['patient_id'] = int(request.POST['patient_id'])
        data['assessment_date'] = datetime.strptime(request.POST['assessment_date'],"%Y-%m-%d %H:%M:%S")
        data['nutritional_notes'] = request.POST['nutritional_notes']
        nutritionalStatus.setFromData(data)
        print "Updating NutritionalStatus with id = %d" % (id)
        o = DBSession.merge(nutritionalStatus)
        DBSession.flush()
        DBSession.refresh(o)
        id = o.id
        # DBSession.commit() # Not needed since Pyramid uses the Zope transaction manager
        print "Update successful"
    except exc.SQLAlchemyError as e:
        print "In nutritional_status_post caught exception of type: "
        print type(e)
        msg = str(e)
        print msg
        DBSession.rollback()
    except Exception as e:
        print "In nutritional_status_post caught exception of type: "
        print type(e)
        msg = str(e)
        DBSession.rollback()
    finally:
        return {'msg': msg, 'id': str(id)}
    return {'msg': msg, 'id': str(id)}
def treatment_plan_post(request):
    try:
        msg = ''
        data = {}
        id = int(request.POST['id'])
        if id > 0:
            treatmentPlan = DBSession.query(TreatmentPlan).get(id)
        else:
            treatmentPlan = TreatmentPlan()
        data['patient_id'] = int(request.POST['patient_id'])
        data['plan_date'] = datetime.strptime(request.POST['plan_date'],"%Y-%m-%d %H:%M:%S")
        data['plan_notes'] = request.POST['plan_notes']
        treatmentPlan.setFromData(data)
        print "Updating TreatmentPlan with id = %d" % (id)
        o = DBSession.merge(treatmentPlan)
        DBSession.flush()
        DBSession.refresh(o)
        id = o.id
        # DBSession.commit() # Not needed since Pyramid uses the Zope transaction manager
        print "Update successful"
    except exc.SQLAlchemyError as e:
        print "In treatment_plan_post caught exception of type: "
        print type(e)
        msg = str(e)
        print msg
        DBSession.rollback()
    except Exception as e:
        print "In treatment_plan_post caught exception of type: "
        print type(e)
        msg = str(e)
        DBSession.rollback()
    finally:
        return {'msg': msg, 'id': str(id)}
    return {'msg': msg, 'id': str(id)}
def braden_scores_delete(request):
    try:
        # bradenScores = DBSession.query(BradenScores).get(request.GET['id'])
        msg = ''
        data = {}
        id = int(request.POST['id'])
        if id <= 0:
            msg = 'Illegal row id (' + str(id) + ') passed to delete routine'
            return
        print "Deleting BradenScores with id = %d" % (id)
        bradenScores = DBSession.query(BradenScores).get(id)
        DBSession.delete(bradenScores)
        DBSession.flush()
        # DBSession.commit() # Not needed since Pyramid uses the Zope transaction manager
        print "Delete successful"
    except exc.SQLAlchemyError as e:
        print "In braden_scores_delete caught exception of type: "
        print type(e)
        msg = str(e)
        print msg
        DBSession.rollback()
    except Exception as e:
        print "In braden_scores_post caught exception of type: "
        print type(e)
        msg = str(e)
        DBSession.rollback()
    finally:
        return {'msg': msg, 'id': str(id)}
    return {'msg': msg, 'id': str(id)}
def patient_short_post(request):
    try:
        msg = ''
        data = {}
        patient_id = int(request.POST['patient_id'])
        if patient_id > 0:
            patientshort = DBSession.query(PatientIdentification).get(patient_id)
        else:
            raise "KeyError"
        patientshort.age = int(request.POST['age'])
        patientshort.medical_history = request.POST['medical_history']
        print "Updating PatientIdentification with patient_id = %d" % (patient_id)
        o = DBSession.merge(patientshort)
        DBSession.flush()
        DBSession.refresh(o)
        patient_id = o.patient_id
        # DBSession.commit() # Not needed since Pyramid uses the Zope transaction manager
        print "Update successful"
    except exc.SQLAlchemyError as e:
        print "In patient_short_post caught exception of type: "
        print type(e)
        msg = str(e)
        print msg
        DBSession.rollback()
    except Exception as e:
        print "In patient_short_post caught exception of type: "
        print type(e)
        msg = str(e)
        DBSession.rollback()
    finally:
        return {'msg': msg, 'patient_id': str(patient_id)}
    return {'msg': msg, 'patient_id': str(patient_id)}
def patient_identification_delete(request):
    try:
        msg = ''
        data = {}
        patient_id = int(request.POST['patient_id'])
        if id <= 0:
            msg = 'Illegal row patient_id (' + str(patient_id) + ') passed to delete routine'
            return
        print "Deleting PatientIdentification with id = %d" % (patient_id)
        patient = DBSession.query(PatientIdentification).get(patient_id)
        DBSession.delete(patient)
        DBSession.flush()
        # DBSession.commit() # Not needed since Pyramid uses the Zope transaction manager
        print "Delete successful"
    except exc.SQLAlchemyError as e:
        print "In patient_identification_delete caught exception of type: "
        print type(e)
        msg = str(e)
        print msg
        DBSession.rollback()
    except Exception as e:
        print "In patient_identification_delete caught exception of type: "
        print type(e)
        msg = str(e)
        DBSession.rollback()
    finally:
        return {'msg': msg, 'patient_id': str(patient_id)}
    return {'msg': msg, 'patient_id': str(patient_id)}
def braden_scores_post(request):
    try:
        # bradenScores = DBSession.query(BradenScores).get(request.GET['id'])
        msg = ''
        data = {}
        id = int(request.POST['id'])
        if id > 0:
            bradenScores = DBSession.query(BradenScores).get(id)
        else:
            bradenScores = BradenScores()
        data['patient_id'] = int(request.POST['patient_id'])
        data['braden_scoring_date'] = datetime.strptime(request.POST['braden_scoring_date'],"%Y-%m-%d %H:%M:%S")
        data['sensory_perception_score'] = int(request.POST['sensory_perception_score'])
        data['moisture_score'] = int(request.POST['moisture_score'])
        data['activity_score'] = int(request.POST['activity_score'])
        data['mobility_score'] = int(request.POST['mobility_score'])
        data['nutrition_score'] = int(request.POST['nutrition_score'])
        data['friction_shear_score'] = int(request.POST['friction_shear_score'])
        bradenScores.setFromData(data)
        print "Updating BradenScores with id = %d" % (id)
        o = DBSession.merge(bradenScores)
        DBSession.flush()
        DBSession.refresh(o)
        id = o.id
        # msg = "Merged"
        # DBSession.commit() # Not needed since Pyramid uses the Zope transaction manager
        print "Update successful"
    except exc.SQLAlchemyError as e:
        print "In braden_scores_post caught exception of type: "
        print type(e)
        msg = str(e)
        print msg
        DBSession.rollback()
    except Exception as e:
        print "In braden_scores_post caught exception of type: "
        print type(e)
        msg = str(e)
        DBSession.rollback()
    finally:
        return {'msg': msg, 'id': str(id)}
    return {'msg': msg, 'id': str(id)}
def patient_assessment_post(request):
    try:
        msg = ''
        data = {}
        id = int(request.POST['id'])
        if id > 0:
            patientAssessment = DBSession.query(PatientAssessment).get(id)
        else:
            patientAssessment = PatientAssessment()
        data['patient_id'] = int(request.POST['patient_id'])
        data['assessment_date'] = datetime.strptime(request.POST['assessment_date'],"%Y-%m-%d %H:%M:%S")
        data['assessment_note'] = request.POST['assessment_note']
        data['education_notes'] = request.POST['education_notes']
        data['education_understanding'] = request.POST['education_understanding']
        data['education_evidenced_by'] = request.POST['education_evidenced_by']
        patientAssessment.setFromData(data)
        print "Updating PatientAssessment with id = %d" % (id)
        o = DBSession.merge(patientAssessment)
        DBSession.flush()
        DBSession.refresh(o)
        id = o.id
        # DBSession.commit() # Not needed since Pyramid uses the Zope transaction manager
        print "Update successful"
    except exc.SQLAlchemyError as e:
        print "In patient_assessment_post caught exception of type: "
        print type(e)
        msg = str(e)
        print msg
        DBSession.rollback()
    except Exception as e:
        print "In patient_assessment_post caught exception of type: "
        print type(e)
        msg = str(e)
        DBSession.rollback()
    finally:
        return {'msg': msg, 'id': str(id)}
    return {'msg': msg, 'id': str(id)}