def create_patient(request, bb_dict): """ Create a Patient Profile using the contents of bb_dict (Medicare BlueButton 2.0 Text converted to json) :param request: :param bb_dict: :return: """ # Get a Crosswalk entry for the user and use the Guid as # the identifier try: x_walk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: x_walk = Crosswalk() x_walk.user = request.user x_walk.save() x_walk = Crosswalk.objects.get(user=request.user) guid = x_walk.guid if settings.DEBUG: print("CrossWalk Match:", x_walk, "\nGUID:", guid, "\nFHIR Id:",x_walk.fhir, "|", x_walk.fhir_url_id ) # Compile Profile content profile = {} profile['resourceType'] = "Patient" profile['mode'] = "create" profile['versionId'] = "1" profile['updated'] = date_to_iso(datetime.datetime.now()) profile['active'] = "True" id_list = [] id_source = {} id_source['system'] = "https://mymedicare.gov" id_source['use'] = "official" id_source['value'] = guid id_list.append(unique_id(id_source)) profile['fhir_identifier'] = id_list person = bb_dict['patient']['name'] names = person.split(" ") # We will take the last entry in names and assign as family name # We will take first entry and assign as given name given = "" family = "" if len(names) > 0: profile['given'] = [names[0]] profile['family'] = [names[len(names)-1]] # Done: Fix call to human_name - breaking out to chars profile['fhir_human_name'] = human_name(profile) telecom_source = {} tel_list = [] rank = 1 telecom_source['system'] = "phone" telecom_source['value'] = bb_dict['patient']['phoneNumber'][0] telecom_source['rank'] = str(rank) tel_list.append(contact_point(telecom_source)) rank += 1 telecom_source['system'] = "email" telecom_source['value'] = bb_dict['patient']['email'] telecom_source['rank'] = str(rank) tel_list.append(contact_point(telecom_source)) profile['fhir_contact_point'] = tel_list addr_source = {} addr_list = [] addr_source['use'] = "primary" addr_source['type'] = "physical" addr_source['line'] = [bb_dict['patient']['address']['addressLine1'], bb_dict['patient']['address']['addressLine2']] addr_source['city'] = bb_dict['patient']['address']['city'] addr_source['state'] = bb_dict['patient']['address']['state'] addr_source['postalCode'] = bb_dict['patient']['address']['zip'] addr_list.append(address(addr_source)) profile['fhir_address'] = addr_list narrative = concat_string("", profile['given'], delimiter=" ", last=" ") narrative = concat_string(narrative, profile['family'], delimiter=" ", last = " ") narrative = concat_string(narrative, [" (id:", guid, ")"]) narrative = concat_string(narrative, addr_source['line'], delimiter=",", last=",") narrative = concat_string(narrative, addr_source['city'], last=" ") narrative = concat_string(narrative, addr_source['state'], last=" ") narrative = concat_string(narrative, addr_source['postalCode'] ) profile['narrative'] = narrative if settings.DEBUG: print("Profile:", profile, "\n====================") # Write Profile txn = {'resourceType' :"Patient", 'display' :'Patient', 'mask' : True, 'server': settings.FHIR_SERVER, 'locn' : "/baseDstu2/Patient", 'template' : 'v1api/fhir_profile/patient', 'extn' : 'json.html',} context = {'txn': txn, 'profile': profile,} fhir_profile = build_fhir_profile(request, context, context['txn']['template'], context['txn']['extn'], ) if settings.DEBUG: print("===============================", "FHIR Profile:\n", fhir_profile, "===============================") # Submit to server target_url = context['txn']['server'] + context['txn']['locn'] headers = {'Content-Type': 'application/json+fhir; charset=UTF-8', 'Accept' : 'text/plain'} try: if profile['mode'] == 'create': r = requests.post(target_url + "?_format=json", data=fhir_profile, headers=headers ) else: r = requests.put(target_url + "/" + x_walk.fhir_url_id + "?_format=json", data=fhir_profile, headers=headers ) r_returned = r.text print("Result from post", r.status_code, "|", r_returned, "|", r.content, "|", r.text, "|", "Headers", r.headers) if r.status_code == 201: url_result = r.headers['content-location'] if settings.DEBUG: print("url_content_location", url_result) result = get_fhir_url(url_result, "Patient") if settings.DEBUG: print("result:", result, "|", result[1], "|") x_walk.fhir_url_id = result[1] x_walk.save() # Get Id from successful write # Update Patient Crosswalk except requests.ConnectionError: messages.error(request,"Problem posting:" + guid) return
def connect_first(request): """ Prompt for MyMedicare.gov user and password Ask for confirmation to connect to MyMedicare.gov call connect with userid and password :param request: :return: """ u = User.objects.get(email=request.user.email) try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: xwalk = Crosswalk() xwalk.user = request.user xwalk.save() xwalk = Crosswalk.objects.get(user=request.user) # xwalk = Crosswalk.objects.get(user=u) form = Medicare_Connect() if settings.DEBUG: print("In getbb.mym_login.connect_first") print("User:"******"Crosswalk:", xwalk) if request.POST: form = Medicare_Connect(request.POST) if form.is_valid(): if settings.DEBUG: print(":" ) if form.cleaned_data['mmg_user'] and form.cleaned_data['mmg_pwd']: context = {} # make the call if settings.DEBUG: print("MAKING THE CALL with:", form.cleaned_data['mmg_user']) print("and ", form.cleaned_data['mmg_pwd']) mmg = {} mmg['mmg_user'] = form.cleaned_data['mmg_user'] mmg['mmg_pwd'] = form.cleaned_data['mmg_pwd'] # see if the call works mmg = connect(request, mmg) # if settings.DEBUG: # print("mmg returned from connect:", mmg) mmg_bb = get_bluebutton(request, mmg) mmg_mail = {} if settings.DEBUG: print("BlueButton returned:", mmg_bb) if mmg_bb['status'] == "OK": u.medicare_connected = True u.medicare_verified = True u.save() xwalk.mmg_user = mmg['mmg_user'] xwalk.mmg_pwd = mmg['mmg_pwd'] xwalk.mmg_name = mmg['mmg_name'] xwalk.mmg_account = mmg['mmg_account'] if mmg_bb['status'] == "OK": # We need to save the BlueButton Text # print("We are okay to update mmg_bbdata", # "\n with ", mmg_bb['mmg_bbdata'][:250]) xwalk.mmg_bbdata = mmg_bb['mmg_bbdata'] xwalk.save() result = bb_to_json(request) if settings.DEBUG: print("BB Conversion:", result) if result['result'] == "OK": # xwalk.mmg_bbjson = result['mmg_bbjson'] xwalk = Crosswalk.objects.get(user=request.user) print("returned json from xwalk:", xwalk.mmg_bbjson) for key, value in xwalk.mmg_bbjson.items(): # print("Key:", key) if key == "patient": for k, v in value.items(): # print("K:", k, "| v:", v) if k == "email": xwalk.mmg_email = v if not xwalk.mmg_bbfhir: if result['result'] == "OK": outcome = json_to_eob(request) if outcome['result'] == "OK": xwalk.mmg_bbfhir = True # if mmg_mail['status'] == "OK": # xwalk.mmg_email = mmg_mail['mmg_email'] # Save the Crosswalk changes xwalk.save() # mmg['mmg_email'] = mmg_mail['mmg_email'] context['mmg'] = mmg return HttpResponseRedirect(reverse('accounts:manage_account'), RequestContext(request), context) else: form = Medicare_Connect() # form['mmg_user'] = "" # form['mmg_pwd'] = "" if settings.DEBUG: print("setting up the GET:") return render(request, 'getbb/medicare_connect.html', {'form': form, 'email': u.email})