Manage Comments
NotesNew comments aren’t shown until they’re approved, so this is where they get approved, or deleted.
APIs#
api/admin_apis.pypython
# Comments
@router.get('/admin/comments', status_code=status.HTTP_200_OK)
def get_all_comments_api(session: Session = Depends(get_session_db)) -> list[CommentOut]:
return session.exec(select(Comment).order_by(Comment.created_date.desc())).all()
@router.put('/admin/comments/{id}', status_code=status.HTTP_200_OK)
def update_comment_approval_api(id: uuid.UUID, approved: bool, session: Session = Depends(get_session_db)) -> CommentOut:
comment = session.get(Comment, id)
if not comment:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"No comment found with id {id}.")
comment.approved = approved
session.add(comment)
session.commit()
session.refresh(comment)
return comment
@router.delete('/admin/comments/{id}', status_code=status.HTTP_204_NO_CONTENT)
def delete_any_comment_api(id: uuid.UUID, session: Session = Depends(get_session_db)) -> None:
comment = session.get(Comment, id)
if not comment:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"No comment found with id {id}.")
session.delete(comment)
session.commit()delete_any_comment_api deletes any comment. The blog’s delete_comment_by_id_api only deletes a comment for its author, which is right for the website but not for administrators.
Views#
web/views/admin_views.pypython
# Comments
@router.get('/admin/comments', response_class=HTMLResponse, status_code=status.HTTP_200_OK)
def admin_comments_view(*, request: Request, session: Session = Depends(get_session_db)):
current_user = get_superuser(request, session)
if current_user is None:
return not_allowed()
context = {
"page": "comments",
"user": current_user,
"comments": get_all_comments_api(session=session),
}
return templates.TemplateResponse(request, 'admin/comments.html', {'context': context})
@router.post('/admin/comments/{id}/update', response_class=HTMLResponse)
def admin_comment_update_view(*,
id: uuid.UUID,
request: Request,
approved: bool = Form(False),
session: Session = Depends(get_session_db)):
if get_superuser(request, session) is None:
return not_allowed()
update_comment_approval_api(id=id, approved=approved, session=session)
return RedirectResponse('/admin/comments', status_code=status.HTTP_303_SEE_OTHER)
@router.post('/admin/comments/{id}/delete', response_class=HTMLResponse)
def admin_comment_delete_view(*, id: uuid.UUID, request: Request, session: Session = Depends(get_session_db)):
if get_superuser(request, session) is None:
return not_allowed()
delete_any_comment_api(id=id, session=session)
return RedirectResponse('/admin/comments', status_code=status.HTTP_303_SEE_OTHER)Template#
web/templates/admin/comments.htmljinja
{% extends "admin/_base.html" %}
{% block content %}
<h2>Comments</h2>
<hr>
<table class="table table-striped">
<thead>
<tr>
<th>Comment</th>
<th>By</th>
<th>On</th>
<th>Date</th>
<th>Approved</th>
<th></th>
</tr>
</thead>
<tbody>
{% for comment in context.comments %}
<tr>
<td>{{ comment.comment }}</td>
<td>{% if comment.comment_author %}{{ comment.comment_author.email }}{% endif %}</td>
<td><a href="/blog/{{ comment.blogpost_id }}">{{ comment.blogpost.title }}</a></td>
<td>{{ comment.created_date.strftime('%d %b %Y') }}</td>
<td>
<form action="/admin/comments/{{ comment.id }}/update" method="post">
<input type="checkbox" name="approved" {% if comment.approved %}checked{% endif %}
onchange="this.form.submit()">
</form>
</td>
<td>
<form action="/admin/comments/{{ comment.id }}/delete" method="post" onsubmit="return confirm('Delete this comment?')">
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</td>
</tr>
{% else %}
<tr><td colspan="6">No comments yet.</td></tr>
{% endfor %}
</tbody>
</table>
{% endblock %}The approval checkbox is its own small form, and onchange="this.form.submit()" submits it as soon as the box is ticked or unticked, so there’s no separate button.