mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
fix(FastAPI): use popupdate when [un]voting
The `aurweb.scripts.popupdate` script is used to maintain the NumVotes and Popularity field. We could do the NumVotes change more simply; however, since this is already a long-term implementation, we're going to use it until we move scripts over to ORM. Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
01fb42c5d9
commit
63498f5edd
3 changed files with 47 additions and 10 deletions
|
@ -30,7 +30,7 @@ from aurweb.models.request_type import DELETION_ID, RequestType
|
||||||
from aurweb.models.user import User
|
from aurweb.models.user import User
|
||||||
from aurweb.packages.search import PackageSearch
|
from aurweb.packages.search import PackageSearch
|
||||||
from aurweb.packages.util import get_pkg_or_base, get_pkgbase_comment, query_notified, query_voted
|
from aurweb.packages.util import get_pkg_or_base, get_pkgbase_comment, query_notified, query_voted
|
||||||
from aurweb.scripts import notify
|
from aurweb.scripts import notify, popupdate
|
||||||
from aurweb.scripts.rendercomment import update_comment_render
|
from aurweb.scripts.rendercomment import update_comment_render
|
||||||
from aurweb.templates import make_context, render_raw_template, render_template
|
from aurweb.templates import make_context, render_raw_template, render_template
|
||||||
|
|
||||||
|
@ -858,6 +858,10 @@ async def pkgbase_vote(request: Request, name: str):
|
||||||
PackageBase=pkgbase,
|
PackageBase=pkgbase,
|
||||||
VoteTS=now)
|
VoteTS=now)
|
||||||
|
|
||||||
|
# Update NumVotes/Popularity.
|
||||||
|
conn = db.ConnectionExecutor(db.get_engine().raw_connection())
|
||||||
|
popupdate.run_single(conn, pkgbase)
|
||||||
|
|
||||||
return RedirectResponse(f"/pkgbase/{name}",
|
return RedirectResponse(f"/pkgbase/{name}",
|
||||||
status_code=int(HTTPStatus.SEE_OTHER))
|
status_code=int(HTTPStatus.SEE_OTHER))
|
||||||
|
|
||||||
|
@ -875,6 +879,10 @@ async def pkgbase_unvote(request: Request, name: str):
|
||||||
with db.begin():
|
with db.begin():
|
||||||
db.session.delete(vote)
|
db.session.delete(vote)
|
||||||
|
|
||||||
|
# Update NumVotes/Popularity.
|
||||||
|
conn = db.ConnectionExecutor(db.get_engine().raw_connection())
|
||||||
|
popupdate.run_single(conn, pkgbase)
|
||||||
|
|
||||||
return RedirectResponse(f"/pkgbase/{name}",
|
return RedirectResponse(f"/pkgbase/{name}",
|
||||||
status_code=int(HTTPStatus.SEE_OTHER))
|
status_code=int(HTTPStatus.SEE_OTHER))
|
||||||
|
|
||||||
|
|
|
@ -5,17 +5,44 @@ from datetime import datetime
|
||||||
import aurweb.db
|
import aurweb.db
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def run_single(conn, pkgbase):
|
||||||
conn = aurweb.db.Connection()
|
""" A single popupdate. The given pkgbase instance will be
|
||||||
conn.execute(("UPDATE PackageBases SET NumVotes = ("
|
refreshed after the database update is done.
|
||||||
|
|
||||||
|
NOTE: This function is compatible only with aurweb FastAPI.
|
||||||
|
|
||||||
|
:param conn: db.Connection[Executor]
|
||||||
|
:param pkgbase: Instance of db.PackageBase
|
||||||
|
"""
|
||||||
|
|
||||||
|
conn.execute("UPDATE PackageBases SET NumVotes = ("
|
||||||
"SELECT COUNT(*) FROM PackageVotes "
|
"SELECT COUNT(*) FROM PackageVotes "
|
||||||
"WHERE PackageVotes.PackageBaseID = PackageBases.ID)"))
|
"WHERE PackageVotes.PackageBaseID = PackageBases.ID) "
|
||||||
|
"WHERE PackageBases.ID = ?", [pkgbase.ID])
|
||||||
|
|
||||||
now = int(datetime.utcnow().timestamp())
|
now = int(datetime.utcnow().timestamp())
|
||||||
conn.execute(("UPDATE PackageBases SET Popularity = ("
|
conn.execute("UPDATE PackageBases SET Popularity = ("
|
||||||
"SELECT COALESCE(SUM(POWER(0.98, (? - VoteTS) / 86400)), 0.0) "
|
"SELECT COALESCE(SUM(POWER(0.98, (? - VoteTS) / 86400)), 0.0) "
|
||||||
"FROM PackageVotes WHERE PackageVotes.PackageBaseID = "
|
"FROM PackageVotes WHERE PackageVotes.PackageBaseID = "
|
||||||
"PackageBases.ID AND NOT VoteTS IS NULL)"), [now])
|
"PackageBases.ID AND NOT VoteTS IS NULL) WHERE "
|
||||||
|
"PackageBases.ID = ?", [now, pkgbase.ID])
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
aurweb.db.session.refresh(pkgbase)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
conn = aurweb.db.Connection()
|
||||||
|
conn.execute("UPDATE PackageBases SET NumVotes = ("
|
||||||
|
"SELECT COUNT(*) FROM PackageVotes "
|
||||||
|
"WHERE PackageVotes.PackageBaseID = PackageBases.ID)")
|
||||||
|
|
||||||
|
now = int(datetime.utcnow().timestamp())
|
||||||
|
conn.execute("UPDATE PackageBases SET Popularity = ("
|
||||||
|
"SELECT COALESCE(SUM(POWER(0.98, (? - VoteTS) / 86400)), 0.0) "
|
||||||
|
"FROM PackageVotes WHERE PackageVotes.PackageBaseID = "
|
||||||
|
"PackageBases.ID AND NOT VoteTS IS NULL)", [now])
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
|
@ -1771,6 +1771,7 @@ def test_pkgbase_vote(client: TestClient, user: User, package: Package):
|
||||||
|
|
||||||
vote = pkgbase.package_votes.filter(PackageVote.UsersID == user.ID).first()
|
vote = pkgbase.package_votes.filter(PackageVote.UsersID == user.ID).first()
|
||||||
assert vote is not None
|
assert vote is not None
|
||||||
|
assert pkgbase.NumVotes == 1
|
||||||
|
|
||||||
# Remove vote.
|
# Remove vote.
|
||||||
endpoint = f"/pkgbase/{pkgbase.Name}/unvote"
|
endpoint = f"/pkgbase/{pkgbase.Name}/unvote"
|
||||||
|
@ -1780,6 +1781,7 @@ def test_pkgbase_vote(client: TestClient, user: User, package: Package):
|
||||||
|
|
||||||
vote = pkgbase.package_votes.filter(PackageVote.UsersID == user.ID).first()
|
vote = pkgbase.package_votes.filter(PackageVote.UsersID == user.ID).first()
|
||||||
assert vote is None
|
assert vote is None
|
||||||
|
assert pkgbase.NumVotes == 0
|
||||||
|
|
||||||
|
|
||||||
def test_pkgbase_disown_as_tu(client: TestClient, tu_user: User,
|
def test_pkgbase_disown_as_tu(client: TestClient, tu_user: User,
|
||||||
|
|
Loading…
Add table
Reference in a new issue