working rss feed for shelves

This commit is contained in:
mattkatz 2023-09-28 21:49:05 -04:00
parent bab28a8fc9
commit 856737e19c
3 changed files with 77 additions and 0 deletions

View file

@ -0,0 +1,4 @@
{{obj.title}} by {{obj.author_text}}
{{obj.description|default:""}}
{% if obj.description %}ISBN: {{item.isbn_10|default:""}}{% endif %}
{% if obj.description %}ISBN13: {{item.isbn_13}}{% endif %}

View file

@ -540,11 +540,21 @@ urlpatterns = [
views.Shelf.as_view(),
name="shelf",
),
re_path(
rf"^{USER_PATH}/(shelf|books)/(?P<shelf_identifier>[\w-]+)(/rss)?/?$",
views.rss_feed.RssShelfFeed(),
name="shelf-rss",
),
re_path(
rf"^{LOCAL_USER_PATH}/(books|shelf)/(?P<shelf_identifier>[\w-]+)(.json)?/?$",
views.Shelf.as_view(),
name="shelf",
),
re_path(
rf"^{LOCAL_USER_PATH}/(books|shelf)/(?P<shelf_identifier>[\w-]+)(/rss)?/?$",
views.rss_feed.RssShelfFeed(),
name="shelf-rss",
),
re_path(r"^create-shelf/?$", views.create_shelf, name="shelf-create"),
re_path(r"^delete-shelf/(?P<shelf_id>\d+)/?$", views.delete_shelf),
re_path(r"^shelve/?$", views.shelve),

View file

@ -3,6 +3,7 @@
from django.contrib.syndication.views import Feed
from django.template.loader import get_template
from django.utils.translation import gettext_lazy as _
from django.shortcuts import get_object_or_404
from ..models import Review, Quotation, Comment
from .helpers import get_user_from_username
@ -177,3 +178,65 @@ class RssCommentsOnlyFeed(Feed):
def item_pubdate(self, item):
"""publication date of the item"""
return item.published_date
class RssShelfFeed(Feed):
"""serialize a shelf activity in rss"""
description_template = "rss/edition.html"
def item_title(self, item):
"""render the item title"""
if hasattr(item, "pure_name") and item.pure_name:
return item.pure_name
authors = item.authors
authors.display_name = f"{item.author_text}:"
template = get_template("rss/title.html")
return template.render({"user": authors, "item_title": item.title}).strip()
def get_object(
self, request, shelf_identifier, username
): # pylint: disable=arguments-differ
"""the user who's posts get serialized"""
user = get_user_from_username(request.user, username)
# always get privacy, don't support rss over anything private
# Shelf.privacy_filter(request.user).filter(user=user).all()
# TODO: get the SHELF of the object
shelf = get_object_or_404(user.shelf_set, identifier=shelf_identifier)
shelf.raise_visible_to_user(request.user)
return shelf
def link(self, obj):
"""link to the shelf"""
return obj.local_path
def title(self, obj):
"""title of the rss feed entry"""
return _(f"Books added to {obj.name}")
def items(self, obj):
"""the user's activity feed"""
return obj.books.order_by("-published_date")[:10]
def item_link(self, item):
"""link to the status"""
return item.local_path
def item_pubdate(self, item):
"""publication date of the item"""
return item.published_date
def description(self, obj):
# if there's a description, lets add it. Not everyone puts a description in.
desc = ""
if obj.description:
desc = f" {obj.description}"
return f"Books added to the '{obj.name}' shelf for {obj.user.name}.{desc}"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# TODO: gotta check that this is the SHELF user!
context["user"] = kwargs["request"].user
context["shelf"] = kwargs["obj"]
return context