Skip to content

Commit

Permalink
revert commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DogukanUrker committed Dec 17, 2024
1 parent f9db932 commit 9c8e847
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "pypi"
flask = "==3.1.0"
flask-wtf = "==1.2.2"
passlib = "==1.7.4"
playwright = "==1.49.1"
playwright = "==1.49.0"
requests = "==2.32.3"
wtforms = "==3.2.1"
pipenv = "==2024.4.0"
Expand Down
6 changes: 4 additions & 2 deletions routes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ def category(category, by="timeStamp", sort="desc"):
cursor = connection.cursor()

# Executing SQL query to retrieve posts of the requested category and sorting them accordingly
query = "SELECT * FROM posts WHERE lower(category) = ? ORDER BY ? ?"
cursor.execute(query, (category.lower(), by, sort))
cursor.execute(
f"""select * from posts where lower(category) = ? order by {by} {sort}""",
[(category.lower())],
)
posts = cursor.fetchall()

# Modify the sorting name for better readability
Expand Down
18 changes: 3 additions & 15 deletions routes/changeUserName.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,9 @@ def changeUserName():
case True:
# Retrieve new username from the form
newUserName = request.form["newUserName"]
newUserName = newUserName.replace(" ", "") # Remove spaces from username
import re
if not re.match(r'^\w+$', newUserName):
flashMessage(
page="changeUserName",
message="invalid",
category="error",
language=session["language"],
)
return render_template(
"changeUserName.html.jinja",
form=form,
siteKey=RECAPTCHA_SITE_KEY,
recaptcha=RECAPTCHA,
)
newUserName = newUserName.replace(
" ", ""
) # Remove spaces from username
Log.sql(
f"Connecting to '{DB_USERS_ROOT}' database"
) # Log the database connection is started
Expand Down
16 changes: 2 additions & 14 deletions routes/editPost.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,10 @@
RECAPTCHA_SECRET_KEY, # Recaptcha secret key
generateurlID, # urlID generator from post title
)
import re
from urllib.parse import urlparse

# Create a blueprint for the edit post route
editPostBlueprint = Blueprint("editPost", __name__)

VALID_URL_IDS = ["validUrlId1", "validUrlId2", "validUrlId3"] # Example list of valid urlIDs

def is_valid_url_id(url_id):
return url_id in VALID_URL_IDS

# Define a route for editing a post
@editPostBlueprint.route("/editpost/<urlID>", methods=["GET", "POST"])
Expand Down Expand Up @@ -291,10 +285,7 @@ def editPost(urlID):
category="success",
language=session["language"],
) # Display a flash message
if is_valid_url_id(sessionUrlId):
return redirect(f"/post/{sessionUrlId}")
else:
return redirect('/')
return redirect(f"/post/{sessionUrlId}")
# Render the edit post template
return render_template(
"/editPost.html.jinja",
Expand Down Expand Up @@ -331,7 +322,4 @@ def editPost(urlID):
category="error",
language=session["language"],
) # Display a flash message
if re.match(r'^[a-zA-Z0-9\-]+$', urlID):
return redirect(f"/login/redirect=&editpost&{urlID}")
else:
return redirect('/login')
return redirect(f"/login/redirect=&editpost&{urlID}")
12 changes: 5 additions & 7 deletions routes/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,14 @@ def index(by="hot", 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
query = ""
params = ()
match by:
case "hot": # If the sorting field is "hot"
query = "SELECT *, (views * 1 / log(1 + (strftime('%s', 'now') - timeStamp) / 3600 + 2)) AS hotScore FROM posts ORDER BY hotScore ?"
params = (sort,)
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
query = "SELECT * FROM posts ORDER BY ? ?"
params = (by, sort)
cursor.execute(query, params) # Execute the parameterized query
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: 3 additions & 7 deletions routes/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,15 @@ def login(direct):
Raises:
401: If the login is unsuccessful.
"""
direct = direct.replace("&", "/").replace('\\', '') # Convert direct link parameter and handle backslashes
from urllib.parse import urlparse # Import urlparse for URL validation
if urlparse(direct).netloc or urlparse(direct).scheme:
# If the direct URL contains a host name or scheme, redirect to home page
direct = '/'
direct = direct.replace("&", "/") # Convert direct link parameter
match LOG_IN:
case True:
match "userName" in session:
case True:
# If user is already logged in, redirect
Log.danger(f'User: "{session["userName"]}" already logged in')
return (
redirect(direct), # Safe redirect after validation
redirect(direct),
301,
)
case False:
Expand Down Expand Up @@ -182,6 +178,6 @@ def login(direct):
)
case False:
return (
redirect(direct), # Safe redirect after validation
redirect(direct),
301,
)
8 changes: 3 additions & 5 deletions utils/flashMessage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from modules import (
load, # Import the load function from the modules module
flash, # Import the flash function from the modules module
Expand All @@ -22,10 +21,9 @@ def flashMessage(
None
"""
text = None # Initialize the text variable
base_path = "./translations"
translationFile = os.path.normpath(os.path.join(base_path, f"{language}.json")) # Define the path to the translation file
if not translationFile.startswith(base_path):
raise Exception("Invalid language path")
translationFile = (
f"./translations/{language}.json" # Define the path to the translation file
)
match exists(translationFile): # Check if the translation file exists
case True:
# If the translation file exists, open and load the JSON data
Expand Down

0 comments on commit 9c8e847

Please sign in to comment.