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-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"
|
|
|
|
|
|
|
|
def get_context_data(self, *, object_list=None, **kwargs):
|
|
|
|
context = super().get_context_data(object_list=object_list, **kwargs)
|
|
|
|
context["title"] = "Drafts"
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().filter(status=Article.DRAFT)
|
|
|
|
|
|
|
|
|
2020-08-14 15:53:42 +02:00
|
|
|
class ArticleDetailView(generic.DetailView):
|
|
|
|
model = Article
|
|
|
|
context_object_name = "article"
|
2020-08-14 16:19:02 +02:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-08-16 18:14:55 +02:00
|
|
|
if self.request.user.is_authenticated:
|
|
|
|
return super().get_queryset()
|
2020-08-14 16:19:02 +02:00
|
|
|
return super().get_queryset().filter(status=Article.PUBLISHED)
|
2020-08-14 22:06:38 +02:00
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
obj = super().get_object(queryset)
|
|
|
|
if not self.request.user.is_authenticated:
|
|
|
|
obj.views_count = F("views_count") + 1
|
|
|
|
obj.save(update_fields=["views_count"])
|
|
|
|
|
|
|
|
return obj
|