From 1778fa5227257f9e73b6628f7458b011bb33072b 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 1/2] misc: add a rss/atom feed cell (#6842) --- combo/data/models.py | 26 ++++++++++++++++++++++++++ combo/public/templates/combo/feed-cell.html | 9 +++++++++ debian/control | 1 + requirements.txt | 1 + setup.py | 1 + 5 files changed, 38 insertions(+) create mode 100644 combo/public/templates/combo/feed-cell.html diff --git a/combo/data/models.py b/combo/data/models.py index 241772f..ba43691 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, 600) + context['feed'] = feedparser.parse(feed_content) + return super(FeedCell, self).render(context) diff --git a/combo/public/templates/combo/feed-cell.html b/combo/public/templates/combo/feed-cell.html new file mode 100644 index 0000000..501850c --- /dev/null +++ b/combo/public/templates/combo/feed-cell.html @@ -0,0 +1,9 @@ +{% load i18n %} +{% for entry in feed.entries %} +{% if entry.link %} +

{{entry.title}}

+ {% else %} +

{{entry.title}}

+{% endif %} +{% if entry.description %}

{{entry.description}}

{% endif %} +{% endfor %} 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