Просмотр исходного кода

Handle latest main article case just as the others

jherve 1 год назад
Родитель
Сommit
9e6500d315
3 измененных файлов с 28 добавлено и 27 удалено
  1. 24 6
      src/de_quoi_parle_le_monde/storage.py
  2. 3 20
      src/de_quoi_parle_le_monde/web.py
  3. 1 1
      templates/index.html

+ 24 - 6
src/de_quoi_parle_le_monde/storage.py

@@ -403,17 +403,35 @@ class Storage:
         featured_article_snapshot_id: int | None = None,
     ):
         async with self.conn as conn:
+            if featured_article_snapshot_id is None:
+                timestamp_query, timestamp_params = (
+                    """
+                    SELECT timestamp_virtual
+                    FROM main_articles_view mav
+                    WHERE site_id = ?
+                    ORDER BY timestamp_virtual DESC
+                    LIMIT 1
+                    """,
+                    [site_id],
+                )
+            else:
+                timestamp_query, timestamp_params = (
+                    """
+                    SELECT timestamp_virtual
+                    FROM main_articles_view mav
+                    WHERE site_id = ? AND featured_article_snapshot_id = ?
+                    """,
+                    [site_id, featured_article_snapshot_id],
+                )
+
             # This query is the union of 3 queries that respectively fetch :
             #   * articles published at the same time as the queried article (including the queried article)
             #   * the article published just after, on the same site
             #   *the article published just before, on the same site
             main_articles = await conn.execute_fetchall(
                 f"""
-                WITH original_timestamp AS
-                (
-                    SELECT timestamp_virtual
-                    FROM main_articles_view mav
-                    WHERE site_id = ? AND featured_article_snapshot_id = ?
+                WITH original_timestamp AS (
+                    {timestamp_query}
                 ), mav_diff AS (
                     SELECT mav.*, unixepoch(mav.timestamp_virtual) - unixepoch((SELECT * FROM original_timestamp)) AS time_diff
                     FROM main_articles_view mav
@@ -437,7 +455,7 @@ class Storage:
                     LIMIT 1
                 )
                 """,
-                [site_id, featured_article_snapshot_id, site_id, site_id],
+                timestamp_params + [site_id, site_id],
             )
 
             return [

+ 3 - 20
src/de_quoi_parle_le_monde/web.py

@@ -29,11 +29,12 @@ async def index(request: Request, storage: Storage = Depends(get_db)):
     )
 
 
+@app.get("/sites/{id}/main_article", response_class=HTMLResponse)
 @app.get("/sites/{id}/main_article/{snapshot_id}", response_class=HTMLResponse)
 async def site_main_article_snapshot(
     request: Request,
     id: int,
-    snapshot_id: int,
+    snapshot_id: int | None = None,
     storage: Storage = Depends(get_db),
     sim_index: SimilaritySearch = Depends(get_similarity_search),
 ):
@@ -46,9 +47,7 @@ async def site_main_article_snapshot(
 
     main_articles = await storage.list_neighbouring_main_articles(id, snapshot_id)
     [focused_article] = [
-        a
-        for a in main_articles
-        if a["site_id"] == id and a["featured_article_snapshot_id"] == snapshot_id
+        a for a in main_articles if a["site_id"] == id and a["time_diff"] == 0
     ]
     simultaneous_articles = [
         a for a in main_articles if a["site_id"] != id and a["time_diff"] == 0
@@ -106,22 +105,6 @@ async def site_main_article_snapshot(
     )
 
 
-@app.get("/sites/{id}/main_article", response_class=HTMLResponse)
-async def site_main_article(
-    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)
 async def admin_index(request: Request):
     return templates.TemplateResponse(

+ 1 - 1
templates/index.html

@@ -9,7 +9,7 @@
 
     <ul>
         {% for s in sites %}
-            <li><a href="{{ url_for('site_main_article', id=s['id']) }}">{{ s["name"] }}</a></li>
+            <li><a href="{{ url_for('site_main_article_snapshot', id=s['id']) }}">{{ s["name"] }}</a></li>
         {% endfor %}
     </ul>
 </body>