mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
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:
parent
bb45ae7ac3
commit
0895dd07ee
5 changed files with 100 additions and 14 deletions
|
@ -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))
|
||||
|
|
|
@ -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 %}
|
||||
|
|
|
@ -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>
|
||||
{% endif %}
|
||||
|
||||
{% if request.user.has_credential("CRED_COMMENT_PIN", approved=[pkgbase.Maintainer]) %}
|
||||
<form class="pin-comment-form" method="post" action="/pkgbase/{{ name }}/">
|
||||
<fieldset style="display:inline;">
|
||||
<input type="hidden" name="action" value="do_PinComment"/>
|
||||
<input type="hidden" name="comment_id" value="{{ comment.ID }}"/>
|
||||
<input type="hidden" name="package_base" value="{{ pkgbase.ID }}"/>
|
||||
<input type="hidden" name="return_to" value="/pkgbase/{{ name }}/"/>
|
||||
<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"/>
|
||||
</fieldset>
|
||||
</form>
|
||||
{% if request.user.has_credential("CRED_COMMENT_PIN", approved=[pkgbase.Maintainer]) %}
|
||||
{% if comment.PinnedTS %}
|
||||
<form class="pin-comment-form"
|
||||
method="post"
|
||||
action="/pkgbase/{{ pkgbase.Name }}/comments/{{ comment.ID }}/unpin"
|
||||
>
|
||||
<fieldset style="display:inline;">
|
||||
<input type="image"
|
||||
class="pin-comment"
|
||||
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 %}
|
||||
{% else %}
|
||||
{% if request.user.has_credential("CRED_COMMENT_UNDELETE", approved=[comment.User]) %}
|
||||
|
|
|
@ -12,7 +12,21 @@
|
|||
</div>
|
||||
{% 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-header">
|
||||
<h3>
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue