Allow disabling comments on article/page
This commit is contained in:
parent
63db5aa0ea
commit
b180def20c
6 changed files with 41 additions and 9 deletions
|
@ -29,8 +29,9 @@ class ArticleAdmin(admin.ModelAdmin):
|
||||||
{
|
{
|
||||||
"fields": (
|
"fields": (
|
||||||
("title", "slug"),
|
("title", "slug"),
|
||||||
("author", "status"),
|
("author", "comments_allowed"),
|
||||||
("published_at", "created_at", "updated_at"),
|
("status", "published_at"),
|
||||||
|
("created_at", "updated_at"),
|
||||||
"views_count",
|
"views_count",
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
18
articles/migrations/0019_article_comments_allowed.py
Normal file
18
articles/migrations/0019_article_comments_allowed.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 3.1 on 2020-09-03 19:16
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("articles", "0018_auto_20200821_1232"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="article",
|
||||||
|
name="comments_allowed",
|
||||||
|
field=models.BooleanField(default=True),
|
||||||
|
),
|
||||||
|
]
|
|
@ -48,6 +48,7 @@ class Article(AdminUrlMixin, models.Model):
|
||||||
author = models.ForeignKey(User, on_delete=models.PROTECT, default=1)
|
author = models.ForeignKey(User, on_delete=models.PROTECT, default=1)
|
||||||
views_count = models.IntegerField(default=0)
|
views_count = models.IntegerField(default=0)
|
||||||
slug = models.SlugField(unique=True, max_length=255)
|
slug = models.SlugField(unique=True, max_length=255)
|
||||||
|
comments_allowed = models.BooleanField(default=True)
|
||||||
|
|
||||||
objects = ArticleManager()
|
objects = ArticleManager()
|
||||||
with_pages = models.Manager()
|
with_pages = models.Manager()
|
||||||
|
|
|
@ -11,5 +11,7 @@
|
||||||
{{ article.get_formatted_content|safe }}
|
{{ article.get_formatted_content|safe }}
|
||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
|
{% if article.comments_allowed %}
|
||||||
{% include 'articles/comment_snippet.html' %}
|
{% include 'articles/comment_snippet.html' %}
|
||||||
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -15,7 +15,9 @@
|
||||||
</p>
|
</p>
|
||||||
</article>
|
</article>
|
||||||
{% empty %}
|
{% empty %}
|
||||||
|
<p>
|
||||||
No reaction yet, write your own!
|
No reaction yet, write your own!
|
||||||
|
</p>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<form id="comment-form" action="{% url 'create-comment' slug=article.slug %}" method="post">
|
<form id="comment-form" action="{% url 'create-comment' slug=article.slug %}" method="post">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||||
from django.db.models import F
|
from django.db.models import F
|
||||||
|
@ -5,7 +7,7 @@ from django.views import generic
|
||||||
from django.views.generic.edit import FormMixin
|
from django.views.generic.edit import FormMixin
|
||||||
|
|
||||||
from articles.forms import CommentForm
|
from articles.forms import CommentForm
|
||||||
from articles.models import Article, Comment
|
from articles.models import Article, Comment, Page
|
||||||
|
|
||||||
|
|
||||||
class ArticlesListView(generic.ListView):
|
class ArticlesListView(generic.ListView):
|
||||||
|
@ -55,10 +57,10 @@ class ArticleDetailView(FormMixin, generic.DetailView):
|
||||||
context["comments"] = article.comments.filter(status=Comment.APPROVED)
|
context["comments"] = article.comments.filter(status=Comment.APPROVED)
|
||||||
return context
|
return context
|
||||||
|
|
||||||
def get_object(self, queryset=None):
|
def get_object(self, queryset=None) -> Union[Article, Page]:
|
||||||
obj = super().get_object(queryset)
|
obj = super().get_object(queryset) # type: Article
|
||||||
if hasattr(obj, "page"):
|
if hasattr(obj, "page"):
|
||||||
obj = obj.page
|
obj = obj.page # type: Page
|
||||||
if not self.request.user.is_authenticated:
|
if not self.request.user.is_authenticated:
|
||||||
obj.views_count = F("views_count") + 1
|
obj.views_count = F("views_count") + 1
|
||||||
obj.save(update_fields=["views_count"])
|
obj.save(update_fields=["views_count"])
|
||||||
|
@ -66,8 +68,14 @@ class ArticleDetailView(FormMixin, generic.DetailView):
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
self.object = self.get_object()
|
self.object = self.get_object() # type: Union[Article, Page]
|
||||||
form = self.get_form()
|
form = self.get_form()
|
||||||
|
|
||||||
|
if not self.object.comments_allowed:
|
||||||
|
messages.error(self.request, "Comments are disabled on this article.")
|
||||||
|
# Bypassing self.form_invalid because we don't want its error message
|
||||||
|
return super().form_invalid(form)
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
return self.form_valid(form)
|
return self.form_valid(form)
|
||||||
else:
|
else:
|
||||||
|
|
Reference in a new issue