ausglamr/blogs/views/feeds.py

261 lines
6.9 KiB
Python
Raw Normal View History

2024-01-04 11:54:56 +11:00
"""rss feeds"""
2024-01-26 11:00:26 +11:00
from itertools import chain
from operator import attrgetter
from django.conf import settings
2024-01-04 11:54:56 +11:00
from django.contrib.syndication.views import Feed
2024-01-26 11:00:26 +11:00
from django.utils.feedgenerator import Atom1Feed
2024-01-04 11:54:56 +11:00
2024-01-26 11:00:26 +11:00
from blogs import models
2024-01-04 11:54:56 +11:00
# pylint: disable=R6301
class ArticleFeed(Feed):
"""Combined RSS feed for all the articles"""
2024-01-26 11:00:26 +11:00
feed_type = Atom1Feed
link = "/blog-articles"
feed_url = "/blog-articles/feed"
feed_guid = f"https://{settings.DOMAIN}/blog-articles/feed"
title = "Aus GLAMR Blog articles"
2024-01-04 11:54:56 +11:00
description = "Posts from Australasian Galleries, Libraries, Archives, Museums, Records and associated blogs"
def items(self):
"""each article in the feed"""
2024-01-26 11:00:26 +11:00
return models.Article.objects.order_by("-pubdate")[:20]
2024-01-04 11:54:56 +11:00
def item_title(self, item):
"""article title"""
return item.title
def item_description(self, item):
"""article description"""
return getattr(item, "description", None) or getattr(item, "summary", None)
def item_author_name(self, item):
"""article author"""
return item.author_name
2024-01-26 11:00:26 +11:00
def item_link(self, item):
"""item url"""
return item.url
2024-01-04 11:54:56 +11:00
def item_pubdate(self, item):
"""article publication date"""
return item.pubdate
2024-01-26 11:00:26 +11:00
def item_updateddate(self, item):
"""updated date"""
return item.updateddate
2024-01-04 11:54:56 +11:00
def item_categories(self, item):
"""article tags"""
categories = []
for tag in item.tags.all():
categories.append(tag.name)
return categories
class EventFeed(Feed):
2024-01-26 11:00:26 +11:00
"""Combined feed for all events and calls for papers"""
feed_type = Atom1Feed
link = "/events"
feed_url = "/events/feed"
feed_guid = f"https://{settings.DOMAIN}/events/feed"
2024-01-04 11:54:56 +11:00
title = "Aus GLAMR events"
description = "Australasian events for Galleries, Libraries, Archives, Museums, Records workers"
def items(self):
2024-01-26 11:00:26 +11:00
"""event and CFP items for the feed"""
events = models.Event.objects.filter(approved=True)
cfps = models.CallForPapers.objects.all()
result_list = sorted(
chain(events, cfps),
key=attrgetter("pubdate"),
reverse=True,
)
return result_list[:20]
2024-01-04 11:54:56 +11:00
def item_title(self, item):
2024-01-26 11:00:26 +11:00
"""event or CFP name"""
return item.name
2024-01-04 11:54:56 +11:00
def item_description(self, item):
2024-01-26 11:00:26 +11:00
"""description or details"""
return (
item.description
if hasattr(item, "description")
2024-06-24 12:23:01 +10:00
else item.details if hasattr(item, "details") else None
2024-01-26 11:00:26 +11:00
)
def item_link(self, item):
"""item url"""
return item.url if hasattr(item, "url") else item.event.url
2024-01-04 11:54:56 +11:00
def item_pubdate(self, item):
2024-01-26 11:00:26 +11:00
"""date event/CFP was registered"""
return item.pubdate
2024-01-04 11:54:56 +11:00
def item_categories(self, item):
"""event GLAMR category"""
2024-01-26 11:00:26 +11:00
if hasattr(item, "category"):
return [models.Category(item.category).label]
class EditionFeed(Feed):
"""Newsletter editions"""
feed_type = Atom1Feed
link = "/newsletter-editions"
feed_url = "/newsletter-editions/feed"
feed_guid = f"https://{settings.DOMAIN}/newsletter-editions/feed"
title = "Aus GLAMR Blog newsletter editions"
description = "Newsletters from Australasian Galleries, Libraries, Archives, Museums, Records and associated blogs"
def items(self):
"""each article in the feed"""
return models.Edition.objects.order_by("-pubdate")[:20]
def item_title(self, item):
"""article title"""
return item.title
def item_description(self, item):
"""article description"""
return item.description
def item_author_name(self, item):
"""article author"""
return item.author_name
def item_link(self, item):
"""item url"""
return item.url
def item_pubdate(self, item):
"""article publication date"""
return item.pubdate
def item_updateddate(self, item):
"""updated date"""
return item.updateddate
def item_categories(self, item):
"""newsletter category"""
return [models.Category(item.newsletter.category).label]
class CombinedFeed(Feed):
"""Combined Atom feed for everything"""
feed_type = Atom1Feed
link = "/"
feed_url = "/feed"
feed_guid = f"https://{settings.DOMAIN}/feed"
title = "Aus GLAMR"
description = "Latest news and opinion from Australasian Galleries, Libraries, Archives, Museums, Records professionals"
categories = [
"GLAM",
"GLAMR",
"Galleries",
"Libraries",
"Archives",
"Museums",
"Records",
]
feed_copyright = "Copyright is owned by individual authors"
ttl = 600
def items(self):
"""items for the feed"""
blog_objects = models.Blog.objects.filter(
approved=True, suspended=False, active=True
)
posts = models.Article.objects.all()
newsletters = models.Newsletter.objects.filter(approved=True, active=True)
editions = models.Edition.objects.all()
groups = models.Group.objects.filter(approved=True)
events = models.Event.objects.filter(approved=True)
cfps = models.CallForPapers.objects.all()
result_list = sorted(
chain(blog_objects, posts, newsletters, editions, groups, events, cfps),
key=attrgetter("pubdate"),
reverse=True,
)
return result_list[:30]
def item_title(self, item):
"""title or name"""
return item.name if hasattr(item, "name") else item.title
def item_description(self, item):
"""description"""
return (
item.description
if hasattr(item, "description")
2024-06-24 12:23:01 +10:00
else item.details if hasattr(item, "details") else None
2024-01-26 11:00:26 +11:00
)
def item_link(self, item):
"""item url"""
return item.url if hasattr(item, "url") else item.event.url
def item_guid(self, item):
"""guid"""
return item.url if hasattr(item, "url") else f"{item.event.url}-cfp-{item.id}"
def item_author_name(self, item):
"""author"""
return getattr(item, "author_name", None)
def item_pubdate(self, item):
"""date item was published"""
pubdate = getattr(item, "pubdate", None)
return pubdate
def item_updateddate(self, item):
"""updated date"""
if hasattr(item, "updateddate"):
return item.updateddate
if hasattr(item, "pubdate"):
return item.pubdate
return None
def item_categories(self, item):
"""GLAMR category or tags"""
if hasattr(item, "category"):
return [models.Category(item.category).label]
2024-01-26 11:00:26 +11:00
if hasattr(item, "tags"):
categories = []
for tag in item.tags.all():
categories.append(tag.name)
return categories
2024-01-26 11:00:26 +11:00
return None