Ticket #6533: syndication-optionaltemplates.diff

File syndication-optionaltemplates.diff, 5.1 KB (added by Julian Bez, 16 years ago)
  • django/contrib/syndication/feeds.py

     
    11from datetime import datetime, timedelta
    2 
     2from django.conf import settings
     3from django.contrib.sites.models import Site, RequestSite
    34from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
    4 from django.template import loader, Template, TemplateDoesNotExist
    5 from django.contrib.sites.models import Site, RequestSite
     5from django.template import RequestContext, loader, Template, \
     6    TemplateDoesNotExist
    67from django.utils import feedgenerator
     8from django.utils.encoding import smart_unicode, iri_to_uri
    79from django.utils.tzinfo import FixedOffset
    8 from django.utils.encoding import smart_unicode, iri_to_uri
    9 from django.conf import settings         
    10 from django.template import RequestContext
    1110
    1211def add_domain(domain, url):
    1312    if not (url.startswith('http://') or url.startswith('https://')):
     
    3231        self.feed_url = request.path
    3332        self.title_template_name = self.title_template or ('feeds/%s_title.html' % slug)
    3433        self.description_template_name = self.description_template or ('feeds/%s_description.html' % slug)
     34        if Site._meta.installed:
     35            self.current_site = Site.objects.get_current()
     36        else:
     37            self.current_site = RequestSite(self.request)
    3538
    3639    def item_link(self, item):
    3740        try:
    3841            return item.get_absolute_url()
    3942        except AttributeError:
    4043            raise ImproperlyConfigured, "Give your %s class a get_absolute_url() method, or define an item_link() method in your Feed class." % item.__class__.__name__
     44       
     45    def item_title(self, item):
     46        try:
     47            title_tmp = loader.get_template(self.title_template_name)
     48        except TemplateDoesNotExist:
     49            return unicode(item)
     50        title_rc = RequestContext(self.request, {'obj': item, 'site': self.current_site})
     51        title_rc.autoescape = False
     52        return title_tmp.render(title_rc)
     53   
     54    def item_description(self, item):
     55        try:
     56            description_tmp = loader.get_template(self.description_template_name)
     57        except TemplateDoesNotExist:
     58            return unicode(item)
     59        description_rc = RequestContext(self.request, {'obj': item, 'site': self.current_site})
     60        return description_tmp.render(description_rc)
    4161
    4262    def __get_dynamic_attr(self, attname, obj, default=None):
    4363        try:
     
    7797        except ObjectDoesNotExist:
    7898            raise FeedDoesNotExist
    7999
    80         if Site._meta.installed:
    81             current_site = Site.objects.get_current()
    82         else:
    83             current_site = RequestSite(self.request)
    84        
    85100        link = self.__get_dynamic_attr('link', obj)
    86         link = add_domain(current_site.domain, link)
     101        link = add_domain(self.current_site.domain, link)
    87102
    88103        feed = self.feed_type(
    89104            title = self.__get_dynamic_attr('title', obj),
     
    91106            link = link,
    92107            description = self.__get_dynamic_attr('description', obj),
    93108            language = settings.LANGUAGE_CODE.decode(),
    94             feed_url = add_domain(current_site.domain,
     109            feed_url = add_domain(self.current_site.domain,
    95110                                  self.__get_dynamic_attr('feed_url', obj)),
    96111            author_name = self.__get_dynamic_attr('author_name', obj),
    97112            author_link = self.__get_dynamic_attr('author_link', obj),
     
    102117            ttl = self.__get_dynamic_attr('ttl', obj),
    103118        )
    104119
    105         try:
    106             title_tmp = loader.get_template(self.title_template_name)
    107         except TemplateDoesNotExist:
    108             title_tmp = Template('{{ obj }}')
    109         try:
    110             description_tmp = loader.get_template(self.description_template_name)
    111         except TemplateDoesNotExist:
    112             description_tmp = Template('{{ obj }}')
    113 
    114120        for item in self.__get_dynamic_attr('items', obj):
    115             link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item))
     121            link = add_domain(self.current_site.domain, self.__get_dynamic_attr('item_link', item))
    116122            enc = None
    117123            enc_url = self.__get_dynamic_attr('item_enclosure_url', item)
    118124            if enc_url:
     
    147153                pubdate = pubdate.replace(tzinfo=FixedOffset(tzOffset))
    148154
    149155            feed.add_item(
    150                 title = title_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})),
     156                title = self.__get_dynamic_attr('item_title', item),
    151157                link = link,
    152                 description = description_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})),
     158                description = self.__get_dynamic_attr('item_description', item),
    153159                unique_id = self.__get_dynamic_attr('item_guid', item, link),
    154160                enclosure = enc,
    155161                pubdate = pubdate,
Back to Top