Add feed for tags

This commit is contained in:
Gabriel Augendre 2021-03-04 18:48:48 +01:00
parent 4ddf916364
commit cf1b2af325
3 changed files with 15 additions and 6 deletions

View file

@ -60,7 +60,7 @@ class Article(models.Model):
ordering = ["-published_at"]
def __str__(self):
return f"{self.title}"
return self.title
def get_absolute_url(self):
return reverse("article-detail", kwargs={"slug": self.slug})

View file

@ -6,6 +6,7 @@ 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"),

View file

@ -1,6 +1,6 @@
from django.contrib.syndication.views import Feed
from articles.models import Article
from articles.models import Article, Tag
from blog import settings
@ -10,16 +10,24 @@ class CompleteFeed(Feed):
link = settings.BLOG["base_url"]
description = settings.BLOG["description"]
def items(self):
def get_queryset(self, obj):
return Article.objects.filter(status=Article.PUBLISHED).order_by(
"-published_at"
)[: self.FEED_LIMIT]
)
def item_title(self, item: Article):
return item.title
def items(self, obj):
return self.get_queryset(obj)[: self.FEED_LIMIT]
def item_description(self, item: Article):
return item.get_formatted_content
def item_pubdate(self, item: Article):
return item.published_at
class TagFeed(CompleteFeed):
def get_object(self, request, *args, **kwargs):
return Tag.objects.get(slug=kwargs.get("slug"))
def get_queryset(self, tag):
return super().get_queryset(tag).filter(tags=tag)