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

120 lines
4 KiB
Python
Raw Normal View History

2021-03-02 20:42:54 +01:00
import operator
from functools import reduce
from typing import Dict
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
2020-08-14 15:53:42 +02:00
from django.views import generic
2020-08-14 14:53:30 +02:00
from articles.models import Article
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"
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"]
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
class ArticlesListView(BaseArticleListView):
queryset = Article.objects.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)
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-02 20:42:54 +01:00
class SearchArticlesListView(BaseArticleListView):
queryset = Article.objects.filter(status=Article.PUBLISHED)
template_name = "articles/article_search.html"
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()
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(
operator.and_, (Q(keywords__icontains=term) for term in search_terms)
)
)
def get_additional_querystring_params(self) -> Dict[str, str]:
search_expression = self.request.GET.get("s")
if search_expression:
return {"s": search_expression}
return {}
2020-12-03 21:15:48 +01:00
class DraftsListView(LoginRequiredMixin, BaseArticleListView):
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"
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):
key = self.request.GET.get("draft_key")
if key:
return Article.objects.filter(draft_key=key)
2020-08-18 18:49:41 +02:00
queryset = super().get_queryset()
if not self.request.user.is_authenticated:
queryset = queryset.filter(status=Article.PUBLISHED)
return queryset
2020-08-18 18:49:41 +02:00
def get_object(self, queryset=None) -> Article:
obj = super().get_object(queryset) # type: Article
if not self.request.user.is_authenticated:
obj.views_count = F("views_count") + 1
obj.save(update_fields=["views_count"])
return obj