open-law/app/controllers/jinja_globals.py

53 lines
1.4 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-19 08:08:52 +00:00
from flask import url_for
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:
text = text.replace(
tag,
f"<a href='#' target='_blank' class='{classes}'>{tag}</a>",
)
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
sub_collection = None
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
sub_collection: m.Collection = collection
collection: m.Collection = collection.parent
book: m.Book = section.version.book
url = url_for(
"book.qa_view",
book_id=book.id,
collection_id=collection.id,
sub_collection_id=sub_collection.id if sub_collection else None,
section_id=section.id,
interpretation_id=interpretation.id,
)
return url