2021-03-02 20:42:54 +01:00
|
|
|
import operator
|
|
|
|
from functools import reduce
|
|
|
|
from typing import Dict
|
|
|
|
|
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
|
2021-03-02 20:42:54 +01:00
|
|
|
from django.db.models import F, Q
|
2021-03-04 18:24:22 +01:00
|
|
|
from django.shortcuts import get_object_or_404
|
2020-08-14 15:53:42 +02:00
|
|
|
from django.views import generic
|
2020-08-14 14:53:30 +02:00
|
|
|
|
2021-03-04 18:24:22 +01:00
|
|
|
from articles.models import Article, Tag
|
2020-08-14 15:53:42 +02:00
|
|
|
|
|
|
|
|
2020-11-24 21:53:26 +01:00
|
|
|
class BaseArticleListView(generic.ListView):
|
2021-03-02 20:42:54 +01:00
|
|
|
model = Article
|
|
|
|
context_object_name = "articles"
|
2020-11-26 11:26:47 +01:00
|
|
|
paginate_by = 10
|
2021-04-18 09:33:02 +02:00
|
|
|
main_title = "Blog posts"
|
|
|
|
html_title = ""
|
2020-11-26 11:26:47 +01:00
|
|
|
|
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"]
|
2021-03-02 20:42:54 +01:00
|
|
|
page_obj = context["page_obj"]
|
|
|
|
if page_obj.has_next():
|
|
|
|
querystring = self.build_querystring({"page": page_obj.next_page_number()})
|
|
|
|
context["next_page_querystring"] = querystring
|
|
|
|
if page_obj.has_previous():
|
|
|
|
querystring = self.build_querystring(
|
|
|
|
{"page": page_obj.previous_page_number()}
|
|
|
|
)
|
|
|
|
context["previous_page_querystring"] = querystring
|
2020-11-24 21:53:26 +01:00
|
|
|
return context
|
|
|
|
|
2021-03-02 20:42:54 +01:00
|
|
|
def get_additional_querystring_params(self) -> Dict[str, str]:
|
|
|
|
return dict()
|
|
|
|
|
|
|
|
def build_querystring(self, initial_queryparams: Dict[str, str]) -> str:
|
|
|
|
querystring = {
|
|
|
|
**initial_queryparams,
|
|
|
|
**self.get_additional_querystring_params(),
|
|
|
|
}
|
|
|
|
return "&".join(map(lambda item: f"{item[0]}={item[1]}", querystring.items()))
|
|
|
|
|
2020-11-24 21:53:26 +01:00
|
|
|
|
2021-03-04 18:24:22 +01:00
|
|
|
class PublicArticleListView(BaseArticleListView):
|
2020-12-24 17:49:47 +01:00
|
|
|
queryset = Article.objects.filter(status=Article.PUBLISHED)
|
2020-08-14 15:53:42 +02:00
|
|
|
|
2021-03-04 18:24:22 +01:00
|
|
|
|
|
|
|
class ArticlesListView(PublicArticleListView):
|
2020-11-24 21:53:26 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-12-24 17:49:47 +01:00
|
|
|
home_article = Article.objects.filter(
|
|
|
|
status=Article.PUBLISHED, is_home=True
|
|
|
|
).first() # type: Article
|
|
|
|
context["article"] = home_article
|
2020-08-16 18:14:55 +02:00
|
|
|
return context
|
|
|
|
|
2020-08-14 15:53:42 +02:00
|
|
|
|
2021-03-04 18:24:22 +01:00
|
|
|
class SearchArticlesListView(PublicArticleListView):
|
2021-03-02 20:42:54 +01:00
|
|
|
template_name = "articles/article_search.html"
|
2021-04-18 09:33:02 +02:00
|
|
|
html_title = "Search"
|
2021-03-02 20:42:54 +01:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["search_expression"] = self.request.GET.get("s") or ""
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
search_expression = self.request.GET.get("s")
|
|
|
|
if not search_expression:
|
|
|
|
return queryset.none()
|
2021-04-18 09:33:02 +02:00
|
|
|
self.html_title = f"Search results for {search_expression}"
|
2021-03-02 20:42:54 +01:00
|
|
|
search_terms = search_expression.split()
|
|
|
|
return queryset.filter(
|
|
|
|
reduce(operator.and_, (Q(title__icontains=term) for term in search_terms))
|
|
|
|
| reduce(
|
|
|
|
operator.and_, (Q(content__icontains=term) for term in search_terms)
|
|
|
|
)
|
|
|
|
| reduce(
|
2021-03-03 17:30:38 +01:00
|
|
|
operator.and_, (Q(tags__name__icontains=term) for term in search_terms)
|
2021-03-02 20:42:54 +01:00
|
|
|
)
|
2021-03-03 17:30:38 +01:00
|
|
|
).distinct()
|
2021-03-02 20:42:54 +01:00
|
|
|
|
|
|
|
def get_additional_querystring_params(self) -> Dict[str, str]:
|
|
|
|
search_expression = self.request.GET.get("s")
|
|
|
|
if search_expression:
|
|
|
|
return {"s": search_expression}
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2021-03-04 18:24:22 +01:00
|
|
|
class TagArticlesListView(PublicArticleListView):
|
2021-03-04 19:33:43 +01:00
|
|
|
tag = None
|
2021-04-18 09:33:02 +02:00
|
|
|
main_title = None
|
|
|
|
html_title = None
|
2021-03-04 19:33:43 +01:00
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.tag = get_object_or_404(Tag, slug=self.kwargs.get("slug"))
|
2021-04-18 09:33:02 +02:00
|
|
|
self.main_title = self.html_title = f"{self.tag.name} articles"
|
2021-03-04 19:33:43 +01:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["feed_title"] = self.tag.get_feed_title()
|
|
|
|
context["feed_url"] = self.tag.get_feed_url()
|
|
|
|
return context
|
|
|
|
|
2021-03-04 18:24:22 +01:00
|
|
|
def get_queryset(self):
|
2021-03-04 19:33:43 +01:00
|
|
|
return super().get_queryset().filter(tags=self.tag)
|
2021-03-04 18:24:22 +01:00
|
|
|
|
|
|
|
|
2020-12-03 21:15:48 +01:00
|
|
|
class DraftsListView(LoginRequiredMixin, BaseArticleListView):
|
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-12-27 20:00:41 +01:00
|
|
|
key = self.request.GET.get("draft_key")
|
|
|
|
if key:
|
2021-03-04 18:24:22 +01:00
|
|
|
return Article.objects.filter(draft_key=key).prefetch_related("tags")
|
2020-12-27 20:00:41 +01:00
|
|
|
|
2021-03-04 18:24:22 +01:00
|
|
|
queryset = super().get_queryset().prefetch_related("tags")
|
2020-12-27 20:00:41 +01:00
|
|
|
if not self.request.user.is_authenticated:
|
|
|
|
queryset = queryset.filter(status=Article.PUBLISHED)
|
|
|
|
return queryset
|
2020-08-18 18:49:41 +02:00
|
|
|
|
2020-12-24 17:49:47 +01:00
|
|
|
def get_object(self, queryset=None) -> Article:
|
2020-09-03 21:29:28 +02:00
|
|
|
obj = super().get_object(queryset) # type: Article
|
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
|
2021-03-06 14:43:04 +01:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs["tags"] = self.object.tags.all()
|
|
|
|
return super().get_context_data(**kwargs)
|