feat(fastapi): add /account/{username}/comments

This commit contains a base template of account comments
in sorted order (based on ColumnTS.desc).

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-10-28 13:01:32 -07:00
parent 9fd07c36eb
commit adb6252f85
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
3 changed files with 116 additions and 0 deletions

View file

@ -624,6 +624,22 @@ async def account(request: Request, username: str):
return render_template(request, "account/show.html", context) return render_template(request, "account/show.html", context)
@router.get("/account/{username}/comments")
@auth_required(redirect="/account/{username}/comments")
async def account_comments(request: Request, username: str):
user = db.query(models.User).filter(
models.User.Username == username
).first()
if not user:
raise HTTPException(status_code=HTTPStatus.NOT_FOUND)
context = make_context(request, "Accounts")
context["username"] = username
context["comments"] = user.package_comments.order_by(
models.PackageComment.CommentTS.desc())
return render_template(request, "account/comments.html", context)
@router.get("/accounts") @router.get("/accounts")
@auth_required(True, redirect="/accounts") @auth_required(True, redirect="/accounts")
@account_type_required({account_type.TRUSTED_USER, @account_type_required({account_type.TRUSTED_USER,

View file

@ -0,0 +1,52 @@
{% extends "partials/layout.html" %}
{% block pageContent %}
<div class="box">
<h2>{{ "Accounts" | tr }}</h2>
<div class="comments">
<div class="comments-header">
<h3>
{{
"Comments for %s%s%s" | tr
| format('<a href="/account/%s">' | format(username),
username,
"</a>")
| safe
}}
</h3>
</div>
{% for comment in comments %}
{% set commented_at = comment.CommentTS | dt | as_timezone(timezone) %}
<h4 id="comment-{{ comment.ID }}" class="comment-header">
{{
"Commented on package %s%s%s on %s%s%s" | tr
| format(
'<a href="/pkgbase/{{ comment.PackageBase.Name }}">',
comment.PackageBase.Name,
"</a>",
'<a href="/account/%s/comments#comment-%s">' | format(
username,
comment.ID
),
commented_at.strftime("%Y-%m-%d %H:%M"),
"</a>"
) | safe
}}
</h4>
<div id="comment-{{ comment.ID }}-content" class="article-content">
<div>
{% if comment.RenderedComment %}
{{ comment.RenderedComment | safe }}
{% else %}
{{ comment.Comments }}
{% endif %}
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}

View file

@ -2523,3 +2523,51 @@ def test_pkgbase_merge_post(client: TestClient, tu_user: User,
PackageRequest.MergeBaseName == target.PackageBase.Name) PackageRequest.MergeBaseName == target.PackageBase.Name)
).first() ).first()
assert pkgreq is not None assert pkgreq is not None
def test_account_comments_unauthorized(client: TestClient, user: User):
""" This test may seem out of place, but it requires packages,
so its being included in the packages routes test suite to
leverage existing fixtures. """
endpoint = f"/account/{user.Username}/comments"
with client as request:
resp = request.get(endpoint, allow_redirects=False)
assert resp.status_code == int(HTTPStatus.SEE_OTHER)
assert resp.headers.get("location").startswith("/login")
def test_account_comments(client: TestClient, user: User, package: Package):
""" This test may seem out of place, but it requires packages,
so its being included in the packages routes test suite to
leverage existing fixtures. """
now = (datetime.utcnow().timestamp())
with db.begin():
# This comment's CommentTS is `now + 1`, so it is found in rendered
# HTML before the rendered_comment, which has a CommentTS of `now`.
comment = db.create(PackageComment,
PackageBase=package.PackageBase,
User=user, Comments="Test comment",
CommentTS=now + 1)
rendered_comment = db.create(PackageComment,
PackageBase=package.PackageBase,
User=user, Comments="Test comment",
RenderedComment="<p>Test comment</p>",
CommentTS=now)
cookies = {"AURSID": user.login(Request(), "testPassword")}
endpoint = f"/account/{user.Username}/comments"
with client as request:
resp = request.get(endpoint, cookies=cookies)
assert resp.status_code == int(HTTPStatus.OK)
root = parse_root(resp.text)
comments = root.xpath('//div[@class="article-content"]/div')
# Assert that we got Comments rendered from the first comment.
assert comments[0].text.strip() == comment.Comments
# And from the second, we have rendered content.
rendered = comments[1].xpath('./p')
expected = rendered_comment.RenderedComment.replace(
"<p>", "").replace("</p>", "")
assert rendered[0].text.strip() == expected