Add drafts list view

This commit is contained in:
Gabriel Augendre 2020-08-16 18:14:55 +02:00
parent 2de4c3cabc
commit 4dcb23970f
2 changed files with 23 additions and 0 deletions

View file

@ -1,3 +1,4 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import F from django.db.models import F
from django.views import generic from django.views import generic
@ -9,15 +10,36 @@ class ArticlesListView(generic.ListView):
paginate_by = 15 paginate_by = 15
context_object_name = "articles" 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): def get_queryset(self):
return super().get_queryset().filter(status=Article.PUBLISHED) 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): class ArticleDetailView(generic.DetailView):
model = Article model = Article
context_object_name = "article" context_object_name = "article"
def get_queryset(self): def get_queryset(self):
if self.request.user.is_authenticated:
return super().get_queryset()
return super().get_queryset().filter(status=Article.PUBLISHED) return super().get_queryset().filter(status=Article.PUBLISHED)
def get_object(self, queryset=None): def get_object(self, queryset=None):

View file

@ -21,5 +21,6 @@ from articles import views
urlpatterns = [ urlpatterns = [
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
path("", views.ArticlesListView.as_view(), name="articles-list"), path("", views.ArticlesListView.as_view(), name="articles-list"),
path("drafts/", views.DraftsListView.as_view(), name="drafts-list"),
path("<int:pk>", views.ArticleDetailView.as_view(), name="article-detail"), path("<int:pk>", views.ArticleDetailView.as_view(), name="article-detail"),
] ]