generated from hjess/PythonTemplateProject
cache fix og title
All checks were successful
Build, Push, and Deploy to Nomad / docker-nomad (push) Successful in 41s
All checks were successful
Build, Push, and Deploy to Nomad / docker-nomad (push) Successful in 41s
This commit is contained in:
@@ -4,7 +4,7 @@ from fastapi import APIRouter, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
import time
|
||||
|
||||
class CategoryController:
|
||||
def __init__(self,data_file="generated_data.json"):
|
||||
@@ -33,15 +33,51 @@ class CategoryController:
|
||||
return json.load(file)
|
||||
|
||||
async def get_index(self, request: Request):
|
||||
"""Index route."""
|
||||
"""
|
||||
Handle requests for the index (home) page.
|
||||
|
||||
This function is executed every time the root route (index page) is accessed.
|
||||
It renders the 'index.html' template and populates it with dynamic data, such as:
|
||||
- 'page_title': A static title for the home page ("Forside").
|
||||
- 'author': The author's name ("Henrik").
|
||||
- 'data': General data accessible to the template.
|
||||
|
||||
Args:
|
||||
request (Request): The HTTP request object.
|
||||
|
||||
Returns:
|
||||
TemplateResponse: A rendered HTML page for the index (home) route.
|
||||
"""
|
||||
return self.templates.TemplateResponse(
|
||||
"index.html",
|
||||
{"request": request, "data": self.data, "page_title": "Forside", "author": "Henrik"},
|
||||
)
|
||||
|
||||
async def get_category(self, request: Request, category_name: str):
|
||||
"""Category route."""
|
||||
"""
|
||||
Handle requests for specific category pages.
|
||||
|
||||
This function is executed every time a category route is accessed.
|
||||
It dynamically retrieves and serves content for the requested category.
|
||||
- Searches for the requested category in 'self.data["categories"]' based on the provided category name.
|
||||
- Reads the 'index.html' file located under 'data/{category_name}/' if it exists.
|
||||
- Returns the rendered 'category.html' template with the following dynamic data:
|
||||
- 'page_title': The name of the category.
|
||||
- 'author': The author of the category.
|
||||
- 'content': The content of the 'index.html' file.
|
||||
- 'timestamp': The current Unix time when the request is processed.
|
||||
- Returns a 404 HTML response if the category is not found or the file does not exist.
|
||||
|
||||
Args:
|
||||
request (Request): The HTTP request object.
|
||||
category_name (str): The name of the category being accessed.
|
||||
|
||||
Returns:
|
||||
TemplateResponse: A rendered HTML page with dynamic category content.
|
||||
HTMLResponse: A 404 response if the category does not exist.
|
||||
"""
|
||||
category = next((cat for cat in self.data["categories"] if cat["path"] == category_name), None)
|
||||
unix_time_now = int( time.time() )
|
||||
if category:
|
||||
category_file = f"data/{category_name}/index.html"
|
||||
if os.path.exists(category_file):
|
||||
@@ -55,6 +91,7 @@ class CategoryController:
|
||||
"page_title": category["name"],
|
||||
"author": category["author"],
|
||||
"content": category_content,
|
||||
"timestamp": unix_time_now
|
||||
},
|
||||
)
|
||||
return HTMLResponse("Kategori ikke fundet", status_code=404)
|
||||
|
||||
Reference in New Issue
Block a user