Skip to content

Commit

Permalink
Fix other endpoint logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tekktrik committed Oct 30, 2024
1 parent a2af802 commit 8053b9c
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions flask_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,26 +143,35 @@ def about() -> str:
@app.route("/other/<pagenum>", methods=["GET"])
def other(pagenum: str = "1") -> str:
"""Route for other work page."""
pagenum = int(pagenum)
if pagenum <= 0:
pagenum = 1
# Load the other activities files and initialize them in a list
other_path = pathlib.Path("assets/other")
other_works = []
for other_filepath in other_path.glob("*.json"):
with open(other_filepath, encoding="utf-8") as otherfile:
other_obj = json.load(otherfile)
other_works.append(other_obj)

max_pages = len(other_works) // 5 + 1
# Calculate the maximum number of pages that can be rendered
# (assuming groups of 5 activities per page), minimum of 1 page
max_pages = (len(other_works) + 4) // 5
max_pages = 1 if max_pages < 1 else max_pages

# Convert the request page number to an integer and redirect
# if it is outside the possible bounds of allowable pages
pagenum = int(pagenum)
if pagenum < 1:
return redirect(url_for("other", pagenum=1))
if pagenum > max_pages:
return redirect(url_for("other", pagenum=max_pages))

# Calculate the start and end indices for the current page number
start_index = (pagenum - 1) * 5
end_index = start_index + 5

if start_index >= len(other_works):
return other(pagenum - 1)

# Sort the list of activities base on the datetime key
other_works.sort(key=lambda x: x["datetime"], reverse=True)

# Render the HTML template
return render_template(
"other.html",
works=other_works[start_index:end_index],
Expand Down

0 comments on commit 8053b9c

Please sign in to comment.