Switch to fbv for article view

This commit is contained in:
Gabriel Augendre 2022-09-23 22:19:16 +01:00
parent 5b5f03afaf
commit 3d7430fe11
3 changed files with 25 additions and 36 deletions

View file

@ -12,7 +12,7 @@ from django.conf import settings
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db import models from django.db import models
from django.db.models import Prefetch from django.db.models import F, Prefetch
from django.template.defaultfilters import slugify from django.template.defaultfilters import slugify
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
@ -132,13 +132,9 @@ class Article(models.Model):
self.save() self.save()
def get_read_time(self) -> int: def get_read_time(self) -> int:
try: content = self.get_formatted_content
content = self.get_formatted_content if content:
if content: return readtime.of_markdown(content).minutes
return readtime.of_html(content).minutes
except ParseError:
print(f"minutes failed for pk {self.pk}")
return 0
return 0 return 0
@cached_property @cached_property
@ -168,3 +164,7 @@ class Article(models.Model):
f"admin:{content_type.app_label}_{content_type.model}_change", f"admin:{content_type.app_label}_{content_type.model}_change",
args=(self.id,), args=(self.id,),
) )
def increment_view_count(self) -> None:
self.views_count = F("views_count") + 1
self.save(update_fields=["views_count"])

View file

@ -10,5 +10,5 @@ urlpatterns = [
path("tag/<slug:slug>/", html.TagArticlesListView.as_view(), name="tag"), path("tag/<slug:slug>/", html.TagArticlesListView.as_view(), name="tag"),
path("feed/", feeds.CompleteFeed(), name="complete-feed"), path("feed/", feeds.CompleteFeed(), name="complete-feed"),
path("api/render/<int:article_pk>/", api.render_article, name="api-render-article"), path("api/render/<int:article_pk>/", api.render_article, name="api-render-article"),
path("<slug:slug>/", html.ArticleDetailView.as_view(), name="article-detail"), path("<slug:slug>/", html.view_article, name="article-detail"),
] ]

View file

@ -6,11 +6,10 @@ from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin 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 F, Q, QuerySet from django.db.models import Q, QuerySet
from django.http.response import HttpResponse, HttpResponseBase from django.http.response import HttpResponse, HttpResponseBase
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404, render
from django.views import generic from django.views import generic
from django.views.generic import DetailView
from articles.models import Article, Tag from articles.models import Article, Tag
@ -127,29 +126,19 @@ class DraftsListView(LoginRequiredMixin, BaseArticleListView):
return context return context
class ArticleDetailView(DetailView): def view_article(request: WSGIRequest, slug: str) -> HttpResponse:
model = Article article = get_article(request, slug)
context_object_name = "article" if not request.user.is_authenticated:
template_name = "articles/article_detail.html" article.increment_view_count()
context = {"article": article, "tags": article.tags.all()}
return render(request, "articles/article_detail.html", context)
def get_queryset(self) -> QuerySet:
key = self.request.GET.get("draft_key")
if key:
return Article.objects.filter(draft_key=key).prefetch_related("tags")
queryset = super().get_queryset().prefetch_related("tags") def get_article(request: WSGIRequest, slug: str) -> Article:
if not self.request.user.is_authenticated: key = request.GET.get("draft_key")
queryset = queryset.filter(status=Article.PUBLISHED) qs = Article.objects.prefetch_related("tags")
return queryset if key:
return get_object_or_404(qs, draft_key=key, slug=slug)
def get_object(self, queryset: QuerySet | None = None) -> Article: if not request.user.is_authenticated:
obj: Article = super().get_object(queryset) qs = qs.filter(status=Article.PUBLISHED)
if not self.request.user.is_authenticated: return get_object_or_404(qs, slug=slug)
obj.views_count = F("views_count") + 1
obj.save(update_fields=["views_count"])
return obj
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
kwargs["tags"] = self.object.tags.all()
return super().get_context_data(**kwargs)