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 <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-10-01 15:47:16 -07:00
parent bb45ae7ac3
commit 0895dd07ee
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
5 changed files with 100 additions and 14 deletions

View file

@ -131,6 +131,10 @@ async def make_single_context(request: Request,
context["comments"] = pkgbase.comments.order_by( context["comments"] = pkgbase.comments.order_by(
PackageComment.CommentTS.desc() PackageComment.CommentTS.desc()
) )
context["pinned_comments"] = pkgbase.comments.filter(
PackageComment.PinnedTS != 0
).order_by(PackageComment.CommentTS.desc())
context["is_maintainer"] = (request.user.is_authenticated() context["is_maintainer"] = (request.user.is_authenticated()
and request.user.ID == pkgbase.MaintainerUID) and request.user.ID == pkgbase.MaintainerUID)
context["notified"] = request.user.notified(pkgbase) 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}", return RedirectResponse(f"/pkgbase/{name}",
status_code=int(HTTPStatus.SEE_OTHER)) 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))

View file

@ -18,7 +18,5 @@
{% set pkgname = result.Name %} {% set pkgname = result.Name %}
{% set pkgbase_id = result.ID %} {% set pkgbase_id = result.ID %}
{% if comments.count() %} {% include "partials/packages/comments.html" %}
{% include "partials/packages/comments.html" %}
{% endif %}
{% endblock %} {% endblock %}

View file

@ -49,16 +49,38 @@
<a href="/pkgbase/{{ pkgname }}/edit-comment/?comment_id={{ comment.ID }}" class="edit-comment" title="Edit comment"><img src="/images/pencil.min.svg" alt="Edit comment" width="11" height="11"></a> <a href="/pkgbase/{{ pkgname }}/edit-comment/?comment_id={{ comment.ID }}" class="edit-comment" title="Edit comment"><img src="/images/pencil.min.svg" alt="Edit comment" width="11" height="11"></a>
{% endif %} {% endif %}
{% if request.user.has_credential("CRED_COMMENT_PIN", approved=[pkgbase.Maintainer]) %} {% if request.user.has_credential("CRED_COMMENT_PIN", approved=[pkgbase.Maintainer]) %}
<form class="pin-comment-form" method="post" action="/pkgbase/{{ name }}/"> {% if comment.PinnedTS %}
<fieldset style="display:inline;"> <form class="pin-comment-form"
<input type="hidden" name="action" value="do_PinComment"/> method="post"
<input type="hidden" name="comment_id" value="{{ comment.ID }}"/> action="/pkgbase/{{ pkgbase.Name }}/comments/{{ comment.ID }}/unpin"
<input type="hidden" name="package_base" value="{{ pkgbase.ID }}"/> >
<input type="hidden" name="return_to" value="/pkgbase/{{ name }}/"/> <fieldset style="display:inline;">
<input type="image" class="pin-comment" src="/images/pin.min.svg" width="11" height="11" alt="{{ 'Pin comment' | tr }}" title="{{ 'Pin comment' | tr }}" name="submit" value="1"/> <input type="image"
</fieldset> class="pin-comment"
</form> src="/images/unpin.min.svg"
alt="{{ 'Unpin comment' | tr }}"
title="{{ 'Unpin comment' | tr }}"
name="submit"
value="1" width="11" height="11" />
</fieldset>
</form>
{% else %}
<form class="pin-comment-form"
method="post"
action="/pkgbase/{{ pkgbase.Name }}/comments/{{ comment.ID }}/pin"
>
<fieldset style="display:inline;">
<input type="image"
class="pin-comment"
src="/images/pin.min.svg"
alt="{{ 'Pin comment' | tr }}"
title="{{ 'Pin comment' | tr }}"
name="submit"
value="1" width="11" height="11" />
</fieldset>
</form>
{% endif %}
{% endif %} {% endif %}
{% else %} {% else %}
{% if request.user.has_credential("CRED_COMMENT_UNDELETE", approved=[comment.User]) %} {% if request.user.has_credential("CRED_COMMENT_UNDELETE", approved=[comment.User]) %}

View file

@ -12,7 +12,21 @@
</div> </div>
{% endif %} {% endif %}
{% if comments %} {% if pinned_comments.count() %}
<div class="comments package-comments">
<div class="comments-header">
<h3>
<span class="text">{{ "Pinned Comments" | tr }}</span>
<span class="arrow"></span>
</h3>
</div>
{% for comment in pinned_comments.all() %}
{% include "partials/packages/comment.html" %}
{% endfor %}
</div>
{% endif %}
{% if comments.count() %}
<div class="comments package-comments"> <div class="comments package-comments">
<div class="comments-header"> <div class="comments-header">
<h3> <h3>

View file

@ -1143,3 +1143,29 @@ def test_pkgbase_comment_undelete_not_found(client: TestClient,
with client as request: with client as request:
resp = request.post(endpoint, cookies=cookies) resp = request.post(endpoint, cookies=cookies)
assert resp.status_code == int(HTTPStatus.NOT_FOUND) 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)