def test_latest_post_date(self): """ Test that both the published and updated dates are considered when determining the latest post date. """ # this feed has a `published` element with the latest date response = self.client.get('/syndication/atom/') feed = minidom.parseString(response.content).firstChild updated = feed.getElementsByTagName('updated')[0].firstChild.wholeText d = Entry.objects.latest('published').published ltz = tzinfo.LocalTimezone(d) latest_published = rfc3339_date(d.replace(tzinfo=ltz)) self.assertEqual(updated, latest_published) # this feed has an `updated` element with the latest date response = self.client.get('/syndication/latest/') feed = minidom.parseString(response.content).firstChild updated = feed.getElementsByTagName('updated')[0].firstChild.wholeText d = Entry.objects.exclude(pk=5).latest('updated').updated ltz = tzinfo.LocalTimezone(d) latest_updated = rfc3339_date(d.replace(tzinfo=ltz)) self.assertEqual(updated, latest_updated)
def get_feed(self, obj, request): current_site = Site.objects.get_current() link = self._Feed__get_dynamic_attr('link', obj) link = add_domain(current_site.domain, link) feed = self.feed_type( title=self._Feed__get_dynamic_attr('title', obj), link=link, description=self._Feed__get_dynamic_attr('description', obj), ) title_tmp = None if self.title_template is not None: try: title_tmp = template.loader.get_template(self.title_template) except template.TemplateDoesNotExist: pass description_tmp = None if self.description_template is not None: try: description_tmp = template.loader.get_template(self.description_template) except template.TemplateDoesNotExist: pass for item in self._Feed__get_dynamic_attr('items', obj): if title_tmp is not None: title = title_tmp.render( template.RequestContext(request, { 'obj': item, 'site': current_site })) else: title = self._Feed__get_dynamic_attr('item_title', item) if description_tmp is not None: description = description_tmp.render( template.RequestContext(request, { 'obj': item, 'site': current_site })) else: description = self._Feed__get_dynamic_attr('item_description', item) link = add_domain( current_site.domain, self._Feed__get_dynamic_attr('item_link', item), ) pubdate = self._Feed__get_dynamic_attr('item_pubdate', item) if pubdate and not hasattr(pubdate, 'tzinfo'): ltz = tzinfo.LocalTimezone(pubdate) pubdate = pubdate.replace(tzinfo=ltz) feed.add_item( title=title, link=link, description=description, unique_id=self._Feed__get_dynamic_attr('item_guid', item, link), pubdate=pubdate, comment_status=self._Feed__get_dynamic_attr('item_comment_status', item, 'open'), comments=self._get_comments(item) ) return feed
def test_naive_datetime_conversion(self): """ Test that datetimes are correctly converted to the local time zone. """ # Naive date times passed in get converted to the local time zone, so # check the recived zone offset against the local offset. response = self.client.get('/syndication/feeds/naive-dates/') doc = minidom.parseString(response.content) updated = doc.getElementsByTagName('updated')[0].firstChild.wholeText tz = tzinfo.LocalTimezone(datetime.datetime.now()) now = datetime.datetime.now(tz) self.assertEqual(updated[-6:], str(now)[-6:])
def po_header(self): localtime = tzinfo.LocalTimezone( datetime.now() ) now = datetime.now(localtime).isoformat() po_header = render_to_string('i18n/po_header.txt', { 'creation_date': now, 'revision_date': now, 'last_translator' : u'anonymous', 'last_translator_email' : u'*****@*****.**', 'language_team' : u'anonymous', 'language_team_email': u'*****@*****.**', }) return po_header
def test_naive_datetime_conversion(self): """ Test that datetimes are correctly converted to the local time zone. """ # Naive date times passed in get converted to the local time zone, so # check the recived zone offset against the local offset. response = self.client.get('/syndication/naive-dates/') doc = minidom.parseString(response.content) updated = doc.getElementsByTagName('updated')[0].firstChild.wholeText d = Entry.objects.latest('date').date ltz = tzinfo.LocalTimezone(d) latest = rfc3339_date(d.replace(tzinfo=ltz)) self.assertEqual(updated, latest)
def get_feed(self, obj, request): """ Returns a feedgenerator.DefaultFeed object, fully populated, for this feed. Raises FeedDoesNotExist for invalid parameters. """ current_site = get_current_site(request) link = self.__get_dynamic_attr('link', obj) link = add_domain(current_site.domain, link, request.is_secure()) feed = self.feed_type( title=self.__get_dynamic_attr('title', obj), subtitle=self.__get_dynamic_attr('subtitle', obj), link=link, description=self.__get_dynamic_attr('description', obj), language=settings.LANGUAGE_CODE, feed_url=add_domain( current_site.domain, self.__get_dynamic_attr('feed_url', obj) or request.path, request.is_secure(), ), author_name=self.__get_dynamic_attr('author_name', obj), author_link=self.__get_dynamic_attr('author_link', obj), author_email=self.__get_dynamic_attr('author_email', obj), categories=self.__get_dynamic_attr('categories', obj), feed_copyright=self.__get_dynamic_attr('feed_copyright', obj), feed_guid=self.__get_dynamic_attr('feed_guid', obj), ttl=self.__get_dynamic_attr('ttl', obj), **self.feed_extra_kwargs(obj)) title_tmp = None if self.title_template is not None: try: title_tmp = loader.get_template(self.title_template) except TemplateDoesNotExist: pass description_tmp = None if self.description_template is not None: try: description_tmp = loader.get_template( self.description_template) except TemplateDoesNotExist: pass for item in self.__get_dynamic_attr('items', obj): if title_tmp is not None: title = title_tmp.render( RequestContext(request, { 'obj': item, 'site': current_site })) else: title = self.__get_dynamic_attr('item_title', item) if description_tmp is not None: description = description_tmp.render( RequestContext(request, { 'obj': item, 'site': current_site })) else: description = self.__get_dynamic_attr('item_description', item) link = add_domain( current_site.domain, self.__get_dynamic_attr('item_link', item), request.is_secure(), ) enc = None enc_url = self.__get_dynamic_attr('item_enclosure_url', item) if enc_url: enc = feedgenerator.Enclosure( url=smart_text(enc_url), length=smart_text( self.__get_dynamic_attr('item_enclosure_length', item)), mime_type=smart_text( self.__get_dynamic_attr('item_enclosure_mime_type', item))) author_name = self.__get_dynamic_attr('item_author_name', item) if author_name is not None: author_email = self.__get_dynamic_attr('item_author_email', item) author_link = self.__get_dynamic_attr('item_author_link', item) else: author_email = author_link = None pubdate = self.__get_dynamic_attr('item_pubdate', item) if pubdate and is_naive(pubdate): ltz = tzinfo.LocalTimezone(pubdate) pubdate = pubdate.replace(tzinfo=ltz) feed.add_item( title=title, link=link, description=description, unique_id=self.__get_dynamic_attr('item_guid', item, link), enclosure=enc, pubdate=pubdate, author_name=author_name, author_email=author_email, author_link=author_link, categories=self.__get_dynamic_attr('item_categories', item), item_copyright=self.__get_dynamic_attr('item_copyright', item), **self.item_extra_kwargs(item)) return feed
def test_rss2_feed(self): """ Test the structure and content of feeds generated by Rss201rev2Feed. """ response = self.client.get('/syndication/rss2/') doc = minidom.parseString(response.content) # Making sure there's only 1 `rss` element and that the correct # RSS version was specified. feed_elem = doc.getElementsByTagName('rss') self.assertEqual(len(feed_elem), 1) feed = feed_elem[0] self.assertEqual(feed.getAttribute('version'), '2.0') # Making sure there's only one `channel` element w/in the # `rss` element. chan_elem = feed.getElementsByTagName('channel') self.assertEqual(len(chan_elem), 1) chan = chan_elem[0] # Find the last build date d = Entry.objects.latest('date').date ltz = tzinfo.LocalTimezone(d) last_build_date = rfc2822_date(d.replace(tzinfo=ltz)) self.assertChildNodes(chan, ['title', 'link', 'description', 'language', 'lastBuildDate', 'item', 'atom:link', 'ttl', 'copyright', 'category']) self.assertChildNodeContent(chan, { 'title': 'My blog', 'description': 'A more thorough description of my blog.', 'link': 'http://example.com/blog/', 'language': 'en', 'lastBuildDate': last_build_date, #'atom:link': '', 'ttl': '600', 'copyright': 'Copyright (c) 2007, Sally Smith', }) self.assertCategories(chan, ['python', 'django']); # Ensure the content of the channel is correct self.assertChildNodeContent(chan, { 'title': 'My blog', 'link': 'http://example.com/blog/', }) # Check feed_url is passed self.assertEqual( chan.getElementsByTagName('atom:link')[0].getAttribute('href'), 'http://example.com/syndication/rss2/' ) # Find the pubdate of the first feed item d = Entry.objects.get(pk=1).date ltz = tzinfo.LocalTimezone(d) pub_date = rfc2822_date(d.replace(tzinfo=ltz)) items = chan.getElementsByTagName('item') self.assertEqual(len(items), Entry.objects.count()) self.assertChildNodeContent(items[0], { 'title': 'My first entry', 'description': 'Overridden description: My first entry', 'link': 'http://example.com/blog/1/', 'guid': 'http://example.com/blog/1/', 'pubDate': pub_date, 'author': '[email protected] (Sally Smith)', }) self.assertCategories(items[0], ['python', 'testing']); for item in items: self.assertChildNodes(item, ['title', 'link', 'description', 'guid', 'category', 'pubDate', 'author'])
def populate_feed(self, feed, items, request): """Populates a :class:`django.utils.feedgenerator.DefaultFeed` instance as is returned by :meth:`get_feed` with the passed-in ``items``.""" if self.item_title_template: title_template = DjangoTemplate(self.item_title_template.code) else: title_template = None if self.item_description_template: description_template = DjangoTemplate( self.item_description_template.code) else: description_template = None node = request.node try: current_site = Site.objects.get_current() except Site.DoesNotExist: current_site = RequestSite(request) if self.feed_length is not None: items = items[:self.feed_length] for item in items: if title_template is not None: title = title_template.render( RequestContext(request, {'obj': item})) else: title = self.__get_dynamic_attr('item_title', item) if description_template is not None: description = description_template.render( RequestContext(request, {'obj': item})) else: description = self.__get_dynamic_attr('item_description', item) link = node.construct_url(self.reverse(obj=item), with_domain=True, request=request, secure=request.is_secure()) enc = None enc_url = self.__get_dynamic_attr('item_enclosure_url', item) if enc_url: enc = feedgenerator.Enclosure( url=smart_unicode( add_domain(current_site.domain, enc_url, request.is_secure())), length=smart_unicode( self.__get_dynamic_attr('item_enclosure_length', item)), mime_type=smart_unicode( self.__get_dynamic_attr('item_enclosure_mime_type', item))) author_name = self.__get_dynamic_attr('item_author_name', item) if author_name is not None: author_email = self.__get_dynamic_attr('item_author_email', item) author_link = self.__get_dynamic_attr('item_author_link', item) else: author_email = author_link = None pubdate = self.__get_dynamic_attr('item_pubdate', item) if pubdate and not pubdate.tzinfo: ltz = tzinfo.LocalTimezone(pubdate) pubdate = pubdate.replace(tzinfo=ltz) feed.add_item( title=title, link=link, description=description, unique_id=self.__get_dynamic_attr('item_guid', item, link), enclosure=enc, pubdate=pubdate, author_name=author_name, author_email=author_email, author_link=author_link, categories=self.__get_dynamic_attr('item_categories', item), item_copyright=self.__get_dynamic_attr('item_copyright', item), **self.item_extra_kwargs(item))