Skip to content
Browse files

Merge pull request #23 from DragonDM/master

Atom pub fix.
  • Loading branch information...
2 parents b939454 + 374100a commit e87f235dea04cff34b7f4b2ad1f56e5ee08845c0 @Cerberus98 Cerberus98 committed Sep 19, 2011
Showing with 39 additions and 8 deletions.
  1. +4 −3 yagi/notifier/atompub.py
  2. +35 −5 yagi/serializer/atom.py
View
7 yagi/notifier/atompub.py
@@ -21,16 +21,17 @@ def notify(notifications):
if auth_user and auth_key:
conn.add_credentials(auth_user, auth_key)
for notification in notifications:
- yagi.serializer.atom.dumps(notifications)
- notification_body = yagi.serializer.atom.dumps([notification])
+ #yagi.serializer.atom.dumps(notifications)
+ notification_body = yagi.serializer.atom.dump_item(notification)
headers = {'Content-Type': 'application/atom+xml'}
conn.follow_all_redirects = True
puburl = topic_url(notification['event_type'])
+ LOG.debug('ATOM Pub post to: %s *******\n%s\n=======' % (puburl, notification_body))
try:
resp, content = conn.request(puburl,
'POST', body=notification_body, headers=headers)
if resp.status != 201:
- LOG.error('ATOM Pub resource create failed for %s' % puburl)
+ LOG.error('ATOM Pub resource create failed for %s Status: %s, %s' % (puburl, resp.status, content) )
except Exception, e:
LOG.error('Error sending notification to ATOM Pub resource%s\n\n%s'
View
40 yagi/serializer/atom.py
@@ -32,11 +32,16 @@ class PagedFeed(feedgenerator.Atom1Feed):
# Get it to care about content elements
def write_items(self, handler):
for item in self.items:
- handler.startElement(u"entry", self.item_attributes(item))
- self.add_item_elements(handler, item)
- handler.addQuickElement(u"content", json.dumps(item['contents']),
- dict(type='application/json'))
- handler.endElement(u"entry")
+ self.write_item(handler, item)
+
+ # Get it to care about content elements
+ def write_item(self, handler, item, root=False):
+ handler.startElement(u"entry",
+ self.root_attributes() if root else {})
+ self.add_item_elements(handler, item)
+ handler.addQuickElement(u"content", json.dumps(item['contents']),
+ dict(type='application/json'))
+ handler.endElement(u"entry")
def add_root_elements(self, handler):
super(PagedFeed, self).add_root_elements(handler)
@@ -73,3 +78,28 @@ def dumps(entities, previous_page=None, next_page=None):
description=unicode(entity['event_type']),
contents=entity['content'])
return feed.writeString('utf-8')
+
+
+def dump_item(entity):
+ """Serializes a single dictionary as an ATOM entry"""
+ from StringIO import StringIO
+
+ outfile = StringIO()
+ handler = feedgenerator.SimplerXMLGenerator(outfile, 'utf-8')
+ handler.startDocument()
+ title = unicode(yagi.config.get('event_feed', 'feed_title'))
+ feed = PagedFeed(
+ title=title,
+ link=_entity_url(),
+ feed_url=_entity_url(),
+ description=title,
+ language=u'en',
+ previous_page_url=None,
+ next_page_url=None)
+
+ feed.add_item(title=unicode(entity['event_type']),
+ link=_entity_link(entity['id'], entity['event_type']),
+ description=unicode(entity['event_type']),
+ contents=entity['content'])
+ feed.write_item(handler, feed.items[0], root=True)
+ return outfile.getvalue()

0 comments on commit e87f235

Please sign in to comment.
Something went wrong with that request. Please try again.