This repository has been archived on 2023-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
python-blog/articles/views/html.py

80 lines
2.5 KiB
Python
Raw Normal View History

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
from django.db.models import F
2020-08-18 18:49:41 +02:00
from django.http import HttpResponseRedirect
2020-08-14 15:53:42 +02:00
from django.views import generic
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-14 15:53:42 +02:00
class ArticleDetailView(generic.DetailView):
model = Article
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):
context = super(ArticleDetailView, self).get_context_data(**kwargs)
article = self.object
if hasattr(article, "article"):
article = article.article
context["comments"] = article.comments.filter(approved=True)
context["comment_form"] = CommentForm()
return context
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
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
class CreateCommentView(generic.CreateView):
model = Article
form_class = CommentForm
def form_valid(self, form):
self.object = self.get_object()
self.comment = form.save(commit=False)
self.comment.article = self.object
self.comment.save()
messages.success(self.request, "Comment successfully saved.")
return HttpResponseRedirect(self.get_success_url())