|
| 1 | +import importlib |
| 2 | +import os.path |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import app.forms.parts as form_parts |
| 6 | +import yaml |
| 7 | +from app.forms.models import FormFlow |
| 8 | + |
| 9 | + |
| 10 | +def load_config(form_slug: str) -> FormFlow: |
| 11 | + if not form_slug: |
| 12 | + raise ValueError("Form slug must be provided") |
| 13 | + |
| 14 | + config_path = os.path.join("/", "app", "app", "forms", "config", f"{form_slug}.yml") |
| 15 | + |
| 16 | + form_config = Path(config_path) |
| 17 | + if not form_config.is_file(): |
| 18 | + raise FileNotFoundError( |
| 19 | + f"Form configuration file not found for form: {form_config}" |
| 20 | + ) |
| 21 | + |
| 22 | + try: |
| 23 | + with open(config_path) as stream: |
| 24 | + return yaml.safe_load(stream) |
| 25 | + except yaml.YAMLError as e: |
| 26 | + raise ValueError(f"Error loading YAML configuration for form {form_slug}: {e}") |
| 27 | + |
| 28 | + |
| 29 | +def form_flow_from_config(config: dict, slug: str) -> FormFlow: |
| 30 | + if not config: |
| 31 | + raise ValueError("Configuration cannot be empty") |
| 32 | + |
| 33 | + form_flow = FormFlow(slug=slug) |
| 34 | + |
| 35 | + if not "startingPage" in config: |
| 36 | + raise ValueError("Configuration must contain 'startingPage'") |
| 37 | + |
| 38 | + starting_page_config = config.get("startingPage", {}) |
| 39 | + starting_page_id = starting_page_config.get("id", "") |
| 40 | + form_flow.create_starting_page( |
| 41 | + id=starting_page_id, |
| 42 | + name=starting_page_config.get("name", ""), |
| 43 | + slug=starting_page_config.get("slug", "/"), |
| 44 | + description=starting_page_config.get("description", ""), |
| 45 | + template=starting_page_config.get("template", ""), |
| 46 | + form=( |
| 47 | + getattr(importlib.import_module( |
| 48 | + f"app.forms.parts.{starting_page_config.get('form')}" |
| 49 | + ), starting_page_config.get('form')) |
| 50 | + if starting_page_config.get("form") |
| 51 | + else None |
| 52 | + ), |
| 53 | + yaml_config=starting_page_config, |
| 54 | + ) |
| 55 | + |
| 56 | + pages_config = {starting_page_id: starting_page_config} |
| 57 | + |
| 58 | + for page in config.get("pages", []): |
| 59 | + id = page.get("id", "") |
| 60 | + if not id or id in pages_config: |
| 61 | + raise ValueError("Each page must have a unique 'id'") |
| 62 | + pages_config.update({id: page}) |
| 63 | + form_flow.create_page( |
| 64 | + id=id, |
| 65 | + name=page.get("name", ""), |
| 66 | + slug=page.get("slug", ""), |
| 67 | + description=page.get("description", ""), |
| 68 | + template=page.get("template", ""), |
| 69 | + form=( |
| 70 | + getattr(importlib.import_module( |
| 71 | + f"app.forms.parts.{page.get('form')}" |
| 72 | + ), page.get('form')) |
| 73 | + if page.get("form") |
| 74 | + else None |
| 75 | + ), |
| 76 | + yaml_config=page, |
| 77 | + ) |
| 78 | + |
| 79 | + for page_id, page in form_flow.get_all_pages(): |
| 80 | + page_config = page.yaml_config |
| 81 | + |
| 82 | + for redirection in page_config.get("redirectWhenComplete", []): |
| 83 | + redirect_page = form_flow.get_page_by_id(redirection.get("page", "")) |
| 84 | + redirect_url = redirection.get("url", "") |
| 85 | + redirect_flask_method = redirection.get("flaskMethod", "") |
| 86 | + if not (redirect_page or redirect_url or redirect_flask_method): |
| 87 | + raise ValueError( |
| 88 | + f"Redirect target page or URL/flaskMethod must be provided for page '{page.slug}'." |
| 89 | + ) |
| 90 | + when = None |
| 91 | + if when_data := redirection.get("when", {}): |
| 92 | + key = when_data.get("key", "") |
| 93 | + value = when_data.get("value", "") |
| 94 | + if key and value: |
| 95 | + when = (key, value) |
| 96 | + page.redirect_when_complete( |
| 97 | + page=redirect_page, |
| 98 | + flask_method=redirect_flask_method, |
| 99 | + url=redirect_url, |
| 100 | + when=when, |
| 101 | + # condition=TODO |
| 102 | + ) |
| 103 | + |
| 104 | + for requirement in page_config.get("requireResponse", []): |
| 105 | + required_page = form_flow.get_page_by_id(requirement.get("page")) |
| 106 | + if not required_page: |
| 107 | + raise ValueError( |
| 108 | + f"Required page '{requirement.get('page')}' not found in form flow as a prerequisite to '{page.slug}'." |
| 109 | + ) |
| 110 | + page.require_response( |
| 111 | + page=required_page, |
| 112 | + key=requirement.get("key"), |
| 113 | + response=requirement.get("value", None), |
| 114 | + ) |
| 115 | + |
| 116 | + if require_completion_of := page_config.get("requireCompletionOf", []): |
| 117 | + required_pages = [ |
| 118 | + form_flow.get_page_by_id(id) for id in require_completion_of |
| 119 | + ] |
| 120 | + if any([page is None for page in required_pages]): |
| 121 | + raise ValueError( |
| 122 | + f"One or more required pages for 'requireCompletionOf' of '{page.slug}' not found in form flow." |
| 123 | + ) |
| 124 | + page.require_completion_of(*required_pages) |
| 125 | + |
| 126 | + if require_completion_of_any := page_config.get("requireCompletionOfAny", []): |
| 127 | + required_pages = [ |
| 128 | + form_flow.get_page_by_id(id) for id in require_completion_of_any |
| 129 | + ] |
| 130 | + if any([page is None for page in required_pages]): |
| 131 | + raise ValueError( |
| 132 | + f"One or more required pages for 'requireCompletionOfAny' of '{page.slug}' not found in form flow." |
| 133 | + ) |
| 134 | + fallback_page = form_flow.get_page_by_id( |
| 135 | + page_config.get("redirectIfNotComplete", None) |
| 136 | + ) |
| 137 | + page.require_completion_of_any( |
| 138 | + pages=required_pages, fallback_page=fallback_page |
| 139 | + ) |
| 140 | + |
| 141 | + return form_flow |
0 commit comments