From 9381e607cd902272a7ffebfe46f006eaa281216a Mon Sep 17 00:00:00 2001 From: Ben Sturmfels Date: Fri, 13 Oct 2023 12:41:20 +1100 Subject: [PATCH] Automatically generate feed enclosure URL prefix --- www/podjango/feeds.py | 18 +++++++++++++++--- www/podjango/urls.py | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/www/podjango/feeds.py b/www/podjango/feeds.py index d66fbb35..cc0c3011 100644 --- a/www/podjango/feeds.py +++ b/www/podjango/feeds.py @@ -17,7 +17,8 @@ # "AGPLv3". If not, see . # -from django.contrib.syndication.views import Feed +from django.contrib.sites.shortcuts import get_current_site +from django.contrib.syndication.views import add_domain, Feed from django.utils.feedgenerator import Rss201rev2Feed #from podjango.apps.staff.models import Person from podjango.apps.cast.models import Cast @@ -181,8 +182,19 @@ class CastFeed(CastFeedBase): author_name = "Free as in Freedom" title_template = "feeds/podcast_title.html" description_template = "feeds/podcast_description.html" + + def get_feed(self, obj, request): + # Enclosure (media) URLs don't automatically get the protocol and + # domain, but these are required for the podcast to work + # properly. added. This provides current_site and is_secure to the feed + # so that we have it in context for use in `item_enclosure_url`. + self.current_site = get_current_site(request) + self.is_secure = request.is_secure() + return super().get_feed(obj, request) + def items(self): return Cast.objects.filter(pub_date__lte=datetime.now()).order_by('-pub_date') + def item_pubdate(self, item): return item.pub_date @@ -221,14 +233,14 @@ class CastFeed(CastFeedBase): class Mp3CastFeed(CastFeed): def item_enclosure_mime_type(self): return "audio/mpeg" def item_enclosure_url(self, item): - return item.mp3_path + return add_domain(self.current_site.domain, item.mp3_path, self.is_secure) def item_enclosure_length(self, item): return item.mp3_length class OggCastFeed(CastFeed): def item_enclosure_mime_type(self): return "audio/ogg" def item_enclosure_url(self, item): - return item.ogg_path + return add_domain(self.current_site.domain, item.ogg_path, self.is_secure) def item_enclosure_length(self, item): return item.ogg_length diff --git a/www/podjango/urls.py b/www/podjango/urls.py index c4d8b0c6..86a9440d 100644 --- a/www/podjango/urls.py +++ b/www/podjango/urls.py @@ -33,8 +33,8 @@ app_name = 'podjango' urlpatterns = [ url(r'^$', frontpage.view), url(r'^cast/', include('podjango.apps.cast.urls')), - url(r'^feeds/cast-ogg/$', Mp3CastFeed(), name='feed-ogg'), - url(r'^feeds/cast-mp3/$', OggCastFeed(), name='feed-mp3'), + url(r'^feeds/cast-ogg/$', OggCastFeed(), name='feed-ogg'), + url(r'^feeds/cast-mp3/$', Mp3CastFeed(), name='feed-mp3'), url(r'^feeds/$', view, name='feeds'), ]