Procházet zdrojové kódy

Add a page per site

jherve před 1 rokem
rodič
revize
157047d87e

+ 22 - 0
src/de_quoi_parle_le_monde/storage.py

@@ -327,6 +327,28 @@ class Storage:
             )
             )
             await conn.commit()
             await conn.commit()
 
 
+    async def list_main_articles(self, site_id: int, limit: int = 5):
+        async with self.conn as conn:
+            main_articles = await conn.execute_fetchall(
+                f"""
+                    SELECT *
+                    FROM main_articles_view
+                    WHERE site_id = ?
+                    ORDER BY timestamp_virtual DESC
+                    LIMIT ?
+                """,
+                [site_id, limit]
+            )
+
+            return [{
+                "snapshot_id": a[1],
+                "featured_article_snapshot_id": a[2],
+                "original_url": a[3],
+                "timestamp_virtual": a[4],
+                "title": a[5],
+                "url": a[6]
+            } for a in main_articles]
+
     async def select_from(self, table):
     async def select_from(self, table):
         async with self.conn as conn:
         async with self.conn as conn:
             return await conn.execute_fetchall(
             return await conn.execute_fetchall(

+ 14 - 2
src/de_quoi_parle_le_monde/web.py

@@ -1,9 +1,12 @@
-from fastapi import FastAPI, Request
+from fastapi import FastAPI, Request, Depends
 from fastapi.responses import HTMLResponse
 from fastapi.responses import HTMLResponse
 from fastapi.staticfiles import StaticFiles
 from fastapi.staticfiles import StaticFiles
 from fastapi.templating import Jinja2Templates
 from fastapi.templating import Jinja2Templates
 
 
 from de_quoi_parle_le_monde.medias import media_collection
 from de_quoi_parle_le_monde.medias import media_collection
+from de_quoi_parle_le_monde.storage import Storage
+
+
 app = FastAPI()
 app = FastAPI()
 
 
 
 
@@ -11,12 +14,21 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
 
 
 templates = Jinja2Templates(directory="templates")
 templates = Jinja2Templates(directory="templates")
 
 
+async def get_db():
+    return await Storage.create()
+
 
 
 @app.get("/", response_class=HTMLResponse)
 @app.get("/", response_class=HTMLResponse)
-async def read_root(request: Request):
+async def index(request: Request):
     return templates.TemplateResponse(request=request, name="index.html", context={})
     return templates.TemplateResponse(request=request, name="index.html", context={})
 
 
 
 
+@app.get("/sites/{id}/main_article", response_class=HTMLResponse)
+async def site_detail(request: Request, id: int, limit: int | None = None, storage: Storage = Depends(get_db)):
+    opt_args = [limit] if limit is not None else []
+    main_articles = await storage.list_main_articles(id, *opt_args)
+    return templates.TemplateResponse(request=request, name="site_detail.html", context={"site_id": id, "main_articles": main_articles})
+
 
 
 @app.get("/admin", response_class=HTMLResponse)
 @app.get("/admin", response_class=HTMLResponse)
 async def admin_index(request: Request):
 async def admin_index(request: Request):

+ 15 - 0
templates/site_detail.html

@@ -0,0 +1,15 @@
+<html>
+<head>
+    <title>Hello</title>
+    <link href="{{ url_for('static', path='/style.css') }}" rel="stylesheet">
+</head>
+<body>
+    <a href="{{ url_for('index') }}">Homepage</a>
+    <h1>Site {{ site_id }}</h1>
+
+    {% for a in main_articles %}
+        <h2>{{ a["title"] }}</h2>
+        <p>{{ a["timestamp_virtual"] }}</p>
+    {% endfor %}
+</body>
+</html>