14 |
14 |
# You should have received a copy of the GNU Affero General Public License
|
15 |
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16 |
16 |
|
|
17 |
import feedparser
|
|
18 |
import hashlib
|
17 |
19 |
import json
|
|
20 |
import requests
|
18 |
21 |
|
19 |
22 |
from django.conf import settings
|
20 |
23 |
from django.contrib.auth.models import Group
|
21 |
24 |
from django.contrib.contenttypes.models import ContentType
|
|
25 |
from django.core.cache import cache
|
22 |
26 |
from django.core.exceptions import ObjectDoesNotExist
|
23 |
27 |
from django.core import serializers
|
24 |
28 |
from django.db import models
|
... | ... | |
34 |
38 |
from .library import register_cell_class, get_cell_classes, get_cell_class
|
35 |
39 |
|
36 |
40 |
from combo import utils
|
|
41 |
from combo.utils import NothingInCacheException
|
37 |
42 |
|
38 |
43 |
|
39 |
44 |
def element_is_visible(element, user=None):
|
... | ... | |
469 |
474 |
if self.anchor:
|
470 |
475 |
context['url'] += '#' + self.anchor
|
471 |
476 |
return super(LinkCell, self).render(context)
|
|
477 |
|
|
478 |
|
|
479 |
@register_cell_class
|
|
480 |
class FeedCell(CellBase):
|
|
481 |
url = models.URLField(_('URL'), blank=True)
|
|
482 |
|
|
483 |
template_name = 'combo/feed-cell.html'
|
|
484 |
|
|
485 |
class Meta:
|
|
486 |
verbose_name = _('RSS/Atom Feed')
|
|
487 |
|
|
488 |
def render(self, context):
|
|
489 |
cache_key = hashlib.md5(self.url).hexdigest()
|
|
490 |
feed_content = cache.get(cache_key)
|
|
491 |
if not context.get('ajax') and feed_content is None:
|
|
492 |
raise NothingInCacheException()
|
|
493 |
if not feed_content:
|
|
494 |
feed_response = requests.get(self.url)
|
|
495 |
if feed_response.status_code == 200:
|
|
496 |
feed_content = feed_response.content
|
|
497 |
cache.set(cache_key, feed_content, 600)
|
|
498 |
if feed_content:
|
|
499 |
context['feed'] = feedparser.parse(feed_content)
|
|
500 |
return super(FeedCell, self).render(context)
|