Skip to content

Commit

Permalink
[py3] Ported django.utils.feedgenerator.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaugustin committed Aug 7, 2012
1 parent 02e6b64 commit 7e01e53
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions django/utils/feedgenerator.py
Expand Up @@ -31,6 +31,8 @@
from django.utils.xmlutils import SimplerXMLGenerator
from django.utils.encoding import force_text, iri_to_uri
from django.utils import datetime_safe
from django.utils import six
from django.utils.six import StringIO
from django.utils.timezone import is_aware

def rfc2822_date(date):
Expand All @@ -44,25 +46,29 @@ def rfc2822_date(date):
dow = days[date.weekday()]
month = months[date.month - 1]
time_str = date.strftime('%s, %%d %s %%Y %%H:%%M:%%S ' % (dow, month))
if not six.PY3: # strftime returns a byte string in Python 2
time_str = time_str.decode('utf-8')
if is_aware(date):
offset = date.tzinfo.utcoffset(date)
timezone = (offset.days * 24 * 60) + (offset.seconds // 60)
hour, minute = divmod(timezone, 60)
return time_str + "%+03d%02d" % (hour, minute)
return time_str + '%+03d%02d' % (hour, minute)
else:
return time_str + '-0000'

def rfc3339_date(date):
# Support datetime objects older than 1900
date = datetime_safe.new_datetime(date)
time_str = date.strftime('%Y-%m-%dT%H:%M:%S')
if not six.PY3: # strftime returns a byte string in Python 2
time_str = time_str.decode('utf-8')
if is_aware(date):
time_str = date.strftime('%Y-%m-%dT%H:%M:%S')
offset = date.tzinfo.utcoffset(date)
timezone = (offset.days * 24 * 60) + (offset.seconds // 60)
hour, minute = divmod(timezone, 60)
return time_str + "%+03d:%02d" % (hour, minute)
return time_str + '%+03d:%02d' % (hour, minute)
else:
return date.strftime('%Y-%m-%dT%H:%M:%SZ')
return time_str + 'Z'

def get_tag_uri(url, date):
"""
Expand Down Expand Up @@ -178,8 +184,7 @@ def writeString(self, encoding):
"""
Returns the feed in the given encoding as a string.
"""
from io import BytesIO
s = BytesIO()
s = StringIO()
self.write(s, encoding)
return s.getvalue()

Expand Down Expand Up @@ -237,7 +242,7 @@ def add_root_elements(self, handler):
handler.addQuickElement("category", cat)
if self.feed['feed_copyright'] is not None:
handler.addQuickElement("copyright", self.feed['feed_copyright'])
handler.addQuickElement("lastBuildDate", rfc2822_date(self.latest_post_date()).decode('utf-8'))
handler.addQuickElement("lastBuildDate", rfc2822_date(self.latest_post_date()))
if self.feed['ttl'] is not None:
handler.addQuickElement("ttl", self.feed['ttl'])

Expand Down Expand Up @@ -271,7 +276,7 @@ def add_item_elements(self, handler, item):
handler.addQuickElement("dc:creator", item["author_name"], {"xmlns:dc": "http://purl.org/dc/elements/1.1/"})

if item['pubdate'] is not None:
handler.addQuickElement("pubDate", rfc2822_date(item['pubdate']).decode('utf-8'))
handler.addQuickElement("pubDate", rfc2822_date(item['pubdate']))
if item['comments'] is not None:
handler.addQuickElement("comments", item['comments'])
if item['unique_id'] is not None:
Expand Down Expand Up @@ -314,7 +319,7 @@ def add_root_elements(self, handler):
if self.feed['feed_url'] is not None:
handler.addQuickElement("link", "", {"rel": "self", "href": self.feed['feed_url']})
handler.addQuickElement("id", self.feed['id'])
handler.addQuickElement("updated", rfc3339_date(self.latest_post_date()).decode('utf-8'))
handler.addQuickElement("updated", rfc3339_date(self.latest_post_date()))
if self.feed['author_name'] is not None:
handler.startElement("author", {})
handler.addQuickElement("name", self.feed['author_name'])
Expand All @@ -340,7 +345,7 @@ def add_item_elements(self, handler, item):
handler.addQuickElement("title", item['title'])
handler.addQuickElement("link", "", {"href": item['link'], "rel": "alternate"})
if item['pubdate'] is not None:
handler.addQuickElement("updated", rfc3339_date(item['pubdate']).decode('utf-8'))
handler.addQuickElement("updated", rfc3339_date(item['pubdate']))

# Author information.
if item['author_name'] is not None:
Expand Down

0 comments on commit 7e01e53

Please sign in to comment.