Manage Blogposts
NotesFinally, the blog posts page lets administrators publish or unpublish any post, or delete it.
APIs#
api/admin_apis.pypython
# Blog posts
@router.get('/admin/blogposts', status_code=status.HTTP_200_OK)
def get_all_blogposts_api(session: Session = Depends(get_session_db)) -> list[BlogPostOut]:
return session.exec(select(BlogPost).order_by(BlogPost.created_date.desc())).all()
@router.put('/admin/blogposts/{id}', status_code=status.HTTP_200_OK)
def update_blogpost_published_api(id: uuid.UUID, published: bool,
session: Session = Depends(get_session_db)) -> BlogPostOut:
post = session.get(BlogPost, id)
if not post:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"No blog post found with id {id}.")
post.published = published
session.add(post)
session.commit()
session.refresh(post)
return postDeleting reuses delete_blogpost_by_id_api from the blog APIs, which also deletes the post’s comments.
Views#
You’ll need to import delete_blogpost_by_id_api from api.blog_apis:
web/views/admin_views.pypython
# Blog posts
@router.get('/admin/blogposts', response_class=HTMLResponse, status_code=status.HTTP_200_OK)
def admin_blogposts_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": "blogposts",
"user": current_user,
"blogposts": get_all_blogposts_api(session=session),
}
return templates.TemplateResponse(request, 'admin/blogposts.html', {'context': context})
@router.post('/admin/blogposts/{id}/update', response_class=HTMLResponse)
def admin_blogpost_update_view(*,
id: uuid.UUID,
request: Request,
published: bool = Form(False),
session: Session = Depends(get_session_db)):
if get_superuser(request, session) is None:
return not_allowed()
update_blogpost_published_api(id=id, published=published, session=session)
return RedirectResponse('/admin/blogposts', status_code=status.HTTP_303_SEE_OTHER)
@router.post('/admin/blogposts/{id}/delete', response_class=HTMLResponse)
def admin_blogpost_delete_view(*, id: uuid.UUID, request: Request, session: Session = Depends(get_session_db)):
if get_superuser(request, session) is None:
return not_allowed()
delete_blogpost_by_id_api(id=id, session=session)
return RedirectResponse('/admin/blogposts', status_code=status.HTTP_303_SEE_OTHER)Template#
web/templates/admin/blogposts.htmljinja
{% extends "admin/_base.html" %}
{% block content %}
<h2>Blog Posts</h2>
<hr>
<table class="table table-striped">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Date</th>
<th>Comments</th>
<th>Published</th>
<th></th>
</tr>
</thead>
<tbody>
{% for post in context.blogposts %}
<tr>
<td><a href="/blog/{{ post.id }}">{{ post.title }}</a></td>
<td>{% if post.blogpost_author %}{{ post.blogpost_author.email }}{% endif %}</td>
<td>{{ post.created_date.strftime('%d %b %Y') }}</td>
<td>{{ post.comments|length }}</td>
<td>
<form action="/admin/blogposts/{{ post.id }}/update" method="post">
<input type="checkbox" name="published" {% if post.published %}checked{% endif %}
onchange="this.form.submit()">
</form>
</td>
<td>
<form action="/admin/blogposts/{{ post.id }}/delete" method="post"
onsubmit="return confirm('Delete this post and its comments?')">
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}post.comments|length counts each post’s comments, using the relationship from the Authorisation section.