From adb6252f85ca7c2150a5e5bae5105ad323f754d0 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Thu, 28 Oct 2021 13:01:32 -0700 Subject: [PATCH] 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 --- aurweb/routers/accounts.py | 16 ++++++++++ templates/account/comments.html | 52 +++++++++++++++++++++++++++++++++ test/test_packages_routes.py | 48 ++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 templates/account/comments.html diff --git a/aurweb/routers/accounts.py b/aurweb/routers/accounts.py index b3203356..152b0a15 100644 --- a/aurweb/routers/accounts.py +++ b/aurweb/routers/accounts.py @@ -624,6 +624,22 @@ async def account(request: Request, username: str): 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") @auth_required(True, redirect="/accounts") @account_type_required({account_type.TRUSTED_USER, diff --git a/templates/account/comments.html b/templates/account/comments.html new file mode 100644 index 00000000..a1012b1b --- /dev/null +++ b/templates/account/comments.html @@ -0,0 +1,52 @@ +{% extends "partials/layout.html" %} + +{% block pageContent %} +
+

{{ "Accounts" | tr }}

+ +
+
+

+ {{ + "Comments for %s%s%s" | tr + | format('' | format(username), + username, + "") + | safe + }} +

+
+ + {% for comment in comments %} + {% set commented_at = comment.CommentTS | dt | as_timezone(timezone) %} +

+ {{ + "Commented on package %s%s%s on %s%s%s" | tr + | format( + '', + comment.PackageBase.Name, + "", + '' | format( + username, + comment.ID + ), + commented_at.strftime("%Y-%m-%d %H:%M"), + "" + ) | safe + }} +

+
+
+ {% if comment.RenderedComment %} + {{ comment.RenderedComment | safe }} + {% else %} + {{ comment.Comments }} + {% endif %} +
+
+ {% endfor %} + +
+ +
+{% endblock %} diff --git a/test/test_packages_routes.py b/test/test_packages_routes.py index 7e67775a..063c8f11 100644 --- a/test/test_packages_routes.py +++ b/test/test_packages_routes.py @@ -2523,3 +2523,51 @@ def test_pkgbase_merge_post(client: TestClient, tu_user: User, PackageRequest.MergeBaseName == target.PackageBase.Name) ).first() 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="

Test comment

", + 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( + "

", "").replace("

", "") + assert rendered[0].text.strip() == expected