| | 65 | def get_feed_kwargs(self, obj): |
|---|
| | 66 | """ |
|---|
| | 67 | Returns the keyword arguments to instatiate the `SyndicationFeed` |
|---|
| | 68 | class specified by the `feed_type` attribute. |
|---|
| | 69 | """ |
|---|
| | 70 | return { |
|---|
| | 71 | 'title' : self.__get_dynamic_attr('title', obj), |
|---|
| | 72 | 'subtitle' : self.__get_dynamic_attr('subtitle', obj), |
|---|
| | 73 | 'link' : add_domain(self.current_site.domain, |
|---|
| | 74 | self.__get_dynamic_attr('link', obj)), |
|---|
| | 75 | 'description' : self.__get_dynamic_attr('description', obj), |
|---|
| | 76 | 'language' : settings.LANGUAGE_CODE.decode(), |
|---|
| | 77 | 'feed_url' : add_domain(self.current_site.domain, |
|---|
| | 78 | self.__get_dynamic_attr('feed_url', obj)), |
|---|
| | 79 | 'author_name' : self.__get_dynamic_attr('author_name', obj), |
|---|
| | 80 | 'author_link' : self.__get_dynamic_attr('author_link', obj), |
|---|
| | 81 | 'author_email' : self.__get_dynamic_attr('author_email', obj), |
|---|
| | 82 | 'categories' : self.__get_dynamic_attr('categories', obj), |
|---|
| | 83 | 'feed_copyright' : self.__get_dynamic_attr('feed_copyright', obj), |
|---|
| | 84 | 'feed_guid' : self.__get_dynamic_attr('feed_guid', obj), |
|---|
| | 85 | 'ttl' : self.__get_dynamic_attr('ttl', obj), |
|---|
| | 86 | } |
|---|
| | 87 | |
|---|
| | 88 | def get_item_kwargs(self, item): |
|---|
| | 89 | """ |
|---|
| | 90 | Returns the keyword arguments to pass to the `add_item` method of the |
|---|
| | 91 | `SyndicationFeed` instance (specified by the `feed_type` attribute). |
|---|
| | 92 | """ |
|---|
| | 93 | link = add_domain(self.current_site.domain, self.__get_dynamic_attr('item_link', item)) |
|---|
| | 94 | enc = None |
|---|
| | 95 | enc_url = self.__get_dynamic_attr('item_enclosure_url', item) |
|---|
| | 96 | if enc_url: |
|---|
| | 97 | enc = feedgenerator.Enclosure( |
|---|
| | 98 | url = smart_unicode(enc_url), |
|---|
| | 99 | length = smart_unicode(self.__get_dynamic_attr('item_enclosure_length', item)), |
|---|
| | 100 | mime_type = smart_unicode(self.__get_dynamic_attr('item_enclosure_mime_type', item)) |
|---|
| | 101 | ) |
|---|
| | 102 | author_name = self.__get_dynamic_attr('item_author_name', item) |
|---|
| | 103 | if author_name is not None: |
|---|
| | 104 | author_email = self.__get_dynamic_attr('item_author_email', item) |
|---|
| | 105 | author_link = self.__get_dynamic_attr('item_author_link', item) |
|---|
| | 106 | else: |
|---|
| | 107 | author_email = author_link = None |
|---|
| | 108 | |
|---|
| | 109 | pubdate = self.__get_dynamic_attr('item_pubdate', item) |
|---|
| | 110 | if pubdate: |
|---|
| | 111 | now = datetime.now() |
|---|
| | 112 | utcnow = datetime.utcnow() |
|---|
| | 113 | |
|---|
| | 114 | # Must always subtract smaller time from larger time here. |
|---|
| | 115 | if utcnow > now: |
|---|
| | 116 | sign = -1 |
|---|
| | 117 | tzDifference = (utcnow - now) |
|---|
| | 118 | else: |
|---|
| | 119 | sign = 1 |
|---|
| | 120 | tzDifference = (now - utcnow) |
|---|
| | 121 | |
|---|
| | 122 | # Round the timezone offset to the nearest half hour. |
|---|
| | 123 | tzOffsetMinutes = sign * ((tzDifference.seconds / 60 + 15) / 30) * 30 |
|---|
| | 124 | tzOffset = timedelta(minutes=tzOffsetMinutes) |
|---|
| | 125 | pubdate = pubdate.replace(tzinfo=FixedOffset(tzOffset)) |
|---|
| | 126 | |
|---|
| | 127 | return {'title' : self.title_tmp.render(RequestContext(self.request, {'obj': item, 'site': self.current_site})), |
|---|
| | 128 | 'link' : link, |
|---|
| | 129 | 'description' : self.description_tmp.render(RequestContext(self.request, {'obj': item, 'site': self.current_site})), |
|---|
| | 130 | 'unique_id' : self.__get_dynamic_attr('item_guid', item, link), |
|---|
| | 131 | 'enclosure' : enc, |
|---|
| | 132 | 'pubdate' : pubdate, |
|---|
| | 133 | 'author_name' : author_name, |
|---|
| | 134 | 'author_email' : author_email, |
|---|
| | 135 | 'author_link' : author_link, |
|---|
| | 136 | 'categories' : self.__get_dynamic_attr('item_categories', item), |
|---|
| | 137 | 'item_copyright' : self.__get_dynamic_attr('item_copyright', item), |
|---|
| | 138 | } |
|---|
| | 139 | |
|---|
| 88 | | feed = self.feed_type( |
|---|
| 89 | | title = self.__get_dynamic_attr('title', obj), |
|---|
| 90 | | subtitle = self.__get_dynamic_attr('subtitle', obj), |
|---|
| 91 | | link = link, |
|---|
| 92 | | description = self.__get_dynamic_attr('description', obj), |
|---|
| 93 | | language = settings.LANGUAGE_CODE.decode(), |
|---|
| 94 | | feed_url = add_domain(current_site.domain, |
|---|
| 95 | | self.__get_dynamic_attr('feed_url', obj)), |
|---|
| 96 | | author_name = self.__get_dynamic_attr('author_name', obj), |
|---|
| 97 | | author_link = self.__get_dynamic_attr('author_link', obj), |
|---|
| 98 | | author_email = self.__get_dynamic_attr('author_email', obj), |
|---|
| 99 | | categories = self.__get_dynamic_attr('categories', obj), |
|---|
| 100 | | feed_copyright = self.__get_dynamic_attr('feed_copyright', obj), |
|---|
| 101 | | feed_guid = self.__get_dynamic_attr('feed_guid', obj), |
|---|
| 102 | | ttl = self.__get_dynamic_attr('ttl', obj), |
|---|
| 103 | | ) |
|---|
| | 161 | # Initializing the feed. |
|---|
| | 162 | feed = self.feed_type(**self.get_feed_kwargs(obj)) |
|---|
| 115 | | link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item)) |
|---|
| 116 | | enc = None |
|---|
| 117 | | enc_url = self.__get_dynamic_attr('item_enclosure_url', item) |
|---|
| 118 | | if enc_url: |
|---|
| 119 | | enc = feedgenerator.Enclosure( |
|---|
| 120 | | url = smart_unicode(enc_url), |
|---|
| 121 | | length = smart_unicode(self.__get_dynamic_attr('item_enclosure_length', item)), |
|---|
| 122 | | mime_type = smart_unicode(self.__get_dynamic_attr('item_enclosure_mime_type', item)) |
|---|
| 123 | | ) |
|---|
| 124 | | author_name = self.__get_dynamic_attr('item_author_name', item) |
|---|
| 125 | | if author_name is not None: |
|---|
| 126 | | author_email = self.__get_dynamic_attr('item_author_email', item) |
|---|
| 127 | | author_link = self.__get_dynamic_attr('item_author_link', item) |
|---|
| 128 | | else: |
|---|
| 129 | | author_email = author_link = None |
|---|
| 130 | | |
|---|
| 131 | | pubdate = self.__get_dynamic_attr('item_pubdate', item) |
|---|
| 132 | | if pubdate: |
|---|
| 133 | | now = datetime.now() |
|---|
| 134 | | utcnow = datetime.utcnow() |
|---|
| 135 | | |
|---|
| 136 | | # Must always subtract smaller time from larger time here. |
|---|
| 137 | | if utcnow > now: |
|---|
| 138 | | sign = -1 |
|---|
| 139 | | tzDifference = (utcnow - now) |
|---|
| 140 | | else: |
|---|
| 141 | | sign = 1 |
|---|
| 142 | | tzDifference = (now - utcnow) |
|---|
| 143 | | |
|---|
| 144 | | # Round the timezone offset to the nearest half hour. |
|---|
| 145 | | tzOffsetMinutes = sign * ((tzDifference.seconds / 60 + 15) / 30) * 30 |
|---|
| 146 | | tzOffset = timedelta(minutes=tzOffsetMinutes) |
|---|
| 147 | | pubdate = pubdate.replace(tzinfo=FixedOffset(tzOffset)) |
|---|
| 148 | | |
|---|
| 149 | | feed.add_item( |
|---|
| 150 | | title = title_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})), |
|---|
| 151 | | link = link, |
|---|
| 152 | | description = description_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})), |
|---|
| 153 | | unique_id = self.__get_dynamic_attr('item_guid', item, link), |
|---|
| 154 | | enclosure = enc, |
|---|
| 155 | | pubdate = pubdate, |
|---|
| 156 | | author_name = author_name, |
|---|
| 157 | | author_email = author_email, |
|---|
| 158 | | author_link = author_link, |
|---|
| 159 | | categories = self.__get_dynamic_attr('item_categories', item), |
|---|
| 160 | | item_copyright = self.__get_dynamic_attr('item_copyright', item), |
|---|
| 161 | | ) |
|---|
| | 176 | feed.add_item(**self.get_item_kwargs(item)) |
|---|