Ticket #6533: syndication-optionaltemplates.diff
File syndication-optionaltemplates.diff, 5.1 KB (added by , 16 years ago) |
---|
-
django/contrib/syndication/feeds.py
1 1 from datetime import datetime, timedelta 2 2 from django.conf import settings 3 from django.contrib.sites.models import Site, RequestSite 3 4 from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist 4 from django.template import loader, Template, TemplateDoesNotExist5 from django.contrib.sites.models import Site, RequestSite 5 from django.template import RequestContext, loader, Template, \ 6 TemplateDoesNotExist 6 7 from django.utils import feedgenerator 8 from django.utils.encoding import smart_unicode, iri_to_uri 7 9 from django.utils.tzinfo import FixedOffset 8 from django.utils.encoding import smart_unicode, iri_to_uri9 from django.conf import settings10 from django.template import RequestContext11 10 12 11 def add_domain(domain, url): 13 12 if not (url.startswith('http://') or url.startswith('https://')): … … 32 31 self.feed_url = request.path 33 32 self.title_template_name = self.title_template or ('feeds/%s_title.html' % slug) 34 33 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) 35 38 36 39 def item_link(self, item): 37 40 try: 38 41 return item.get_absolute_url() 39 42 except AttributeError: 40 43 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) 41 61 42 62 def __get_dynamic_attr(self, attname, obj, default=None): 43 63 try: … … 77 97 except ObjectDoesNotExist: 78 98 raise FeedDoesNotExist 79 99 80 if Site._meta.installed:81 current_site = Site.objects.get_current()82 else:83 current_site = RequestSite(self.request)84 85 100 link = self.__get_dynamic_attr('link', obj) 86 link = add_domain( current_site.domain, link)101 link = add_domain(self.current_site.domain, link) 87 102 88 103 feed = self.feed_type( 89 104 title = self.__get_dynamic_attr('title', obj), … … 91 106 link = link, 92 107 description = self.__get_dynamic_attr('description', obj), 93 108 language = settings.LANGUAGE_CODE.decode(), 94 feed_url = add_domain( current_site.domain,109 feed_url = add_domain(self.current_site.domain, 95 110 self.__get_dynamic_attr('feed_url', obj)), 96 111 author_name = self.__get_dynamic_attr('author_name', obj), 97 112 author_link = self.__get_dynamic_attr('author_link', obj), … … 102 117 ttl = self.__get_dynamic_attr('ttl', obj), 103 118 ) 104 119 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 114 120 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)) 116 122 enc = None 117 123 enc_url = self.__get_dynamic_attr('item_enclosure_url', item) 118 124 if enc_url: … … 147 153 pubdate = pubdate.replace(tzinfo=FixedOffset(tzOffset)) 148 154 149 155 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), 151 157 link = link, 152 description = description_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})),158 description = self.__get_dynamic_attr('item_description', item), 153 159 unique_id = self.__get_dynamic_attr('item_guid', item, link), 154 160 enclosure = enc, 155 161 pubdate = pubdate,