Example #1
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['member'] = self.get_member()
        data = fetch_member_data(context['member'], 'sharemyhealth')
        context['updated_at'] = parse_timestamp(data.get('updated_at'))
        if context['updated_at']:
            context['time_since_update'] = (
                datetime.now(timezone.utc) - context['updated_at']
            )
        fhir_data = data.get('fhir_data')
        if settings.DEBUG:
            context['data'] = fhir_data

        if fhir_data is None or 'entry' not in fhir_data or not fhir_data['entry']:
            delete_memoized(fetch_member_data, context[
                            'member'], 'sharemyhealth')

        prescriptions = get_prescriptions(
            fhir_data, id=context[
                'resource_id'], incl_practitioners=True, json=True
        )
        if not prescriptions:
            return Http404()
        else:
            context['prescription'] = next(iter(prescriptions.values()))
            return context
Example #2
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['member'] = self.get_member()
        # Get the data for the member, and set it in the context
        data = fetch_member_data(context['member'], 'sharemyhealth')
        context['updated_at'] = parse_timestamp(data.get('updated_at'))
        if context['updated_at']:
            context['time_since_update'] = (
                datetime.now(timezone.utc) - context['updated_at']
            )
        fhir_data = data.get('fhir_data')
        if settings.DEBUG:
            context['data'] = data

        if fhir_data is None or 'entry' not in fhir_data or not fhir_data['entry']:
            delete_memoized(fetch_member_data, context[
                            'member'], 'sharemyhealth')

        # put the current resources in the summary tab.  We will not show the
        # other options in this tab.
        conditions_data = get_resource_data(fhir_data, 'Condition')
        prescription_data = get_prescriptions(data)

        all_records = RECORDS
        summarized_records = []
        notes_headers = ['Agent Name', 'Organization', 'Date']
        for record in all_records:
            # adding data for each resourceType in response from endpoint
            if record['name'] == 'Diagnoses':
                record['count'] = len(conditions_data)
                record['data'] = conditions_data
                summarized_records.append(record)

            if record['name'] == 'Prescriptions':
                record['count'] = len(prescription_data)
                record['data'] = prescription_data
                summarized_records.append(record)

            context.setdefault('summarized_records', summarized_records)

        context.setdefault('notes_headers', notes_headers)
        # TODO: include notes in the context data.

        return context
Example #3
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        data = fetch_member_data(context['member'], 'sharemyhealth')
        if data is None or 'entry' not in data or not data['entry']:
            delete_memoized(fetch_member_data, context['member'],
                            'sharemyhealth')

        # if in the future we need more info
        # location_data = get_resource_data(data, 'Location')
        # provider_data = get_resource_data(data, 'Practitioner')

        # encounter resourceType seems to hold enough info to show provider
        # name, location name and date of visit info
        encounter_data = get_resource_data(data, 'Encounter')
        providers_headers = ['Doctor’s Name', 'Clinic', 'Date Last Seen']

        providers = []
        for encounter in encounter_data:
            if 'participant' not in encounter:
                continue
            provider = {}
            provider['doctor-name'] = encounter['participant'][0][
                'individual']['display']
            provider['clinic'] = encounter['location'][0]['location'][
                'display']
            provider['date-last-seen'] = parse_timestamp(
                encounter['period']['start'])
            providers.append(provider)

            # A way to get more provider info from provider_data
            # provider_id = encounter['participant'][0]['individual']['reference'].split('/')[1]
            # [provider for provider in provider_data if provider['id'] == provider_id][0]

            # A way to get more location info from location_data
            # location_id = encounter['location'][0]['location']['reference'].split('/')[1]
            # [location for location in location_data if location['id'] == location_id][0]

        providers.sort(key=lambda p: p['date-last-seen'], reverse=True)

        context.setdefault('providers_headers', providers_headers)
        context.setdefault('providers', providers)

        return context
Example #4
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['member'] = self.get_member()
        data = fetch_member_data(context['member'], 'sharemyhealth')
        context['updated_at'] = parse_timestamp(data.get('updated_at'))
        if context['updated_at']:
            context['time_since_update'] = (
                datetime.now(timezone.utc) - context['updated_at']
            )
        fhir_data = data.get('fhir_data')
        if settings.DEBUG:
            context['data'] = fhir_data

        if fhir_data is None or 'entry' not in fhir_data or not fhir_data['entry']:
            delete_memoized(fetch_member_data, context[
                            'member'], 'sharemyhealth')

        encounters = get_resource_data(
            fhir_data, 'Encounter', constructor=Encounter.from_data
        )
        encounters.sort(key=lambda e: e.period.start,
                        reverse=True)  # latest ones first
        practitioners = get_resource_data(
            fhir_data, 'Practitioner', constructor=Practitioner.from_data
        )
        for index, practitioner in enumerate(practitioners):
            practitioner.last_encounter = practitioner.next_encounter(
                encounters)
            practitioners[index] = practitioner

        practitioners.sort(
            key=lambda p: (
                p.last_encounter.period.start
                if p.last_encounter
                else datetime(1, 1, 1, tzinfo=timezone.utc)
            ),
            reverse=True,
        )

        context['practitioners'] = practitioners
        return context
Example #5
0
 def birthdate(self):
     date = self.id_token_payload.get('birthdate')
     if date is not None:
         date = parse_timestamp(date).date()
     return date
Example #6
0
    def get_context_data(self, **kwargs):
        """Add records data into the context."""
        context = super().get_context_data(**kwargs)
        member = context['member']
        resource_name = self.kwargs.get('resource_name') or 'list'

        # Get the data for the member, and set it in the context
        data = fetch_member_data(member, 'sharemyhealth')
        if data is None or 'entry' not in data or not data['entry']:
            delete_memoized(fetch_member_data, member, 'sharemyhealth')

        if resource_name == 'list':
            conditions_data = get_resource_data(data, 'Condition')
            observation_data = get_resource_data(data, 'Observation')
            encounter_data = get_resource_data(data, 'Encounter')
            prescription_data = get_prescriptions(data)
            allergies = get_allergies(data, keys=['id'])
            all_records = RECORDS
            for record in all_records:
                # adding data for each resoureType in response from
                # endpoint
                if record['name'] == 'Diagnoses':
                    record['count'] = len(conditions_data)
                    record['data'] = conditions_data

                if record['name'] == 'Lab Results':
                    record['count'] = len(observation_data)
                    record['data'] = observation_data

                if record['name'] == 'Prescriptions':
                    record['count'] = len(prescription_data)
                    record['data'] = prescription_data

                if record['name'] == 'Procedures':
                    record['count'] = len(encounter_data)
                    record['data'] = encounter_data

                if record['name'] == 'Allergies':
                    record['count'] = len(allergies)
                    record['data'] = allergies

            context.setdefault('all_records', all_records)

        elif resource_name == 'diagnoses':
            conditions_data = get_resource_data(data, 'Condition')
            headers = ['Date', 'Code', 'Diagnosis', 'Provider']
            diagnoses = []
            for condition in conditions_data:
                diagnosis = dict(
                    Date=(condition.get('assertedDate')
                          and parse_timestamp(condition['assertedDate'])
                          or None),
                    Code=condition['code']['coding'][0].get('code', None),
                    Diagnosis=condition['code']['coding'][0].get(
                        'display', None),
                    Provider=condition.get('provider', None),
                )
                diagnoses.append(diagnosis)

            # sort diagnoses in order of date descending
            diagnoses.sort(key=lambda d: d['Date'] or datetime(1, 1, 1),
                           reverse=True)

            context.setdefault('title', 'Diagnoses')
            context.setdefault('headers', headers)
            context.setdefault('content_list', diagnoses)

        elif resource_name == 'lab-results':
            observation_data = get_resource_data(data, 'Observation')
            headers = ['Date', 'Code', 'Lab Result', 'Value']
            lab_results = []
            for observation in observation_data:
                lab = dict(
                    Date=(observation.get('effectivePeriod', {}).get('start')
                          and parse_timestamp(
                              observation['effectivePeriod']['start'])
                          or None),
                    Code=observation['code']['coding'][0].get('code', None),
                    Display=observation['code']['coding'][0].get(
                        'display', None),
                )
                lab_value = observation.get('valueQuantity', None)
                lab['Value'] = (str(list(lab_value.values())[0]) +
                                list(lab_value.values())[1]
                                if lab_value else None)
                lab_results.append(lab)

            # sort lab_results in order of date descending
            lab_results.sort(key=lambda d: d['Date'] or datetime(1, 1, 1),
                             reverse=True)

            context.setdefault('title', 'Lab Results')
            context.setdefault('headers', headers)
            context.setdefault('content_list', lab_results)

        elif resource_name == 'procedures':
            encounter_data = get_resource_data(data, 'Encounter')
            headers = ['Date', 'Type', 'Practitioner', 'Location']
            procedures = []
            for encounter in encounter_data:
                procedure = dict(
                    Date=(encounter.get('period', {}).get('start')
                          and parse_timestamp(encounter['period']['start'])
                          or None),
                    Type=encounter['type'][0]['text'],
                    Practitioner=', '.join([
                        participant['individual']['display']
                        for participant in encounter.get('participant', [])
                        if 'Practitioner' in participant['individual']
                        ['reference']
                    ]),
                    Location=encounter['location'][0]['location']['display'],
                )
                procedures.append(procedure)

            # sort procedures in order of date descending
            procedures.sort(key=lambda d: d['Date'] or datetime(1, 1, 1),
                            reverse=True)

            context.setdefault('title', 'Procedures')
            context.setdefault('headers', headers)
            context.setdefault('content_list', procedures)

        elif resource_name == 'prescriptions':
            prescription_data = get_prescriptions(data)
            headers = ['Date', 'Medication', 'Provider(s)']
            all_records = []

            # sort prescriptions by start date descending
            med_names = [
                np[0] for np in sorted(
                    [(name, prescription)
                     for name, prescription in prescription_data.items()],
                    key=lambda np: np[1]['statements'] and np[1]['statements'][
                        0].effectivePeriod.start or datetime(1, 1, 1),
                    reverse=True,
                )
            ]
            # set up the display data
            for med_name in med_names:
                prescription = prescription_data[med_name]
                record = {
                    'Date':
                    prescription['statements']
                    and prescription['statements'][0].effectivePeriod.start
                    or None,
                    'Medication':
                    med_name,
                    'Provider(s)':
                    ', '.join([
                        request.requester.agent.display
                        for request in prescription['requests']
                    ]),
                }
                record['links'] = {
                    'Medication':
                    f'/member/{member.id}/modal/prescription/{prescription["medication"].id}'
                }
                all_records.append(record)

            context.setdefault('title', 'Prescriptions')
            context.setdefault('headers', headers)
            context.setdefault('content_list', all_records)

        elif resource_name == 'allergies':
            allergies = get_allergies(
                data,
                keys=['id', 'assertedDate', 'code'] +
                ['clinicalStatus', 'verificationStatus', 'reaction'],
            )
            headers = [
                'Asserted', 'Code', 'Status', 'Verification', 'Reaction'
            ]
            default_timestamp = datetime(1,
                                         1,
                                         1,
                                         tzinfo=timezone(timedelta(0)))
            all_records = sorted(
                [{
                    'Asserted':
                    allergy.assertedDate,
                    'Code':
                    allergy.code.text,
                    'Status':
                    allergy.clinicalStatus.text,
                    'Verification':
                    allergy.verificationStatus.text,
                    'Reaction':
                    ', '.join([
                        ', '.join([
                            manifestation.text
                            for manifestation in manifestations
                        ]) for manifestations in [
                            reaction.manifestation
                            for reaction in allergy.reaction
                        ]
                    ]),
                } for allergy in allergies],
                key=lambda r: r.get('Asserted') or default_timestamp,
                reverse=True,
            )
            context.setdefault('title', 'Allergies')
            context.setdefault('headers', headers)
            context.setdefault('content_list', all_records)

        return context
Example #7
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['member'] = self.get_member()
        data = fetch_member_data(context['member'], 'sharemyhealth')
        context['updated_at'] = parse_timestamp(data.get('updated_at'))
        if context['updated_at']:
            context['time_since_update'] = (
                datetime.now(timezone.utc) - context['updated_at']
            )
        fhir_data = data.get('fhir_data')

        if fhir_data is None or 'entry' not in fhir_data or not fhir_data['entry']:
            delete_memoized(fetch_member_data, context[
                            'member'], 'sharemyhealth')

        context['practitioner'] = next(
            iter(
                get_resource_data(
                    fhir_data,
                    'Practitioner',
                    constructor=Practitioner.from_data,
                    id=self.kwargs['provider_id'],
                )
            ),
            None,
        )
        if not context['practitioner']:
            raise Http404()

        prescriptions = [
            {
                'date': next(
                    iter(
                        sorted(
                            [
                                statement.period.start
                                for statement in prescription['statements']
                            ],
                            reverse=True,
                        )
                    ),
                    None,
                ),
                'type': 'Prescription',
                'display': prescription['medication'].code.text,
                'prescription': prescription,
            }
            for prescription in get_prescriptions(
                fhir_data, incl_practitioners=True
            ).values()
            if context['practitioner'].id in prescription['practitioners'].keys()
        ]
        procedures = [
            {
                'date': procedure.performedPeriod.start,
                'type': 'Procedure',
                'display': procedure.code.text,
                'procedure': procedure,
            }
            for procedure in get_resource_data(
                fhir_data, 'Procedure', constructor=Procedure.from_data
            )
        ]
        context['records'] = sorted(
            prescriptions + procedures,
            key=lambda r: r['date'] or datetime(1, 1, 1, tzinfo=timezone.utc),
            reverse=True,
        )

        return context
Example #8
0
    def get_context_data(self, **kwargs):
        """Add records data into the context."""
        context = super().get_context_data(**kwargs)
        context['member'] = self.get_member()
        resource_name = self.kwargs.get('resource_name') or 'list'

        # Get the data for the member, and set it in the context
        data = fetch_member_data(context['member'], 'sharemyhealth')
        context['updated_at'] = parse_timestamp(data.get('updated_at'))
        if context['updated_at']:
            context['time_since_update'] = (
                datetime.now(timezone.utc) - context['updated_at']
            )
        fhir_data = data.get('fhir_data')
        if settings.DEBUG:
            context['data'] = data

        logging.debug(
            "fhir_data records: %r",
            fhir_data and fhir_data.get(
                'entry') and len(fhir_data.get('entry')),
        )

        if fhir_data is None or 'entry' not in fhir_data or not fhir_data['entry']:
            delete_memoized(fetch_member_data, context[
                            'member'], 'sharemyhealth')

        if resource_name == 'list':
            conditions_data = get_resource_data(fhir_data, 'Condition')
            observation_data = get_resource_data(fhir_data, 'Observation')
            procedure_data = get_resource_data(fhir_data, 'Procedure')
            prescription_data = get_prescriptions(fhir_data)
            allergies = get_allergies(fhir_data, keys=['id'])
            all_records = RECORDS
            for record in all_records:
                # adding data for each resoureType in response from
                # endpoint
                if record['name'] == 'Diagnoses':
                    record['count'] = len(conditions_data)
                    record['data'] = conditions_data

                if record['name'] == 'Lab Results':
                    record['count'] = len(observation_data)
                    record['data'] = observation_data

                if record['name'] == 'Prescriptions':
                    record['count'] = len(prescription_data)
                    record['data'] = prescription_data

                if record['name'] == 'Procedures':
                    record['count'] = len(procedure_data)
                    record['data'] = procedure_data

                if record['name'] == 'Allergies':
                    record['count'] = len(allergies)
                    record['data'] = allergies

            context.setdefault('all_records', all_records)

        elif resource_name == 'diagnoses':
            conditions_data = get_resource_data(
                fhir_data, 'Condition', Condition.from_data
            )
            headers = ['Diagnosis', 'Status', 'Verification']
            diagnoses = sorted(
                [
                    dict(
                        Diagnosis=condition.code.text,
                        Status=condition.clinicalStatus,
                        Verification=condition.verificationStatus,
                    )
                    for condition in conditions_data
                ],
                key=lambda diagnosis: (
                    ['active', 'recurrence', 'inactive', 'remission', 'resolved'].index(
                        diagnosis['Status']
                    ),
                    diagnosis['Diagnosis'],
                ),
            )

            context.setdefault('title', 'Diagnoses')
            context.setdefault('headers', headers)
            context.setdefault('content_list', diagnoses)

        elif resource_name == 'lab-results':
            observation_data = get_resource_data(
                fhir_data, 'Observation', constructor=Observation.from_data
            )
            headers = ['Date', 'Code', 'Display', 'Lab Result']
            lab_results = sorted(
                [
                    {
                        'Date': observation.effectivePeriod.start,
                        'Code': (
                            observation.code.coding[0].code
                            if len(observation.code.coding) > 0
                            else None
                        ),
                        'Display': observation.code.text,
                        'Lab Result': observation.valueQuantity,
                    }
                    for observation in observation_data
                ],
                key=lambda observation: observation['Date']
                or datetime(1, 1, 1, tzinfo=timezone.utc),
                reverse=True,
            )

            context.setdefault('title', 'Lab Results')
            context.setdefault('headers', headers)
            context.setdefault('content_list', lab_results)

        elif resource_name == 'procedures':
            procedures_data = sorted(
                get_resource_data(
                    fhir_data, 'Procedure', constructor=Procedure.from_data
                ),
                key=lambda procedure: (
                    procedure.performedPeriod.start
                    or datetime(1, 1, 1, tzinfo=timezone.utc)
                ),
                reverse=True,
            )
            headers = ['Date', 'Status', 'Description', 'Provider']
            procedures = [
                {
                    'Date': procedure.performedPeriod.start,
                    'Status': procedure.status,
                    'Description': procedure.code.text,
                    'Provider': mark_safe(
                        '; '.join(
                            [
                                '<a class="modal-link" href="{url}">{name}</a>'.format(
                                    name=performer.actor.display,
                                    url=reverse_lazy(
                                        'member:provider_detail',
                                        kwargs={
                                            'pk': context['member'].id,
                                            'provider_id': performer.actor.id,
                                        },
                                    ),
                                )
                                for performer in procedure.performer
                                if 'Practitioner' in performer.actor.reference
                            ]
                        )
                    ),
                }
                for procedure in procedures_data
            ]

            context.setdefault('title', 'Procedures')
            context.setdefault('headers', headers)
            context.setdefault('content_list', procedures)

        elif resource_name == 'prescriptions':
            prescription_data = get_prescriptions(fhir_data)
            headers = ['Date', 'Medication', 'Provider(s)']
            all_records = []

            # sort prescriptions by start date descending
            med_names = [
                np[0]
                for np in sorted(
                    [
                        (name, prescription)
                        for name, prescription in prescription_data.items()
                    ],
                    key=lambda np: np[1]['statements']
                    and np[1]['statements'][0].effectivePeriod.start
                    or datetime(1, 1, 1, tzinfo=timezone.utc),
                    reverse=True,
                )
            ]
            # set up the display data
            for med_name in med_names:
                prescription = prescription_data[med_name]
                record = {
                    'Date': prescription['statements']
                    and prescription['statements'][0].effectivePeriod.start
                    or None,
                    'Medication': med_name,
                    'Provider(s)': ', '.join(
                        [
                            request.requester.agent.display
                            for request in prescription['requests']
                        ]
                    ),
                }
                record['links'] = {
                    'Medication': f"/member/{context['member'].id}"
                    + f"/modal/prescription/{prescription['medication'].id}"
                }
                all_records.append(record)

            context.setdefault('title', 'Prescriptions')
            context.setdefault('headers', headers)
            context.setdefault('content_list', all_records)

        elif resource_name == 'allergies':
            allergies = get_allergies(
                fhir_data,
                keys=['id', 'assertedDate', 'code']
                + ['clinicalStatus', 'verificationStatus', 'reaction'],
            )
            headers = ['Asserted', 'Code', 'Status',
                       'Verification', 'Reaction']
            default_timestamp = datetime(
                1, 1, 1, tzinfo=timezone(timedelta(0)))
            all_records = sorted(
                [
                    {
                        'Asserted': allergy.assertedDate,
                        'Code': allergy.code.text,
                        'Status': allergy.clinicalStatus.text,
                        'Verification': allergy.verificationStatus.text,
                        'Reaction': ', '.join(
                            [
                                ', '.join(
                                    [
                                        manifestation.text
                                        for manifestation in manifestations
                                    ]
                                )
                                for manifestations in [
                                    reaction.manifestation
                                    for reaction in allergy.reaction
                                ]
                            ]
                        ),
                    }
                    for allergy in allergies
                ],
                key=lambda r: r.get('Asserted') or default_timestamp,
                reverse=True,
            )
            context.setdefault('title', 'Allergies')
            context.setdefault('headers', headers)
            context.setdefault('content_list', all_records)

        return context
Example #9
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['member'] = self.get_member()
        # Get the data for the member, and set it in the context
        data = fetch_member_data(context['member'], 'sharemyhealth')
        context['updated_at'] = parse_timestamp(data.get('updated_at'))
        context['timestamp'] = data.get('updated_at', "No timestamp")
        if context['updated_at']:
            context['time_since_update'] = (datetime.now(timezone.utc) -
                                            context['updated_at'])
            context['updated_at'] = context['updated_at'].timestamp()

        fhir_data = load_test_fhir_data(data)
        # fhir_data = data.get('fhir_data')
        if settings.DEBUG:
            context['data'] = data
        #
        # get resource bundles
        #
        resource_list = RESOURCES
        # Observation mixes lab results and vital signs
        resource_list.remove('Observation')

        resources = get_converted_fhir_resource(fhir_data)
        if len(resources.entry) > 0:
            resources = resources.entry
        else:
            resources = []
        context.setdefault('resources', resources)
        labs = get_lab_results(fhir_data)
        if len(labs.entry) > 0:
            labs = labs.entry
        else:
            labs = []
        context.setdefault('labs', labs)
        vitals = get_vital_signs(fhir_data)
        if len(vitals.entry) > 0:
            vitals = vitals.entry
        else:
            vitals = []
        context.setdefault('vitals', vitals)

        counts = resource_count(resources)
        context.setdefault('counts', counts)
        # print(counts)
        #
        #####
        if fhir_data is None or 'entry' not in fhir_data or not fhir_data[
                'entry']:
            delete_memoized(fetch_member_data, context['member'],
                            'sharemyhealth')

        # all_records = RECORDS
        all_records = RECORDS_STU3
        summarized_records = []
        notes_headers = ['Agent Name', 'Organization', 'Date']
        for record in all_records:

            if record['call_type'].lower() == "fhir":
                entries = get_converted_fhir_resource(fhir_data,
                                                      record['resources'])
                record['data'] = entries['entry']
                record['count'] = len(entries['entry'])
                summarized_records.append(record)
            elif record['call_type'].lower() == 'custom':
                if record['name'] == 'VitalSigns':
                    entries = get_vital_signs(fhir_data)
                    record['data'] = entries['entry']
                    record['count'] = len(entries['entry'])
                    summarized_records.append(record)
                elif record['name'] == 'LabResults':
                    entries = get_lab_results(fhir_data)
                    record['data'] = entries['entry']
                    record['count'] = len(entries['entry'])
                    summarized_records.append(record)
            else:  # skip
                pass

        context.setdefault('summarized_records', summarized_records)

        context.setdefault('notes_headers', notes_headers)
        # TODO: include notes in the context data.

        return context
Example #10
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        print(context)

        context['member'] = self.get_member()
        resource_name = self.kwargs.get('resource_name') or 'list'
        data = fetch_member_data(context['member'], 'sharemyhealth')
        context['updated_at'] = parse_timestamp(data.get('updated_at'))
        if context['updated_at']:
            context['time_since_update'] = (datetime.now(timezone.utc) -
                                            context['updated_at'])
        context['back_to'] = 'member:providers'

        ####
        # this will only pull a local fhir file if VPC_ENV is not prod|stage|dev
        fhir_data = load_test_fhir_data(data)
        # fhir_data = data.get('fhir_data')
        if settings.DEBUG:
            context['data'] = data

        logging.debug(
            "fhir_data records: %r",
            fhir_data and fhir_data.get('entry')
            and len(fhir_data.get('entry')),
        )

        if fhir_data is None or 'entry' not in fhir_data or not fhir_data[
                'entry']:
            delete_memoized(fetch_member_data, context['member'],
                            'sharemyhealth')

        if resource_name == 'list':
            provider_related = []
            for r in RECORDS_STU3:
                if r['name'] in PROVIDER_RESOURCES:
                    provider_related.append(r)
            all_records = provider_related
            summarized_records = []
            for record in all_records:
                if record['call_type'].lower() == "fhir":
                    # print("record processing for ", record['name'])
                    entries = get_converted_fhir_resource(
                        fhir_data, record['resources'])
                    record['data'] = entries['entry']
                    record['count'] = len(entries['entry'])
                    summarized_records.append(record)
                elif record['call_type'].lower() == 'custom':
                    pass
                else:  # skip
                    pass

            context['back_to'] = 'member:providers'
            context.setdefault('all_records', summarized_records)

        else:
            resource_profile = RECORDS_STU3[find_index(RECORDS_STU3, "slug",
                                                       resource_name)]

            # print("Resource Profile", resource_profile)

            if resource_profile:
                title = resource_profile['display']
                headers = resource_profile['headers']
                exclude = resource_profile['exclude']
                # second_fields = headers
                # second_fields.append(exclude)
            else:
                title = resource_name
                headers = [
                    'id',
                ]
                exclude = ['']
                # second_fields

            # ff = find_list_entry(FIELD_TITLES, "profile", resource_profile['name'])
            # print("Friendly:", ff)
            # print("headers:", headers)
            # print("Exclude:", exclude)
            # print("second_fields:", second_fields)
            if "sort" in resource_profile:
                sort_field = resource_profile['sort']
            else:
                sort_field = ""

            title = resource_profile['display']
            if resource_profile['call_type'] == 'custom':
                if resource_profile['slug'] == 'labresults':
                    entries = get_lab_results(fhir_data, resource_profile)
                elif resource_profile['slug'] == 'vitalsigns':
                    entries = get_vital_signs(fhir_data, resource_profile)
            elif resource_profile['call_type'] == 'skip':
                entries = {'entry': []}
            else:
                entries = get_converted_fhir_resource(
                    fhir_data, [resource_profile['name']])
            content_list = path_extract(entries['entry'], resource_profile)
            context.setdefault(
                'friendly_fields',
                find_list_entry(FIELD_TITLES, "profile",
                                resource_profile['name']))
            context.setdefault('title', title)
            context.setdefault('headers', headers)
            context.setdefault('exclude', exclude)
            # context.setdefault('content_list', content_list)
            context.setdefault('resource_profile', resource_profile)
            sorted_content = sort_json(content_list, sort_field)
            context.setdefault('content_list', sorted_content)
            context['back_to'] = 'member:providers'

        return context
Example #11
0
    def get_context_data(self, **kwargs):
        """Add records data into the context."""
        context = super().get_context_data(**kwargs)
        context['member'] = self.get_member()
        resource_name = self.kwargs.get('resource_name') or 'list'

        # Get the data for the member, and set it in the context
        data = fetch_member_data(context['member'], 'sharemyhealth')
        context['updated_at'] = parse_timestamp(data.get('updated_at'))
        context['timestamp'] = data.get('updated_at', "No timestamp")
        if context['updated_at']:
            context['time_since_update'] = (datetime.now(timezone.utc) -
                                            context['updated_at'])
        fhir_data = load_test_fhir_data(data)
        # fhir_data = data.get('fhir_data')
        if settings.DEBUG:
            context['data'] = data

        logging.debug(
            "fhir_data records: %r",
            fhir_data and fhir_data.get('entry')
            and len(fhir_data.get('entry')),
        )

        if fhir_data is None or 'entry' not in fhir_data or not fhir_data[
                'entry']:
            delete_memoized(fetch_member_data, context['member'],
                            'sharemyhealth')

        if resource_name == 'list':
            all_records = RECORDS_STU3
            summarized_records = []
            for record in all_records:
                if record['call_type'].lower() == "fhir":
                    # print("record processing for ", record['name'])
                    entries = get_converted_fhir_resource(
                        fhir_data, record['resources'])
                    record['data'] = entries['entry']
                    record['count'] = len(entries['entry'])
                    summarized_records.append(record)
                elif record['call_type'].lower() == 'custom':
                    if record['name'] == 'VitalSigns':
                        entries = get_vital_signs(fhir_data, record)
                        record['data'] = entries['entry']
                        record['count'] = len(entries['entry'])
                        summarized_records.append(record)
                    elif record['name'] == 'LabResults':
                        entries = get_lab_results(fhir_data, record)
                        record['data'] = entries['entry']
                        record['count'] = len(entries['entry'])
                        summarized_records.append(record)
                else:  # skip
                    pass

            context.setdefault('all_records', summarized_records)

        else:
            resource_profile = RECORDS_STU3[find_index(RECORDS_STU3, "slug",
                                                       resource_name)]

            # print("Resource Profile", resource_profile)

            if resource_profile:
                title = resource_profile['display']
                headers = resource_profile['headers']
                exclude = resource_profile['exclude']
                # second_fields = headers
                # second_fields.append(exclude)
            else:
                title = resource_name
                headers = [
                    'id',
                ]
                exclude = ['']
                # second_fields

            # ff = find_list_entry(FIELD_TITLES, "profile", resource_profile['name'])
            # print("Friendly:", ff)
            # print("headers:", headers)
            # print("Exclude:", exclude)
            # print("second_fields:", second_fields)
            if "sort" in resource_profile:
                sort_field = resource_profile['sort']
            else:
                sort_field = ""

            title = resource_profile['display']
            if resource_profile['call_type'] == 'custom':
                if resource_profile['slug'] == 'labresults':
                    entries = get_lab_results(fhir_data, resource_profile)
                elif resource_profile['slug'] == 'vitalsigns':
                    entries = get_vital_signs(fhir_data, resource_profile)
                    vitalsigns = create_vital_sign_view_by_date(
                        entries['entry'])

                    print("vitalsigns:", vitalsigns)

                    context.setdefault('vitalsigns', vitalsigns)
            elif resource_profile['call_type'] == 'skip':
                entries = {'entry': []}
            else:
                entries = get_converted_fhir_resource(
                    fhir_data, [resource_profile['name']])
            content_list = path_extract(entries['entry'], resource_profile)
            context.setdefault(
                'friendly_fields',
                find_list_entry(FIELD_TITLES, "profile",
                                resource_profile['name']))
            context.setdefault('title', title)
            context.setdefault('headers', headers)
            context.setdefault('exclude', exclude)
            # context.setdefault('content_list', content_list)
            context.setdefault('resource_profile', resource_profile)
            sorted_content = sort_json(content_list, sort_field)
            context.setdefault('content_list', sorted_content)

        return context