~crocmagnon/blog

ref: 698a2db6a78daea02449fff8ed401e36f5034faa blog/articles/views/html.py -rw-r--r-- 3.6 KiB
698a2db6Gabriel Augendre Update articles list look with blog title and description 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from typing import Union

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import F
from django.views import generic
from django.views.generic.edit import FormMixin

from articles.forms import CommentForm
from articles.models import Article, Comment, Page


class ArticlesListView(generic.ListView):
    model = Article
    paginate_by = 15
    context_object_name = "articles"
    queryset = Article.without_pages.filter(status=Article.PUBLISHED)

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(object_list=object_list, **kwargs)
        context["title_header"] = "Articles"
        context["blog_title"] = settings.BLOG["title"]
        context["blog_description"] = settings.BLOG["description"]
        return context


class DraftsListView(generic.ListView, LoginRequiredMixin):
    model = Article
    paginate_by = 15
    context_object_name = "articles"
    queryset = Article.objects.filter(status=Article.DRAFT)

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(object_list=object_list, **kwargs)
        context["title"] = "Drafts"
        context["title_header"] = context["title"]
        context["blog_title"] = settings.BLOG["title"]
        context["blog_description"] = settings.BLOG["description"]
        return context


class ArticleDetailView(FormMixin, generic.DetailView):
    model = Article
    form_class = CommentForm
    context_object_name = "article"
    template_name = "articles/article_detail.html"

    def get_queryset(self):
        queryset = super().get_queryset()
        if self.request.user.is_authenticated:
            return queryset
        return queryset.filter(status=Article.PUBLISHED)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        article = self.object
        if hasattr(article, "article"):
            article = article.article
        context["comments"] = article.comments.filter(status=Comment.APPROVED)
        return context

    def get_object(self, queryset=None) -> Union[Article, Page]:
        obj = super().get_object(queryset)  # type: Article
        if hasattr(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"])

        return obj

    def post(self, request, *args, **kwargs):
        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:
            return self.form_invalid(form)

    def form_invalid(self, form):
        messages.error(
            self.request,
            'Your comment couldn\'t be saved, see <a href="#comment-form">the form below</a>.',
        )
        return super().form_invalid(form)

    def form_valid(self, form):
        comment = form.save(commit=False)
        comment.article = self.object
        comment.save()
        messages.success(self.request, "Comment successfully saved, pending review.")
        return super().form_valid(form)

    def get_success_url(self):
        return self.object.get_absolute_url()