Le 20 déc. 07 à 10:27, Daniel de la Cuesta a écrit :
> Hi all,
>
> I want to add the geo tag (with latitude and longitude) to each item
> of my feeds:
>
> <georss:point>45.256 -71.92</georss:point>
> I am using the feeds framework:
> class LatestEvents(Feed): title = "Latest Events" link
> = "/events/" def items(self): return
> Event.objects.order_by('-start_date')[:5]
> Is there any way to add the new node to each item returned by
> "items()"
> Thank you.
Daniel,
Attached is my current solution which is not really elegant but works.
That's why I cross post to the developers mailing-list, maybe we can
find a better way to handle that? There is a lot of duplicate code in
my solution (and maybe there is a better one?, let me know) but I
could easily generate a patch against the trunk for that.
Btw, I had noticed a bug in the SyndicationFeed class of django
feedgenerator, language is the only parameter where force_unicode is
applied and it returns u"None". This "conversion" cause problem in the
"if self.feed['language'] is not None:" test. If you replace
force_unicode by to_unicode it works. I'd submitted a bug and a patch
on Trac, see #6303.
While we are at feeds, I got a "cosmetic" question. It seems that each
generated feed in Django is not highlighted by Firefox when you read
the source. Is there a reason to that? I can't find a way to find the
cause.
Thanks,
David
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---
from django.utils.xmlutils import SimplerXMLGenerator
from django.utils.feedgenerator import SyndicationFeed, Atom1Feed, rfc3339_date, get_tag_uri
class GeoSyndicationFeed(SyndicationFeed):
def add_item(self, title, link, description, author_email=None,
author_name=None, author_link=None, pubdate=None, comments=None,
unique_id=None, enclosure=None, categories=(), item_copyright=None,
ttl=None, georss=()):
"""
Adds an item to the feed. All args are expected to be Python Unicode
objects except pubdate, which is a datetime.datetime object, and
enclosure, which is an instance of the Enclosure class.
"""
super(GeoSyndicationFeed, self).add_item(title=title, link=link,
description=description,
author_email=author_email, author_name=author_name,
author_link=author_link, pubdate=pubdate, comments=comments,
unique_id=unique_id, enclosure=enclosure, categories=categories,
item_copyright=item_copyright, ttl=ttl)
self.items[-1]['georss'] = georss and u"%s %s" % georss or None
class GeoAtom1Feed(Atom1Feed, GeoSyndicationFeed):
def write(self, outfile, encoding):
handler = SimplerXMLGenerator(outfile, encoding)
handler.startDocument()
if self.feed['language'] != u'None':
handler.startElement(u"feed", {
u"xmlns": self.ns,
u"xml:lang": self.feed['language'],
u"xmlns:georss": u"http://www.georss.org/georss",
u"xmlns:gml": u"http://www.opengis.net/gml"})
else:
handler.startElement(u"feed", {
u"xmlns": self.ns,
u"xmlns:georss": u"http://www.georss.org/georss",
u"xmlns:gml": u"http://www.opengis.net/gml"})
handler.addQuickElement(u"title", self.feed['title'])
handler.addQuickElement(u"link", "", {u"rel": u"alternate", u"href": self.feed['link']})
if self.feed['feed_url'] is not None:
handler.addQuickElement(u"link", "", {u"rel": u"self", u"href": self.feed['feed_url']})
handler.addQuickElement(u"id", self.feed['id'])
handler.addQuickElement(u"updated", rfc3339_date(self.latest_post_date()).decode('ascii'))
if self.feed['author_name'] is not None:
handler.startElement(u"author", {})
handler.addQuickElement(u"name", self.feed['author_name'])
if self.feed['author_email'] is not None:
handler.addQuickElement(u"email", self.feed['author_email'])
if self.feed['author_link'] is not None:
handler.addQuickElement(u"uri", self.feed['author_link'])
handler.endElement(u"author")
if self.feed['subtitle'] is not None:
handler.addQuickElement(u"subtitle", self.feed['subtitle'])
for cat in self.feed['categories']:
handler.addQuickElement(u"category", "", {u"term": cat})
if self.feed['feed_copyright'] is not None:
handler.addQuickElement(u"rights", self.feed['feed_copyright'])
self.write_items(handler)
handler.endElement(u"feed")
def write_items(self, handler):
for item in self.items:
handler.startElement(u"entry", {})
handler.addQuickElement(u"title", item['title'])
handler.addQuickElement(u"link", u"", {u"href": item['link'], u"rel": u"alternate"})
if item['pubdate'] is not None:
handler.addQuickElement(u"updated", rfc3339_date(item['pubdate']).decode('ascii'))
# Author information.
if item['author_name'] is not None:
handler.startElement(u"author", {})
handler.addQuickElement(u"name", item['author_name'])
if item['author_email'] is not None:
handler.addQuickElement(u"email", item['author_email'])
if item['author_link'] is not None:
handler.addQuickElement(u"uri", item['author_link'])
handler.endElement(u"author")
# Unique ID.
if item['unique_id'] is not None:
unique_id = item['unique_id']
else:
unique_id = get_tag_uri(item['link'], item['pubdate'])
handler.addQuickElement(u"id", unique_id)
# Summary.
if item['description'] is not None:
handler.addQuickElement(u"summary", item['description'], {u"type": u"html"})
# Enclosure.
if item['enclosure'] is not None:
handler.addQuickElement(u"link", '',
{u"rel": u"enclosure",
u"href": item['enclosure'].url,
u"length": item['enclosure'].length,
u"type": item['enclosure'].mime_type})
# Categories.
for cat in item['categories']:
handler.addQuickElement(u"category", u"", {u"term": cat})
# Rights.
if item['item_copyright'] is not None:
handler.addQuickElement(u"rights", item['item_copyright'])
# GeoRSS
if item['georss'] is not None:
handler.startElement(u"georss:where", {})
handler.startElement(u"gml:Point", {})
handler.addQuickElement(u"gml:pos", item['georss'])
handler.endElement(u"gml:Point")
handler.endElement(u"georss:where")
handler.endElement(u"entry")