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})
Example #2
0
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})