feat(FastAPI): add /pkgbase/{name}/voters (get)

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-09-22 18:33:58 -07:00
parent 836af2d588
commit fbd91f346a
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
3 changed files with 54 additions and 0 deletions

View file

@ -195,3 +195,12 @@ async def package_base(request: Request, name: str) -> Response:
context["packages"] = pkgbase.packages.all()
return render_template(request, "pkgbase.html", context)
@router.get("/pkgbase/{name}/voters")
async def package_base_voters(request: Request, name: str) -> Response:
# Get the PackageBase.
pkgbase = get_pkgbase(name)
context = make_context(request, "Voters")
context["pkgbase"] = pkgbase
return render_template(request, "pkgbase/voters.html", context)

View file

@ -0,0 +1,27 @@
{% extends "partials/layout.html" %}
{% block pageContent %}
<div class="box">
<h2>
{{ "Votes" | tr }} for
<a href="/pkgbase/{{ pkgbase.Name }}">
{{ pkgbase.Name }}
</a>
</h2>
<div class="boxbody">
<ul>
{% for pkg_vote in pkgbase.package_votes %}
<li>
<a href="/account/{{ pkg_vote.User.Username }}">
{{ pkg_vote.User.Username }}
</a>
{% set voted_at = pkg_vote.VoteTS | dt | as_timezone(timezone) %}
({{ voted_at.strftime("%Y-%m-%d %H:%M") }})
</li>
{% endfor %}
</ul>
</div>
</div>
{% endblock %}

View file

@ -912,3 +912,21 @@ def test_packages_per_page(client: TestClient, maintainer: User):
root = parse_root(response.text)
rows = root.xpath('//table[@class="results"]/tbody/tr')
assert len(rows) == 250
def test_pkgbase_voters(client: TestClient, maintainer: User, package: Package):
pkgbase = package.PackageBase
endpoint = f"/pkgbase/{pkgbase.Name}/voters"
now = int(datetime.utcnow().timestamp())
with db.begin():
db.create(PackageVote, User=maintainer, PackageBase=pkgbase,
VoteTS=now)
with client as request:
resp = request.get(endpoint)
assert resp.status_code == int(HTTPStatus.OK)
root = parse_root(resp.text)
rows = root.xpath('//div[@class="box"]//ul/li')
assert len(rows) == 1