From a7ad56c0d08757c680c63eb9ba0db8e1a5d6558f Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 5 Dec 2023 19:23:23 -0300 Subject: [PATCH] Refactor update_frontend_node_with_template_values function --- src/backend/langflow/api/utils.py | 58 ++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/src/backend/langflow/api/utils.py b/src/backend/langflow/api/utils.py index 1fad1df5c..e531cf8fb 100644 --- a/src/backend/langflow/api/utils.py +++ b/src/backend/langflow/api/utils.py @@ -64,32 +64,48 @@ def update_frontend_node_with_template_values(frontend_node, raw_template_data): :param raw_template_data: A dict representing raw template data. :return: Updated frontend node. """ - if not frontend_node or "template" not in frontend_node or not raw_template_data or not raw_template_data.template: + if not is_valid_data(frontend_node, raw_template_data): return frontend_node - frontend_template = frontend_node.get("template", {}) - raw_template = raw_template_data.template or {} - - for key, value_dict in raw_template.items(): - frontend_field = frontend_template.get(key) - if key == "code" or not isinstance(value_dict, dict) or not frontend_field: - continue - - value = value_dict.get("value") - template_field_type = value_dict.get("type") - frontend_node_field_type = frontend_field.get("type") - file_path = value_dict.get("file_path") - if value is not None and key in frontend_template and template_field_type == frontend_node_field_type: - frontend_template[key]["value"] = value - if file_path is not None and key in frontend_template and template_field_type == frontend_node_field_type: - # check if file_path exists - if not Path(file_path).exists(): - frontend_template[key]["value"] = "" - else: - frontend_template[key]["file_path"] = file_path + update_template_values(frontend_node["template"], raw_template_data.template) return frontend_node +def is_valid_data(frontend_node, raw_template_data): + """ Check if the data is valid for processing. """ + return frontend_node and "template" in frontend_node and raw_template_data and hasattr(raw_template_data, 'template') + +def update_template_values(frontend_template, raw_template): + """ Updates the frontend template with values from the raw template. """ + for key, value_dict in raw_template.items(): + if key == "code" or not isinstance(value_dict, dict): + continue + + update_template_field(frontend_template, key, value_dict) + +def update_template_field(frontend_template, key, value_dict): + """ Updates a specific field in the frontend template. """ + template_field = frontend_template.get(key) + if not template_field or template_field.get("type") != value_dict.get("type"): + return + + if "value" in value_dict: + template_field["value"] = value_dict["value"] + + if "file_path" in value_dict: + file_path_value = get_file_path_value(value_dict["file_path"]) + if not file_path_value: + # If the file does not exist, remove the value from the template_field["value"] + template_field["value"] = "" + template_field["file_path"] = file_path_value + + + +def get_file_path_value(file_path): + """ Get the file path value if the file exists, else return empty string. """ + return file_path if Path(file_path).exists() else "" + + def validate_is_component(flows: List["Flow"]): for flow in flows: