def test_crosswalk_fhir_id(self): """ Get the Crosswalk FHIR_Id """ u = User.objects.create_user( username="******", first_name="Billybob", last_name="Button", email="*****@*****.**", password="******", ) UserProfile.objects.create(user=u, user_type="DEV", create_applications=True) x = Crosswalk() x.user = u x.fhir_id = "Patient/23456" x.save() result = crosswalk_patient_id(u) self.assertEqual(x.fhir_id, result) # Test the dt_reference for Patient result = dt_patient_reference(u) expect = {"reference": x.fhir_id} self.assertEqual(result, expect)
def setUp(self): # Setup the RequestFactory # I could probably update this to use a Mock() self.factory = RequestFactory() self.user = User.objects.create_user(username="******", email="fred4@...", password="******") xwalk = Crosswalk() xwalk.user = self.user xwalk.fhir_id = "Patient/12345" xwalk.save()
def test_crosswalk_fhir_id(self): """ Get the Crosswalk FHIR_Id """ u = User.objects.create_user( username="******", first_name="Billybob", last_name="Button", email='*****@*****.**', password="******", ) UserProfile.objects.create(user=u, user_type="DEV", create_applications=True) x = Crosswalk() x.user = u x.fhir_id = "Patient/23456" x.set_hicn(uuid.uuid4()) x.save() result = crosswalk_patient_id(u) self.assertEqual(x.fhir_id, result) # Test the dt_reference for Patient result = dt_patient_reference(u) expect = {'reference': x.fhir_id} self.assertEqual(result, expect)
def setUp(self): # Setup the RequestFactory # I could probably update this to use a Mock() self.factory = RequestFactory() self.user = User.objects.create_user(username='******', email='fred4@...', password='******') xwalk = Crosswalk() xwalk.user = self.user xwalk.fhir_id = "Patient/12345" xwalk.set_hicn(uuid.uuid4()) xwalk.save()
def test_user_logged_in_crosswalk_no_fhir_id(self): """ check_crosswalk - User Logged in. Entry in xwalk. No fhir_id """ request = self.factory.get('/create_test_account/bb_upload/') request.user = self.user request.messages = self.messages # setattr(request, 'session', 'session') # messages = FallbackStorage(request) # setattr(request, '_messages', messages) xwalk = Crosswalk() xwalk.user = request.user xwalk.save() result = check_crosswalk(request) # print("Crosswalk found Result:%s" % result.content) self.assertContains(result, 'Paste the contents of a Blue Button Text File', count=None, status_code=200)
def test_user_logged_in_crosswalk_fhir_id(self): """ check_crosswalk - User Logged in. Entry in xwalk with FHIR_ID """ request = self.factory.get('/create_test_account/bb_upload/') request.user = self.user request._messages = FakeMessages() # setattr(request, 'session', 'session') # request.messages = self.messages # setattr(request, '_messages', messages) xwalk = Crosswalk() xwalk.user = self.user xwalk.fhir_id = "12345678" xwalk.save() expected = 'Account is already linked to a FHIR resource.' result = check_crosswalk(request) result = result # print("Crosswalk found " # "with FHIR ID Result:%s" % request._messages.pop) self.assertEqual(request._messages.pop, expected)
def update_crosswalk(user, server, id): """ Look up Crosswalk for user and add patient_id """ try: xwalk = Crosswalk.objects.get(user=user) except Crosswalk.DoesNotExist: xwalk = Crosswalk() xwalk.user = user if xwalk.fhir_id: return xwalk else: xwalk.fhir_id = id xwalk.fhir_source = server xwalk.save() return xwalk
def convert_bb(request): """ Read bb_text from CrossWalk Convert to JSON (bb_to_json) Analyze JSON Get Claims Search for Claims Check for Unique Patient Get Unique Patient Store ID in Crosswalk """ try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: xwalk = Crosswalk() xwalk.user = request.user # We may want to add the default FHIR Server to the crosswalk entry # before we save. # xwalk.fhir_source = xwalk.save() # Do we have Blue Button text to work with? bb_text = check_for_bb_text(xwalk.user) if not bb_text: return HttpResponseRedirect(reverse('home')) # Convert from text to JSON bb_result = bb_to_json(request, bb_text.bb_content) bb_json = bb_result['mmg_bbjson'] # print("JSON:", bb_result['mmg_bbjson']) # Extract Claims from bb_json bb_claims = getbbclm(request, bb_result['mmg_bbjson']) # print("\nClaims:", bb_claims) fhir_claims = eval_claims(request, bb_claims) # Take bb_claims and get a unique list of patients # Hopefully we only have ONE bb_patient_ids = unique_keys(fhir_claims, key="patient") # print("Patient IDs:", bb_patient_ids) if len(bb_patient_ids) is 0: # We have a problem... # Too many or Too Few Patient IDs returned from Claims messages.error(request, "Unable to find a Patient Match") elif len(bb_patient_ids) > 1: messages.error(request, "Unable to match to a unique Patient ID") else: messages.info(request, "We found a match %s" % bb_patient_ids[0]) if not xwalk.fhir_id: # We need the Id from Patient ID # It should be in format Patient/{ID} fhir_id = bb_patient_ids[0].split('/') if len(fhir_id) > 1: fhir_id_num = fhir_id[-1] xwalk.fhir_id = fhir_id_num mc_prof = {} if bb_json['result'] == "OK": mc_prof['bb_json'] = bb_json['mmg_bbjson'] mc_prof['email'] = get_bbemail(request, bb_json) mc_prof['profile'] = get_bbprof(request, bb_json) # Extract claims from Blue Button JSON mc_prof['claims'] = getbbclm(request, bb_json) return render(request, 'eimm/bluebutton_analytics.html', {'content': bb_json, 'profile': mc_prof, 'profilep': pretty_json(mc_prof['profile']), 'claimsp': pretty_json(mc_prof['claims']) }) return HttpResponseRedirect(reverse('home'))
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: """ try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: xwalk = Crosswalk() xwalk.user = request.user # We may want to add the default FHIR Server to the crosswalk entry # before we save. # xwalk.fhir_source = xwalk.save() xwalk = Crosswalk.objects.get(user=request.user) form = Medicare_Connect() # logger.debug("In eimm.mym_login.connect_first:" # "User:%s \nCrosswalk:%s" % (xwalk.user, xwalk)) if request.POST: form = Medicare_Connect(request.POST) if form.is_valid(): if form.cleaned_data['mmg_user'] and form.cleaned_data['mmg_pwd']: context = {} # make the call # logger.debug("MAKING THE CALL with:", # form.cleaned_data['mmg_user'], # "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 mmg['status'] == 'FAIL': # we had a problem with login messages.info(request, mark_safe("Do you need to connect to " "<a href='https://mymedicare.gov'" " target='_blank'>" "MyMedicare.gov</a> to check your" " account?")) return render(request, 'eimm/medicare_connect.html', {'form': form}) # Now we go and get the BlueButton Text file mmg_bb = get_bluebutton(request, mmg) # mmg_mail = {} # logger.debug("Blue Button returned:", mmg_bb) mc_prof = {} if mmg_bb['status'] == "OK": pretty_name = pretty_json(split_name(mmg['mmg_name'])) mc_prof['user'] = mmg['mmg_user'] mc_prof['password'] = mmg['mmg_pwd'] mc_prof['name'] = mmg['mmg_name'] mc_prof['HumanName'] = pretty_name mc_prof['account'] = mmg['mmg_account'] # need to check what is returned in mmg_account. messages.success(request, "Connection succeeded for " + mc_prof['name'] + "[" + mc_prof['user'] + "].") # Update the Medicare user name to the Crosswalk if xwalk.mb_user == '': xwalk.mb_user = mc_prof['user'] if mmg_bb['status'] == "OK": # We need to save the Blue Button Text # print("We are okay to update mmg_bbdata", # "\n with ", mmg_bb['mmg_bbdata'][:250]) mc_prof['bb_data'] = mmg_bb['mmg_bbdata'] # Save the Blue Button text to the Crosswalk # TODO: Save to BlueButtonText write_bb_text = bb_update_or_create(xwalk.user, mmg_bb['mmg_bbdata']) # xwalk.bb_text = mmg_bb['mmg_bbdata'] if write_bb_text: bb_text = mmg_bb['mmg_bbdata'] else: bb_text = '{}' # Convert the text to JSON result = bb_to_json(request, bb_text) # logger.debug("BB Conversion:", result) if result['result'] == "OK": mc_prof['bb_json'] = result['mmg_bbjson'] mc_prof['email'] = get_bbemail(request, mc_prof['bb_json']) mc_prof['profile'] = get_bbprof(request, mc_prof['bb_json']) # Extract claims from Blue Button JSON mc_prof['claims'] = getbbclm(request, mc_prof['bb_json']) # logger.debug("returned json from xwalk:", result) # 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() context['mmg'] = mmg return render(request, 'eimm/bluebutton_analytics.html', {'content': context, 'profile': mc_prof, 'profilep': pretty_json(mc_prof['profile']), 'claimsp': pretty_json(mc_prof['claims']) }) else: form = Medicare_Connect() # logger.debug("setting up the GET:") return render(request, 'eimm/medicare_connect.html', {'form': form})
def convert_bb(request): """ Read bb_text from CrossWalk Convert to JSON (bb_to_json) Analyze JSON Get Claims Search for Claims Check for Unique Patient Get Unique Patient Store ID in Crosswalk """ try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: xwalk = Crosswalk() xwalk.user = request.user # We may want to add the default FHIR Server to the crosswalk entry # before we save. # xwalk.fhir_source = xwalk.save() # Do we have Blue Button text to work with? bb_text = check_for_bb_text(xwalk.user) if not bb_text: return HttpResponseRedirect(reverse('home')) # Convert from text to JSON bb_result = bb_to_json(request, bb_text.bb_content) bb_json = bb_result['mmg_bbjson'] # print("JSON:", bb_result['mmg_bbjson']) # Extract Claims from bb_json bb_claims = getbbclm(request, bb_result['mmg_bbjson']) # print("\nClaims:", bb_claims) fhir_claims = eval_claims(request, bb_claims) # Take bb_claims and get a unique list of patients # Hopefully we only have ONE bb_patient_ids = unique_keys(fhir_claims, key="patient") # print("Patient IDs:", bb_patient_ids) if len(bb_patient_ids) is 0: # We have a problem... # Too many or Too Few Patient IDs returned from Claims messages.error(request, "Unable to find a Patient Match") elif len(bb_patient_ids) > 1: messages.error(request, "Unable to match to a unique Patient ID") else: messages.info(request, "We found a match %s" % bb_patient_ids[0]) if not xwalk.fhir_id: # We need the Id from Patient ID # It should be in format Patient/{ID} fhir_id = bb_patient_ids[0].split('/') if len(fhir_id) > 1: fhir_id_num = fhir_id[-1] xwalk.fhir_id = fhir_id_num mc_prof = {} if bb_json['result'] == "OK": mc_prof['bb_json'] = bb_json['mmg_bbjson'] mc_prof['email'] = get_bbemail(request, bb_json) mc_prof['profile'] = get_bbprof(request, bb_json) # Extract claims from Blue Button JSON mc_prof['claims'] = getbbclm(request, bb_json) return render( request, 'eimm/bluebutton_analytics.html', { 'content': bb_json, 'profile': mc_prof, 'profilep': pretty_json(mc_prof['profile']), 'claimsp': pretty_json(mc_prof['claims']) }) return HttpResponseRedirect(reverse('home'))
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: """ try: xwalk = Crosswalk.objects.get(user=request.user) except Crosswalk.DoesNotExist: xwalk = Crosswalk() xwalk.user = request.user # We may want to add the default FHIR Server to the crosswalk entry # before we save. # xwalk.fhir_source = xwalk.save() xwalk = Crosswalk.objects.get(user=request.user) form = Medicare_Connect() # logger.debug("In eimm.mym_login.connect_first:" # "User:%s \nCrosswalk:%s" % (xwalk.user, xwalk)) if request.POST: form = Medicare_Connect(request.POST) if form.is_valid(): if form.cleaned_data['mmg_user'] and form.cleaned_data['mmg_pwd']: context = {} # make the call # logger.debug("MAKING THE CALL with:", # form.cleaned_data['mmg_user'], # "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 mmg['status'] == 'FAIL': # we had a problem with login messages.info( request, mark_safe("Do you need to connect to " "<a href='https://mymedicare.gov'" " target='_blank'>" "MyMedicare.gov</a> to check your" " account?")) return render(request, 'eimm/medicare_connect.html', {'form': form}) # Now we go and get the BlueButton Text file mmg_bb = get_bluebutton(request, mmg) # mmg_mail = {} # logger.debug("Blue Button returned:", mmg_bb) mc_prof = {} if mmg_bb['status'] == "OK": pretty_name = pretty_json(split_name(mmg['mmg_name'])) mc_prof['user'] = mmg['mmg_user'] mc_prof['password'] = mmg['mmg_pwd'] mc_prof['name'] = mmg['mmg_name'] mc_prof['HumanName'] = pretty_name mc_prof['account'] = mmg['mmg_account'] # need to check what is returned in mmg_account. messages.success( request, "Connection succeeded for " + mc_prof['name'] + "[" + mc_prof['user'] + "].") # Update the Medicare user name to the Crosswalk if xwalk.mb_user == '': xwalk.mb_user = mc_prof['user'] if mmg_bb['status'] == "OK": # We need to save the Blue Button Text # print("We are okay to update mmg_bbdata", # "\n with ", mmg_bb['mmg_bbdata'][:250]) mc_prof['bb_data'] = mmg_bb['mmg_bbdata'] # Save the Blue Button text to the Crosswalk # TODO: Save to BlueButtonText write_bb_text = bb_update_or_create( xwalk.user, mmg_bb['mmg_bbdata']) # xwalk.bb_text = mmg_bb['mmg_bbdata'] if write_bb_text: bb_text = mmg_bb['mmg_bbdata'] else: bb_text = '{}' # Convert the text to JSON result = bb_to_json(request, bb_text) # logger.debug("BB Conversion:", result) if result['result'] == "OK": mc_prof['bb_json'] = result['mmg_bbjson'] mc_prof['email'] = get_bbemail( request, mc_prof['bb_json']) mc_prof['profile'] = get_bbprof( request, mc_prof['bb_json']) # Extract claims from Blue Button JSON mc_prof['claims'] = getbbclm( request, mc_prof['bb_json']) # logger.debug("returned json from xwalk:", result) # 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() context['mmg'] = mmg return render( request, 'eimm/bluebutton_analytics.html', { 'content': context, 'profile': mc_prof, 'profilep': pretty_json(mc_prof['profile']), 'claimsp': pretty_json(mc_prof['claims']) }) else: form = Medicare_Connect() # logger.debug("setting up the GET:") return render(request, 'eimm/medicare_connect.html', {'form': form})