Add drafts list view
This commit is contained in:
parent
2de4c3cabc
commit
4dcb23970f
2 changed files with 23 additions and 0 deletions
|
@ -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):
|
||||
|
|
|
@ -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("<int:pk>", views.ArticleDetailView.as_view(), name="article-detail"),
|
||||
]
|
||||
|
|
Reference in a new issue