From a549f3f7a6a7185a09638c1817b15483617c0dfa Mon Sep 17 00:00:00 2001 From: Henrik Jess Nielsen Date: Fri, 20 Dec 2024 23:15:44 +0100 Subject: [PATCH] cache fix og title --- app/controllers/category_controller.py | 43 ++++++++++++++++++-- data/Kultur/index.html | 56 +++++++++++++------------- templates/base_template.html | 5 ++- templates/category.html | 2 +- 4 files changed, 72 insertions(+), 34 deletions(-) diff --git a/app/controllers/category_controller.py b/app/controllers/category_controller.py index 0573fff..24e8264 100644 --- a/app/controllers/category_controller.py +++ b/app/controllers/category_controller.py @@ -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) diff --git a/data/Kultur/index.html b/data/Kultur/index.html index 3346e27..ee9f684 100644 --- a/data/Kultur/index.html +++ b/data/Kultur/index.html @@ -54,29 +54,29 @@
  • UCI El Corte Inglés: Den moderne biografoplevelse med de nyeste blockbusters. Sammenligneligt med Palads i København, men med en lidt mere luksuriøs oplevelse i selve komplekset.
  • - -