Allow disabling comments on article/page

This commit is contained in:
Gabriel Augendre 2020-09-03 21:29:28 +02:00
parent 63db5aa0ea
commit b180def20c
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
6 changed files with 41 additions and 9 deletions

View file

@ -29,8 +29,9 @@ class ArticleAdmin(admin.ModelAdmin):
{
"fields": (
("title", "slug"),
("author", "status"),
("published_at", "created_at", "updated_at"),
("author", "comments_allowed"),
("status", "published_at"),
("created_at", "updated_at"),
"views_count",
)
},

View 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),
),
]

View file

@ -48,6 +48,7 @@ class Article(AdminUrlMixin, models.Model):
author = models.ForeignKey(User, on_delete=models.PROTECT, default=1)
views_count = models.IntegerField(default=0)
slug = models.SlugField(unique=True, max_length=255)
comments_allowed = models.BooleanField(default=True)
objects = ArticleManager()
with_pages = models.Manager()

View file

@ -11,5 +11,7 @@
{{ article.get_formatted_content|safe }}
</div>
</article>
{% if article.comments_allowed %}
{% include 'articles/comment_snippet.html' %}
{% endif %}
{% endblock %}

View file

@ -15,7 +15,9 @@
</p>
</article>
{% empty %}
<p>
No reaction yet, write your own!
</p>
{% endfor %}
<form id="comment-form" action="{% url 'create-comment' slug=article.slug %}" method="post">
{% csrf_token %}

View file

@ -1,3 +1,5 @@
from typing import Union
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import F
@ -5,7 +7,7 @@ from django.views import generic
from django.views.generic.edit import FormMixin
from articles.forms import CommentForm
from articles.models import Article, Comment
from articles.models import Article, Comment, Page
class ArticlesListView(generic.ListView):
@ -55,10 +57,10 @@ class ArticleDetailView(FormMixin, generic.DetailView):
context["comments"] = article.comments.filter(status=Comment.APPROVED)
return context
def get_object(self, queryset=None):
obj = super().get_object(queryset)
def get_object(self, queryset=None) -> Union[Article, Page]:
obj = super().get_object(queryset) # type: Article
if hasattr(obj, "page"):
obj = obj.page
obj = obj.page # type: Page
if not self.request.user.is_authenticated:
obj.views_count = F("views_count") + 1
obj.save(update_fields=["views_count"])
@ -66,8 +68,14 @@ class ArticleDetailView(FormMixin, generic.DetailView):
return obj
def post(self, request, *args, **kwargs):
self.object = self.get_object()
self.object = self.get_object() # type: Union[Article, Page]
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():
return self.form_valid(form)
else: