Flex Reader on Google App Engine

This entry is part of 3 in the series Flex on GAE

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.

Figure 4: The add feed dialog.

addFeed small thumb

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()

Series Navigation
0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
Share
This entry was posted in programming and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">