I'm thinking of using Django as the front-end to an experimental
Atom-enabled 'store' as outlined by Joe Gregorio at
http://www.xml.com/pub/a/2005/09/21/atom-store-web-database.html
I'm a real newbie to Django, having spent the last 7 years in
Zope-land, and am looking at Django as an adjunct to the many many Zope
systems we run here (I just had reason to play around with
PageTemplates and PythonScripts this morning and the frustration was
intense!)
Any thoughts gratefully received.
Tone
Thing is, there seems to be something amiss;
from django.utils import feedgenerator
import datetime
f = feedgenerator.Atom1Feed(
title = u"My Weblog",
link = u"http://www.example.com/",
description = u"In which I write about what I ate today.",
categories = ('sex', 'drugs', 'rock and roll'),
language = u"en",
)
myenclosure = feedgenerator.Enclosure(
url =
u'https://resourcebank.ncl.ac.uk/resource/access/download/fa85c3c1-0ff5-466c-8cb1-83936d4b1b93/CPT_presentation_Case_B_-_Group_3.ppt',
length = u'1019821',
mime_type = u'application/powerpoint',
)
when = datetime.datetime(2005,11,24)
print when
f.add_item(title=u"Hot dog today",
link = u"http://www.example.com/entries/1/",
pubdate = feedgenerator.rfc3339_date(when),
description = u"<p>sort it out son!</p>",
enclosure = myenclosure,
unique_id = None,
)
print f.writeString('utf8')
The resourcebank link does not exist ;)
What comes out is this;
2005-11-24 00:00:00
--> ['__add__', '__class__', '__delattr__', '__doc__', '__eq__',
'__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
'__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__',
'__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__str__',
'__sub__', 'astimezone', 'combine', 'ctime', 'date', 'day', 'dst',
'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat',
'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'now',
'replace', 'resolution', 'second', 'strftime', 'time', 'timetuple',
'timetz', 'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp',
'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year']
--> ['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__ge__', '__getattribute__', '__getitem__',
'__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__',
'__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',
'__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count',
'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index',
'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind',
'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']
Traceback (most recent call last):
File "/Users/tonymcd/Projects/Python/atom/django_atom.py", line 29,
in ?
print f.writeString('utf8')
File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/utils/feedgenerator.py",
line 99, in writeString
self.write(s, encoding)
File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/utils/feedgenerator.py",
line 201, in write
handler.addQuickElement(u"updated",
rfc3339_date(self.latest_post_date()).decode('ascii'))
File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/utils/feedgenerator.py",
line 32, in rfc3339_date
return date.strftime('%Y-%m-%dT%H:%M:%SZ')
AttributeError: 'str' object has no attribute 'strftime'
The --> is a debug line put into feedgenerator.rfc3339
def rfc3339_date(date):
print "-->", dir(date)
return date.strftime('%Y-%m-%dT%H:%M:%SZ')
There's two outputs (first when rfc3339 is called in my code and second
when writeString('utf8') takes over).
Can anyone point out where I'm being completely stupid here?
-rob
It seems that the routine feedgenerator.writeString is sending rfc3399
a string rather than a datetime.datetime object, which it gets from the
write method of Atom1Feed - the relevant bit is here...
handler.addQuickElement(u"updated",
rfc3339_date(self.latest_post_date()).decode('ascii'))
Which leads to this (with my debug prints);
def latest_post_date(self):
"""
Returns the latest item's pubdate. If none of them have a
pubdate,
this returns the current date/time.
"""
updates = [i['pubdate'] for i in self.items if i['pubdate'] is
not None]
print "UPDATES", updates
if len(updates) > 0:
updates.sort()
print "updates:", type(updates[-1])
return updates[-1]
else:
print "datetime.datetime.now()", datetime.datetime.now()
return datetime.datetime.now()
What happens now?
Well, this;
UPDATES ['2005-11-25T11:51:01Z']
updates: <type 'str'>
Which leads to the blow-up.
I would have thought that other people would have seen this which is
why I re-iterate "what stupid thing am I doing here?" ;)
many thanks
Tone
Okay, but I meant the add_item method wants a datetime. From your
original post:
f.add_item(title=u"Hot dog today",
link = u"http://www.example.com/entries/1/",
pubdate = feedgenerator.rfc3339_date(when),
description = u"<p>sort it out son!</p>",
enclosure = myenclosure,
unique_id = None,
)
Looks like pubdate is supposed to be a datetime object, but clearly the
result of feedgenerator.rfc3339_date() is a string. Try this instead:
f.add_item(title=u"Hot dog today",
link = u"http://www.example.com/entries/1/",
pubdate = when,
description = u"<p>sort it out son!</p>",
enclosure = myenclosure,
unique_id = None,
)
Note that I haven't used this feed stuff yet, so I'm not sure if this
is ultimately what you want to do - but at least I think it will solve
the problem you're having!
-rob
Thanks very much indeed Rob, it works fine now!
cheers
Tone