Changes between Version 7 and Version 8 of UsingFeedsInCode


Ignore:
Timestamp:
Jul 20, 2006, 4:39:56 PM (15 years ago)
Author:
nick
Comment:

Django example

Legend:

Unmodified
Added
Removed
Modified
  • UsingFeedsInCode

    v7 v8  
    66
    77== PHP ==
     8
     9''' to follow '''
    810
    911== Ruby On Rails ==
     
    118120
    119121== Django (Python) ==
     122
     123An example for grabbing the nodes in a Category, and outputting the nodes titles, links and lat+long follows.
     124
     125''Note: this assumes an app called '''og''' has been set up''
     126
     127in og/models/og.py, have this:
     128{{{from django.core import meta
     129
     130# Class to return the Atom stuff
     131class ogatom(meta.Model):
     132    def __repr__(self): return "An OpenGuides Atom parser"
     133
     134    # Test method
     135    def _module_hello():
     136        return "hello"
     137    def _module_get_entries():
     138        from xml.dom import minidom
     139        from xml.dom import EMPTY_NAMESPACE
     140        import urllib
     141
     142        # Grab the feed
     143        url = "http://cotswolds.openguides.org/wiki/wiki.cgi?action=index;index_type=category;index_value=Pubs;format=atom"
     144        contents = urllib.urlopen(url)
     145
     146        # XML Parse
     147        doc = minidom.parse(contents)
     148        doc.normalize()
     149
     150        # Grab out interesting bits
     151        main_title = doc.getElementsByTagName(u"title")[0].firstChild.data
     152        entries_xml = doc.getElementsByTagName(u"entry")
     153
     154        # We want title, link, and geo
     155        entries = []
     156        for entry in entries_xml:
     157            title = entry.getElementsByTagName(u"title")[0].firstChild.data
     158            link_tag = entry.getElementsByTagName(u"link")[0]
     159            link = link_tag.getAttribute(u"href")
     160            e = { 'title': title, 'link':link }
     161
     162            lat_tags = entry.getElementsByTagName("geo:lat")
     163            long_tags = entry.getElementsByTagName("geo:long")
     164            if len(lat_tags) > 0:
     165                e['lat'] = lat_tags[0].firstChild.data
     166            if len(long_tags) > 0:
     167                e['long'] = long_tags[0].firstChild.data
     168            entries.append(e)
     169
     170        return entries
     171}}}
     172
     173and in og/views.py have this:
     174{{{
     175# Pre 0.90
     176from django.utils.httpwrappers import HttpResponse
     177# 0.90 onwards
     178#from django.http import HttpResponse
     179
     180from django.models import og
     181
     182def index(request):
     183    entries = og.ogatoms.get_entries()
     184    content = "<h1>Entries from feed:</h1>\n"
     185    for entry in entries:
     186        content += "<div>\n"
     187        content += "<b><a href='%s'>%s</a></b>\n" % (entry['link'],entry['title'])
     188        if entry.has_key("lat"):
     189            content += "<br /><span style='margin-left: 20px'>Latitude: %s</span
     190" % entry['lat']
     191        if entry.has_key("long"):
     192            content += "<br /><span style='margin-left: 20px'>Longitude: %s</span" % entry['long']
     193        content += "</div>\n"
     194    return HttpResponse(content)
     195}}}
     196
     197Finally wire up in urls.py
    120198
    121199== !JavaScript ==