From 8afd9456a034038a636414cf3fd49dd871153f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fukan=20=C3=9Crker?= Date: Tue, 17 Dec 2024 14:37:51 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Dynamic=20post=20feed=20#119?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/index.py | 16 ++++++++++++---- .../tailwindUI/components/sortMenu.html.jinja | 10 ++++++++++ translations/de.json | 3 ++- translations/en.json | 3 ++- translations/es.json | 3 ++- translations/fr.json | 3 ++- translations/ja.json | 3 ++- translations/pl.json | 3 ++- translations/pt.json | 3 ++- translations/ru.json | 3 ++- translations/tr.json | 3 ++- translations/uk.json | 3 ++- translations/zh.json | 3 ++- 13 files changed, 44 insertions(+), 15 deletions(-) diff --git a/routes/index.py b/routes/index.py index d3068c35..feb18c94 100755 --- a/routes/index.py +++ b/routes/index.py @@ -30,7 +30,7 @@ @indexBlueprint.route("/") # Define a route for the home page with sorting parameters @indexBlueprint.route("/by=/sort=") -def index(by="timeStamp", sort="desc"): +def index(by="hot", sort="desc"): """ This function maps the home page route ("/") to the index function. @@ -41,7 +41,7 @@ def index(by="timeStamp", sort="desc"): The index.html.jinja template displays the title and content of each post. Parameters: - by (str): The field to sort by. Options are "timeStamp", "title", "views", "category", "lastEditTimeStamp". + by (str): The field to sort by. Options are "timeStamp", "title", "views", "category", "lastEditTimeStamp", "hot". sort (str): The order to sort in. Options are "asc" or "desc". Returns: @@ -49,7 +49,7 @@ def index(by="timeStamp", sort="desc"): """ # Define valid options for sorting and filtering - byOptions = ["timeStamp", "title", "views", "category", "lastEditTimeStamp"] + byOptions = ["timeStamp", "title", "views", "category", "lastEditTimeStamp", "hot"] sortOptions = ["asc", "desc"] # Check if the provided sorting options are valid, if not, redirect to the default route @@ -69,7 +69,15 @@ def index(by="timeStamp", sort="desc"): # Create a cursor object for executing queries cursor = connection.cursor() # Select all the columns from the posts table and order them by the specified field and sorting order - cursor.execute(f"select * from posts order by {by} {sort}") + match by: + case "hot": # If the sorting field is "hot" + cursor.execute( + f"SELECT *, (views * 1 / log(1 + (strftime('%s', 'now') - timeStamp) / 3600 + 2)) AS hotScore FROM posts ORDER BY hotScore {sort}" + ) # Execute the query to sort by hotness + pass + case _: # For all other sorting fields + cursor.execute(f"select * from posts order by {by} {sort}") # Execute the query to sort by the specified field + # Fetch all the results as a list of tuples posts = cursor.fetchall() diff --git a/templates/tailwindUI/components/sortMenu.html.jinja b/templates/tailwindUI/components/sortMenu.html.jinja index 948f7827..ce05aa03 100644 --- a/templates/tailwindUI/components/sortMenu.html.jinja +++ b/templates/tailwindUI/components/sortMenu.html.jinja @@ -31,6 +31,11 @@ + + + @@ -54,6 +59,11 @@ + + + diff --git a/translations/de.json b/translations/de.json index 58640e6e..193c4b3c 100644 --- a/translations/de.json +++ b/translations/de.json @@ -164,7 +164,8 @@ "title": "Titel", "views": "Ansichten", "category": "Kategorie", - "edit": "Letztes Bearbeitungsdatum" + "edit": "Letztes Bearbeitungsdatum", + "hot": "Heiß" }, "search": { "title": "Suche", diff --git a/translations/en.json b/translations/en.json index 6f0866b5..07425786 100644 --- a/translations/en.json +++ b/translations/en.json @@ -164,7 +164,8 @@ "title": "Title", "views": "Views", "category": "Category", - "edit": "Last Edit Date" + "edit": "Last Edit Date", + "hot": "Hot" }, "search": { "title": "Search", diff --git a/translations/es.json b/translations/es.json index d895b44c..ef685ce0 100644 --- a/translations/es.json +++ b/translations/es.json @@ -164,7 +164,8 @@ "title": "Título", "views": "Vistas", "category": "Categoría", - "edit": "Fecha de Última Edición" + "edit": "Fecha de Última Edición", + "hot": "Caliente" }, "search": { "title": "Buscar", diff --git a/translations/fr.json b/translations/fr.json index 4c101bba..85930822 100644 --- a/translations/fr.json +++ b/translations/fr.json @@ -164,7 +164,8 @@ "title": "Titre", "views": "Vues", "category": "Catégorie", - "edit": "Dernière date de modification" + "edit": "Dernière date de modification", + "hot": "Chaud" }, "search": { "title": "Recherche", diff --git a/translations/ja.json b/translations/ja.json index a163b269..57a41e68 100644 --- a/translations/ja.json +++ b/translations/ja.json @@ -165,7 +165,8 @@ "title": "タイトル", "views": "ビュー", "category": "カテゴリ", - "edit": "最終編集日" + "edit": "最終編集日", + "hot": "ホット" }, "search": { "title": "検索", diff --git a/translations/pl.json b/translations/pl.json index f0060358..0988a196 100644 --- a/translations/pl.json +++ b/translations/pl.json @@ -164,7 +164,8 @@ "title": "Tytuł", "views": "Wyświetlenia", "category": "Kategoria", - "edit": "Data ostatniej edycji" + "edit": "Data ostatniej edycji", + "hot": "Gorące" }, "search": { "title": "Szukaj", diff --git a/translations/pt.json b/translations/pt.json index eb164954..c7f586e3 100644 --- a/translations/pt.json +++ b/translations/pt.json @@ -164,7 +164,8 @@ "title": "Título", "views": "Visualizações", "category": "Categoria", - "edit": "Última Data de Edição" + "edit": "Última Data de Edição", + "hot": "Quente" }, "search": { "title": "Buscar", diff --git a/translations/ru.json b/translations/ru.json index c0969b83..0821c7d9 100644 --- a/translations/ru.json +++ b/translations/ru.json @@ -164,7 +164,8 @@ "title": "Заголовок", "views": "Просмотры", "category": "Категория", - "edit": "Дата последнего редактирования" + "edit": "Дата последнего редактирования", + "hot": "Популярные" }, "search": { "title": "Поиск", diff --git a/translations/tr.json b/translations/tr.json index d9ebc981..eb7faf32 100644 --- a/translations/tr.json +++ b/translations/tr.json @@ -164,7 +164,8 @@ "title": "Başlık", "views": "Görüntülenme", "category": "Kategori", - "edit": "Son Düzenleme Tarihi" + "edit": "Son Düzenleme Tarihi", + "hot": "Popülerlik" }, "search": { "title": "Arama", diff --git a/translations/uk.json b/translations/uk.json index 0cf50f8e..29950ee2 100644 --- a/translations/uk.json +++ b/translations/uk.json @@ -164,7 +164,8 @@ "title": "Назва", "views": "Перегляди", "category": "Категорія", - "edit": "Остання дата редагування" + "edit": "Остання дата редагування", + "hot": "Гаряче" }, "search": { "title": "Пошук", diff --git a/translations/zh.json b/translations/zh.json index 62e72038..4f5a6847 100644 --- a/translations/zh.json +++ b/translations/zh.json @@ -163,7 +163,8 @@ "title": "标题", "views": "浏览量", "category": "分类", - "edit": "最后编辑日期" + "edit": "最后编辑日期", + "hot": "热门" }, "search": { "title": "搜索",