Ticket #6618: syndication-optionaltemplates.diff

File syndication-optionaltemplates.diff, 4.3 KB (added by Julian Bez, 16 years ago)

Makes templates optional + includes the fix for #6533

  • django/contrib/syndication/feeds.py

     
    2929        self.feed_url = request.path
    3030        self.title_template_name = self.title_template or ('feeds/%s_title.html' % slug)
    3131        self.description_template_name = self.description_template or ('feeds/%s_description.html' % slug)
     32        if Site._meta.installed:
     33            self.current_site = Site.objects.get_current()
     34        else:
     35            self.current_site = RequestSite(self.request)
    3236
    3337    def item_link(self, item):
    3438        try:
    3539            return item.get_absolute_url()
    3640        except AttributeError:
    3741            raise ImproperlyConfigured, "Give your %s class a get_absolute_url() method, or define an item_link() method in your Feed class." % item.__class__.__name__
     42       
     43    def item_title(self, item):
     44        try:
     45            title_tmp = loader.get_template(self.title_template_name)
     46        except TemplateDoesNotExist:
     47            return unicode(item)
     48        title_rc = RequestContext(self.request, {'obj': item, 'site': self.current_site})
     49        title_rc.autoescape = False
     50        return title_tmp.render(title_rc)
     51   
     52    def item_description(self, item):
     53        try:
     54            description_tmp = loader.get_template(self.description_template_name)
     55        except TemplateDoesNotExist:
     56            return unicode(item)
     57        description_rc = RequestContext(self.request, {'obj': item, 'site': self.current_site})
     58        description_rc.autoescape = False
     59        return description_tmp.render(description_rc)
    3860
    3961    def __get_dynamic_attr(self, attname, obj, default=None):
    4062        try:
     
    7496        except ObjectDoesNotExist:
    7597            raise FeedDoesNotExist
    7698
    77         if Site._meta.installed:
    78             current_site = Site.objects.get_current()
    79         else:
    80             current_site = RequestSite(self.request)
    81 
    8299        link = self.__get_dynamic_attr('link', obj)
    83         link = add_domain(current_site.domain, link)
     100        link = add_domain(self.current_site.domain, link)
    84101
    85102        feed = self.feed_type(
    86103            title = self.__get_dynamic_attr('title', obj),
     
    88105            link = link,
    89106            description = self.__get_dynamic_attr('description', obj),
    90107            language = settings.LANGUAGE_CODE.decode(),
    91             feed_url = add_domain(current_site.domain,
     108            feed_url = add_domain(self.current_site.domain,
    92109                                  self.__get_dynamic_attr('feed_url', obj)),
    93110            author_name = self.__get_dynamic_attr('author_name', obj),
    94111            author_link = self.__get_dynamic_attr('author_link', obj),
     
    99116            ttl = self.__get_dynamic_attr('ttl', obj),
    100117        )
    101118
    102         try:
    103             title_tmp = loader.get_template(self.title_template_name)
    104         except TemplateDoesNotExist:
    105             title_tmp = Template('{{ obj }}')
    106         try:
    107             description_tmp = loader.get_template(self.description_template_name)
    108         except TemplateDoesNotExist:
    109             description_tmp = Template('{{ obj }}')
    110 
    111119        for item in self.__get_dynamic_attr('items', obj):
    112             link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item))
     120            link = add_domain(self.current_site.domain, self.__get_dynamic_attr('item_link', item))
    113121            enc = None
    114122            enc_url = self.__get_dynamic_attr('item_enclosure_url', item)
    115123            if enc_url:
     
    125133            else:
    126134                author_email = author_link = None
    127135            feed.add_item(
    128                 title = title_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})),
     136                title = self.__get_dynamic_attr('item_title', item),
    129137                link = link,
    130                 description = description_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})),
     138                description = self.__get_dynamic_attr('item_description', item),
    131139                unique_id = self.__get_dynamic_attr('item_guid', item, link),
    132140                enclosure = enc,
    133141                pubdate = self.__get_dynamic_attr('item_pubdate', item),
Back to Top