summaryrefslogtreecommitdiffstats
path: root/coffin/contrib/syndication/feeds.py
blob: f8f0701f9cf451f881397ae0ef4bb8185530dbe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from django.contrib.syndication.feeds import *       # merge modules

import sys
from django.contrib.syndication.feeds import Feed as DjangoFeed
from coffin.template import loader as coffin_loader


class Feed(DjangoFeed):
    """A ``Feed`` implementation that renders it's title and
    description templates using Jinja2.

    Unfortunately, Django's base ``Feed`` class is not very extensible
    in this respect at all. For a real solution, we'd have to essentially
    have to duplicate the whole class. So for now, we use this terrible
    non-thread safe hack.

    Another, somewhat crazy option would be:
        * Render the templates ourselves through Jinja2 (possible
          introduce new attributes to avoid having to rewrite the
          existing ones).
        * Make the rendered result available to Django/the superclass by
          using a custom template loader using a prefix, say
          "feed:<myproject.app.views.MyFeed>". The loader would simply
          return the Jinja-rendered template (escaped), the Django template
          mechanism would find no nodes and just pass the output through.
    Possible even worse than this though.
    """

    def get_feed(self, *args, **kwargs):
        parent_module = sys.modules[DjangoFeed.__module__]
        old_loader = parent_module.loader
        parent_module.loader = coffin_loader
        try:
            return super(Feed, self).get_feed(*args, **kwargs)
        finally:
            parent_module.loader = old_loader