2.2 The Feed and EntryComment model
Entities are described by so called models. A model is a Python class subclassed from the Google App Engine Model class. The attributes of a Model are instances of Property classes. For each data type a property class is defined. In my models of Listing 10, I use StringProperty, IntegerProperty and the more exotic UserProperty. The UserProperty represents a Google user account.
Listing 10: The Feed and EntryComment models
1 2 3 4 5 6 7 8 9 10 11 12 | from google.appengine.ext import db class Feed(db.Model): link = db.StringProperty(required=True) title = db.StringProperty() class EntryComment(db.Model): comments = db.StringProperty() rating = db.IntegerProperty() link = db.StringProperty(required=True) title = db.StringProperty() user = db.UserProperty(required=True) |
2.3 Adding a feed
When the user clicks on the “Add Feed” button a small window appears. The window contains a form that lets you add feeds to the datastore.
Listing 11: Add Feed dialog – AddFeed.mxml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?xml version="1.0" encoding="utf-8"?> <iic:ClosableTitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:iic="net.ivanidris.flex.components.*" title="Add Feed"> <mx:Script> <![CDATA[ private function onAdd():void { var url:String = '/addFeed.py?q=' + escape(addUrl.text); var request:URLRequest = new URLRequest(url); var loader:URLLoader = new URLLoader(request); loader.addEventListener("complete", onServerResponse); } private function onServerResponse(e:Event):void { removeMe(); } ]]> </mx:Script> <mx:Form> <mx:FormItem> <mx:Label text="Fill in a feed URL"/> </mx:FormItem> <mx:FormItem> <mx:TextInput id="addUrl"/> <mx:Button id="addSubscription" label="Add" click="onAdd();"/> </mx:FormItem> <mx:FormItem> <mx:Label text="For instance http://digg.com/rss/containertechnology.xml"/> </mx:FormItem> </mx:Form> </iic:ClosableTitleWindow> |
Listing 11 demonstrates the Flex Form layout container. A single Form container can contain several FormItem containers. In the TextInput the exact URL of the feed must be specified, and then the form can be submitted by clicking the button below. Upon form submission a request is sent to addFeed.py in Listing 12 and the popup is closed. addFeed.py does a lookup for the specified feed utilizing the Google AJAX Feed API. Eventually, the script puts the result in a Feed object to be stored in the datastore.
Listing 12: Adding a feed with Python – addFeed.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import cgi import urllib from net.ivanidris.flexreader import Feed from google.appengine.api import urlfetch from django.utils import simplejson form = cgi.FieldStorage() query = urllib.quote(form.getfirst("q", "")) url = "http://ajax.googleapis.com/ajax/services/feed/lookup?v=1.0&num=100&q=" + query result = urlfetch.fetch(url) if result.status_code == 200: json = simplejson.loads(result.content.strip()) if json['responseStatus'] == 200: if json['responseData']['url'] == json['responseData']['query']: feed = Feed.Feed(key_name = json['responseData']['url'], link = json['responseData']['url']) feed.put() |
More From ivanidris
ivanidris Recommends
- Seven Characteristics of Stepper Motors | Solder In The Veins (Solder In The Veins)
- 10 Questions with Facebook Research Engineer – Andrei Alexandrescu (Server-Side Magazine)

