From f84cb409468839059ae202809882a8eb2a38372a Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 24 Sep 2022 15:30:33 +0100 Subject: [PATCH] Refactor views --- .pre-commit-config.yaml | 2 +- src/articles/urls.py | 20 ++++++++-------- src/articles/views/__init__.py | 20 ++++++++++++++++ src/articles/views/html/__init__.py | 0 .../views/{html.py => html/list_articles.py} | 22 ++---------------- src/articles/views/html/single_article.py | 23 +++++++++++++++++++ 6 files changed, 57 insertions(+), 30 deletions(-) create mode 100644 src/articles/views/html/__init__.py rename src/articles/views/{html.py => html/list_articles.py} (84%) create mode 100644 src/articles/views/html/single_article.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 785d458..13a00e4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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] diff --git a/src/articles/urls.py b/src/articles/urls.py index 329ba53..6bc6e4f 100644 --- a/src/articles/urls.py +++ b/src/articles/urls.py @@ -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//feed/", feeds.TagFeed(), name="tag-feed"), - path("tag//", html.TagArticlesListView.as_view(), name="tag"), - path("feed/", feeds.CompleteFeed(), name="complete-feed"), - path("api/render//", api.render_article, name="api-render-article"), - path("/", 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//feed/", views.TagFeed(), name="tag-feed"), + path("tag//", views.TagArticlesListView.as_view(), name="tag"), + path("feed/", views.CompleteFeed(), name="complete-feed"), + path( + "api/render//", views.render_article, name="api-render-article" + ), + path("/", views.view_article, name="article-detail"), ] diff --git a/src/articles/views/__init__.py b/src/articles/views/__init__.py index e69de29..f44d50d 100644 --- a/src/articles/views/__init__.py +++ b/src/articles/views/__init__.py @@ -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", +] diff --git a/src/articles/views/html/__init__.py b/src/articles/views/html/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/articles/views/html.py b/src/articles/views/html/list_articles.py similarity index 84% rename from src/articles/views/html.py rename to src/articles/views/html/list_articles.py index 15e3923..159fd24 100644 --- a/src/articles/views/html.py +++ b/src/articles/views/html/list_articles.py @@ -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) diff --git a/src/articles/views/html/single_article.py b/src/articles/views/html/single_article.py new file mode 100644 index 0000000..7b2d104 --- /dev/null +++ b/src/articles/views/html/single_article.py @@ -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)