From 0895dd07ee621d24007a30b4f3d076d7b55c23f0 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Fri, 1 Oct 2021 15:47:16 -0700 Subject: [PATCH] feat(FastAPI): add /pkgbase/{name}/comments/{id}/pin (post) In addition, fix up some templates to display pinned comments, and include the unpin form input for pinned comments, which is not yet implemented. Signed-off-by: Kevin Morris --- aurweb/routers/packages.py | 26 ++++++++++++++ templates/packages/show.html | 4 +-- templates/partials/packages/comment.html | 42 +++++++++++++++++------ templates/partials/packages/comments.html | 16 ++++++++- test/test_packages_routes.py | 26 ++++++++++++++ 5 files changed, 100 insertions(+), 14 deletions(-) diff --git a/aurweb/routers/packages.py b/aurweb/routers/packages.py index 92fc9361..681cde4f 100644 --- a/aurweb/routers/packages.py +++ b/aurweb/routers/packages.py @@ -131,6 +131,10 @@ async def make_single_context(request: Request, context["comments"] = pkgbase.comments.order_by( PackageComment.CommentTS.desc() ) + context["pinned_comments"] = pkgbase.comments.filter( + PackageComment.PinnedTS != 0 + ).order_by(PackageComment.CommentTS.desc()) + context["is_maintainer"] = (request.user.is_authenticated() and request.user.ID == pkgbase.MaintainerUID) context["notified"] = request.user.notified(pkgbase) @@ -343,3 +347,25 @@ async def pkgbase_comment_undelete(request: Request, name: str, id: int): return RedirectResponse(f"/pkgbase/{name}", status_code=int(HTTPStatus.SEE_OTHER)) + + +@router.post("/pkgbase/{name}/comments/{id}/pin") +@auth_required(True) +async def pkgbase_comment_pin(request: Request, name: str, id: int): + pkgbase = get_pkg_or_base(name, PackageBase) + comment = get_pkgbase_comment(pkgbase, id) + + has_cred = request.user.has_credential("CRED_COMMENT_PIN", + approved=[pkgbase.Maintainer]) + if not has_cred: + _ = l10n.get_translator_for_request(request) + raise HTTPException( + status_code=int(HTTPStatus.UNAUTHORIZED), + detail=_("You are not allowed to pin this comment.")) + + now = int(datetime.utcnow().timestamp()) + with db.begin(): + comment.PinnedTS = now + + return RedirectResponse(f"/pkgbase/{name}", + status_code=int(HTTPStatus.SEE_OTHER)) diff --git a/templates/packages/show.html b/templates/packages/show.html index 0bf8d9fd..ba531fc8 100644 --- a/templates/packages/show.html +++ b/templates/packages/show.html @@ -18,7 +18,5 @@ {% set pkgname = result.Name %} {% set pkgbase_id = result.ID %} - {% if comments.count() %} - {% include "partials/packages/comments.html" %} - {% endif %} + {% include "partials/packages/comments.html" %} {% endblock %} diff --git a/templates/partials/packages/comment.html b/templates/partials/packages/comment.html index 6af5cd9e..97f11723 100644 --- a/templates/partials/packages/comment.html +++ b/templates/partials/packages/comment.html @@ -49,16 +49,38 @@ Edit comment {% endif %} - {% if request.user.has_credential("CRED_COMMENT_PIN", approved=[pkgbase.Maintainer]) %} -
-
- - - - - -
-
+ {% if request.user.has_credential("CRED_COMMENT_PIN", approved=[pkgbase.Maintainer]) %} + {% if comment.PinnedTS %} +
+
+ +
+
+ {% else %} +
+
+ +
+
+ {% endif %} {% endif %} {% else %} {% if request.user.has_credential("CRED_COMMENT_UNDELETE", approved=[comment.User]) %} diff --git a/templates/partials/packages/comments.html b/templates/partials/packages/comments.html index 7c8a32e5..56b5ab03 100644 --- a/templates/partials/packages/comments.html +++ b/templates/partials/packages/comments.html @@ -12,7 +12,21 @@ {% endif %} -{% if comments %} +{% if pinned_comments.count() %} +
+
+

+ {{ "Pinned Comments" | tr }} + +

+
+ {% for comment in pinned_comments.all() %} + {% include "partials/packages/comment.html" %} + {% endfor %} +
+{% endif %} + +{% if comments.count() %}

diff --git a/test/test_packages_routes.py b/test/test_packages_routes.py index 47b5ed81..6bf4b975 100644 --- a/test/test_packages_routes.py +++ b/test/test_packages_routes.py @@ -1143,3 +1143,29 @@ def test_pkgbase_comment_undelete_not_found(client: TestClient, with client as request: resp = request.post(endpoint, cookies=cookies) assert resp.status_code == int(HTTPStatus.NOT_FOUND) + + +def test_pkgbase_comment_pin(client: TestClient, + maintainer: User, + package: Package, + comment: PackageComment): + cookies = {"AURSID": maintainer.login(Request(), "testPassword")} + comment_id = comment.ID + pkgbasename = package.PackageBase.Name + endpoint = f"/pkgbase/{pkgbasename}/comments/{comment_id}/pin" + with client as request: + resp = request.post(endpoint, cookies=cookies) + assert resp.status_code == int(HTTPStatus.SEE_OTHER) + + +def test_pkgbase_comment_pin_unauthorized(client: TestClient, + user: User, + package: Package, + comment: PackageComment): + cookies = {"AURSID": user.login(Request(), "testPassword")} + comment_id = comment.ID + pkgbasename = package.PackageBase.Name + endpoint = f"/pkgbase/{pkgbasename}/comments/{comment_id}/pin" + with client as request: + resp = request.post(endpoint, cookies=cookies) + assert resp.status_code == int(HTTPStatus.UNAUTHORIZED)