diff options
| author | Bobby <[email protected]> | 2022-09-20 02:10:57 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-09-20 02:10:57 -0400 |
| commit | 794c197e0bd07209e90ad1d2f5c6fbdc65d2d2a7 (patch) | |
| tree | 435d8e53fb85c5bf31d75096cb4d9b5610468be7 | |
| parent | c5254b6f39939bc0bc6719b854b67928af74c9ea (diff) | |
| download | thatcomputerscientist-794c197e0bd07209e90ad1d2f5c6fbdc65d2d2a7.tar.xz thatcomputerscientist-794c197e0bd07209e90ad1d2f5c6fbdc65d2d2a7.zip | |
Function to publish and unpublish posts
| -rw-r--r-- | blog_admin/urls.py | 2 | ||||
| -rw-r--r-- | blog_admin/views.py | 20 | ||||
| -rw-r--r-- | templates/blog/post.html | 2 | ||||
| -rw-r--r-- | templates/blog_admin/posts.html | 4 |
4 files changed, 25 insertions, 3 deletions
diff --git a/blog_admin/urls.py b/blog_admin/urls.py index a8f6bc49..670d2a43 100644 --- a/blog_admin/urls.py +++ b/blog_admin/urls.py @@ -11,6 +11,8 @@ urlpatterns = [ path('posts/new', views.new_post, name='new-post'), path('posts/search', views.posts_search, name='posts-search'), path('posts/<str:slug>/edit', views.edit_post, name='edit-post'), + path('posts/<str:slug>/publish', views.publish_post, name='publish-post'), + path('posts/<str:slug>/unpublish', views.unpublish_post, name='unpublish-post'), path('comments', views.comments, name='comments'), path('categories', views.categories, name='categories'), path('categories/new', views.new_category, name='new-category'), diff --git a/blog_admin/views.py b/blog_admin/views.py index ca107390..8f63be54 100644 --- a/blog_admin/views.py +++ b/blog_admin/views.py @@ -104,6 +104,26 @@ def edit_post(request, slug): else: return redirect('blog:home') +def publish_post(request, slug): + if request.user.is_authenticated and (request.user.is_superuser or request.user.is_staff): + post = Post.objects.get(slug = slug) + post.is_public = True + post.save() + messages.success(request, 'Post published successfully!') + return redirect('blog-admin:posts') + else: + return redirect('blog:home') + +def unpublish_post(request, slug): + if request.user.is_authenticated and (request.user.is_superuser or request.user.is_staff): + post = Post.objects.get(slug = slug) + post.is_public = False + post.save() + messages.success(request, 'Post unpublished successfully!') + return redirect('blog-admin:posts') + else: + return redirect('blog:home') + def comments(request): pass diff --git a/templates/blog/post.html b/templates/blog/post.html index 948ebb95..9116daeb 100644 --- a/templates/blog/post.html +++ b/templates/blog/post.html @@ -1,7 +1,7 @@ {% extends 'blog/partials/base.html' %} {% block content %} <div class="main"> <article> - <h1 style="margin-bottom: 12px">{{ post.title }}</h1> + <h1 style="margin-bottom: 12px; font-size: 2rem;">{{ post.title }}</h1> <p style="line-height: 1.6em;"> Posted on <em><u>{{ post.date | date:"M d, Y" }}</u></em> by <em><a href="#">{{ post.author }}</a> in <a href="#">{{ post.category }}</a></em> <br> diff --git a/templates/blog_admin/posts.html b/templates/blog_admin/posts.html index 5d56f0c5..67d6bd37 100644 --- a/templates/blog_admin/posts.html +++ b/templates/blog_admin/posts.html @@ -28,9 +28,9 @@ <a href="{% url 'blog-admin:edit-post' post.slug %}">Edit</a> <a href="#" class="error">Delete</a> {% if post.is_public %} - <a href="#" class="error">UnPublish</a> + <a href="{% url 'blog-admin:unpublish-post' post.slug %}" class="error">Unpublish</a> {% else %} - <a href="#" class="success">Publish</a> + <a href="{% url 'blog-admin:publish-post' post.slug %}" class="success">Publish</a> {% endif %} </td> </tr> |
