generated from hjess/PythonTemplateProject
Lets test
This commit is contained in:
0
app/controllers/__init__.py
Normal file
0
app/controllers/__init__.py
Normal file
56
app/controllers/category_controller.py
Normal file
56
app/controllers/category_controller.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import os
|
||||
import json
|
||||
from fastapi import APIRouter, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
|
||||
class CategoryController:
|
||||
def __init__(self):
|
||||
"""Initialize the controller."""
|
||||
self.router = APIRouter()
|
||||
self.templates = Jinja2Templates(directory="templates")
|
||||
self.data = self._load_mock_data()
|
||||
self._add_routes()
|
||||
|
||||
def _load_mock_data(self):
|
||||
"""Load mock data from a JSON file."""
|
||||
with open("mock_data.json") as file:
|
||||
return json.load(file)
|
||||
|
||||
def _add_routes(self):
|
||||
"""Add routes to the router."""
|
||||
self.router.add_api_route("/", self.get_index, methods=["GET"], response_class=HTMLResponse)
|
||||
self.router.add_api_route(
|
||||
"/category/{category_name}",
|
||||
self.get_category,
|
||||
methods=["GET"],
|
||||
response_class=HTMLResponse,
|
||||
)
|
||||
|
||||
async def get_index(self, request: Request):
|
||||
"""Index 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."""
|
||||
category = next((cat for cat in self.data["categories"] if cat["path"] == category_name), None)
|
||||
if category:
|
||||
category_file = f"data/{category_name}/index.html"
|
||||
if os.path.exists(category_file):
|
||||
with open(category_file) as file:
|
||||
category_content = file.read()
|
||||
return self.templates.TemplateResponse(
|
||||
"category.html",
|
||||
{
|
||||
"request": request,
|
||||
"data": self.data,
|
||||
"page_title": category["name"],
|
||||
"author": category["author"],
|
||||
"content": category_content,
|
||||
},
|
||||
)
|
||||
return HTMLResponse("Kategori ikke fundet", status_code=404)
|
||||
65
app/controllers/dynamic_controller.py
Normal file
65
app/controllers/dynamic_controller.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import os
|
||||
import json
|
||||
from fastapi import APIRouter, Request
|
||||
from fastapi.responses import HTMLResponse, Response
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
class DynamicController:
|
||||
def __init__(self, data_dir: str):
|
||||
"""Initialize the dynamic controller."""
|
||||
self.router = APIRouter()
|
||||
self.templates = Jinja2Templates(directory="templates")
|
||||
self.data_dir = data_dir
|
||||
self.data = self._load_mock_data()
|
||||
self._add_dynamic_routes()
|
||||
|
||||
def _load_mock_data(self):
|
||||
"""Load mock data from a JSON file."""
|
||||
with open("mock_data.json") as file:
|
||||
return json.load(file)
|
||||
|
||||
def _add_dynamic_routes(self):
|
||||
"""Scan data directory and create dynamic routes."""
|
||||
for root, dirs, files in os.walk(self.data_dir):
|
||||
for directory in dirs:
|
||||
route_path = f"/{directory}" # Create route based on directory name
|
||||
directory_path = os.path.join(root, directory)
|
||||
|
||||
# Register route dynamically
|
||||
self.router.add_api_route(
|
||||
route_path,
|
||||
self._serve_dynamic_template(directory, directory_path),
|
||||
methods=["GET"],
|
||||
response_class=HTMLResponse,
|
||||
)
|
||||
|
||||
def _serve_dynamic_template(self, route_name: str, directory_path: str):
|
||||
"""Closure to serve templates for each route."""
|
||||
|
||||
async def route_handler(request: Request):
|
||||
# Look for index.html or render fallback content
|
||||
index_html = os.path.join(directory_path, "index.html")
|
||||
if os.path.exists(index_html):
|
||||
with open(index_html, "r", encoding="utf-8") as file:
|
||||
content = file.read()
|
||||
# Find the author for this route from preloaded data
|
||||
for category in self.data.get("categories", []):
|
||||
if category["path"] == route_name:
|
||||
author_name = category["author"]
|
||||
break
|
||||
# Pass required data to the template
|
||||
return self.templates.TemplateResponse(
|
||||
"category.html",
|
||||
{
|
||||
"request": request,
|
||||
"page_title": route_name.capitalize(),
|
||||
"content": content,
|
||||
"author": author_name,
|
||||
"data": self.data, # Pass additional data if needed
|
||||
},
|
||||
)
|
||||
|
||||
# Fallback: Return a 404 if no content is found
|
||||
return Response(f"No content found for {route_name}", status_code=404)
|
||||
|
||||
return route_handler
|
||||
Reference in New Issue
Block a user