From 01712cd94d430770fd6e28a0bc36e14cb12b4954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 24 Aug 2015 14:32:06 +0200 Subject: [PATCH] misc: add a rss/atom feed cell (#6842) --- combo/data/models.py | 26 ++++++++++++++++++++++++++ debian/control | 1 + requirements.txt | 1 + setup.py | 1 + 4 files changed, 29 insertions(+) diff --git a/combo/data/models.py b/combo/data/models.py index 241772f..0bd638c 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -14,11 +14,14 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import feedparser import json +import requests from django.conf import settings from django.contrib.auth.models import Group from django.contrib.contenttypes.models import ContentType +from django.core.cache import cache from django.core.exceptions import ObjectDoesNotExist from django.core import serializers from django.db import models @@ -34,6 +37,7 @@ import cmsplugin_blurp.utils from .library import register_cell_class, get_cell_classes, get_cell_class from combo import utils +from combo.utils import NothingInCacheException def element_is_visible(element, user=None): @@ -469,3 +473,25 @@ class LinkCell(CellBase): if self.anchor: context['url'] += '#' + self.anchor return super(LinkCell, self).render(context) + + +@register_cell_class +class FeedCell(CellBase): + url = models.URLField(_('URL'), blank=True) + + template_name = 'combo/feed-cell.html' + + class Meta: + verbose_name = _('RSS/Atom Feed') + + def render(self, context): + cache_key = self.url + feed_content = cache.get(cache_key) + if not context.get('ajax') and feed_content is None: + raise NothingInCacheException() + feed_response = requests.get(self.url) + if feed_response.status_code == 200: + feed_content = feed_response.content + cache.set(cache_key, feed_content, 3600) + context['feed'] = feedparser.parse(feed_content) + return super(FeedCell, self).render(context) diff --git a/debian/control b/debian/control index 51ffe60..1d91bca 100644 --- a/debian/control +++ b/debian/control @@ -12,6 +12,7 @@ Depends: ${misc:Depends}, ${python:Depends}, python-django (>= 1.7), python-gadjo, python-requests, + python-feedparser, python-django-cmsplugin-blurp Recommends: python-django-mellon Description: Portal Management System (Python module) diff --git a/requirements.txt b/requirements.txt index 8a3cab7..d41f377 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ django-ckeditor django-cmsplugin-blurp django-jsonfield requests +feedparser diff --git a/setup.py b/setup.py index 272ecf3..b253c3b 100644 --- a/setup.py +++ b/setup.py @@ -106,6 +106,7 @@ setup( 'django-ckeditor', 'gadjo', 'django-cmsplugin-blurp', + 'feedparser' 'django-jsonfield', ], zip_safe=False, -- 2.5.0