def get_prov(target, org, author, sender, activity, now): prov = getattr(fhirtemplates, f'prov_{activity}') # hardcoded template for now resource = pyfhir(prov) #convert to fhirclient model resource.id = str(uuid.uuid1()) resource.recorded = FD.FHIRDate(f'{str(now.isoformat())}Z') resource.target[0].reference = update_ref(target) resource.agent[0].who.reference = update_ref(author) resource.agent[0].onBehalfOf.reference = update_ref(org) resource.agent[1].who.reference = update_ref(sender) return resource
def bundler(resources, type, validate_me=False): # input list of fhirclient objects return a json copy of Bundle type = type now = datetime.datetime.utcnow() fhir_now = FD.FHIRDate(f'{str(now.isoformat())}Z') resources_copy = deepcopy(resources) app.logger.info(f"creating bundle of type = {type}...") bundle = pyfhir({'resourceType': 'Bundle'}) #bundle.id = f'davinci-notifications-bundle-{now.strftime("%Y%m%d%H%M%S.%f")}' bundle.id = str(uuid.uuid1()) if validate_me and type == 'message': #add meta profiles bundle.meta = M.Meta() bundle.meta.profile = [profile_list["Bundle"]] bundle.meta.lastUpdated = fhir_now bundle.type = type bundle.timestamp = fhir_now bundle.entry = [] ref_map = {} for r in resources_copy: # append list of resources create replace id with uuids. app.logger.info(f'res = {r}') entry = B.BundleEntry() old_ref = f'{r.resource_type}/{r.id}' try: entry.fullUrl = uuid.UUID(r.id).urn # TODO get new uuid except ValueError: app.logger.info( f'resource = {r.resource_type} id = {r.id} is not uuid') r.id = str(uuid.uuid1()) entry.fullUrl = uuid.UUID(r.id).urn ref_map[old_ref] = entry.fullUrl if not validate_me: #remove meta profiles if r.meta: #remove meta r.meta = None #if r.resource_type is not "MessageHeader": #r.id = None #remove old_ids r.text = None #remove text entry.resource = r if type in ['transaction', 'batch']: entry.request = B.BundleEntryRequest( dict(method='POST', url='$process-message')) bundle.entry.append(entry) #app.logger.info(f'ref_map = {dumps(ref_map, indent = 2)}') #app.logger.info(f'meta elements = {[i.resource.meta.profile for i in bundle.entry]}') b_json = dumps(bundle.as_json(), indent=2) # replace old references with new urns for old_ref, new_urn in ref_map.items(): b_json = b_json.replace(old_ref, new_urn) return (b_json)
f_jurisdiction = CC.CodeableConcept({ "coding": [ { "system": "urn:iso:std:iso:3166", "code": "US" } ] }) conf_url = 'http://hl7.org/fhir/StructureDefinition/capabilitystatement-expectation' combo_url = 'http://hl7.org/fhir/StructureDefinition/capabilitystatement-search-parameter-combination' # dict to for SP to get right canonicals, may use spreadsheet or package file in future. sp_specials = { 'us-core-includeprovenance': 'http://hl7.org/fhir/us/core/SearchParameter/us-core-includeprovenance'} none_list = ['', ' ', 'none', 'n/a', 'N/A', 'N', 'False'] sep_list = (',', ';', ' ', ', ', '; ') f_now = D.FHIRDate(str(date.today())) def markdown(text, *args, **kwargs): return commonmark(text, *args, **kwargs) def main(): if (len(sys.argv) < 2): print( "Error: missing xlsx file - correct usage is:\n\tpython3 R4CapStatement_Maker.py [xlsx file]") return xls = sys.argv[1] print('....creating CapabilityStatement.....') # Read the config sheet from the spreadsheet
def fwd(): ''' reassemble message bundle: new bundle id, timestamp new messageheader.id and sender and source and destination if "intermed-no-change" is checked then add provenance for MH elif "intermed-change" is checked then add provenance for MH and and sender Org and remove coverage use prov template as dicts with variable add static text for forwarding messages ''' now = datetime.datetime.utcnow() fhir_now = FD.FHIRDate(f'{str(now.isoformat())}Z') #get existing bundle and modify app.logger.info(f'****** see what is in session = {session}') f_name = session['f_names'][-1] app.logger.info( f'line nnnn f_name list = {session["f_names"]} f_name item = {f_name}') data = read_in(in_path=app.root_path, f_name=f_name) # most recent saved bundle #data = cache.get('notification_bundle') #app.logger.info(f'data = {data}') #convert to r4models b = pyfhir(loads(data)) b.id = str(uuid.uuid1()) b.timestamp = fhir_now mh = b.entry[0].resource mh.id = str(uuid.uuid1()) b.entry[0].fullUrl = uuid.UUID(mh.id).urn mh.sender.reference = "urn:uuid:4f7c997a-d6a4-11ea-814c-b5baa7182d44" # hardcoded for now mh.sender.display = "Da Vinci Intermediary" # hardcoded for now mh.source.name = "Da Vinci Intermediary Source Application" mh.source.endpoint = "https://example.org/Endpoints/P999" mh.source.contact.system = 'phone' mh.source.contact.value = '+1-800-555-5555' session['resource_list'][0] = f'MessageHeader/{mh.id}' ################### ADD Provenance ################################SHOULD BE REMOVED NOT NEEDED ANYMORE app.logger.info( f'************intermed is {request.form["intermed"]}*************') provenance = get_prov( target=f'MessageHeader/{mh.id}', author=mh.author.reference, org=mh.responsible.reference, sender=mh.sender.reference, activity=request.form['intermed'], now=now, ) prov_entry = B.BundleEntry() prov_entry.fullUrl = uuid.UUID(provenance.id).urn prov_entry.resource = provenance b.entry.insert(1, prov_entry) session['resource_list'].insert(1, f'Provenance/{provenance.id}') ################### ADD new sender Org ################################ sender = getattr(fhirtemplates, 'sender') # hardcode org for now org = pyfhir(sender) #convert to fhirclient model add_profile(org) org_entry = B.BundleEntry() org_entry.fullUrl = uuid.UUID(org.id).urn org_entry.resource = org b.entry.append(org_entry) session['resource_list'].append(f'Organization/{org.id}') ################### Remove Coverage and Referenced Org ################################SHOULD BE REMOVED NOT NEEDED ANYMORE if request.form[ 'intermed'] == 'amend': # example for intermediary as sender with change in content try: coverage_index = next((index for (index, r) in enumerate(b.entry) if r.resource.resource_type == 'Coverage')) except StopIteration: pass else: coverage = b.entry.pop(coverage_index).resource session['resource_list'].pop(coverage_index) payor_url = coverage.payor[0].reference try: payor_index = next((index for (index, r) in enumerate(b.entry) if r.fullUrl == payor_url)) except StopIteration: pass else: b.entry.pop(payor_index) session['resource_list'].pop(payor_index) # writing to ig examples file and running the IG Build: notification_bundle = dumps(b.as_json(), indent=2) #app.logger.info(f'notification_bundle = {message_bundles[0]}') f_name = f'davinci-notification-bundle-{b.id}.json' write_out(app.root_path, f_name, notification_bundle) app.logger.info( f'writing example notification bundle to {app.root_path}/test_output/{f_name}' ) session['f_names'].append( f_name) # keep track of f_names for session to delete later session.modified = True app.logger.info(f'****** see what is in session = {session}***') return render_template( 'sub_template5.html', title="Forwarding Notification Bundle Prep", endpoint_urls=alerts_servers, endpoint='$process-message', notification_bundle=notification_bundle, b_id=b.id, forwarding=True, )