website/hub/services/feeds.py

82 lines
2.8 KiB
Python
Raw Normal View History

2025-07-17 10:53:05 +02:00
"""
RSS Feeds for the Servala website
"""
from django.contrib.syndication.views import Feed
from django.urls import reverse
from django.utils.feedgenerator import Rss201rev2Feed
from django.utils.html import strip_tags
2025-07-17 11:00:20 +02:00
from django.conf import settings
from django.contrib.sites.models import Site
2025-07-17 10:53:05 +02:00
from .models import Article
class ArticleRSSFeed(Feed):
"""RSS feed for published articles"""
title = "Servala Articles"
link = "/articles/"
description = "Latest articles about cloud services, consulting partners, and technology insights from Servala"
feed_type = Rss201rev2Feed
def items(self):
"""Return the latest 20 published articles"""
return Article.objects.filter(is_published=True).order_by("-article_date")[:20]
def item_title(self, item):
"""Return the article title"""
return item.title
def item_description(self, item):
"""Return the article excerpt with 'Read more' link"""
2025-07-17 11:00:20 +02:00
# Get the current site domain for absolute URLs
try:
current_site = Site.objects.get_current()
domain = current_site.domain
protocol = "https" if getattr(settings, "USE_TLS", True) else "http"
base_url = f"{protocol}://{domain}"
except:
# Fallback if Site framework is not configured
base_url = "https://servala.com"
# Use the excerpt and add a proper HTML read more link
2025-07-17 10:53:05 +02:00
excerpt = strip_tags(item.excerpt)
2025-07-17 11:00:20 +02:00
article_url = f"{base_url}{item.get_absolute_url()}"
# Return HTML content for the RSS description
return f'{excerpt} <a href="{article_url}">Read more...</a>'
2025-07-17 10:53:05 +02:00
def item_link(self, item):
"""Return the link to the article detail page"""
return item.get_absolute_url()
def item_guid(self, item):
"""Return a unique identifier for the item"""
return f"article-{item.id}"
def item_pubdate(self, item):
"""Return the publication date"""
# Convert date to datetime for RSS compatibility
from datetime import datetime, time
from django.utils import timezone
# Combine the date with midnight time and make it timezone-aware
dt = datetime.combine(item.article_date, time.min)
return timezone.make_aware(dt, timezone.get_current_timezone())
def item_author_name(self, item):
"""Return the author name"""
return item.author.get_full_name() or item.author.username
def item_categories(self, item):
"""Return categories for the article"""
categories = []
# Add meta keywords as categories if available
if item.meta_keywords:
keywords = [keyword.strip() for keyword in item.meta_keywords.split(",")]
categories.extend(keywords)
return categories