def get_patient(request, Access_Mode=None, *args, **kwargs): """ Display Patient Profile :param request: :param Access_Mode = [None], Open :param args: :param kwargs: :return: """ # Access_Mode = None = Do Crosswalk using Request.user # Access_Mode = OPEN = use kwargs['patient_id'] if settings.DEBUG: print("Request.GET :", request.GET) print("Access_Mode :", Access_Mode) print("KWargs :", kwargs) print("Args :", args) if Access_Mode == "OPEN" and kwargs['patient_id'] != "": # Lookup using patient_id for fhir_url_id key = kwargs['patient_id'].strip() else: # DONE: Setup Patient API so that ID is not required # DONE: Do CrossWalk Lookup to get Patient ID if settings.DEBUG: print("Request User Beneficiary(Patient):", request.user) try: xwalk = Crosswalk.objects.get(user=request.user.id) except Crosswalk.DoesNotExist: reason = "Unable to find Patient ID for user:%s[%s]" % ( request.user, request.user.id) messages.error(request, reason) return kickout_404(reason) # return HttpResponseRedirect(reverse('api:v1:home')) if xwalk.fhir_url_id == "": err_msg = [ 'Crosswalk lookup failed: Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return kickout_404(exit_message) # return HttpResponseRedirect(reverse('api:v1:home')) key = xwalk.fhir_url_id.strip() if settings.DEBUG: print("Crosswalk :", xwalk) print("GUID :", xwalk.guid) print("FHIR :", xwalk.fhir) print("FHIR URL ID :", key) # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" # fhir_server_configuration = {"SERVER":"http://fhir-test.bbonfhir.com:8081", # "PATH":"", # "RELEASE":"/baseDstu2"} # FHIR_SERVER_CONF = fhir_server_configuration # FHIR_SERVER = FHIR_SERVER_CONF['SERVER'] + FHIR_SERVER_CONF['PATH'] # Since this is BlueButton and we are dealing with Patient Records # We need to limit the id search to the specific beneficiary. # A BlueButton user should not be able to request a patient profile # that is not their own. # We do this via the CrossWalk. The xwalk.fhir_url_id is the patient # id as used in the url. eg. /Patient/23/ # FHIR also allows an enquiry with ?_id=23. We need to detect that # and remove it from the parameters that are passed. # All other query parameters should be passed through to the # FHIR server. # Add URL Parameters to skip_parm to ignore or perform custom # processing with them. Use lower case values for matching. # DO NOT USE Uppercase skip_parm = ['_id', '_format'] mask = True pass_to = FhirServerUrl() pass_to += "/Patient" pass_to += "/" pass_to = pass_to + key + "/" # We need to detect if a format was requested in the URL Parameters # ie. _format=json|xml # modify get_format to default to return nothing. ie. make no change # internal data handling will be JSON # _format will drive external display # if no _format setting we will display in html (Current mode) # if valid _format string we will pass content through to display in # raw format get_fmt = get_format(request.GET) if settings.DEBUG: print("pass_to:", pass_to) pass_to = pass_to + build_params(request.GET, skip_parm) if settings.DEBUG: print("pass_to added to:", pass_to) mask_to = settings.DOMAIN # Set Context context = { 'display': "Patient", 'name': "Patient", 'mask': mask, 'key': key, 'get_fmt': get_fmt, 'in_fmt': in_fmt, # 'output' : "test output ", # 'args' : args, # 'kwargs' : kwargs, # 'get' : request.GET, 'pass_to': pass_to, 'template': 'v1api/patient.html', } if settings.DEBUG: print("Calling Requests with:", pass_to) try: r = requests.get(pass_to) context = process_page(request, r, context) return publish_page(request, context) # # Setup the page # # if settings.DEBUG: # print("Context-result:", context['result']) # # print("Context-converted:", json.dumps(context['result'], sort_keys=False)) # # print("Context:",context) # # if get_fmt == 'xml' or get_fmt == 'json': # if settings.DEBUG: # print("Mode = ", get_fmt) # print("Context['result']: ", context['result']) # if get_fmt == "xml": # return HttpResponse(context['result'], # content_type='application/' + get_fmt) # if get_fmt == "json": # #return HttpResponse(context['result'], mimetype="application/json") # return JsonResponse(context['import_text'], safe=False) # # else: # # if context['text'] == "No user readable content to display" or context['text']=="": # # result = json.loads(context['result'], object_pairs_hook=OrderedDict) # print("Result::", result) # context['text'] += "<br/> extracting information from returned record:<br/>" # context['text'] += "<table>\n" # if 'name' in result: # patient_name = result['name'][0]['given'][0] # patient_name += " " # patient_name += result['name'][0]['family'][0] # context['text'] += tr_build_item("Patient Name ", # patient_name) # if 'address' in result: # context['text'] += tr_build_item("Patient Address", # result['address'][0]['line'][0]) # if 'birthDate' in result: # context['text'] += tr_build_item("Birth Date", result['birthDate']) # # if 'identifier' in result: # context['text'] += tr_build_item("Patient ID", # result['identifier'][0]['value']) # context['text'] += "</table>" # # return render_to_response('v1api/patient.html', # RequestContext(request, # context, )) except requests.ConnectionError: pass return cms_not_connected(request, 'api:v1:home')
def PatientExplanationOfBenefit(request, patient_id=None, *args, **kwargs): """ Function-based interface to ExplanationOfBenefit :param request: :return: """ if patient_id == None: try: xwalk = Crosswalk.objects.get(user=request.user.id) patient_id = xwalk.fhir_url_id except Crosswalk.DoesNotExist: reason = "Unable to find Patient ID for user:%s[%s]" % (request.user, request.user.id) messages.error(request, reason) return kickout_404(reason) # return HttpResponseRedirect(reverse('api:v1:home')) if xwalk.fhir_url_id == "": err_msg = ['Crosswalk lookup failed: Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return kickout_404(exit_message) if patient_id == "": err_msg = ['Sorry, No Patient Id provided', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return HttpResponseRedirect(reverse('api:v1:home')) if settings.DEBUG: print("In apps.v1api.views.eob.PatientExplanationOfBenefit Function") print("request:", request.GET) process_mode = request.META['REQUEST_METHOD'] in_fmt = "json" get_fmt = get_format(request.GET) Txn = {'name': "ExplanationOfBenefit", 'display': 'EOB', 'mask': True, 'template': 'v1api/eob.html', 'in_fmt': in_fmt, } if settings.DEBUG: print("Request.GET :", request.GET) print("KWargs :", kwargs) print("Patient :", patient_id) # We should have the xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) pass_to = FhirServerUrl() pass_to += "/ExplanationOfBenefit/" key = patient_id patient_filter= "?patient=Patient/" + key pass_to += patient_filter skip_parm = ['_id', '_format'] pass_to = pass_to + "&" + build_params(request.GET, skip_parm)[1:] # Set Context context = {'display':"EOB", 'name': "ExplanationOfBenefit", 'mask': True, 'key': key, 'get_fmt': get_fmt, 'in_fmt': in_fmt, 'pass_to': pass_to, } if settings.DEBUG: print("Calling requests with pass_to:", pass_to) try: r = requests.get(pass_to) if get_fmt == "xml": xml_text = minidom.parseString(r.text) print("XML_TEXT:", xml_text.toxml()) root = ET.fromstring(r.text) # root_out = etree_to_dict(r.text) json_string = "" # json_out = xml_str_to_json_str(r.text, json_string) if settings.DEBUG: print("Root ET XML:", root) # print("XML:", root_out) # print("JSON_OUT:", json_out,":", json_string) drill_down = ['Bundle', 'entry', 'Patient', ] level = 0 tag0 = xml_text.getElementsByTagName("text") # tag1 = tag0.getElementsByTagName("entry") print("Patient?:", tag0) print("DrillDown:", drill_down[level]) print("root find:", root.find(drill_down[level])) pretty_xml = xml_text.toprettyxml() #if settings.DEBUG: # print("TEXT:", text) # # print("Pretty XML:", pretty_xml) context['result'] = pretty_xml # convert context['text'] = pretty_xml else: convert = OrderedDict(r.json()) # result = mark_safe(convert) if settings.DEBUG: print("Convert:", convert) # print("Next Level - entry:", convert['entry']) # print("\n ANOTHER Level- text:", convert['entry'][0]) content = OrderedDict(convert) text = "" if settings.DEBUG: print("Content:", content) print("resourceType:", content['resourceType']) if 'text' in content: if 'div' in content['text']: print("text:", content['text']['div']) # context['result'] = r.json() # convert import_text = json.loads(r.text, object_pairs_hook=OrderedDict) context['result'] = json.dumps(import_text, indent=4, sort_keys=False) if 'text' in content: if 'div' in content['text']: context['text'] = content['text']['div'] else: context['text'] = "" else: context['text'] = "No user readable content to display" if 'error' in content: context['error'] = context['issue'] # Setup the page if settings.DEBUG: print("Context-result:", context['result']) # print("Context-converted:", json.dumps(context['result'], sort_keys=False)) # print("Context:",context) if get_fmt == 'xml' or get_fmt == 'json': if settings.DEBUG: print("Mode = ", get_fmt) print("Context['result']: ", context['result']) if get_fmt == "xml": return HttpResponse(context['result'], content_type='application/' + get_fmt) if get_fmt == "json": #return HttpResponse(context['result'], mimetype="application/json") return JsonResponse(import_text, safe=False ) else: return render_to_response(Txn['template'], RequestContext(request, context, )) except requests.ConnectionError: print("Whoops - Problem connecting to FHIR Server") messages.error(request, "FHIR Server is unreachable. Are you on the CMS Network?") return HttpResponseRedirect(reverse('api:v1:home'))
def get_eob(request, eob_id=None, Access_Mode=None, *args, **kwargs): """ Display one or more EOBs but Always limit scope to Patient_Id :param request: :param eob_id: Request a specific EOB :param args: :param kwargs: :return: """ if settings.DEBUG: print("Request User Beneficiary(Patient):", request.user, "\nFor EOB Enquiry ") print("Request.GET :", request.GET) print("Access_Mode :", Access_Mode) print("KWargs :", kwargs) print("Args :", args) if Access_Mode == "OPEN": # Lookup using eob_id without patient filter key = "" else: try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: messages.error(request, "Unable to find Patient ID") return HttpResponseRedirect(reverse('api:v1:home')) if xwalk.fhir_url_id == "": err_msg = [ 'Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return HttpResponseRedirect(reverse('api:v1:home')) key = xwalk.fhir_url_id.strip() if settings.DEBUG: print("FHIR URL ID :", key) # We should have the xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # This is a hack to limit EOBs returned to this user only. # id_source['system'] = "https://mymedicare.gov/claims/beneficiary" # id_source['use'] = "official" # id_source['value'] = "Patient/"+str(patient_id) # id_list.append(unique_id(id_source)) # this search works: # http://fhir.bbonfhir.com:8080/fhir-p/ # search?serverId=bbonfhir_dev # &resource=ExplanationOfBenefit # ¶m.0.0=https%3A%2F%2Fmymedicare.gov%2Fclaims%2Fbeneficiary # ¶m.0.1=Patient%2F4995401 # ¶m.0.name=identifier # ¶m.0.type=token # &sort_by= # &sort_direction= # &resource-search-limit= # http://ec2-52-4-198-86.compute-1.amazonaws.com:8081/baseDstu2/ # ExplanationOfBenefit/?patient=Patient/131052&_format=json # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) skip_parm = ['_id', '_format', 'patient'] mask = True pass_to = FhirServerUrl() pass_to += "/ExplanationOfBenefit" pass_to += "/" if eob_id == None: pass else: pass_to += eob_id # We can allow an EOB but we MUST add a search Parameter # to limit the items found to those relevant to the Patient Id #if eob_id: # pass_to = eob_id + "/" # Now apply the search restriction to limit to patient _id #pass_to = pass_to + key + "/" if not key == "": pass_to += "?patient=" pass_to += "Patient/" pass_to += key pass_to = pass_to + "&" + build_params(request.GET, skip_parm)[1:] if settings.DEBUG: print("Pass_to from build_params:", pass_to) if settings.DEBUG: print("Calling requests with pass_to:", pass_to) # Set Context context = { 'display': 'EOB', 'name': 'ExplanationOfBenefit', 'mask': mask, 'key': key, 'eob_id': eob_id, 'get_fmt': get_fmt, 'in_fmt': in_fmt, 'pass_to': pass_to, 'template': 'v1api/eob.html', } try: r = requests.get(pass_to) context = process_page(request, r, context) return publish_page(request, context) except requests.ConnectionError: pass return cms_not_connected(request, 'api:v1:home')
def get_eob_view(request, eob_id, *args, **kwargs): """ Display one or more EOBs but Always limit scope to Patient_Id :param request: :param eob_id: Request a specific EOB :param args: :param kwargs: :return: """ if settings.DEBUG: print("Request User Beneficiary(Patient):", request.user, "\nFor Single EOB") try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: messages.error(request, "Unable to find Patient ID") return HttpResponseRedirect(reverse('api:v1:home')) if xwalk.fhir_url_id == "": err_msg = [ 'Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return HttpResponseRedirect(reverse('api:v1:home')) if settings.DEBUG: print("Request.GET :", request.GET) print("KWargs :", kwargs) print("Crosswalk :", xwalk) print("GUID :", xwalk.guid) print("FHIR :", xwalk.fhir) print("FHIR URL ID :", xwalk.fhir_url_id) # We should have the xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) # DONE: Define Transaction Dictionary to enable generic presentation of API Call skip_parm = ['_id', '_format'] key = xwalk.fhir_url_id.strip() mask = True pass_to = FhirServerUrl() pass_to += "/ExplanationOfBenefit/" # We can allow an EOB but we MUST add a search Parameter # to limit the items found to those relevant to the Patient Id if eob_id: pass_to = pass_to + eob_id + "/" # Now apply the search restriction to limit to patient _id #pass_to = pass_to + key + "/" pass_to = pass_to + "?patient=" pass_to = pass_to + "Patient/" pass_to = pass_to + xwalk.fhir_url_id.strip() pass_to = pass_to + "&" + build_params(request.GET, skip_parm) if settings.DEBUG: print("Pass_to from build_params:", pass_to) if settings.DEBUG: print("Calling requests with pass_to:", pass_to) # Set Context context = { 'name': "ExplanationOfBenefit", 'display': 'EOB', 'mask': mask, 'key': key, 'get_fmt': get_fmt, 'in_fmt': in_fmt, # 'output' : "test output ", # 'args' : args, # 'kwargs' : kwargs, # 'get' : request.GET, 'pass_to': pass_to, 'template': 'v1api/eob.html', } try: r = requests.get(pass_to) context = process_page(request, r, context) return publish_page(request, context) except requests.ConnectionError: pass return cms_not_connected(request, 'api:v1:home')
def get_eob_view(request, eob_id, *args, **kwargs): """ Display one or more EOBs but Always limit scope to Patient_Id :param request: :param eob_id: Request a specific EOB :param args: :param kwargs: :return: """ if settings.DEBUG: print("Request User Beneficiary(Patient):", request.user, "\nFor Single EOB") try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: messages.error(request, "Unable to find Patient ID") return HttpResponseRedirect(reverse('api:v1:home')) if xwalk.fhir_url_id == "": err_msg = [ 'Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return HttpResponseRedirect(reverse('api:v1:home')) if settings.DEBUG: print("Request.GET :", request.GET) print("KWargs :", kwargs) print("Crosswalk :", xwalk) print("GUID :", xwalk.guid) print("FHIR :", xwalk.fhir) print("FHIR URL ID :", xwalk.fhir_url_id) # We should have the xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) # DONE: Define Transaction Dictionary to enable generic presentation of API Call Txn = { 'name': "ExplanationOfBenefit", 'display': 'EOB', 'mask': True, # 'server': settings.FHIR_SERVER, # 'locn': "/baseDstu2/ExplanationOfBenefit/", 'template': 'v1api/eob.html', 'in_fmt': in_fmt, } skip_parm = ['_id', '_format'] key = xwalk.fhir_url_id.strip() mask = False if 'mask' in Txn: mask = Txn['mask'] pass_to = FhirServerUrl() pass_to += "/ExplanationOfBenefit/" # We can allow an EOB but we MUST add a search Parameter # to limit the items found to those relevant to the Patient Id if eob_id: pass_to = pass_to + eob_id + "/" # Now apply the search restriction to limit to patient _id #pass_to = pass_to + key + "/" pass_to = pass_to + "?patient=" pass_to = pass_to + "Patient/" pass_to = pass_to + xwalk.fhir_url_id.strip() pass_to = pass_to + "&" + build_params(request.GET, skip_parm) if settings.DEBUG: print("Pass_to from build_params:", pass_to) if settings.DEBUG: print("Calling requests with pass_to:", pass_to) # Set Context context = { 'display': Txn['display'], 'name': Txn['name'], 'mask': mask, 'key': key, 'get_fmt': get_fmt, 'in_fmt': Txn['in_fmt'], # 'output' : "test output ", # 'args' : args, # 'kwargs' : kwargs, # 'get' : request.GET, 'pass_to': pass_to, } try: r = requests.get(pass_to) if get_fmt == "xml": xml_text = minidom.parseString(r.text) pretty_xml = xml_text.toprettyxml() context['result'] = pretty_xml # convert context['text'] = pretty_xml return HttpResponse(context['result'], content_type='application/' + get_fmt) else: # get_fmt == "json" or None: convert = OrderedDict(r.json()) # result = mark_safe(convert) if settings.DEBUG: print("Convert:", convert) content = OrderedDict(convert) text = "" context['result'] = r.json() # convert if 'text' in content: context['text'] = content['text']['div'] if 'issue' in content: context['error'] = content['issue'] else: if settings.DEBUG: print("Resource:", convert['entry']) context['text'] = convert['entry'] if get_fmt == "json": return JsonResponse(context['result'], ) return render_to_response(Txn['template'], RequestContext( request, context, )) except requests.ConnectionError: print("Whoops - Problem connecting to FHIR Server") messages.error( request, "FHIR Server is unreachable. " "Are you on the CMS Network?") return HttpResponseRedirect(reverse('api:v1:home'))
def get(self, request, patient_id, *args, **kwargs): # This is a patient profile GET # # use request.user to lookup a crosswalk # get the FHIR Patient ID # Call the FHIR Patient Profile # Return the result print("in the get!") if settings.DEBUG: print("in Patients.get with", patient_id) xwalk_id = lookup_xwalk(request, ) if settings.DEBUG: print("crosswalk:", xwalk_id) if xwalk_id == None: return HttpResponseRedirect(reverse_lazy('api:v1:home')) if settings.DEBUG: print("now we need to evaluate the parameters and arguments" " to work with ", xwalk_id, "and ", request.user) print("GET Parameters:", request.GET, ":") if patient_id == xwalk_id: key = patient_id else: key = xwalk_id.strip() in_fmt = "json" Txn = {'name': "Patient", 'display': 'Patient', 'mask': True, 'server': settings.FHIR_SERVER, 'locn': "/baseDstu2/Patient/", 'template': 'v1api/patient.html', 'in_fmt': in_fmt, } skip_parm = ['_id', 'access_token', 'client_id', 'response_type', 'state'] # access_token can be passed in as a part of OAuth protected request. # as can: state=random_state_string&response_type=code&client_id=ABCDEF # Remove it before passing url through to FHIR Server pass_params = build_params(request.GET, skip_parm) pass_to = Txn['server'] + Txn['locn'] + key + "/" print("Here is the URL to send, %s now get parameters" % pass_to) if pass_params != "": pass_to = pass_to + pass_params try: r = requests.get(pass_to) except requests.ConnectionError: if settings.DEBUG: print("Problem connecting to FHIR Server") messages.error(request, "FHIR Server is unreachable." ) return HttpResponseRedirect(reverse_lazy('api:v1:home')) text_out = "" if '_format=xml' in pass_to: text_out= minidom.parseString(r.text).toprettyxml() else: text_out = r.json() if settings.DEBUG: print("What we got back was:", text_out) return HttpResponse('This is the Patient Pass Thru %s using %s ' 'and with response of %s ' % (xwalk_id, pass_to, text_out ))
def get_eob_view(request, eob_id, *args, **kwargs): """ Display one or more EOBs but Always limit scope to Patient_Id :param request: :param eob_id: Request a specific EOB :param args: :param kwargs: :return: """ if settings.DEBUG: print("Request User Beneficiary(Patient):", request.user, "\nFor Single EOB") try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: messages.error(request, "Unable to find Patient ID") return HttpResponseRedirect(reverse('api:v1:home')) if xwalk.fhir_url_id == "": err_msg = ['Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return HttpResponseRedirect(reverse('api:v1:home')) if settings.DEBUG: print("Request.GET :", request.GET) print("KWargs :", kwargs) print("Crosswalk :", xwalk) print("GUID :", xwalk.guid) print("FHIR :", xwalk.fhir) print("FHIR URL ID :", xwalk.fhir_url_id) # We should have the xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) # DONE: Define Transaction Dictionary to enable generic presentation of API Call skip_parm = ['_id', '_format'] key = xwalk.fhir_url_id.strip() mask = True pass_to = FhirServerUrl() pass_to += "/ExplanationOfBenefit/" # We can allow an EOB but we MUST add a search Parameter # to limit the items found to those relevant to the Patient Id if eob_id: pass_to = pass_to + eob_id + "/" # Now apply the search restriction to limit to patient _id #pass_to = pass_to + key + "/" pass_to = pass_to + "?patient=" pass_to = pass_to + "Patient/" pass_to = pass_to + xwalk.fhir_url_id.strip() pass_to = pass_to + "&" + build_params(request.GET, skip_parm) if settings.DEBUG: print("Pass_to from build_params:", pass_to) if settings.DEBUG: print("Calling requests with pass_to:", pass_to) # Set Context context = {'name': "ExplanationOfBenefit", 'display': 'EOB', 'mask': mask, 'key': key, 'get_fmt': get_fmt, 'in_fmt': in_fmt, # 'output' : "test output ", # 'args' : args, # 'kwargs' : kwargs, # 'get' : request.GET, 'pass_to': pass_to, 'template': 'v1api/eob.html', } try: r = requests.get(pass_to) context = process_page(request, r, context) return publish_page(request, context) except requests.ConnectionError: pass return cms_not_connected(request,'api:v1:home')
def get_eob_view(request, eob_id, *args, **kwargs): """ Display one or more EOBs but Always limit scope to Patient_Id :param request: :param eob_id: Request a specific EOB :param args: :param kwargs: :return: """ if settings.DEBUG: print("Request User Beneficiary(Patient):", request.user, "\nFor Single EOB") try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: messages.error(request, "Unable to find Patient ID") return HttpResponseRedirect(reverse('api:v1:home')) if xwalk.fhir_url_id == "": err_msg = ['Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return HttpResponseRedirect(reverse('api:v1:home')) if settings.DEBUG: print("Request.GET :", request.GET) print("KWargs :", kwargs) print("Crosswalk :", xwalk) print("GUID :", xwalk.guid) print("FHIR :", xwalk.fhir) print("FHIR URL ID :", xwalk.fhir_url_id) # We should have the xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) # DONE: Define Transaction Dictionary to enable generic presentation of API Call Txn = {'name': "ExplanationOfBenefit", 'display': 'EOB', 'mask': True, # 'server': settings.FHIR_SERVER, # 'locn': "/baseDstu2/ExplanationOfBenefit/", 'template': 'v1api/eob.html', 'in_fmt': in_fmt, } skip_parm = ['_id', '_format'] key = xwalk.fhir_url_id.strip() mask = False if 'mask' in Txn: mask = Txn['mask'] pass_to = FhirServerUrl() pass_to += "/ExplanationOfBenefit/" # We can allow an EOB but we MUST add a search Parameter # to limit the items found to those relevant to the Patient Id if eob_id: pass_to = pass_to + eob_id + "/" # Now apply the search restriction to limit to patient _id #pass_to = pass_to + key + "/" pass_to = pass_to + "?patient=" pass_to = pass_to + "Patient/" pass_to = pass_to + xwalk.fhir_url_id.strip() pass_to = pass_to + "&" + build_params(request.GET, skip_parm) if settings.DEBUG: print("Pass_to from build_params:", pass_to) if settings.DEBUG: print("Calling requests with pass_to:", pass_to) # Set Context context = {'display': Txn['display'], 'name': Txn['name'], 'mask': mask, 'key': key, 'get_fmt': get_fmt, 'in_fmt': Txn['in_fmt'], # 'output' : "test output ", # 'args' : args, # 'kwargs' : kwargs, # 'get' : request.GET, 'pass_to': pass_to, } try: r = requests.get(pass_to) if get_fmt == "xml": xml_text = minidom.parseString(r.text) pretty_xml = xml_text.toprettyxml() context['result'] = pretty_xml # convert context['text'] = pretty_xml return HttpResponse(context['result'], content_type='application/' + get_fmt) else: # get_fmt == "json" or None: convert = OrderedDict(r.json()) # result = mark_safe(convert) if settings.DEBUG: print("Convert:", convert) content = OrderedDict(convert) text = "" context['result'] = r.json() # convert if 'text' in content: context['text'] = content['text']['div'] if 'issue' in content: context['error'] = content['issue'] else: if settings.DEBUG: print("Resource:", convert['entry']) context['text'] = convert['entry'] if get_fmt == "json": return JsonResponse(context['result'], ) return render_to_response(Txn['template'], RequestContext(request, context, )) except requests.ConnectionError: print("Whoops - Problem connecting to FHIR Server") messages.error(request, "FHIR Server is unreachable. " "Are you on the CMS Network?") return HttpResponseRedirect(reverse('api:v1:home'))
def ExplanationOfBenefit(request, eob_id=None, Access_Mode=None, *args, **kwargs): """ Function-based interface to ExplanationOfBenefit :param request: :return: Use Cases: 1. No eob_id: Do Search and apply patient=Patient/User.Crosswalk.fhir_url_id 2. eob_id: Do Search and get eob with patient filter http://bluebuttonhapi-test.hhsdevcloud.us/baseDstu2/ExplanationOfBenefit ?_id=1286291&patient=Patient/1286160 3. eob_id and Access_mode = OPEN """ if settings.DEBUG: print("In apps.v1api.views.eob.ExplanationOfBenefit Function") print("request:", request.GET) process_mode = request.META['REQUEST_METHOD'] patient_id = lookup_xwalk(request) # try: # xwalk = Crosswalk.objects.get(user=request.user) # except Crosswalk.DoesNotExist: # messages.error(request, "Unable to find Patient ID") # return HttpResponseRedirect(reverse('api:v1:home')) # if patient_id == None: return HttpResponseRedirect(reverse('api:v1:home')) in_fmt = "json" get_fmt = get_format(request.GET) if settings.DEBUG: print("Request.GET :", request.GET) print("KWargs :", kwargs) print("FHIR URL ID :", patient_id) # We should have the xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) pass_to = FhirServerUrl() pass_to += "/ExplanationOfBenefit/" key = patient_id.strip() patient_filter= "patient=Patient/" + key # pass_to += patient_filter skip_parm = ['_id', '_format'] got_parms = build_params(request.GET, skip_parm)[1:] if got_parms: print("Got parms:", got_parms) pass_to += "?" + got_parms if Access_Mode == "OPEN": pass else: if "?" in pass_to: pass_to += "&" + patient_filter else: pass_to += "?" + patient_filter if eob_id: if "?" in pass_to: pass_to += "&" else: pass_to += "?" pass_to += "_id=" + eob_id print("Calling:", pass_to) # Set Context context = {'display':"EOB", 'name': "ExplanationOfBenefit", 'mask': True, 'key': key, 'eob': eob_id, 'get_fmt': get_fmt, 'in_fmt': in_fmt, 'pass_to': pass_to, 'template': 'v1api/eob.html', } if settings.DEBUG: print("Calling requests with pass_to:", pass_to) # We need to replace FHIR Server with External Server reference rewrite_from = settings.FHIR_SERVER_CONF['REWRITE_FROM'] rewrite_to = settings.FHIR_SERVER_CONF['REWRITE_TO'] try: r = requests.get(pass_to) context = process_page(request, r, context) return publish_page(request, context) except requests.ConnectionError: pass return cms_not_connected(request, 'api:v1:home')
def get_eob(request, eob_id=None, Access_Mode=None, *args, **kwargs): """ Display one or more EOBs but Always limit scope to Patient_Id :param request: :param eob_id: Request a specific EOB :param args: :param kwargs: :return: """ if settings.DEBUG: print("Request User Beneficiary(Patient):", request.user, "\nFor EOB Enquiry ") print("Request.GET :", request.GET) print("Access_Mode :", Access_Mode) print("KWargs :", kwargs) print("Args :", args) if Access_Mode == "OPEN": # Lookup using eob_id without patient filter key = "" else: try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: messages.error(request, "Unable to find Patient ID") return HttpResponseRedirect(reverse('api:v1:home')) if xwalk.fhir_url_id == "": err_msg = ['Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return HttpResponseRedirect(reverse('api:v1:home')) key = xwalk.fhir_url_id.strip() if settings.DEBUG: print("FHIR URL ID :", key) # We should have the xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # This is a hack to limit EOBs returned to this user only. # id_source['system'] = "https://mymedicare.gov/claims/beneficiary" # id_source['use'] = "official" # id_source['value'] = "Patient/"+str(patient_id) # id_list.append(unique_id(id_source)) # this search works: # http://fhir.bbonfhir.com:8080/fhir-p/ # search?serverId=bbonfhir_dev # &resource=ExplanationOfBenefit # ¶m.0.0=https%3A%2F%2Fmymedicare.gov%2Fclaims%2Fbeneficiary # ¶m.0.1=Patient%2F4995401 # ¶m.0.name=identifier # ¶m.0.type=token # &sort_by= # &sort_direction= # &resource-search-limit= # http://ec2-52-4-198-86.compute-1.amazonaws.com:8081/baseDstu2/ # ExplanationOfBenefit/?patient=Patient/131052&_format=json # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) skip_parm = ['_id', '_format', 'patient'] mask = True pass_to = FhirServerUrl() pass_to += "/ExplanationOfBenefit" pass_to += "/" if eob_id == None: pass else: pass_to += eob_id # We can allow an EOB but we MUST add a search Parameter # to limit the items found to those relevant to the Patient Id #if eob_id: # pass_to = eob_id + "/" # Now apply the search restriction to limit to patient _id #pass_to = pass_to + key + "/" if not key == "": pass_to += "?patient=" pass_to += "Patient/" pass_to += key pass_to = pass_to + "&" + build_params(request.GET, skip_parm)[1:] if settings.DEBUG: print("Pass_to from build_params:", pass_to) if settings.DEBUG: print("Calling requests with pass_to:", pass_to) # Set Context context = {'display': 'EOB', 'name': 'ExplanationOfBenefit', 'mask': mask, 'key': key, 'eob_id': eob_id, 'get_fmt': get_fmt, 'in_fmt': in_fmt, 'pass_to': pass_to, 'template': 'v1api/eob.html', } try: r = requests.get(pass_to) context = process_page(request,r,context) return publish_page(request, context) except requests.ConnectionError: pass return cms_not_connected(request, 'api:v1:home')
def PatientExplanationOfBenefit(request, patient_id=None, *args, **kwargs): """ Function-based interface to ExplanationOfBenefit :param request: :return: """ if patient_id == None: patient_id = lookup_xwalk(request) if patient_id == None: err_msg = ['Crosswalk lookup failed: Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return kickout_404(exit_message) if patient_id == "": err_msg = ['Sorry, No Patient Id provided', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return HttpResponseRedirect(reverse('api:v1:home')) if settings.DEBUG: print("In apps.v1api.views.eob.PatientExplanationOfBenefit Function") print("request:", request.GET) process_mode = request.META['REQUEST_METHOD'] in_fmt = "json" get_fmt = get_format(request.GET) if settings.DEBUG: print("Request.GET :", request.GET) print("KWargs :", kwargs) print("Patient :", patient_id) # We should have the patient_id from xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) pass_to = FhirServerUrl() pass_to += "/ExplanationOfBenefit/" key = patient_id patient_filter= "?patient=Patient/" + key pass_to += patient_filter skip_parm = ['_id', '_format'] pass_to = pass_to + "&" + build_params(request.GET, skip_parm)[1:] # Set Context context = {'display':"EOB", 'name': "ExplanationOfBenefit", 'mask': True, 'key': key, 'get_fmt': get_fmt, 'in_fmt': in_fmt, 'pass_to': pass_to, 'template': 'v1api/eob.html', } if settings.DEBUG: print("Calling requests with pass_to:", pass_to) try: r = requests.get(pass_to) context = process_page(request, r, context) return publish_page(request, context) except requests.ConnectionError: pass return cms_not_connected(request, 'api:v1:home')
def get_patient(request, *args, **kwargs): """ Display Patient Profile :param request: :param args: :param kwargs: :return: """ # DONE: Setup Patient API so that ID is not required # DONE: Do CrossWalk Lookup to get Patient ID if settings.DEBUG: print("Request User Beneficiary(Patient):", request.user) try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: messages.error(request, "Unable to find Patient ID") return HttpResponseRedirect(reverse('api:v1:home')) if xwalk.fhir_url_id == "": err_msg = ['Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return HttpResponseRedirect(reverse('api:v1:home')) if settings.DEBUG: print("Request.GET :", request.GET) print("KWargs :", kwargs) print("Crosswalk :", xwalk) print("GUID :", xwalk.guid) print("FHIR :", xwalk.fhir) print("FHIR URL ID :", xwalk.fhir_url_id) # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" # DONE: Define Transaction Dictionary to enable generic presentation of API Call Txn = {'name': "Patient", 'display': 'Patient', 'mask': True, 'server': settings.FHIR_SERVER, 'locn': "/baseDstu2/Patient/", 'template': 'v1api/patient.html', 'in_fmt': in_fmt, } # Since this is BlueButton and we are dealing with Patient Records # We need to limit the id search to the specific beneficiary. # A BlueButton user should not be able to request a patient profile # that is not their own. # We do this via the CrossWalk. The xwalk.fhir_url_id is the patient # id as used in the url. eg. /Patient/23/ # FHIR also allows an enquiry with ?_id=23. We need to detect that # and remove it from the parameters that are passed. # All other query parameters should be passed through to the # FHIR server. # Add URL Parameters to skip_parm to ignore or perform custom # processing with them. Use lower case values for matching. # DO NOT USE Uppercase skip_parm = ['_id', '_format'] key = xwalk.fhir_url_id.strip() mask = False if 'mask' in Txn: mask = Txn['mask'] pass_to = Txn['server'] + Txn['locn'] pass_to = pass_to + key + "/" # We need to detect if a format was requested in the URL Parameters # ie. _format=json|xml # modify get_format to default to return nothing. ie. make no change # internal data handling will be JSON # _format will drive external display # if no _format setting we will display in html (Current mode) # if valid _format string we will pass content through to display in # raw format get_fmt = get_format(request.GET) pass_to = pass_to + "?" + build_params(request.GET, skip_parm) mask_to = settings.DOMAIN # Set Context context = {'display': Txn['display'], 'name': Txn['name'], 'mask': mask, 'key': key, 'get_fmt': get_fmt, 'in_fmt': Txn['in_fmt'], # 'output' : "test output ", # 'args' : args, # 'kwargs' : kwargs, # 'get' : request.GET, 'pass_to': pass_to, } if settings.DEBUG: print("Calling Requests with:", pass_to) try: r = requests.get(pass_to) if get_fmt == "xml": xml_text = minidom.parseString(r.text) print("XML_TEXT:", xml_text.toxml()) root = ET.fromstring(r.text) # root_out = etree_to_dict(r.text) json_string = "" # json_out = xml_str_to_json_str(r.text, json_string) if settings.DEBUG: print("Root ET XML:", root) # print("XML:", root_out) # print("JSON_OUT:", json_out,":", json_string) drill_down = ['Bundle', 'entry', 'Patient', ] level = 0 tag0 = xml_text.getElementsByTagName("text") # tag1 = tag0.getElementsByTagName("entry") print("Patient?:", tag0) print("DrillDown:", drill_down[level]) print("root find:", root.find(drill_down[level])) for element in root: print("Child Element:", element) if drill_down[level] in element: level += 1 for element2 in element: print("Element2:", element2) if drill_down[level] in element2: print("Element2.iter()", element2.iter()) text = root[4][0][0][2][1].findtext("text") pretty_xml = xml_text.toprettyxml() if settings.DEBUG: print("TEXT:", text) # print("Pretty XML:", pretty_xml) context['result'] = pretty_xml # convert context['text'] = pretty_xml else: convert = OrderedDict(r.json()) # result = mark_safe(convert) if settings.DEBUG: print("Convert:", convert) # print("Next Level - entry:", convert['entry']) # print("\n ANOTHER Level- text:", convert['entry'][0]) content = OrderedDict(convert) text = "" if settings.DEBUG: print("Content:", content) print("resourceType:", content['resourceType']) print("text:", content['text']['div']) context['result'] = r.json() # convert context['text'] = content['text']['div'] if 'error' in content: context['error'] = context['issue'] # Setup the page if settings.DEBUG: print() # print("Context:",context) if get_fmt == 'xml' or get_fmt == 'json': if settings.DEBUG: print("Mode = ", get_fmt) print("Context['result']: ", context['result']) if get_fmt == "xml": return HttpResponse(context['result'], content_type='application/' + get_fmt) if get_fmt == "json": return JsonResponse(context['result'], ) else: return render_to_response(Txn['template'], RequestContext(request, context, )) except requests.ConnectionError: print("Whoops - Problem connecting to FHIR Server") messages.error(request, "FHIR Server is unreachable. Are you on the CMS Network?") return HttpResponseRedirect(reverse('api:v1:home'))
def get_eob(request, *args, **kwargs): """ Display one or more EOBs but Always limit scope to Patient_Id :param request: :param eob_id: Request a specific EOB :param args: :param kwargs: :return: """ if settings.DEBUG: print("Request User Beneficiary(Patient):", request.user, "\nFor EOB Enquiry ") try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: messages.error(request, "Unable to find Patient ID") return HttpResponseRedirect(reverse('api:v1:home')) if xwalk.fhir_url_id == "": err_msg = ['Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return HttpResponseRedirect(reverse('api:v1:home')) if settings.DEBUG: print("Request.GET :", request.GET) print("KWargs :", kwargs) print("Crosswalk :", xwalk) print("GUID :", xwalk.guid) print("FHIR :", xwalk.fhir) print("FHIR URL ID :", xwalk.fhir_url_id) # We should have the xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # This is a hack to limit EOBs returned to this user only. # id_source['system'] = "https://mymedicare.gov/claims/beneficiary" # id_source['use'] = "official" # id_source['value'] = "Patient/"+str(patient_id) # id_list.append(unique_id(id_source)) # this search works: # http://fhir.bbonfhir.com:8080/fhir-p/ # search?serverId=bbonfhir_dev # &resource=ExplanationOfBenefit # ¶m.0.0=https%3A%2F%2Fmymedicare.gov%2Fclaims%2Fbeneficiary # ¶m.0.1=Patient%2F4995401 # ¶m.0.name=identifier # ¶m.0.type=token # &sort_by= # &sort_direction= # &resource-search-limit= # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) # DONE: Define Transaction Dictionary to enable generic presentation of API Call Txn = {'name': "ExplanationOfBenefit", 'display': 'EOB', 'mask': True, 'server': settings.FHIR_SERVER, 'locn': "/baseDstu2/ExplanationOfBenefit/", 'template': 'v1api/eob.html', 'in_fmt': in_fmt, } skip_parm = ['_id', '_format'] key = xwalk.fhir_url_id.strip() mask = False if 'mask' in Txn: mask = Txn['mask'] pass_to = Txn['server'] + Txn['locn'] # We can allow an EOB but we MUST add a search Parameter # to limit the items found to those relevant to the Patient Id #if eob_id: # pass_to = eob_id + "/" # Now apply the search restriction to limit to patient _id #pass_to = pass_to + key + "/" pass_to = pass_to + "?identifier=" pass_to = pass_to + "https://mymedicare.gov/claims/beneficiary|" pass_to = pass_to + "Patient/" pass_to = pass_to + xwalk.fhir_url_id.strip() pass_to = pass_to + "&" + build_params(request.GET, skip_parm) if settings.DEBUG: print("Pass_to from build_params:", pass_to) if settings.DEBUG: print("Calling requests with pass_to:", pass_to) # Set Context context = {'display': Txn['display'], 'name': Txn['name'], 'mask': mask, 'key': key, 'get_fmt': get_fmt, 'in_fmt': Txn['in_fmt'], # 'output' : "test output ", # 'args' : args, # 'kwargs' : kwargs, # 'get' : request.GET, 'pass_to': pass_to, } try: r = requests.get(pass_to) if get_fmt == "xml": xml_text = minidom.parseString(r.text) pretty_xml = xml_text.toprettyxml() context['result'] = pretty_xml # convert context['text'] = pretty_xml return HttpResponse(context['result'], content_type='application/' + get_fmt) else: # get_fmt == "json" or None: convert = OrderedDict(r.json()) # result = mark_safe(convert) if settings.DEBUG: print("Convert:", convert) content = OrderedDict(convert) text = "" context['result'] = r.json() # convert if 'text' in content: context['text'] = content['resource']['text']['div'] else: if settings.DEBUG: print("Resource:", convert['entry']) context['text'] = convert['entry'] if get_fmt == "json": return JsonResponse(context['result'], ) return render_to_response(Txn['template'], RequestContext(request, context, )) except requests.ConnectionError: print("Whoops - Problem connecting to FHIR Server") messages.error(request, "FHIR Server is unreachable. " "Are you on the CMS Network?") return HttpResponseRedirect(reverse('api:v1:home'))
def get(self, request, eob_id, *args, **kwargs): # This is an ExplanationOfBenefit profile GET # # use request.user to lookup a crosswalk # get the FHIR Patient ID # Call the FHIR Patient Profile # Return the result # EOB will need to look up request.user and apply a filter on the EOB # The filter in search Parameters will be the GUID # We need to load the GUID when we are loading EOBs and # Patient Records. # http://ec2-52-4-198-86.compute-1.amazonaws.com:8081/baseDstu2/ # ExplanationOfBenefit/?patient=Patient/131052&_format=json if settings.DEBUG: print("in EOB.get with", eob_id) xwalk_id = lookup_xwalk(request, ) if settings.DEBUG: print("crosswalk:", xwalk_id) if xwalk_id == None: return HttpResponseRedirect(reverse_lazy('api:v1:home')) if settings.DEBUG: print("now we need to evaluate the parameters and arguments" " to work with ", xwalk_id, "and ", request.user) print("GET Parameters:", request.GET, ":") if patient_id == xwalk_id: key = eob_id else: key = xwalk_id.strip() in_fmt = "json" Txn = {'name': "ExplanationOfBenefit", 'display': 'ExplanationOfBenefit', 'mask': True, 'server': settings.FHIR_SERVER, 'locn': "/baseDstu2/ExplanationOfBenefit/", 'template': 'v1api/eob.html', 'in_fmt': in_fmt, } skip_parm = ['_id'] pass_params = build_params(request.GET, skip_parm) if settings.DEBUG: print("Pass_params=", pass_params) pass_to = Txn['server'] + Txn['locn'] + key + "/" print("Here is the URL to send, %s now get parameters" % pass_to) if pass_params != "": pass_to = pass_to + pass_params try: r = requests.get(pass_to) except requests.ConnectionError: if settings.DEBUG: print("Problem connecting to FHIR Server") messages.error(request, "FHIR Server is unreachable." ) return HttpResponseRedirect(reverse_lazy('api:v1:home')) text_out = "" if '_format=xml' in pass_to: text_out= minidom.parseString(r.text).toprettyxml() else: text_out = r.json() return HttpResponse('This is the EOB Pass Thru %s using %s ' 'and with response of %s' % (xwalk_id, pass_to, text_out))
def PatientExplanationOfBenefit(request, patient_id=None, *args, **kwargs): """ Function-based interface to ExplanationOfBenefit :param request: :return: """ if patient_id == None: patient_id = lookup_xwalk(request) if patient_id == None: err_msg = [ 'Crosswalk lookup failed: Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return kickout_404(exit_message) if patient_id == "": err_msg = [ 'Sorry, No Patient Id provided', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return HttpResponseRedirect(reverse('api:v1:home')) if settings.DEBUG: print("In apps.v1api.views.eob.PatientExplanationOfBenefit Function") print("request:", request.GET) process_mode = request.META['REQUEST_METHOD'] in_fmt = "json" get_fmt = get_format(request.GET) if settings.DEBUG: print("Request.GET :", request.GET) print("KWargs :", kwargs) print("Patient :", patient_id) # We should have the patient_id from xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) pass_to = FhirServerUrl() pass_to += "/ExplanationOfBenefit/" key = patient_id patient_filter = "?patient=Patient/" + key pass_to += patient_filter skip_parm = ['_id', '_format'] pass_to = pass_to + "&" + build_params(request.GET, skip_parm)[1:] # Set Context context = { 'display': "EOB", 'name': "ExplanationOfBenefit", 'mask': True, 'key': key, 'get_fmt': get_fmt, 'in_fmt': in_fmt, 'pass_to': pass_to, 'template': 'v1api/eob.html', } if settings.DEBUG: print("Calling requests with pass_to:", pass_to) try: r = requests.get(pass_to) context = process_page(request, r, context) return publish_page(request, context) except requests.ConnectionError: pass return cms_not_connected(request, 'api:v1:home')
def get_eob(request, Access_Mode=None, *args, **kwargs): """ Display one or more EOBs but Always limit scope to Patient_Id :param request: :param eob_id: Request a specific EOB :param args: :param kwargs: :return: """ if settings.DEBUG: print("Request User Beneficiary(Patient):", request.user, "\nFor EOB Enquiry ") print("Request.GET :", request.GET) print("Access_Mode :", Access_Mode) print("KWargs :", kwargs) print("Args :", args) if Access_Mode == "OPEN" and kwargs['patient_id']!="": # Lookup using patient_id for fhir_url_id key = kwargs['patient_id'].strip() else: try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: messages.error(request, "Unable to find Patient ID") return HttpResponseRedirect(reverse('api:v1:home')) if xwalk.fhir_url_id == "": err_msg = ['Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return HttpResponseRedirect(reverse('api:v1:home')) key = xwalk.fhir_url_id.strip() if settings.DEBUG: print("FHIR URL ID :", key) # We should have the xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # This is a hack to limit EOBs returned to this user only. # id_source['system'] = "https://mymedicare.gov/claims/beneficiary" # id_source['use'] = "official" # id_source['value'] = "Patient/"+str(patient_id) # id_list.append(unique_id(id_source)) # this search works: # http://fhir.bbonfhir.com:8080/fhir-p/ # search?serverId=bbonfhir_dev # &resource=ExplanationOfBenefit # ¶m.0.0=https%3A%2F%2Fmymedicare.gov%2Fclaims%2Fbeneficiary # ¶m.0.1=Patient%2F4995401 # ¶m.0.name=identifier # ¶m.0.type=token # &sort_by= # &sort_direction= # &resource-search-limit= # http://ec2-52-4-198-86.compute-1.amazonaws.com:8081/baseDstu2/ # ExplanationOfBenefit/?patient=Patient/131052&_format=json # # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) # DONE: Define Transaction Dictionary to enable generic presentation of API Call Txn = {'name': "ExplanationOfBenefit", 'display': 'EOB', 'mask': True, 'template': 'v1api/eob.html', 'in_fmt': in_fmt, } skip_parm = ['_id', '_format', 'patient'] mask = True pass_to = FhirServerUrl() pass_to += "/ExplanationOfBenefit" pass_to += "/" # We can allow an EOB but we MUST add a search Parameter # to limit the items found to those relevant to the Patient Id #if eob_id: # pass_to = eob_id + "/" # Now apply the search restriction to limit to patient _id #pass_to = pass_to + key + "/" pass_to += "?patient=" pass_to += "Patient/" pass_to += key pass_to = pass_to + "&" + build_params(request.GET, skip_parm)[1:] if settings.DEBUG: print("Pass_to from build_params:", pass_to) if settings.DEBUG: print("Calling requests with pass_to:", pass_to) # Set Context context = {'display': 'EOB', 'name': 'ExplanationOfBenefit', 'mask': mask, 'key': key, 'get_fmt': get_fmt, 'in_fmt': in_fmt, 'pass_to': pass_to, } try: r = requests.get(pass_to) context = process_page(request,r,context) # Setup the page if settings.DEBUG: print("Context-result:", context['result']) # print("Context-converted:", json.dumps(context['result'], sort_keys=False)) # print("Context:",context) if get_fmt == 'xml' or get_fmt == 'json': if settings.DEBUG: print("Mode = ", get_fmt) print("Context['result']: ", context['result']) if get_fmt == "xml": return HttpResponse(context['result'], content_type='application/' + get_fmt) if get_fmt == "json": #return HttpResponse(context['result'], mimetype="application/json") return JsonResponse(context['import_text'], safe=False) else: if context['text'] == "No user readable content to display" or context['text']=="": result = json.loads(context['result'], object_pairs_hook=OrderedDict) print("Result::", result) context['text'] += "<br/> extracting information from returned record:<br/>" context['text'] += "<table>\n" if 'name' in result: patient_name = result['name'][0]['given'][0] patient_name += " " patient_name += result['name'][0]['family'][0] context['text'] += tr_build_item("Patient Name ", patient_name) if 'address' in result: context['text'] += tr_build_item("Patient Address", result['address'][0]['line'][0]) if 'birthDate' in result: context['text'] += tr_build_item("Birth Date", result['birthDate']) if 'identifier' in result: context['text'] += tr_build_item("Patient ID", result['identifier'][0]['value']) context['text'] += "</table>" return render_to_response('v1api/eob.html', RequestContext(request, context, )) except requests.ConnectionError: print("Whoops - Problem connecting to FHIR Server") messages.error(request, "FHIR Server is unreachable. " "Are you on the CMS Network?") return HttpResponseRedirect(reverse('api:v1:home'))
def ExplanationOfBenefit(request, eob_id=None, Access_Mode=None, *args, **kwargs): """ Function-based interface to ExplanationOfBenefit :param request: :return: Use Cases: 1. No eob_id: Do Search and apply patient=Patient/User.Crosswalk.fhir_url_id 2. eob_id: Do Search and get eob with patient filter http://bluebuttonhapi-test.hhsdevcloud.us/baseDstu2/ExplanationOfBenefit ?_id=1286291&patient=Patient/1286160 3. eob_id and Access_mode = OPEN """ if settings.DEBUG: print("In apps.v1api.views.eob.ExplanationOfBenefit Function") print("request:", request.GET) process_mode = request.META['REQUEST_METHOD'] patient_id = lookup_xwalk(request) # try: # xwalk = Crosswalk.objects.get(user=request.user) # except Crosswalk.DoesNotExist: # messages.error(request, "Unable to find Patient ID") # return HttpResponseRedirect(reverse('api:v1:home')) # if patient_id == None: return HttpResponseRedirect(reverse('api:v1:home')) in_fmt = "json" get_fmt = get_format(request.GET) if settings.DEBUG: print("Request.GET :", request.GET) print("KWargs :", kwargs) print("FHIR URL ID :", patient_id) # We should have the xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) pass_to = FhirServerUrl() pass_to += "/ExplanationOfBenefit/" key = patient_id.strip() patient_filter = "patient=Patient/" + key # pass_to += patient_filter skip_parm = ['_id', '_format'] got_parms = build_params(request.GET, skip_parm)[1:] if got_parms: print("Got parms:", got_parms) pass_to += "?" + got_parms if Access_Mode == "OPEN": pass else: if "?" in pass_to: pass_to += "&" + patient_filter else: pass_to += "?" + patient_filter if eob_id: if "?" in pass_to: pass_to += "&" else: pass_to += "?" pass_to += "_id=" + eob_id print("Calling:", pass_to) # Set Context context = { 'display': "EOB", 'name': "ExplanationOfBenefit", 'mask': True, 'key': key, 'eob': eob_id, 'get_fmt': get_fmt, 'in_fmt': in_fmt, 'pass_to': pass_to, 'template': 'v1api/eob.html', } if settings.DEBUG: print("Calling requests with pass_to:", pass_to) # We need to replace FHIR Server with External Server reference rewrite_from = settings.FHIR_SERVER_CONF['REWRITE_FROM'] rewrite_to = settings.FHIR_SERVER_CONF['REWRITE_TO'] try: r = requests.get(pass_to) context = process_page(request, r, context) return publish_page(request, context) except requests.ConnectionError: pass return cms_not_connected(request, 'api:v1:home')
def get_patient(request, Access_Mode=None, *args, **kwargs): """ Display Patient Profile :param request: :param Access_Mode = [None], Open :param args: :param kwargs: :return: """ # Access_Mode = None = Do Crosswalk using Request.user # Access_Mode = OPEN = use kwargs['patient_id'] if settings.DEBUG: print("Request.GET :", request.GET) print("Access_Mode :", Access_Mode) print("KWargs :", kwargs) print("Args :", args) if Access_Mode == "OPEN" and kwargs['patient_id']!="": # Lookup using patient_id for fhir_url_id key = kwargs['patient_id'].strip() else: # DONE: Setup Patient API so that ID is not required # DONE: Do CrossWalk Lookup to get Patient ID if settings.DEBUG: print("Request User Beneficiary(Patient):", request.user) try: xwalk = Crosswalk.objects.get(user=request.user.id) except Crosswalk.DoesNotExist: reason = "Unable to find Patient ID for user:%s[%s]" % (request.user, request.user.id) messages.error(request, reason) return kickout_404(reason) # return HttpResponseRedirect(reverse('api:v1:home')) if xwalk.fhir_url_id == "": err_msg = ['Crosswalk lookup failed: Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return kickout_404(exit_message) # return HttpResponseRedirect(reverse('api:v1:home')) key = xwalk.fhir_url_id.strip() if settings.DEBUG: print("Crosswalk :", xwalk) print("GUID :", xwalk.guid) print("FHIR :", xwalk.fhir) print("FHIR URL ID :", key) # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" # fhir_server_configuration = {"SERVER":"http://fhir-test.bbonfhir.com:8081", # "PATH":"", # "RELEASE":"/baseDstu2"} # FHIR_SERVER_CONF = fhir_server_configuration # FHIR_SERVER = FHIR_SERVER_CONF['SERVER'] + FHIR_SERVER_CONF['PATH'] # Since this is BlueButton and we are dealing with Patient Records # We need to limit the id search to the specific beneficiary. # A BlueButton user should not be able to request a patient profile # that is not their own. # We do this via the CrossWalk. The xwalk.fhir_url_id is the patient # id as used in the url. eg. /Patient/23/ # FHIR also allows an enquiry with ?_id=23. We need to detect that # and remove it from the parameters that are passed. # All other query parameters should be passed through to the # FHIR server. # Add URL Parameters to skip_parm to ignore or perform custom # processing with them. Use lower case values for matching. # DO NOT USE Uppercase skip_parm = ['_id', '_format'] mask = True pass_to = FhirServerUrl() pass_to += "/Patient" pass_to += "/" pass_to = pass_to + key + "/" # We need to detect if a format was requested in the URL Parameters # ie. _format=json|xml # modify get_format to default to return nothing. ie. make no change # internal data handling will be JSON # _format will drive external display # if no _format setting we will display in html (Current mode) # if valid _format string we will pass content through to display in # raw format get_fmt = get_format(request.GET) print("pass_to:", pass_to) pass_to = pass_to + build_params(request.GET, skip_parm) print("pass_to added to:", pass_to) mask_to = settings.DOMAIN # Set Context context = {'display':"Patient", 'name': "Patient", 'mask': mask, 'key': key, 'get_fmt': get_fmt, 'in_fmt': in_fmt, # 'output' : "test output ", # 'args' : args, # 'kwargs' : kwargs, # 'get' : request.GET, 'pass_to': pass_to, } if settings.DEBUG: print("Calling Requests with:", pass_to) try: r = requests.get(pass_to) context = process_page(request,r,context) # Setup the page if settings.DEBUG: print("Context-result:", context['result']) # print("Context-converted:", json.dumps(context['result'], sort_keys=False)) # print("Context:",context) if get_fmt == 'xml' or get_fmt == 'json': if settings.DEBUG: print("Mode = ", get_fmt) print("Context['result']: ", context['result']) if get_fmt == "xml": return HttpResponse(context['result'], content_type='application/' + get_fmt) if get_fmt == "json": #return HttpResponse(context['result'], mimetype="application/json") return JsonResponse(context['import_text'], safe=False) else: if context['text'] == "No user readable content to display" or context['text']=="": result = json.loads(context['result'], object_pairs_hook=OrderedDict) print("Result::", result) context['text'] += "<br/> extracting information from returned record:<br/>" context['text'] += "<table>\n" if 'name' in result: patient_name = result['name'][0]['given'][0] patient_name += " " patient_name += result['name'][0]['family'][0] context['text'] += tr_build_item("Patient Name ", patient_name) if 'address' in result: context['text'] += tr_build_item("Patient Address", result['address'][0]['line'][0]) if 'birthDate' in result: context['text'] += tr_build_item("Birth Date", result['birthDate']) if 'identifier' in result: context['text'] += tr_build_item("Patient ID", result['identifier'][0]['value']) context['text'] += "</table>" return render_to_response('v1api/patient.html', RequestContext(request, context, )) except requests.ConnectionError: print("Whoops - Problem connecting to FHIR Server") messages.error(request, "FHIR Server is unreachable. Are you on the CMS Network?") return HttpResponseRedirect(reverse('api:v1:home'))
def get_eob(request, Access_Mode=None, *args, **kwargs): """ Display one or more EOBs but Always limit scope to Patient_Id :param request: :param eob_id: Request a specific EOB :param args: :param kwargs: :return: """ if settings.DEBUG: print("Request User Beneficiary(Patient):", request.user, "\nFor EOB Enquiry ") print("Request.GET :", request.GET) print("Access_Mode :", Access_Mode) print("KWargs :", kwargs) print("Args :", args) if Access_Mode == "OPEN" and kwargs['patient_id'] != "": # Lookup using patient_id for fhir_url_id key = kwargs['patient_id'].strip() else: try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: messages.error(request, "Unable to find Patient ID") return HttpResponseRedirect(reverse('api:v1:home')) if xwalk.fhir_url_id == "": err_msg = [ 'Sorry, We were unable to find', 'your record', ] exit_message = concat_string("", msg=err_msg, delimiter=" ", last=".") messages.error(request, exit_message) return HttpResponseRedirect(reverse('api:v1:home')) key = xwalk.fhir_url_id.strip() if settings.DEBUG: print("FHIR URL ID :", key) # We should have the xwalk.FHIR_url_id # So we will construct the EOB Identifier to include # This is a hack to limit EOBs returned to this user only. # id_source['system'] = "https://mymedicare.gov/claims/beneficiary" # id_source['use'] = "official" # id_source['value'] = "Patient/"+str(patient_id) # id_list.append(unique_id(id_source)) # this search works: # http://fhir.bbonfhir.com:8080/fhir-p/ # search?serverId=bbonfhir_dev # &resource=ExplanationOfBenefit # ¶m.0.0=https%3A%2F%2Fmymedicare.gov%2Fclaims%2Fbeneficiary # ¶m.0.1=Patient%2F4995401 # ¶m.0.name=identifier # ¶m.0.type=token # &sort_by= # &sort_direction= # &resource-search-limit= # http://ec2-52-4-198-86.compute-1.amazonaws.com:8081/baseDstu2/ # ExplanationOfBenefit/?patient=Patient/131052&_format=json # # We will deal internally in JSON Format if caller does not choose # a format in_fmt = "json" get_fmt = get_format(request.GET) # DONE: Define Transaction Dictionary to enable generic presentation of API Call Txn = { 'name': "ExplanationOfBenefit", 'display': 'EOB', 'mask': True, 'template': 'v1api/eob.html', 'in_fmt': in_fmt, } skip_parm = ['_id', '_format', 'patient'] mask = True pass_to = FhirServerUrl() pass_to += "/ExplanationOfBenefit" pass_to += "/" # We can allow an EOB but we MUST add a search Parameter # to limit the items found to those relevant to the Patient Id #if eob_id: # pass_to = eob_id + "/" # Now apply the search restriction to limit to patient _id #pass_to = pass_to + key + "/" pass_to += "?patient=" pass_to += "Patient/" pass_to += key pass_to = pass_to + "&" + build_params(request.GET, skip_parm)[1:] if settings.DEBUG: print("Pass_to from build_params:", pass_to) if settings.DEBUG: print("Calling requests with pass_to:", pass_to) # Set Context context = { 'display': 'EOB', 'name': 'ExplanationOfBenefit', 'mask': mask, 'key': key, 'get_fmt': get_fmt, 'in_fmt': in_fmt, 'pass_to': pass_to, } try: r = requests.get(pass_to) context = process_page(request, r, context) # Setup the page if settings.DEBUG: print("Context-result:", context['result']) # print("Context-converted:", json.dumps(context['result'], sort_keys=False)) # print("Context:",context) if get_fmt == 'xml' or get_fmt == 'json': if settings.DEBUG: print("Mode = ", get_fmt) print("Context['result']: ", context['result']) if get_fmt == "xml": return HttpResponse(context['result'], content_type='application/' + get_fmt) if get_fmt == "json": #return HttpResponse(context['result'], mimetype="application/json") return JsonResponse(context['import_text'], safe=False) else: if context[ 'text'] == "No user readable content to display" or context[ 'text'] == "": result = json.loads(context['result'], object_pairs_hook=OrderedDict) print("Result::", result) context[ 'text'] += "<br/> extracting information from returned record:<br/>" context['text'] += "<table>\n" if 'name' in result: patient_name = result['name'][0]['given'][0] patient_name += " " patient_name += result['name'][0]['family'][0] context['text'] += tr_build_item( "Patient Name ", patient_name) if 'address' in result: context['text'] += tr_build_item( "Patient Address", result['address'][0]['line'][0]) if 'birthDate' in result: context['text'] += tr_build_item("Birth Date", result['birthDate']) if 'identifier' in result: context['text'] += tr_build_item( "Patient ID", result['identifier'][0]['value']) context['text'] += "</table>" return render_to_response('v1api/eob.html', RequestContext( request, context, )) except requests.ConnectionError: print("Whoops - Problem connecting to FHIR Server") messages.error( request, "FHIR Server is unreachable. " "Are you on the CMS Network?") return HttpResponseRedirect(reverse('api:v1:home'))