diff --git a/api/app.py b/api/app.py index 1c17e23..c55f023 100644 --- a/api/app.py +++ b/api/app.py @@ -1,4 +1,3 @@ -# app.py from flask import Flask, jsonify, request import os import logging @@ -7,6 +6,7 @@ from dotenv import load_dotenv from werkzeug.middleware.proxy_fix import ProxyFix import importlib.util +# Load environment variables from .env file load_dotenv() app = Flask(__name__) @@ -40,7 +40,6 @@ def load_blueprints_from_directory(directory): if filename.endswith('.py') and not filename.startswith('__'): module_name = filename[:-3] # Remove .py extension file_path = os.path.join(directory, filename) - spec = importlib.util.spec_from_file_location(module_name, file_path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) @@ -51,5 +50,23 @@ def load_blueprints_from_directory(directory): # Load blueprints load_blueprints_from_directory('endpoints') +# Sitemap endpoint function +def sitemap(): + # Get all registered routes + routes = [] + for rule in app.url_map.iter_rules(): + # Skip the sitemap route itself to avoid infinite recursion + if rule.endpoint != 'sitemap': + routes.append({ + 'rule': rule.rule, + 'methods': list(rule.methods), + 'endpoint': rule.endpoint + }) + + return jsonify(routes) + +# Register the sitemap endpoint +app.add_url_rule('/sitemap', view_func=sitemap, methods=['GET']) + if __name__ == '__main__': app.run(debug=True) diff --git a/api/endpoints/create_legislation_node.py b/api/endpoints/create_legislation_node.py new file mode 100644 index 0000000..24c923c --- /dev/null +++ b/api/endpoints/create_legislation_node.py @@ -0,0 +1,58 @@ +from flask import Blueprint, jsonify +import json +import os +import logging +from neo4j import GraphDatabase +from dotenv import load_dotenv + + +bp = Blueprint('create_legislation_node', __name__) + + +@bp.route('/create_legislation_node', methods=['GET']) +def create_legislation_nodes(): + # Assuming the legislation cache is stored in a JSON file + legislation_cache_path = os.getenv("LEGISLATION_CACHE_PATH") + + if not legislation_cache_path: + return jsonify({"error": "Legislation cache path is not set"}), 400 + + try: + with open(legislation_cache_path, 'r') as f: + legislation_data = json.load(f) + except FileNotFoundError: + return jsonify({"error": "Legislation cache file not found"}), 404 + except json.JSONDecodeError: + return jsonify({"error": "Failed to decode JSON from the legislation cache"}), 500 + + # Ensure legislation_data is a list of dictionaries + if not isinstance(legislation_data, list): + return jsonify({"error": "Legislation data should be a list"}), 400 + + created_nodes = [] + + for item in legislation_data: + if not item.get("id"): + continue + + # Prepare the properties for the node + properties = {k: v for k, v in item.items()} + + # Create or merge a node in Neo4j with all properties + driver = get_driver() + with driver.session() as session: + query = "MERGE (l:Legislation {id: $id})" + for key in properties: + if key != 'id': + query += f" ON CREATE SET l.{key} = ${key}" + + result = session.run(query, **properties) + + # Log the creation or merging of the node + neo4j_logger.info(f"Created/Merged Legislation node with ID: {item.get('id')} and properties: {properties}") + + driver.close() + + created_nodes.append({"id": item.get("id"), "properties": properties}) + + return jsonify({"message": "Legislation nodes created/merged successfully", "nodes": created_nodes}), 201