Skip to content
Cannot retrieve contributors at this time
# -*- coding: utf-8 -*-
import logging
from fhirclient import client
from fhirclient.models.medication import Medication
from fhirclient.models.medicationrequest import MedicationRequest
from flask import Flask, request, redirect, session
# app setup
smart_defaults = {
'app_id': 'my_web_app',
'api_base': '',
'redirect_uri': 'http://localhost:8000/fhir-app/',
app = Flask(__name__)
def _save_state(state):
session['state'] = state
def _get_smart():
state = session.get('state')
if state:
return client.FHIRClient(state=state, save_func=_save_state)
return client.FHIRClient(settings=smart_defaults, save_func=_save_state)
def _logout():
if 'state' in session:
smart = _get_smart()
def _reset():
if 'state' in session:
del session['state']
def _get_prescriptions(smart):
bundle = MedicationRequest.where({'patient': smart.patient_id}).perform(smart.server)
pres = [be.resource for be in bundle.entry] if bundle is not None and bundle.entry is not None else None
if pres is not None and len(pres) > 0:
return pres
return None
def _get_medication_by_ref(ref, smart):
med_id = ref.split("/")[1]
return, smart.server).code
def _med_name(med):
if med.coding:
name = next((coding.display for coding in med.coding if coding.system == ''), None)
if name:
return name
if med.text and med.text:
return med.text
return "Unnamed Medication(TM)"
def _get_med_name(prescription, client=None):
if prescription.medicationCodeableConcept is not None:
med = prescription.medicationCodeableConcept
return _med_name(med)
elif prescription.medicationReference is not None and client is not None:
med = _get_medication_by_ref(prescription.medicationReference.reference, client)
return _med_name(med)
return 'Error: medication not found'
# views
def index():
""" The app's main page.
smart = _get_smart()
body = "<h1>Hello</h1>"
if smart.ready and smart.patient is not None: # "ready" may be true but the access token may have expired, making smart.patient = None
name = smart.human_name([0] if and len( > 0 else 'Unknown')
# generate simple body text
body += "<p>You are authorized and ready to make API requests for <em>{0}</em>.</p>".format(name)
pres = _get_prescriptions(smart)
if pres is not None:
body += "<p>{0} prescriptions: <ul><li>{1}</li></ul></p>".format("His" if 'male' == smart.patient.gender else "Her", '</li><li>'.join([_get_med_name(p,smart) for p in pres]))
body += "<p>(There are no prescriptions for {0})</p>".format("him" if 'male' == smart.patient.gender else "her")
body += """<p><a href="/logout">Change patient</a></p>"""
auth_url = smart.authorize_url
if auth_url is not None:
body += """<p>Please <a href="{0}">authorize</a>.</p>""".format(auth_url)
body += """<p>Running against a no-auth server, nothing to demo here. """
body += """<p><a href="/reset" style="font-size:small;">Reset</a></p>"""
return body
def callback():
""" OAuth2 callback interception.
smart = _get_smart()
except Exception as e:
return """<h1>Authorization Error</h1><p>{0}</p><p><a href="/">Start over</a></p>""".format(e)
return redirect('/')
def logout():
return redirect('/')
def reset():
return redirect('/')
# start the app
if '__main__' == __name__:
import flaskbeaker
logging.basicConfig(level=logging.DEBUG), port=8000)