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-08-14 15:53:42 +02:00
|
|
|
from articles.models import Article
|
|
|
|
|
|
|
|
|
|
|
|
class ArticlesListView(generic.ListView):
|
|
|
|
model = Article
|
|
|
|
paginate_by = 15
|
|
|
|
context_object_name = "articles"
|
|
|
|
|
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 16:19:02 +02:00
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().filter(status=Article.PUBLISHED)
|
|
|
|
|
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-08-17 09:57:24 +02:00
|
|
|
queryset = Article.with_pages.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 09:57:24 +02:00
|
|
|
queryset = Article.with_pages.all()
|
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
|
|
|
|
context["comments"] = article.comments.filter(approved=True)
|
|
|
|
return context
|
2020-08-14 22:06:38 +02:00
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
obj = super().get_object(queryset)
|
2020-08-17 13:17:23 +02:00
|
|
|
if hasattr(obj, "page"):
|
|
|
|
obj = obj.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):
|
|
|
|
self.object = self.get_object()
|
|
|
|
form = self.get_form()
|
|
|
|
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):
|
|
|
|
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 18:49:41 +02:00
|
|
|
messages.success(self.request, "Comment successfully saved.")
|
2020-08-18 19:44:13 +02:00
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return self.object.get_absolute_url()
|