2020-09-03 21:29:28 +02:00
|
|
|
from typing import Union
|
|
|
|
|
2020-08-18 18:49:41 +02:00
|
|
|
from django.contrib import messages
|
2020-08-16 18:14:55 +02:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2020-08-14 22:06:38 +02:00
|
|
|
from django.db.models import F
|
2020-08-14 15:53:42 +02:00
|
|
|
from django.views import generic
|
2020-08-18 19:44:13 +02:00
|
|
|
from django.views.generic.edit import FormMixin
|
2020-08-14 14:53:30 +02:00
|
|
|
|
2020-08-18 18:49:41 +02:00
|
|
|
from articles.forms import CommentForm
|
2020-09-03 21:29:28 +02:00
|
|
|
from articles.models import Article, Comment, Page
|
2020-08-14 15:53:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ArticlesListView(generic.ListView):
|
|
|
|
model = Article
|
|
|
|
paginate_by = 15
|
|
|
|
context_object_name = "articles"
|
2020-09-03 22:00:28 +02:00
|
|
|
queryset = Article.without_pages.filter(status=Article.PUBLISHED)
|
2020-08-14 15:53:42 +02:00
|
|
|
|
2020-08-16 18:14:55 +02:00
|
|
|
def get_context_data(self, *, object_list=None, **kwargs):
|
|
|
|
context = super().get_context_data(object_list=object_list, **kwargs)
|
|
|
|
context["title"] = "Articles"
|
|
|
|
return context
|
|
|
|
|
2020-08-14 15:53:42 +02:00
|
|
|
|
2020-08-16 18:14:55 +02:00
|
|
|
class DraftsListView(generic.ListView, LoginRequiredMixin):
|
|
|
|
model = Article
|
|
|
|
paginate_by = 15
|
|
|
|
context_object_name = "articles"
|
2020-09-03 22:00:28 +02:00
|
|
|
queryset = Article.objects.filter(status=Article.DRAFT)
|
2020-08-16 18:14:55 +02:00
|
|
|
|
|
|
|
def get_context_data(self, *, object_list=None, **kwargs):
|
|
|
|
context = super().get_context_data(object_list=object_list, **kwargs)
|
|
|
|
context["title"] = "Drafts"
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2020-08-18 19:44:13 +02:00
|
|
|
class ArticleDetailView(FormMixin, generic.DetailView):
|
2020-08-14 15:53:42 +02:00
|
|
|
model = Article
|
2020-08-18 19:44:13 +02:00
|
|
|
form_class = CommentForm
|
2020-08-14 15:53:42 +02:00
|
|
|
context_object_name = "article"
|
2020-08-17 13:17:23 +02:00
|
|
|
template_name = "articles/article_detail.html"
|
2020-08-14 16:19:02 +02:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-08-18 18:49:41 +02:00
|
|
|
queryset = super().get_queryset()
|
2020-08-16 18:14:55 +02:00
|
|
|
if self.request.user.is_authenticated:
|
2020-08-18 18:49:41 +02:00
|
|
|
return queryset
|
|
|
|
return queryset.filter(status=Article.PUBLISHED)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2020-08-18 19:44:13 +02:00
|
|
|
context = super().get_context_data(**kwargs)
|
2020-08-18 18:49:41 +02:00
|
|
|
article = self.object
|
|
|
|
if hasattr(article, "article"):
|
|
|
|
article = article.article
|
2020-08-18 21:40:54 +02:00
|
|
|
context["comments"] = article.comments.filter(status=Comment.APPROVED)
|
2020-08-18 18:49:41 +02:00
|
|
|
return context
|
2020-08-14 22:06:38 +02:00
|
|
|
|
2020-09-03 21:29:28 +02:00
|
|
|
def get_object(self, queryset=None) -> Union[Article, Page]:
|
|
|
|
obj = super().get_object(queryset) # type: Article
|
2020-08-17 13:17:23 +02:00
|
|
|
if hasattr(obj, "page"):
|
2020-09-03 21:29:28 +02:00
|
|
|
obj = obj.page # type: Page
|
2020-08-14 22:06:38 +02:00
|
|
|
if not self.request.user.is_authenticated:
|
|
|
|
obj.views_count = F("views_count") + 1
|
|
|
|
obj.save(update_fields=["views_count"])
|
|
|
|
|
|
|
|
return obj
|
2020-08-18 18:49:41 +02:00
|
|
|
|
2020-08-18 19:44:13 +02:00
|
|
|
def post(self, request, *args, **kwargs):
|
2020-09-03 21:29:28 +02:00
|
|
|
self.object = self.get_object() # type: Union[Article, Page]
|
2020-08-18 19:44:13 +02:00
|
|
|
form = self.get_form()
|
2020-09-03 21:29:28 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-08-18 19:44:13 +02:00
|
|
|
if form.is_valid():
|
|
|
|
return self.form_valid(form)
|
|
|
|
else:
|
|
|
|
return self.form_invalid(form)
|
2020-08-18 18:49:41 +02:00
|
|
|
|
2020-08-18 19:44:13 +02:00
|
|
|
def form_invalid(self, form):
|
2020-08-18 19:51:54 +02:00
|
|
|
messages.error(
|
2020-08-18 20:44:51 +02:00
|
|
|
self.request,
|
|
|
|
'Your comment couldn\'t be saved, see <a href="#comment-form">the form below</a>.',
|
2020-08-18 19:51:54 +02:00
|
|
|
)
|
2020-08-18 19:44:13 +02:00
|
|
|
return super().form_invalid(form)
|
2020-08-18 18:49:41 +02:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
2020-08-18 19:44:13 +02:00
|
|
|
comment = form.save(commit=False)
|
|
|
|
comment.article = self.object
|
|
|
|
comment.save()
|
2020-08-18 20:44:51 +02:00
|
|
|
messages.success(
|
2020-08-19 12:13:44 +02:00
|
|
|
self.request, "Comment successfully saved, pending review.",
|
2020-08-18 20:44:51 +02:00
|
|
|
)
|
2020-08-18 19:44:13 +02:00
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return self.object.get_absolute_url()
|