Skip to content

Commit

Permalink
Merge pull request #11 from PuckiSilver/master
Browse files Browse the repository at this point in the history
actually allow converting string to semver tuple for sorting & remove deprecated unused route (/projects/)
  • Loading branch information
HoodieRocks authored Dec 27, 2023
2 parents 98ed57b + 2eb6b3d commit 637bbb8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 56 deletions.
52 changes: 0 additions & 52 deletions routes/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,58 +201,6 @@ def search_projects() -> dict[str, Any] | tuple[str, int]:
}


@DeprecationWarning
@projects.route("/", methods=["GET"])
def all_projects() -> dict[str, Any] | tuple[str, int]:
page = request.args.get("page", 1)
page = int(page)
sort = request.args.get("sort", "updated")
tags = request.args.get("category", "")

# SQL stuff
conn = util.make_connection()
if sort == "updated":
if tags == "":
r = util.exec_query(
conn,
"select rowid, * from projects where status = 'live' ORDER BY updated DESC",
).all()
else:
r = util.exec_query(
conn,
"select rowid, * from projects where status = 'live' and category like :q ORDER BY updated DESC",
q=f"%{tags}%",
).all()
elif sort == "downloads":
if tags == "":
r = util.exec_query(
conn,
"select rowid, * from projects where status = 'live' ORDER BY downloads DESC",
).all()
else:
r = util.exec_query(
conn,
"select rowid, * from projects where status = 'live' and category like :q ORDER BY downloads DESC",
q=f"%{tags}%",
).all()
else:
return "Unknown sorting method.", 400

out: list[dict[str, Any]] = []

for item in r[(page - 1) * 24 : page * 24]:
try:
temp = parse_project(item, request, conn)
except:
conn.rollback()

return "Something bad happened", 500

out.append(temp)

return {"count": len(out), "result": out, "pages": str(math.ceil(len(r) / 24))}


@projects.route("/id/<int:id>")
def get_project_by_id(id: int) -> dict[str, Any] | tuple[str, int]:
conn = util.make_connection()
Expand Down
10 changes: 6 additions & 4 deletions utilities/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,18 @@ def send_notif(conn: Connection, title: str, msg: str, receiver: int):


# Custom sorting function for semver
def semver_key(version: str):
def semver_key(version: str) -> tuple[int, int, int]:
# Replace 'x' in the version with a high number for comparison
version = version.replace("x", "999999")
# Use regex to match major, minor, and patch numbers
match = re.match(r"(\d+)\.(\d+)\.(\d+)", version)
match = re.match(r"(\d+)(?:\.(\d+))?(?:\.(\d+))?", version)

if match:
# If there is a match, extract major, minor, and patch numbers
major, minor, patch = map(int, match.groups())
return major, minor, patch
groups = map(int, match.groups())
return (groups[0],
groups[1] if len(groups) >= 2 else 0,
groups[2] if len(groups) == 3 else 0)
else:
# If no match is found, return a tuple of zeros
return 0, 0, 0
Expand Down

0 comments on commit 637bbb8

Please sign in to comment.