Skip to content

Commit

Permalink
fix: rss
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-filicetti committed May 29, 2024
1 parent 4a27f93 commit 2ccb0db
Showing 1 changed file with 60 additions and 8 deletions.
68 changes: 60 additions & 8 deletions src/cms/publications/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Http404)
from django.template import Template, Context
from django.urls import reverse
from django.utils.feedgenerator import Rss201rev2Feed
from django.utils.translation import gettext_lazy as _

from cms.contexts.handlers import BaseContentHandler
Expand Down Expand Up @@ -122,30 +123,81 @@ def as_view(self):
return HttpResponse(template.render(context), status=200)


class ExtendedRSSFeed(Rss201rev2Feed):
"""
Create a type of RSS feed that has content:encoded elements.
"""
def root_attributes(self):
attrs = super(ExtendedRSSFeed, self).root_attributes()
# Because I'm adding a <content:encoded> field, I first need to declare
# the content namespace. For more information on how this works, check
# out: http://validator.w3.org/feed/docs/howto/declare_namespaces.html
attrs['xmlns:content'] = 'http://purl.org/rss/1.0/modules/content/'
return attrs

def add_item_elements(self, handler, item):
super(ExtendedRSSFeed, self).add_item_elements(handler, item)

if item['content_encoded']:
content = f"<content:encoded><![CDATA[{item['content_encoded']}]]></content:encoded>"
else:
content = "<content:encoded/>"
handler._write(content)


class PublicationRssHandler(BaseContentHandler, Feed):
feed_type = ExtendedRSSFeed
# description_template = 'feeds/list_detail_content_encoded.html'


def items(self):
query_params = publication_context_base_filter()
# category = self.request.GET.get('category')
# if category:
# query_params['publication__category__pk'] = category
return PublicationContext.objects.filter(webpath=self.page.webpath,
**query_params)[:10]
items = PublicationContext.objects\
.filter(webpath=self.page.webpath,
**query_params)\
.select_related('publication')[:10]
# i18n
lang = getattr(self.request, 'LANGUAGE_CODE', None)
if lang:
for item in items:
item.publication.translate_as(lang=lang)
return items

def item_title(self, item):
return item.publication.title

def item_description(self, item):
return item.publication.subheading

# def item_author_name(self, item):
# if (item.author.get_full_name()):
# return item.author.get_full_name()
# else:
# return item.author

def item_pubdate(self, item):
return item.date_start

def item_categories(self, item):
return item.publication.categories.values_list('name', flat=True)

def item_content_encoded(self, item):
return item.publication.content

def item_extra_kwargs(self, item):
return {'content_encoded': self.item_content_encoded(item)}

def as_view(self):
match_dict = self.match.groupdict()
self.page = Page.objects.filter(is_active=True,
webpath__site=self.website,
webpath__fullpath=match_dict.get('webpath', '/')).first()
webpath__fullpath=match_dict.get('webpath', '/'))\
.select_related('webpath','webpath__site').first()

if not self.page:
raise Http404('Unknown Web Page')
raise Http404()

self.title = self.page.webpath.name
self.title = f'{self.page.webpath.name} - {self.page.webpath.site.name}'
self.link = f"{self.page.webpath.fullpath}{CMS_PUBLICATION_LIST_PREFIX_PATH}"
self.description = f"{self.page.webpath.name} RSS feed"

Expand Down

0 comments on commit 2ccb0db

Please sign in to comment.