From ab1479925bb7d473caaf46e6890336572fa833cd Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Fri, 4 Feb 2022 18:02:13 -0800 Subject: [PATCH 1/2] fix: tu last votes listing vote id Signed-off-by: Kevin Morris --- aurweb/routers/trusted_user.py | 21 ++++++++++++--------- templates/partials/tu/last_votes.html | 6 +++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/aurweb/routers/trusted_user.py b/aurweb/routers/trusted_user.py index 2dc5b4f7..e9ad2ce2 100644 --- a/aurweb/routers/trusted_user.py +++ b/aurweb/routers/trusted_user.py @@ -5,7 +5,7 @@ from http import HTTPStatus from fastapi import APIRouter, Form, HTTPException, Request from fastapi.responses import RedirectResponse, Response -from sqlalchemy import and_, or_ +from sqlalchemy import and_, func, or_ from aurweb import db, l10n, logging, models, time from aurweb.auth import creds, requires_auth @@ -80,18 +80,21 @@ async def trusted_user(request: Request, if past_by == "asc" else past_votes.all() context["past_off"] = past_off - # TODO - # We order last votes by TUVote.VoteID and User.Username. - # This is really bad. We should add a Created column to - # TUVote of type Timestamp and order by that instead. - last_votes_by_tu = db.query(models.TUVote).filter( + last_vote = func.max(models.TUVote.VoteID).label("LastVote") + last_votes_by_tu = db.query(models.TUVote).join( + models.User + ).join(models.TUVoteInfo).filter( and_(models.TUVote.VoteID == models.TUVoteInfo.ID, + models.User.ID == models.TUVote.UserID, models.TUVoteInfo.End <= ts, - models.TUVote.UserID == models.User.ID, or_(models.User.AccountTypeID == 2, models.User.AccountTypeID == 4)) - ).group_by(models.User.ID).order_by( - models.TUVote.VoteID.desc(), models.User.Username.asc()) + ).with_entities( + models.TUVote.UserID, + func.max(models.TUVote.VoteID).label("LastVote"), + models.User.Username + ).group_by(models.TUVote.UserID).order_by( + last_vote.desc(), models.User.Username.asc()) context["last_votes_by_tu"] = last_votes_by_tu.all() context["current_by_next"] = "asc" if current_by == "desc" else "desc" diff --git a/templates/partials/tu/last_votes.html b/templates/partials/tu/last_votes.html index 94b9c1e8..a8a80d32 100644 --- a/templates/partials/tu/last_votes.html +++ b/templates/partials/tu/last_votes.html @@ -18,10 +18,10 @@ {% else %} {% for vote in votes %} - {{ vote.User.Username }} + {{ vote.Username }} - - {{ vote.VoteID }} + + {{ vote.LastVote }} From 987f9eab3b4170cfd170285cb3b15fd3c889f709 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Fri, 4 Feb 2022 18:04:47 -0800 Subject: [PATCH 2/2] fix: link to user account in last votes by tu listing Signed-off-by: Kevin Morris --- templates/partials/tu/last_votes.html | 6 +++++- test/test_trusted_user_routes.py | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/templates/partials/tu/last_votes.html b/templates/partials/tu/last_votes.html index a8a80d32..30d620d4 100644 --- a/templates/partials/tu/last_votes.html +++ b/templates/partials/tu/last_votes.html @@ -18,7 +18,11 @@ {% else %} {% for vote in votes %} - {{ vote.Username }} + + + {{ vote.Username }} + + {{ vote.LastVote }} diff --git a/test/test_trusted_user_routes.py b/test/test_trusted_user_routes.py index 0e6ca9ce..0ea44d10 100644 --- a/test/test_trusted_user_routes.py +++ b/test/test_trusted_user_routes.py @@ -251,6 +251,7 @@ def test_tu_index(client, tu_user): # Check to see the rows match up to our user and related vote. username, vote_id = rows[0] + username = username.xpath("./a")[0] vote_id = vote_id.xpath("./a")[0] assert username.text.strip() == tu_user.Username assert int(vote_id.text.strip()) == vote_records[1].ID @@ -464,6 +465,7 @@ def test_tu_index_last_votes(client, tu_user, user): last_vote = rows[0] user, vote_id = last_vote.xpath("./td") + user = user.xpath("./a")[0] vote_id = vote_id.xpath("./a")[0] assert user.text.strip() == tu_user.Username