Refactor views
This commit is contained in:
parent
2f93bfb236
commit
f84cb40946
6 changed files with 57 additions and 30 deletions
|
@ -68,7 +68,7 @@ repos:
|
|||
- id: prettier
|
||||
types_or: [javascript, css]
|
||||
- repo: https://github.com/pre-commit/mirrors-eslint
|
||||
rev: v8.23.1
|
||||
rev: v8.24.0
|
||||
hooks:
|
||||
- id: eslint
|
||||
args: [--fix]
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
from django.urls import path
|
||||
|
||||
from articles.views import api, feeds, html
|
||||
from articles import views
|
||||
|
||||
urlpatterns = [
|
||||
path("", html.ArticlesListView.as_view(), name="articles-list"),
|
||||
path("drafts/", html.DraftsListView.as_view(), name="drafts-list"),
|
||||
path("search/", html.SearchArticlesListView.as_view(), name="search"),
|
||||
path("tag/<slug:slug>/feed/", feeds.TagFeed(), name="tag-feed"),
|
||||
path("tag/<slug:slug>/", html.TagArticlesListView.as_view(), name="tag"),
|
||||
path("feed/", feeds.CompleteFeed(), name="complete-feed"),
|
||||
path("api/render/<int:article_pk>/", api.render_article, name="api-render-article"),
|
||||
path("<slug:slug>/", html.view_article, name="article-detail"),
|
||||
path("", views.ArticlesListView.as_view(), name="articles-list"),
|
||||
path("drafts/", views.DraftsListView.as_view(), name="drafts-list"),
|
||||
path("search/", views.SearchArticlesListView.as_view(), name="search"),
|
||||
path("tag/<slug:slug>/feed/", views.TagFeed(), name="tag-feed"),
|
||||
path("tag/<slug:slug>/", views.TagArticlesListView.as_view(), name="tag"),
|
||||
path("feed/", views.CompleteFeed(), name="complete-feed"),
|
||||
path(
|
||||
"api/render/<int:article_pk>/", views.render_article, name="api-render-article"
|
||||
),
|
||||
path("<slug:slug>/", views.view_article, name="article-detail"),
|
||||
]
|
||||
|
|
|
@ -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",
|
||||
]
|
0
src/articles/views/html/__init__.py
Normal file
0
src/articles/views/html/__init__.py
Normal file
|
@ -7,8 +7,8 @@ from django.contrib.auth.mixins import LoginRequiredMixin
|
|||
from django.core.handlers.wsgi import WSGIRequest
|
||||
from django.core.paginator import Page
|
||||
from django.db.models import Q, QuerySet
|
||||
from django.http.response import HttpResponse, HttpResponseBase
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.http import HttpResponseBase
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.views import generic
|
||||
|
||||
from articles.models import Article, Tag
|
||||
|
@ -124,21 +124,3 @@ class DraftsListView(LoginRequiredMixin, BaseArticleListView):
|
|||
context["title"] = "Drafts"
|
||||
context["title_header"] = context["title"]
|
||||
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)
|
23
src/articles/views/html/single_article.py
Normal file
23
src/articles/views/html/single_article.py
Normal 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)
|
Reference in a new issue