Refactor views

This commit is contained in:
Gabriel Augendre 2022-09-24 15:30:33 +01:00
parent 2f93bfb236
commit f84cb40946
6 changed files with 57 additions and 30 deletions

View file

@ -68,7 +68,7 @@ repos:
- id: prettier - id: prettier
types_or: [javascript, css] types_or: [javascript, css]
- repo: https://github.com/pre-commit/mirrors-eslint - repo: https://github.com/pre-commit/mirrors-eslint
rev: v8.23.1 rev: v8.24.0
hooks: hooks:
- id: eslint - id: eslint
args: [--fix] args: [--fix]

View file

@ -1,14 +1,16 @@
from django.urls import path from django.urls import path
from articles.views import api, feeds, html from articles import views
urlpatterns = [ urlpatterns = [
path("", html.ArticlesListView.as_view(), name="articles-list"), path("", views.ArticlesListView.as_view(), name="articles-list"),
path("drafts/", html.DraftsListView.as_view(), name="drafts-list"), path("drafts/", views.DraftsListView.as_view(), name="drafts-list"),
path("search/", html.SearchArticlesListView.as_view(), name="search"), path("search/", views.SearchArticlesListView.as_view(), name="search"),
path("tag/<slug:slug>/feed/", feeds.TagFeed(), name="tag-feed"), path("tag/<slug:slug>/feed/", views.TagFeed(), name="tag-feed"),
path("tag/<slug:slug>/", html.TagArticlesListView.as_view(), name="tag"), path("tag/<slug:slug>/", views.TagArticlesListView.as_view(), name="tag"),
path("feed/", feeds.CompleteFeed(), name="complete-feed"), path("feed/", views.CompleteFeed(), name="complete-feed"),
path("api/render/<int:article_pk>/", api.render_article, name="api-render-article"), path(
path("<slug:slug>/", html.view_article, name="article-detail"), "api/render/<int:article_pk>/", views.render_article, name="api-render-article"
),
path("<slug:slug>/", views.view_article, name="article-detail"),
] ]

View file

@ -0,0 +1,20 @@
from .api import render_article
from .feeds import CompleteFeed, TagFeed
from .html.list_articles import (
ArticlesListView,
DraftsListView,
SearchArticlesListView,
TagArticlesListView,
)
from .html.single_article import view_article
__all__ = [
"view_article",
"render_article",
"CompleteFeed",
"TagFeed",
"ArticlesListView",
"DraftsListView",
"SearchArticlesListView",
"TagArticlesListView",
]

View file

View file

@ -7,8 +7,8 @@ from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.handlers.wsgi import WSGIRequest from django.core.handlers.wsgi import WSGIRequest
from django.core.paginator import Page from django.core.paginator import Page
from django.db.models import Q, QuerySet from django.db.models import Q, QuerySet
from django.http.response import HttpResponse, HttpResponseBase from django.http import HttpResponseBase
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404
from django.views import generic from django.views import generic
from articles.models import Article, Tag from articles.models import Article, Tag
@ -124,21 +124,3 @@ class DraftsListView(LoginRequiredMixin, BaseArticleListView):
context["title"] = "Drafts" context["title"] = "Drafts"
context["title_header"] = context["title"] context["title_header"] = context["title"]
return context return context
def view_article(request: WSGIRequest, slug: str) -> HttpResponse:
article = get_article(request, slug)
if not request.user.is_authenticated:
article.increment_view_count()
context = {"article": article, "tags": article.tags.all()}
return render(request, "articles/article_detail.html", context)
def get_article(request: WSGIRequest, slug: str) -> Article:
key = request.GET.get("draft_key")
qs = Article.objects.prefetch_related("tags")
if key:
return get_object_or_404(qs, draft_key=key, slug=slug)
if not request.user.is_authenticated:
qs = qs.filter(status=Article.PUBLISHED)
return get_object_or_404(qs, slug=slug)

View file

@ -0,0 +1,23 @@
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from articles.models import Article
def view_article(request: WSGIRequest, slug: str) -> HttpResponse:
article = get_article(request, slug)
if not request.user.is_authenticated:
article.increment_view_count()
context = {"article": article, "tags": article.tags.all()}
return render(request, "articles/article_detail.html", context)
def get_article(request: WSGIRequest, slug: str) -> Article:
key = request.GET.get("draft_key")
qs = Article.objects.prefetch_related("tags")
if key:
return get_object_or_404(qs, draft_key=key, slug=slug)
if not request.user.is_authenticated:
qs = qs.filter(status=Article.PUBLISHED)
return get_object_or_404(qs, slug=slug)