2020-09-03 21:29:28 +02:00
|
|
|
from typing import Union
|
|
|
|
|
2020-09-12 17:20:56 +02:00
|
|
|
from django.conf import settings
|
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-14 14:53:30 +02:00
|
|
|
|
2020-11-10 16:26:27 +01:00
|
|
|
from articles.models import Article, Page
|
2020-08-14 15:53:42 +02:00
|
|
|
|
|
|
|
|
2020-11-24 21:53:26 +01:00
|
|
|
class BaseArticleListView(generic.ListView):
|
2020-11-26 11:26:47 +01:00
|
|
|
paginate_by = 10
|
|
|
|
|
2020-11-24 21:53:26 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["blog_title"] = settings.BLOG["title"]
|
|
|
|
context["blog_description"] = settings.BLOG["description"]
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class ArticlesListView(BaseArticleListView):
|
|
|
|
model = Article
|
2020-08-14 15:53:42 +02:00
|
|
|
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-11-24 21:53:26 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-11-26 11:26:47 +01:00
|
|
|
index_page = Page.objects.filter(
|
|
|
|
status=Article.PUBLISHED, position=0
|
|
|
|
).first() # type: Page
|
2020-11-28 20:09:37 +01:00
|
|
|
context["article"] = index_page
|
2020-08-16 18:14:55 +02:00
|
|
|
return context
|
|
|
|
|
2020-08-14 15:53:42 +02:00
|
|
|
|
2020-11-24 21:53:26 +01:00
|
|
|
class DraftsListView(BaseArticleListView, LoginRequiredMixin):
|
2020-08-16 18:14:55 +02:00
|
|
|
model = Article
|
|
|
|
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
|
|
|
|
2020-11-24 21:53:26 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-08-16 18:14:55 +02:00
|
|
|
context["title"] = "Drafts"
|
2020-09-12 17:20:56 +02:00
|
|
|
context["title_header"] = context["title"]
|
2020-08-16 18:14:55 +02:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
2020-11-10 16:26:27 +01:00
|
|
|
class ArticleDetailView(generic.DetailView):
|
2020-08-14 15:53:42 +02:00
|
|
|
model = Article
|
|
|
|
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)
|
|
|
|
|
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
|