Automatically generate feed enclosure URL prefix

This commit is contained in:
Ben Sturmfels 2023-10-13 12:41:20 +11:00
parent 6393441126
commit 9381e607cd
Signed by: bsturmfels
GPG key ID: 023C05E2C9C068F0
2 changed files with 17 additions and 5 deletions

View file

@ -17,7 +17,8 @@
# "AGPLv3". If not, see <http://www.gnu.org/licenses/>. # "AGPLv3". If not, see <http://www.gnu.org/licenses/>.
# #
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 django.utils.feedgenerator import Rss201rev2Feed
#from podjango.apps.staff.models import Person #from podjango.apps.staff.models import Person
from podjango.apps.cast.models import Cast from podjango.apps.cast.models import Cast
@ -181,8 +182,19 @@ class CastFeed(CastFeedBase):
author_name = "Free as in Freedom" author_name = "Free as in Freedom"
title_template = "feeds/podcast_title.html" title_template = "feeds/podcast_title.html"
description_template = "feeds/podcast_description.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): def items(self):
return Cast.objects.filter(pub_date__lte=datetime.now()).order_by('-pub_date') return Cast.objects.filter(pub_date__lte=datetime.now()).order_by('-pub_date')
def item_pubdate(self, item): def item_pubdate(self, item):
return item.pub_date return item.pub_date
@ -221,14 +233,14 @@ class CastFeed(CastFeedBase):
class Mp3CastFeed(CastFeed): class Mp3CastFeed(CastFeed):
def item_enclosure_mime_type(self): return "audio/mpeg" def item_enclosure_mime_type(self): return "audio/mpeg"
def item_enclosure_url(self, item): 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): def item_enclosure_length(self, item):
return item.mp3_length return item.mp3_length
class OggCastFeed(CastFeed): class OggCastFeed(CastFeed):
def item_enclosure_mime_type(self): return "audio/ogg" def item_enclosure_mime_type(self): return "audio/ogg"
def item_enclosure_url(self, item): 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): def item_enclosure_length(self, item):
return item.ogg_length return item.ogg_length

View file

@ -33,8 +33,8 @@ app_name = 'podjango'
urlpatterns = [ urlpatterns = [
url(r'^$', frontpage.view), url(r'^$', frontpage.view),
url(r'^cast/', include('podjango.apps.cast.urls')), url(r'^cast/', include('podjango.apps.cast.urls')),
url(r'^feeds/cast-ogg/$', Mp3CastFeed(), name='feed-ogg'), url(r'^feeds/cast-ogg/$', OggCastFeed(), name='feed-ogg'),
url(r'^feeds/cast-mp3/$', OggCastFeed(), name='feed-mp3'), url(r'^feeds/cast-mp3/$', Mp3CastFeed(), name='feed-mp3'),
url(r'^feeds/$', view, name='feeds'), url(r'^feeds/$', view, name='feeds'),
] ]