def render_mesure_as_html( mesure: dict, indicateurs: List[dict] = None, template_file="referentiels/html/mesure_citergie.j2", ) -> str: env = build_jinja_environment() template = env.get_template(template_file) years = range(2016, 2023) if indicateurs is None: indicateurs = [] # TODO /!\ This should not be hard-coded here, since it is defined elsewhere /!\ avancement_noms = { "faite": "Faite", "programmee": "Prévue", "en_cours": "En cours", "pas_faite": "Pas faite", "non_concernee": "Non concernée", } renderer = HTMLRenderer() description = Document(mesure["description"]) mesure["description"] = renderer.render(description) rendered = template.render( mesure=mesure, avancement_noms=avancement_noms, indicateurs=indicateurs, years=years, ) soup = BeautifulSoup(rendered, "html.parser") return soup.prettify()
def render_object(definition: dict, template_file="shared/python/objects.j2") -> str: env = build_jinja_environment() template = env.get_template(template_file) objects = objects_py(definition["yaml"]) rendered = template.render(objects=objects, comments=definition["comments"]) return format_str(rendered, mode=FileMode())
def render_thematiques_as_typescript( thematiques: Dict[str, str], template_file="shared/ts/thematiques.j2") -> str: """Render all thématiques into a single typescript file.""" env = build_jinja_environment() template = env.get_template(template_file) rendered = template.render(thematiques=thematiques) return jsbeautifier.beautify(rendered)
def render_object(definition: dict, template_file="shared/ts/objects.j2") -> str: env = build_jinja_environment() template = env.get_template(template_file) objects = objects_ts(definition["yaml"]) rendered = template.render(objects=objects, comments=definition["comments"]) return jsbeautifier.beautify(rendered)
def render_classes(definition: dict, pathname: str, template_file="shared/ts/classes.j2") -> str: env = build_jinja_environment() template = env.get_template(template_file) classes = classes_ts(definition["yaml"]) rendered = template.render(classes=classes, comments=definition["comments"], pathname=pathname) return jsbeautifier.beautify(rendered)
def render_actions_as_python( actions: List[dict], template_file="shared/python/actions_referentiel.j2") -> str: """Render all actions into a single python file.""" env = build_jinja_environment() def add_points(actions: List[dict]): for action in actions: action["points"] = action.get("points", None) add_points(action["actions"]) add_points(actions) template = env.get_template(template_file) rendered = template.render(actions=actions) return format_str(rendered, mode=FileMode())
def render_indicators_as_typescript( indicateurs: List[dict], template_file="shared/ts/indicateurs_referentiel.j2") -> str: env = build_jinja_environment() template = env.get_template(template_file) for indicateur in indicateurs: indicateur["thematique_id"] = extract_thematique_id(indicateur) indicateur["action_ids"] = extract_action_ids(indicateur) indicateur["description"] = extract_description(indicateur) indicateur["unite"] = extract_unite(indicateur) indicateur["uid"] = indicateur["id"] rendered = template.render(indicateurs=indicateurs) return jsbeautifier.beautify(rendered)
def _render_actions_as_typescript(files: List[str]) -> None: assert files actions = [] for file in files: md = load_md(file) action = build_action(md) actions.append(action) typescript = render_actions_as_typescript(actions) env = build_jinja_environment() to_json = env.get_template("tests/single_value_to_json.j2") for action in actions: nom_json = to_json.render(value=action["nom"]) assert escape_to_html(nom_json) in typescript
def render_actions_as_typescript( actions: List[dict], template_file="shared/ts/actions_referentiel.j2") -> str: """Render all actions into a single typescript file.""" env = build_jinja_environment() add_points(actions) render_field_text_to_html(actions, "description") render_field_text_to_html(actions, "contexte") render_field_text_to_html(actions, "exemples") render_field_text_to_html(actions, "preuve") render_field_text_to_html(actions, "ressources") template = env.get_template(template_file) rendered = template.render(actions=actions) return jsbeautifier.beautify(rendered)
def render_mesures_summary_as_html( mesures: List[dict], template_file="referentiels/html/mesures_summary_citergie.j2") -> str: """Renders mesures summmary into a single html string""" env = build_jinja_environment() template = env.get_template(template_file) thematiques = get_thematiques() by_theme = {} for mesure in mesures: theme = (mesure["climat_pratic_id"].strip() if "climat_pratic_id" in mesure.keys() else "") theme = theme if theme else "Pas de thème" if theme not in by_theme.keys(): by_theme[theme] = [] by_theme[theme].append(mesure) rendered = template.render(mesures=by_theme, thematiques=thematiques) soup = BeautifulSoup(rendered, "html.parser") return soup.prettify()
def render_indicators_as_html( indicateurs: List[dict], template_file="referentiels/html/indicateurs_citergie.j2") -> str: """Renders all indicators into a single html string""" env = build_jinja_environment() template = env.get_template(template_file) thematiques = get_thematiques() years = range(2016, 2023) by_thematique = {} renderer = HTMLRenderer() for indicateur in indicateurs: thematique = indicateur["climat_pratic_ids"][0] description = Document(indicateur["description"]) indicateur["description"] = renderer.render(description) if thematique not in by_thematique.keys(): by_thematique[thematique] = [] by_thematique[thematique].append(indicateur) rendered = template.render(indicateurs=by_thematique, years=years, thematiques=thematiques) soup = BeautifulSoup(rendered, "html.parser") return soup.prettify()
def test_build_jinja_environment(): env = build_jinja_environment() value = "test" template = env.get_template("tests/single_value.j2") rendered = template.render(value=value) assert rendered == value
def render_template(template_file: str, data: dict) -> str: env = build_jinja_environment() template = env.get_template(template_file) classes = classes_python(data) rendered = template.render(classes=classes) return format_str(rendered, mode=FileMode())