Skip to content

Commit

Permalink
🔥 Dynamic post feed #119
Browse files Browse the repository at this point in the history
  • Loading branch information
DogukanUrker committed Dec 17, 2024
1 parent ccfc609 commit 8afd945
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 15 deletions.
16 changes: 12 additions & 4 deletions routes/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@indexBlueprint.route("/")
# Define a route for the home page with sorting parameters
@indexBlueprint.route("/by=<by>/sort=<sort>")
def index(by="timeStamp", sort="desc"):
def index(by="hot", sort="desc"):
"""
This function maps the home page route ("/") to the index function.
Expand All @@ -41,15 +41,15 @@ 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:
The rendered template of the home page with sorted posts according to the provided sorting options.
"""

# 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
Expand All @@ -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()

Expand Down
10 changes: 10 additions & 0 deletions templates/tailwindUI/components/sortMenu.html.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<option value="{{source}}/by=lastEditTimeStamp/sort=desc" class="block px-4 py-2 font-medium">
{{ translations.sortMenu.edit }}
</option>

<!-- Option for sorting by hot in descending order -->
<option value="{{source}}/by=hot/sort=desc" class="block px-4 py-2 font-medium">
{{ translations.sortMenu.hot }}
</option>
</optgroup>
<!-- Group for descending sorting options -->
<optgroup label="{{ translations.sortMenu.asc }}" class="font-bold">
Expand All @@ -54,6 +59,11 @@
<option value="{{source}}/by=lastEditTimeStamp/sort=asc" class="block px-4 py-2 font-medium">
{{ translations.sortMenu.edit }}
</option>

<!-- Option for sorting by hot in ascending order -->
<option value="{{source}}/by=hot/sort=asc" class="block px-4 py-2 font-medium">
{{ translations.sortMenu.hot }}
</option>
</optgroup>
</select>
<!-- Icon indicating sorting -->
Expand Down
3 changes: 2 additions & 1 deletion translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@
"title": "Titel",
"views": "Ansichten",
"category": "Kategorie",
"edit": "Letztes Bearbeitungsdatum"
"edit": "Letztes Bearbeitungsdatum",
"hot": "Heiß"
},
"search": {
"title": "Suche",
Expand Down
3 changes: 2 additions & 1 deletion translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@
"title": "Title",
"views": "Views",
"category": "Category",
"edit": "Last Edit Date"
"edit": "Last Edit Date",
"hot": "Hot"
},
"search": {
"title": "Search",
Expand Down
3 changes: 2 additions & 1 deletion translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion translations/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@
"title": "タイトル",
"views": "ビュー",
"category": "カテゴリ",
"edit": "最終編集日"
"edit": "最終編集日",
"hot": "ホット"
},
"search": {
"title": "検索",
Expand Down
3 changes: 2 additions & 1 deletion translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion translations/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion translations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@
"title": "Заголовок",
"views": "Просмотры",
"category": "Категория",
"edit": "Дата последнего редактирования"
"edit": "Дата последнего редактирования",
"hot": "Популярные"
},
"search": {
"title": "Поиск",
Expand Down
3 changes: 2 additions & 1 deletion translations/tr.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion translations/uk.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@
"title": "Назва",
"views": "Перегляди",
"category": "Категорія",
"edit": "Остання дата редагування"
"edit": "Остання дата редагування",
"hot": "Гаряче"
},
"search": {
"title": "Пошук",
Expand Down
3 changes: 2 additions & 1 deletion translations/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@
"title": "标题",
"views": "浏览量",
"category": "分类",
"edit": "最后编辑日期"
"edit": "最后编辑日期",
"hot": "热门"
},
"search": {
"title": "搜索",
Expand Down

0 comments on commit 8afd945

Please sign in to comment.