Skip to content

Commit

Permalink
feat: per-user RSS feed (#1232)
Browse files Browse the repository at this point in the history
* feat: per-user RSS feed

* fix formatting

* Code cleanup

* Fix formatting

* Remove redundant code
  • Loading branch information
aigoncharov authored Jul 12, 2024
1 parent 6bfca56 commit 7f41ee7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions club/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from posts.api import md_show_post, api_show_post, json_feed
from posts.models.post import Post
from posts.rss import NewPostsRss
from posts.user_rss import UserPostsRss
from posts.sitemaps import sitemaps
from posts.views.admin_actions import admin_post, announce_post, curate_post
from posts.views.api import toggle_post_bookmark
Expand Down Expand Up @@ -205,6 +206,7 @@
# feeds
path("sitemap.xml", sitemap, {"sitemaps": sitemaps}, name="sitemap"),
path("posts.rss", NewPostsRss(), name="rss"),
path("user/<slug:user_slug>/posts.rss", UserPostsRss(), name="user_rss"),
path("feed.json", json_feed, name="json_feed"),
re_path(r"^{}/{}/feed.json$".format(POST_TYPE_RE, ORDERING_RE), json_feed, name="json_feed_ordering"),

Expand Down
2 changes: 2 additions & 0 deletions frontend/html/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
}
</script>

{% block feeds %}
<link rel="alternate" type="application/rss+xml"
title="Вастрик.Клуб: Новые посты"
href="{{ settings.APP_HOST }}{% url "rss" %}" />

<link rel="alternate" title="Вастрик.Клуб: JSON Feed"
type="application/feed+json"
href="{{ settings.APP_HOST }}{% url "json_feed" %}" />
‍{% endblock %}

{% block og_tags %}
{% include "common/og.html" %}
Expand Down
6 changes: 6 additions & 0 deletions frontend/html/users/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
{{ user.full_name }} — {{ block.super }}
{% endblock %}

{% block feeds %}
<link rel="alternate" type="application/rss+xml"
title="Вастрик.Клуб: Посты {{ user.slug }}"
href="{{ settings.APP_HOST }}{% url "user_rss" user.slug %}" />
‍{% endblock %}

{% block og_tags %}
<meta property="og:title" content="{{ user.full_name }} — {{ settings.APP_NAME }}">
<meta property="og:site_name" content="{{ settings.APP_NAME }}">
Expand Down
23 changes: 23 additions & 0 deletions posts/user_rss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.shortcuts import get_object_or_404

from posts.rss import NewPostsRss
from posts.models.post import Post
from users.models.user import User


class UserPostsRss(NewPostsRss):
def get_object(self, _, user_slug=None):
return get_object_or_404(User, slug=user_slug)

def link(self, user):
return f"/user/{user.slug}/posts.rss"

def title(self, user):
return f"Вастрик.Клуб: Посты {user.slug}"

def items(self, user):
return Post.visible_objects()\
.filter(is_approved_by_moderator=True)\
.filter(author=user) \
.exclude(type=Post.TYPE_INTRO)\
.order_by("-published_at", "-created_at")[:self.limit]

0 comments on commit 7f41ee7

Please sign in to comment.