From 4edadd7623a4e2dcf46e6b8cde8202ca8f058574 Mon Sep 17 00:00:00 2001 From: zwitschi Date: Mon, 27 Apr 2026 18:48:22 +0200 Subject: [PATCH] Refactor code for improved readability and consistency across templates and frontend logic Co-authored-by: Copilot --- frontend/app/main.py | 13 +- frontend/app/static/app.js | 36 +-- frontend/app/templates/admin.html | 30 ++- frontend/app/templates/generate_text.html | 51 +++-- frontend/app/templates/generate_video.html | 104 ++++++--- frontend/tests/test_frontend.py | 246 +++++++++++++++++---- 6 files changed, 351 insertions(+), 129 deletions(-) diff --git a/frontend/app/main.py b/frontend/app/main.py index a613bf1..02484b8 100644 --- a/frontend/app/main.py +++ b/frontend/app/main.py @@ -70,7 +70,8 @@ def login(): if request.method == "POST": email = request.form["email"] password = request.form["password"] - resp = _api("POST", "/auth/login", json={"email": email, "password": password}) + resp = _api("POST", "/auth/login", + json={"email": email, "password": password}) if resp.status_code == 200: data = resp.json() session["access_token"] = data["access_token"] @@ -90,7 +91,8 @@ def register(): if request.method == "POST": email = request.form["email"] password = request.form["password"] - resp = _api("POST", "/auth/register", json={"email": email, "password": password}) + resp = _api("POST", "/auth/register", + json={"email": email, "password": password}) if resp.status_code == 201: flash("Account created. Please log in.", "success") return redirect(url_for("login")) @@ -207,7 +209,8 @@ def admin(): @admin_required def admin_set_role(user_id: str): role = request.form.get("role", "user") - _api("PUT", f"/users/{user_id}/role", token=session["access_token"], json={"role": role}) + _api("PUT", f"/users/{user_id}/role", + token=session["access_token"], json={"role": role}) flash(f"Role updated to '{role}'.", "success") return redirect(url_for("admin")) @@ -238,7 +241,8 @@ def profile(): resp = _api("PUT", "/users/me", token=token, json=payload) if resp.status_code == 200: updated = resp.json() - session["user_email"] = updated.get("email", session.get("user_email", "")) + session["user_email"] = updated.get( + "email", session.get("user_email", "")) flash("Profile updated.", "success") else: flash(resp.json().get("detail", "Update failed."), "error") @@ -246,4 +250,3 @@ def profile(): resp = _api("GET", "/users/me", token=token) user = resp.json() if resp.status_code == 200 else {} return render_template("profile.html", user=user) - diff --git a/frontend/app/static/app.js b/frontend/app/static/app.js index 4159418..3c5996f 100644 --- a/frontend/app/static/app.js +++ b/frontend/app/static/app.js @@ -1,36 +1,40 @@ -document.addEventListener('DOMContentLoaded', () => { +document.addEventListener("DOMContentLoaded", () => { // ── Loading overlay ──────────────────────────────────── - const overlay = document.getElementById('loading-overlay'); + const overlay = document.getElementById("loading-overlay"); - document.querySelectorAll('form').forEach((form) => { - form.addEventListener('submit', () => { - if (overlay) overlay.classList.add('active'); + document.querySelectorAll("form").forEach((form) => { + form.addEventListener("submit", () => { + if (overlay) overlay.classList.add("active"); }); }); // ── Hamburger menu ───────────────────────────────────── - const hamburger = document.querySelector('.hamburger'); - const navLinks = document.querySelector('.nav-links'); + const hamburger = document.querySelector(".hamburger"); + const navLinks = document.querySelector(".nav-links"); if (hamburger && navLinks) { - hamburger.addEventListener('click', () => { - navLinks.classList.toggle('open'); + hamburger.addEventListener("click", () => { + navLinks.classList.toggle("open"); }); } // ── Generate dropdown tabs ───────────────────────────── - document.querySelectorAll('.tab-btn').forEach((btn) => { - btn.addEventListener('click', () => { + document.querySelectorAll(".tab-btn").forEach((btn) => { + btn.addEventListener("click", () => { const target = btn.dataset.tab; - const container = btn.closest('.tabs-container'); + const container = btn.closest(".tabs-container"); if (!container) return; - container.querySelectorAll('.tab-btn').forEach((b) => b.classList.remove('active')); - container.querySelectorAll('.tab-panel').forEach((p) => p.classList.remove('active')); + container + .querySelectorAll(".tab-btn") + .forEach((b) => b.classList.remove("active")); + container + .querySelectorAll(".tab-panel") + .forEach((p) => p.classList.remove("active")); - btn.classList.add('active'); + btn.classList.add("active"); const panel = container.querySelector(`#tab-${target}`); - if (panel) panel.classList.add('active'); + if (panel) panel.classList.add("active"); }); }); }); diff --git a/frontend/app/templates/admin.html b/frontend/app/templates/admin.html index 70f93e1..5bb35b2 100644 --- a/frontend/app/templates/admin.html +++ b/frontend/app/templates/admin.html @@ -1,5 +1,4 @@ -{% extends "base.html" %} -{% block title %}Admin — AI Allucanget{% endblock %} +{% extends "base.html" %} {% block title %}Admin — AI Allucanget{% endblock %} {% block content %}

Admin Dashboard

@@ -41,25 +40,38 @@
-
- + +
{% if u.id != session.get('user_id') %} -
- + +
{% endif %}
{% else %} - No users found. + + No users found. + {% endfor %} diff --git a/frontend/app/templates/generate_text.html b/frontend/app/templates/generate_text.html index db48497..d75800f 100644 --- a/frontend/app/templates/generate_text.html +++ b/frontend/app/templates/generate_text.html @@ -1,35 +1,44 @@ -{% extends "base.html" %} -{% block title %}Text Generation — AI Allucanget{% endblock %} -{% block content %} +{% extends "base.html" %} {% block title %}Text Generation — AI Allucanget{% +endblock %} {% block content %}

Text Generation

- + - +
{% if error %} -
{{ error }}
- {% endif %} - - {% if result %} -
-

Result

-
{{ result.content }}
- {% if result.usage %} -

- Tokens: {{ result.usage.get('total_tokens', '—') }} -

- {% endif %} -
+
{{ error }}
+ {% endif %} {% if result %} +
+

Result

+
{{ result.content }}
+ {% if result.usage %} +

+ Tokens: {{ result.usage.get('total_tokens', '—') }} +

+ {% endif %} +
{% endif %}
{% endblock %} diff --git a/frontend/app/templates/generate_video.html b/frontend/app/templates/generate_video.html index 8b7acba..7c5e7a8 100644 --- a/frontend/app/templates/generate_video.html +++ b/frontend/app/templates/generate_video.html @@ -1,28 +1,43 @@ -{% extends "base.html" %} -{% block title %}Video Generation — AI Allucanget{% endblock %} -{% block content %} +{% extends "base.html" %} {% block title %}Video Generation — AI Allucanget{% +endblock %} {% block content %}

Video Generation

- - + +
- + - + - + + - + - + - +