From 95f80fbe91095e19e44afd58b593e776ecc3799b Mon Sep 17 00:00:00 2001 From: Sudo-JHare Date: Fri, 11 Apr 2025 09:29:06 +1000 Subject: [PATCH] Updated DOM added some DOM fixes for collapse and expand events on view page and fallback display for resources. --- app.py | 55 ++++++-- services.py | 4 +- templates/cp_view_processed_ig.html | 190 ++++++++++++++++------------ 3 files changed, 156 insertions(+), 93 deletions(-) diff --git a/app.py b/app.py index 7cbeaf4..9cd5bb7 100644 --- a/app.py +++ b/app.py @@ -289,7 +289,7 @@ def unload_ig(): flash("Invalid package ID.", "error") return redirect(url_for('view_igs')) - processed_ig = ProcessedIg.query.get(ig_id) + processed_ig = db.session.get(ProcessedIg, ig_id) if processed_ig: try: db.session.delete(processed_ig) @@ -318,16 +318,54 @@ def get_structure_definition(): resource_identifier = request.args.get('resource_type') if not all([package_name, package_version, resource_identifier]): return jsonify({"error": "Missing query parameters"}), 400 + + # First, try to get the structure definition from the specified package tgz_path = os.path.join(app.config['FHIR_PACKAGES_DIR'], services._construct_tgz_filename(package_name, package_version)) - if not os.path.exists(tgz_path): - return jsonify({"error": f"Package file not found: {tgz_path}"}), 404 - sd_data, _ = services.find_and_extract_sd(tgz_path, resource_identifier) + sd_data = None + fallback_used = False + + if os.path.exists(tgz_path): + sd_data, _ = services.find_and_extract_sd(tgz_path, resource_identifier) + + # If not found, fall back to the core FHIR package (hl7.fhir.r4.core#4.0.1) if sd_data is None: - return jsonify({"error": f"SD for '{resource_identifier}' not found."}), 404 + logger.debug(f"Structure definition for '{resource_identifier}' not found in {package_name}#{package_version}, attempting fallback to hl7.fhir.r4.core#4.0.1") + core_package_name = "hl7.fhir.r4.core" + core_package_version = "4.0.1" + + # Ensure the core package is downloaded + core_tgz_path = os.path.join(app.config['FHIR_PACKAGES_DIR'], services._construct_tgz_filename(core_package_name, core_package_version)) + if not os.path.exists(core_tgz_path): + logger.debug(f"Core package {core_package_name}#{core_package_version} not found, attempting to download") + try: + result = services.import_package_and_dependencies(core_package_name, core_package_version) + if result['errors'] and not result['downloaded']: + logger.error(f"Failed to download core package: {result['errors'][0]}") + return jsonify({"error": f"SD for '{resource_identifier}' not found in {package_name}#{package_version}, and failed to download core package: {result['errors'][0]}"}), 404 + except Exception as e: + logger.error(f"Error downloading core package: {str(e)}") + return jsonify({"error": f"SD for '{resource_identifier}' not found in {package_name}#{package_version}, and error downloading core package: {str(e)}"}), 500 + + # Try to extract the structure definition from the core package + if os.path.exists(core_tgz_path): + sd_data, _ = services.find_and_extract_sd(core_tgz_path, resource_identifier) + if sd_data is None: + return jsonify({"error": f"SD for '{resource_identifier}' not found in {package_name}#{package_version} or in core package {core_package_name}#{core_package_version}."}), 404 + fallback_used = True + else: + return jsonify({"error": f"SD for '{resource_identifier}' not found in {package_name}#{package_version}, and core package {core_package_name}#{core_package_version} could not be located."}), 404 + elements = sd_data.get('snapshot', {}).get('element', []) or sd_data.get('differential', {}).get('element', []) processed_ig = ProcessedIg.query.filter_by(package_name=package_name, version=package_version).first() must_support_paths = processed_ig.must_support_elements.get(resource_identifier, []) if processed_ig else [] - return jsonify({"elements": elements, "must_support_paths": must_support_paths}) + + response = { + "elements": elements, + "must_support_paths": must_support_paths, + "fallback_used": fallback_used, + "source_package": f"{core_package_name}#{core_package_version}" if fallback_used else f"{package_name}#{package_version}" + } + return jsonify(response) @app.route('/get-example') def get_example_content(): @@ -579,10 +617,5 @@ with app.app_context(): db.create_all() logger.debug("Database initialization complete") -# Add route to serve favicon -@app.route('/favicon.ico') -def favicon(): - return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon') - if __name__ == '__main__': app.run(debug=True) \ No newline at end of file diff --git a/services.py b/services.py index e6d2f32..4b41e64 100644 --- a/services.py +++ b/services.py @@ -101,7 +101,7 @@ def find_and_extract_sd(tgz_path, resource_identifier): # Public version if fileobj: fileobj.close() # Ensure resource cleanup if sd_data is None: - logger.warning(f"SD matching '{resource_identifier}' not found within archive {os.path.basename(tgz_path)}") + logger.info(f"SD matching '{resource_identifier}' not found within archive {os.path.basename(tgz_path)} - caller may attempt fallback") except tarfile.TarError as e: logger.error(f"TarError reading {tgz_path} in find_and_extract_sd: {e}") raise tarfile.TarError(f"Error reading package archive: {e}") from e @@ -182,7 +182,6 @@ def extract_dependencies(tgz_path): error_message = f"Unexpected error extracting deps: {e}"; logger.error(error_message, exc_info=True); dependencies = None return dependencies, error_message - # --- Recursive Import Orchestrator --- def import_package_and_dependencies(initial_name, initial_version): """Orchestrates recursive download and dependency extraction.""" @@ -247,7 +246,6 @@ def import_package_and_dependencies(initial_name, initial_version): logger.info(f"Import finished. Processed: {proc_count}, Downloaded/Verified: {dl_count}, Errors: {err_count}") return results - # --- Package File Content Processor (V6.2 - Fixed MS path handling) --- def process_package_file(tgz_path): """ Extracts types, profile status, MS elements, and examples from a downloaded .tgz package (Single Pass). """ diff --git a/templates/cp_view_processed_ig.html b/templates/cp_view_processed_ig.html index a5f366d..b04a38a 100644 --- a/templates/cp_view_processed_ig.html +++ b/templates/cp_view_processed_ig.html @@ -1,9 +1,7 @@ {% extends "base.html" %} {% block content %} -
+
FHIRFLARE Ig Toolkit

{{ title }}

@@ -49,10 +47,10 @@ center"> @@ -148,17 +147,8 @@ center"> {% else %} {% endif %} - - +