Ejemplo n.º 1
0
def edit_patient(request, patient_id):
    if request.method == "POST":
        data = json.loads(request.POST.get('result'))
        patinfo = data.get('patient')

        if patinfo:
            #this is all quite similar to creating a new patient; the code should probably be
            #consolidated

            patient = loader.get_patient(patinfo['_id'])
            patient.first_name = patinfo['fname']
            patient.last_name = patinfo['lname']
            patient.birthdate = string_to_datetime(patinfo['dob']).date() if patinfo['dob'] else None
            patient.birthdate_estimated = patinfo['dob_est']
            patient.gender = patinfo['sex']

            if patinfo.get('phone'):
                patient.phones = [CPhone(is_default=True, number=patinfo['phone'])]
            else:
                patient.phones = []
            
            patient.address = CAddress(village=patinfo.get('village'),
                                       address=patinfo.get('address'),
                                       clinic_id=settings.BHOMA_CLINIC_ID,
                                       zone=patinfo['chw_zone'],
                                       zone_empty_reason=patinfo['chw_zone_na'])

            #update mother info only if was edited during patient edit, or if no linkage has yet been created
            if patinfo.get('mother_edited') or (patinfo.get('motherable') and (len(patient.relationships) == 0 or patient.relationships[0].patient_uuid == None)):
                mother = relationship_from_instance(patinfo)
                patient.relationships = [mother] if mother else []

            patient.save()

        return HttpResponseRedirect(reverse("single_patient", args=(data.get('_id'),)))

    return render_to_response(request, "bhoma_touchscreen.html", 
                              {'form': {'name': 'patient edit', 
                                        'wfobj': 'wfEditPatient',
                                        'wfargs': json.dumps(patient_id)}, 
                               'mode': 'workflow',
                               'dynamic_scripts': ["%spatient/javascripts/patient_reg.js" \
                                                   % settings.STATIC_URL,] })
Ejemplo n.º 2
0
def patient_select(request):
    """
    Entry point for patient select/registration workflow
    """
    if request.method == "POST":
        data = json.loads(request.POST.get('result'))
        merge_id = data.get("merge_with", None) 
        create_new = data.get("new") and not merge_id 
        pat_dict = data.get("patient")

        if not data:
            return HttpResponseRedirect('/')
        elif create_new:
            
            # Here's an example format:
            # u'patient': {u'dob': u'2000-02-02', u'sex': u'm', 
            #              u'lname': u'alskdjf', u'phone': None, 
            #              u'fname': u'f8rask', u'village': u'PP'f, 
            #              u'dob_est': False, u'id': u'727272727272'}}
            # 
            def map_basic_data(pat_dict):
                # let's use the same keys, for now this will have to suffice
                new_dict = {}
                mapping = (("dob",  "birthdate"), 
                           ("fname",  "first_name"),
                           ("lname",  "last_name"),
                           ("dob_est",  "birthdate_estimated"),
                           ("sex",  "gender"),
                           ("id",  "patient_id")
                           )
                for oldkey, newkey in mapping:
                    new_dict[newkey] = pat_dict.get(oldkey)
                return new_dict
            clean_data = map_basic_data(pat_dict)
            patient = patient_from_instance(clean_data)
            if pat_dict.get("phone"):
                patient.phones=[CPhone(is_default=True, number=pat_dict["phone"])]
            else:
                patient.phones = []
            
            # TODO: create an encounter for this reg?
            patient.address = CAddress(village=pat_dict.get("village"),
                                       address=pat_dict.get("address"),
                                       clinic_id=settings.BHOMA_CLINIC_ID,
                                       zone=pat_dict.get("chw_zone"),
                                       zone_empty_reason=pat_dict.get("chw_zone_na"))
            patient.clinic_ids = [settings.BHOMA_CLINIC_ID,]
            
            if pat_dict.get('motherable'):
                mother = relationship_from_instance(pat_dict)
                patient.relationships = [mother] if mother else []

            patient.save()
            return HttpResponseRedirect(reverse("single_patient", args=(patient.get_id,)))
        elif merge_id:
            return HttpResponseRedirect(reverse("single_patient", args=(merge_id,)))
        elif pat_dict is not None:
            # we get a real couch patient object in this case
            pat_uid = pat_dict["_id"]
            return HttpResponseRedirect(reverse("single_patient", args=(pat_uid,)))
        
    return render_to_response(request, "bhoma_touchscreen.html", 
                              {'form': {'name': 'patient reg', 
                                        'wfobj': 'wfGetPatient'}, 
                               'mode': 'workflow',
                               'dynamic_scripts': ["%spatient/javascripts/patient_reg.js" \
                                                   % settings.STATIC_URL,] })