-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathindex.py
executable file
·97 lines (75 loc) · 3.66 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
This file contains the routes for the Flask application.
The Blueprint "index" is used to define the home page of the application.
The route "/" maps the index function to the home page.
The index function retrieves all posts from the database and passes them to the index.html.jinja template.
The posts variable is passed to the index.html.jinja template as a list of dictionaries.
The index.html.jinja template displays the title and content of each post.
"""
from modules import (
DB_POSTS_ROOT, # Importing the constant for the path to the posts database
Blueprint, # Importing the Blueprint class for creating Flask blueprints
Log, # A class for logging messages
load, # A function for loading JSON data from files
redirect, # Importing the redirect function for redirecting requests
render_template, # Importing the render_template function for rendering Jinja templates
session, # A session object for storing user session data
sqlite3, # Importing the SQLite module for working with SQLite databases
)
# Create a blueprint for the home page with the name "index" and the current module name
indexBlueprint = Blueprint("index", __name__)
# Define a route for the home page
@indexBlueprint.route("/")
# Define a route for the home page with sorting parameters
@indexBlueprint.route("/by=<by>/sort=<sort>")
def index(by="hot", sort="desc"):
"""
This function maps the home page route ("/") to the index function.
It retrieves all posts from the database and passes them to the index.html.jinja template.
The posts variable is passed to the index.html.jinja template as a list of dictionaries.
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", "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.
"""
# Original copy of by
_by = by
# Define valid options for sorting and filtering
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
match by not in byOptions or sort not in sortOptions:
case True:
Log.warning(
f"The provided sorting options are not valid: By: {by} Sort: {sort}"
)
return redirect("/")
# Modify the sorting name for better readability
match by:
case "timeStamp":
by = "create"
case "lastEditTimeStamp":
by = "edit"
language = session.get("language") # Get the language from the session
translationFile = (
f"./translations/{language}.json" # Define the path to the translation file
)
with open(
translationFile, "r", encoding="utf-8"
) as file: # Open the translation file in read mode
translations = load(file) # Load the JSON data from the file
translations = translations["sortMenu"] # Get the translation for the sort menu
sortName = translations[by] + " - " + translations[sort]
# Log a info message that posts sorted by the specified field
Log.info(f"Sorting posts on index page by: {sortName}")
# Return the rendered template of the home page and pass the posts list and sorting name as keyword arguments
return render_template(
"index.html.jinja",
sortName=sortName,
source="",
sortBy=_by,
orderBy=sort,
categoryBy="all",
)