bookwyrm/bookwyrm/views/rss_feed.py

239 lines
7.8 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" serialize user's posts in rss feed """
2021-01-20 22:15:15 +00:00
from django.contrib.syndication.views import Feed
2021-08-09 14:38:43 +00:00
from django.template.loader import get_template
from django.utils.translation import gettext_lazy as _
2023-09-29 01:49:05 +00:00
from django.shortcuts import get_object_or_404
2023-01-23 14:17:20 +00:00
from ..models import Review, Quotation, Comment
2021-08-09 14:38:43 +00:00
2021-10-06 17:37:09 +00:00
from .helpers import get_user_from_username
2021-01-20 22:15:15 +00:00
2021-01-29 17:28:00 +00:00
# pylint: disable=no-self-use, unused-argument
2021-01-20 22:15:15 +00:00
class RssFeed(Feed):
2021-04-26 16:15:42 +00:00
"""serialize user's posts in rss feed"""
2021-03-08 16:49:10 +00:00
description_template = "rss/content.html"
2021-08-09 14:38:43 +00:00
def item_title(self, item):
"""render the item title"""
if hasattr(item, "pure_name") and item.pure_name:
return item.pure_name
title_template = get_template("snippets/status/header_content.html")
title = title_template.render({"status": item})
template = get_template("rss/title.html")
return template.render({"user": item.user, "item_title": title}).strip()
2021-01-20 22:15:15 +00:00
2021-06-18 21:12:56 +00:00
def get_object(self, request, username): # pylint: disable=arguments-differ
2021-04-26 16:15:42 +00:00
"""the user who's posts get serialized"""
return get_user_from_username(request.user, username)
2021-01-20 22:15:15 +00:00
def link(self, obj):
2021-04-26 16:15:42 +00:00
"""link to the user's profile"""
2021-01-20 22:15:15 +00:00
return obj.local_path
def title(self, obj):
2021-04-26 16:15:42 +00:00
"""title of the rss feed entry"""
2021-08-09 14:38:43 +00:00
return _(f"Status updates from {obj.display_name}")
2021-01-20 22:15:15 +00:00
def items(self, obj):
2021-04-26 16:15:42 +00:00
"""the user's activity feed"""
2023-05-29 05:42:37 +00:00
return (
obj.status_set.select_subclasses()
.filter(
privacy__in=["public", "unlisted"],
)
.order_by("-published_date")[:10]
)
2021-01-29 17:28:00 +00:00
2021-01-20 22:15:15 +00:00
def item_link(self, item):
2021-04-26 16:15:42 +00:00
"""link to the status"""
2021-01-20 22:15:15 +00:00
return item.local_path
2023-01-17 20:53:44 +00:00
2023-05-29 04:55:26 +00:00
def item_pubdate(self, item):
"""publication date of the item"""
return item.published_date
2023-01-17 20:53:44 +00:00
class RssReviewsOnlyFeed(Feed):
"""serialize user's reviews in rss feed"""
description_template = "rss/content.html"
def item_title(self, item):
"""render the item title"""
if hasattr(item, "pure_name") and item.pure_name:
return item.pure_name
title_template = get_template("snippets/status/header_content.html")
title = title_template.render({"status": item})
template = get_template("rss/title.html")
return template.render({"user": item.user, "item_title": title}).strip()
def get_object(self, request, username): # pylint: disable=arguments-differ
"""the user who's posts get serialized"""
return get_user_from_username(request.user, username)
def link(self, obj):
"""link to the user's profile"""
return obj.local_path
def title(self, obj):
"""title of the rss feed entry"""
return _(f"Reviews from {obj.display_name}")
def items(self, obj):
"""the user's activity feed"""
2023-01-23 14:17:20 +00:00
return Review.objects.filter(
user=obj,
2023-01-17 20:53:44 +00:00
privacy__in=["public", "unlisted"],
2023-05-29 05:38:53 +00:00
).order_by("-published_date")[:10]
2023-01-17 20:53:44 +00:00
def item_link(self, item):
"""link to the status"""
return item.local_path
2023-05-29 04:55:26 +00:00
def item_pubdate(self, item):
"""publication date of the item"""
return item.published_date
2023-01-17 20:53:44 +00:00
class RssQuotesOnlyFeed(Feed):
"""serialize user's quotes in rss feed"""
description_template = "rss/content.html"
def item_title(self, item):
"""render the item title"""
if hasattr(item, "pure_name") and item.pure_name:
return item.pure_name
title_template = get_template("snippets/status/header_content.html")
title = title_template.render({"status": item})
template = get_template("rss/title.html")
return template.render({"user": item.user, "item_title": title}).strip()
def get_object(self, request, username): # pylint: disable=arguments-differ
"""the user who's posts get serialized"""
return get_user_from_username(request.user, username)
def link(self, obj):
"""link to the user's profile"""
return obj.local_path
def title(self, obj):
"""title of the rss feed entry"""
return _(f"Quotes from {obj.display_name}")
def items(self, obj):
"""the user's activity feed"""
2023-01-23 14:17:20 +00:00
return Quotation.objects.filter(
user=obj,
2023-01-17 20:53:44 +00:00
privacy__in=["public", "unlisted"],
2023-05-29 05:38:53 +00:00
).order_by("-published_date")[:10]
2023-01-17 20:53:44 +00:00
def item_link(self, item):
"""link to the status"""
return item.local_path
2023-05-29 04:55:26 +00:00
def item_pubdate(self, item):
"""publication date of the item"""
return item.published_date
2023-01-17 20:53:44 +00:00
class RssCommentsOnlyFeed(Feed):
"""serialize user's quotes in rss feed"""
description_template = "rss/content.html"
def item_title(self, item):
"""render the item title"""
if hasattr(item, "pure_name") and item.pure_name:
return item.pure_name
title_template = get_template("snippets/status/header_content.html")
title = title_template.render({"status": item})
template = get_template("rss/title.html")
return template.render({"user": item.user, "item_title": title}).strip()
def get_object(self, request, username): # pylint: disable=arguments-differ
"""the user who's posts get serialized"""
return get_user_from_username(request.user, username)
def link(self, obj):
"""link to the user's profile"""
return obj.local_path
def title(self, obj):
"""title of the rss feed entry"""
return _(f"Comments from {obj.display_name}")
def items(self, obj):
"""the user's activity feed"""
2023-01-23 14:17:20 +00:00
return Comment.objects.filter(
user=obj,
2023-01-17 20:53:44 +00:00
privacy__in=["public", "unlisted"],
2023-05-29 05:38:53 +00:00
).order_by("-published_date")[:10]
2023-01-17 20:53:44 +00:00
def item_link(self, item):
"""link to the status"""
return item.local_path
2023-05-29 05:25:01 +00:00
2023-05-29 04:55:26 +00:00
def item_pubdate(self, item):
"""publication date of the item"""
return item.published_date
2023-09-29 01:49:05 +00:00
class RssShelfFeed(Feed):
"""serialize a shelf activity in rss"""
description_template = "rss/edition.html"
def item_title(self, item):
"""render the item title"""
authors = item.authors
if item.author_text:
authors.display_name = f"{item.author_text}:"
else:
authors.description = ""
2023-09-29 01:49:05 +00:00
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 shelf that gets serialized"""
2023-09-29 01:49:05 +00:00
user = get_user_from_username(request.user, username)
# always get privacy, don't support rss over anything private
# get the SHELF of the object
shelf = get_object_or_404(
user.shelf_set,
identifier=shelf_identifier,
privacy__in=["public", "unlisted"],
)
2023-09-29 01:49:05 +00:00
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"{obj.user.display_name}s {obj.name} shelf")
2023-09-29 01:49:05 +00:00
def items(self, obj):
"""the user's activity feed"""
return obj.books.order_by("-shelfbook__shelved_date")[:10]
2023-09-29 01:49:05 +00:00
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):
2023-10-01 10:05:54 +00:00
"""description of the shelf including the shelf name and user."""
2023-09-29 01:49:05 +00:00
# if there's a description, lets add it. Not everyone puts a description in.
if desc := obj.description:
return _(f"{obj.user.display_name}s {obj.name} shelf: {desc}")
return _(f"Books added to {obj.user.name}s {obj.name} shelf")