Files
kakebo/app/public/liveview_handlers/navigation.py
Andros Fenollosa 7d6197cc17 Translate all UI text and seed data from Spanish to English
- Translate all templates: login, dashboard, expense success, categories
  CRUD, subcategory CRUD, month start page, nav bar, partials
- Translate seed categories: Survival, Leisure & vice, Culture, Extras
  with all subcategories in English
- Translate form placeholders and view titles
- Translate error messages (login, delete confirmations)
- Change LANGUAGE_CODE from "es" to "en-us"
- Change html lang from "es" to "en"
- Format all Python files with Ruff (tabs)
2026-03-12 08:20:59 +01:00

95 lines
2.3 KiB
Python

import re
from liveview import liveview_handler, send
from django.test import Client
from app.public.liveview_handlers.modal import _inject_liveview_submit_page
@liveview_handler("navigate")
def navigate(consumer, content):
url = content.get("data", {}).get("data_url", "/")
client = Client()
if hasattr(consumer, "scope") and "user" in consumer.scope:
user = consumer.scope["user"]
if user and user.is_authenticated:
client.force_login(user)
ws_session = consumer.scope.get("session")
if ws_session:
session = client.session
for key, value in ws_session.items():
if not key.startswith("_auth_user"):
session[key] = value
session.save()
try:
response = client.get(
url,
follow=True,
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
SERVER_NAME="localhost",
)
if response.status_code == 200:
html_content = response.content.decode("utf-8")
main_match = re.search(r"<main[^>]*>(.*?)</main>", html_content, re.DOTALL)
if main_match:
main_content = _inject_liveview_submit_page(main_match.group(1), url)
title_match = re.search(r"<title>(.*?)</title>", html_content)
page_title = title_match.group(1) if title_match else "Kakebo"
script_matches = re.findall(
r"<script[^>]*>(.*?)</script>", html_content, re.DOTALL
)
inline_scripts = []
for match in script_matches:
script_content = match.strip()
if script_content and not script_content.startswith("http"):
inline_scripts.append(f"<script>{script_content}</script>")
main_content_with_scripts = main_content
if inline_scripts:
main_content_with_scripts = (
main_content + "\n" + "\n".join(inline_scripts)
)
send(
consumer,
{
"target": "#main-content",
"html": main_content_with_scripts,
"url": url,
"title": page_title,
},
)
else:
send(
consumer,
{
"target": "#main-content",
"html": '<div class="alert alert-error">Could not load page content</div>',
},
)
else:
send(
consumer,
{
"target": "#main-content",
"html": f'<div class="alert alert-error">Error {response.status_code}</div>',
},
)
except Exception as e:
send(
consumer,
{
"target": "#main-content",
"html": f'<div class="alert alert-error">{e}</div>',
},
)