From 4dcb23970f610ee86ab956307b88ef158df66371 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 16 Aug 2020 18:14:55 +0200 Subject: [PATCH] Add drafts list view --- articles/views.py | 22 ++++++++++++++++++++++ blog/urls.py | 1 + 2 files changed, 23 insertions(+) diff --git a/articles/views.py b/articles/views.py index 300ae55..a623704 100644 --- a/articles/views.py +++ b/articles/views.py @@ -1,3 +1,4 @@ +from django.contrib.auth.mixins import LoginRequiredMixin from django.db.models import F from django.views import generic @@ -9,15 +10,36 @@ class ArticlesListView(generic.ListView): 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"] = "Articles" + return context + def get_queryset(self): return super().get_queryset().filter(status=Article.PUBLISHED) +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) + + class ArticleDetailView(generic.DetailView): model = Article context_object_name = "article" def get_queryset(self): + if self.request.user.is_authenticated: + return super().get_queryset() return super().get_queryset().filter(status=Article.PUBLISHED) def get_object(self, queryset=None): diff --git a/blog/urls.py b/blog/urls.py index 4008530..04add88 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -21,5 +21,6 @@ from articles import views urlpatterns = [ path("admin/", admin.site.urls), path("", views.ArticlesListView.as_view(), name="articles-list"), + path("drafts/", views.DraftsListView.as_view(), name="drafts-list"), path("", views.ArticleDetailView.as_view(), name="article-detail"), ]