open-law/app/controllers/jinja_globals.py

60 lines
1.5 KiB
Python
Raw Normal View History

2023-05-22 06:54:20 +00:00
import re
from flask import current_app
2023-04-28 13:03:48 +00:00
from flask_wtf import FlaskForm
2023-05-30 11:48:28 +00:00
from flask import url_for, render_template
2023-05-19 08:08:52 +00:00
from app import models as m
2023-04-28 13:03:48 +00:00
2023-05-22 06:54:20 +00:00
TAG_REGEX = re.compile(r"\[.*?\]")
2023-04-28 13:03:48 +00:00
# Using: {{ form_hidden_tag() }}
def form_hidden_tag():
form = FlaskForm()
return form.hidden_tag()
2023-05-22 06:54:20 +00:00
# Using: {{ display_tags("Some text with [tags] here") }}
def display_tags(text: str):
tags = current_app.config["TAG_REGEX"].findall(text)
classes = ["text-orange-500", "!no-underline"]
classes = " ".join(classes)
for tag in tags:
2023-05-24 13:45:38 +00:00
url = url_for(
"search.tag_search_interpretations",
tag_name=tag.lower().replace("[", "").replace("]", ""),
)
2023-05-22 06:54:20 +00:00
text = text.replace(
tag,
2023-05-24 13:45:38 +00:00
f"<a href='{url}' class='{classes}'>{tag}</a>",
2023-05-22 06:54:20 +00:00
)
return text
2023-05-19 08:08:52 +00:00
# Using: {{ build_qa_url(interpretation) }}
def build_qa_url_using_interpretation(interpretation: m.Interpretation):
section: m.Section = interpretation.section
collection: m.Collection = section.collection
2023-05-19 11:36:55 +00:00
if collection.parent and not collection.parent.is_root:
2023-05-19 08:08:52 +00:00
collection: m.Collection = collection.parent
book: m.Book = section.version.book
url = url_for(
"book.qa_view",
book_id=book.id,
interpretation_id=interpretation.id,
)
return url
2023-05-30 11:48:28 +00:00
def recursive_render(collection: m.Collection, book: m.Book):
return render_template(
"book/components/sub_collection_tab_content.html",
collection=collection,
book=book,
)