<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Identity Interface &#187; maps</title>
	<atom:link href="http://jonshern.com/category/maps/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonshern.com</link>
	<description>I should know better</description>
	<lastBuildDate>Sat, 21 Jan 2012 23:10:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting started with the Google App Engine.</title>
		<link>http://jonshern.com/2008/05/18/getting-started-with-the-google-app-engine/</link>
		<comments>http://jonshern.com/2008/05/18/getting-started-with-the-google-app-engine/#comments</comments>
		<pubDate>Sun, 18 May 2008 11:30:43 +0000</pubDate>
		<dc:creator>Jon Shern</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[maps]]></category>
		<category><![CDATA[technology]]></category>

		<guid isPermaLink="false">http://jonshern.com/?p=105</guid>
		<description><![CDATA[The tutorial goes over some basic concepts surrounding Google's App Engine Framework, demonstrates using the Google App Engine to store data, and using Django templates to create a GeoRss feed that is consumed by Google maps.
Setup your environment
I chose eclipse as my ide.
The nice thing about eclipse is if you add the lib directories of [...]]]></description>
			<content:encoded><![CDATA[<p>The tutorial goes over some basic concepts surrounding Google's App Engine Framework, demonstrates using the Google App Engine to store data, and using Django templates to create a GeoRss feed that is consumed by Google maps.</p>
<p><strong>Setup your environment</strong></p>
<p>I chose eclipse as my ide.<br />
The nice thing about eclipse is if you add the lib directories of whatever you are using (including the Google App Engine) you will get some intellisense.</p>
<p>Download the necessary components.<br />
<a href="https://www.google.com/accounts/ServiceLogin?service=ah&#038;continue=http://appengine.google.com/_ah/login%3Fcontinue%3Dhttp://appengine.google.com/&#038;ltmpl=ae&#038;sig=2441550b0617bb4eb9e7f8c3eb9e63b1">Google app engine</a><br />
<a href="http://www.eclipse.org/downloads/">Eclipse</a><br />
<a href="http://www.fabioz.com/pydev/manual_101_install.html">Installing Pydev</a><br />
The documentation helped, but the link was bad. I used http://pydev.sf.net/updates/.</p>
<p><strong>The guts of the python file</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">cgi</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> wsgiref.<span style="color: black;">handlers</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> google.<span style="color: black;">appengine</span>.<span style="color: black;">api</span> <span style="color: #ff7700;font-weight:bold;">import</span> users
<span style="color: #ff7700;font-weight:bold;">from</span> google.<span style="color: black;">appengine</span>.<span style="color: black;">ext</span> <span style="color: #ff7700;font-weight:bold;">import</span> webapp
<span style="color: #ff7700;font-weight:bold;">from</span> google.<span style="color: black;">appengine</span>.<span style="color: black;">ext</span> <span style="color: #ff7700;font-weight:bold;">import</span> db
<span style="color: #ff7700;font-weight:bold;">from</span> google.<span style="color: black;">appengine</span>.<span style="color: black;">ext</span>.<span style="color: black;">webapp</span> <span style="color: #ff7700;font-weight:bold;">import</span> template
&nbsp;
_DEBUG=<span style="color: #008000;">True</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Business<span style="color: black;">&#40;</span>db.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    ......
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    application = webapp.<span style="color: black;">WSGIApplication</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>
            <span style="color: black;">&#40;</span><span style="color: #483d8b;">'/'</span>,MainPage<span style="color: black;">&#41;</span>,
            <span style="color: black;">&#40;</span><span style="color: #483d8b;">'/createbusiness.do'</span>,BusinessSignup<span style="color: black;">&#41;</span>,
            <span style="color: black;">&#40;</span><span style="color: #483d8b;">'/georssfeed.xml'</span>,GeoRssFeed<span style="color: black;">&#41;</span>
            <span style="color: black;">&#93;</span>,debug=_DEBUG<span style="color: black;">&#41;</span>
    wsgiref.<span style="color: black;">handlers</span>.<span style="color: black;">CGIHandler</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">run</span><span style="color: black;">&#40;</span>application<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The main method is where we map our urls to the classes we have defined within the python file.<br />
Each class that handles requests should have a get or a post method.<br />
When a get or a post occurs it will be routed automagically to the appropriate method.</p>
<p><strong>Creating the table</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Business<span style="color: black;">&#40;</span>db.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    name = db.<span style="color: black;">StringProperty</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    description = db.<span style="color: black;">StringProperty</span><span style="color: black;">&#40;</span>multiline=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    url = db.<span style="color: black;">URLProperty</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    location = db.<span style="color: black;">StringProperty</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    latitude = db.<span style="color: black;">StringProperty</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    longitude = db.<span style="color: black;">StringProperty</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    address = db.<span style="color: black;">StringProperty</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    created = db.<span style="color: black;">DateTimeProperty</span><span style="color: black;">&#40;</span>auto_now_add=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Description can contain line breaks so we specify multiline=True<br />
Created is of type DateTime and has the property auto_now_add set to true<br />
created is set to the current time the first time the model instance is stored in the datastore, unless the property has already been assigned a value.</p>
<p>There is also an auto_now property that can be used to set the current time each time the record is created or updated. Useful for modified dates.</p>
<p><strong><br />
Handling the request</strong></p>
<p>In one of googles examples(Task List) they used a base class for the request.<br />
Here is my modified version.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> BaseRequestHandler<span style="color: black;">&#40;</span>webapp.<span style="color: black;">RequestHandler</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Supplies a common template generation function&quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> generate<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,template_name,template_values=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>:
        values = <span style="color: black;">&#123;</span>
                  <span style="color: #483d8b;">'request'</span>: <span style="color: #008000;">self</span>.<span style="color: black;">request</span>,
                  <span style="color: #483d8b;">'debug'</span>: <span style="color: #008000;">self</span>.<span style="color: black;">request</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'deb'</span><span style="color: black;">&#41;</span>,
                  <span style="color: #483d8b;">'application_name'</span>: <span style="color: #483d8b;">'Local Business Directory'</span>
                  <span style="color: black;">&#125;</span>
&nbsp;
        values.<span style="color: black;">update</span><span style="color: black;">&#40;</span>template_values<span style="color: black;">&#41;</span>
        directory = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span>
        path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>directory,<span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'templates'</span>,template_name<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">response</span>.<span style="color: black;">out</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>template.<span style="color: black;">render</span><span style="color: black;">&#40;</span>path,values,debug=_DEBUG<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This does a few nice things.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">        values = <span style="color: black;">&#123;</span>
                  <span style="color: #483d8b;">'request'</span>: <span style="color: #008000;">self</span>.<span style="color: black;">request</span>,
                  <span style="color: #483d8b;">'debug'</span>: <span style="color: #008000;">self</span>.<span style="color: black;">request</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'deb'</span><span style="color: black;">&#41;</span>,
                  <span style="color: #483d8b;">'application_name'</span>: <span style="color: #483d8b;">'Local Business Directory'</span>
                  <span style="color: black;">&#125;</span>
        values.<span style="color: black;">update</span><span style="color: black;">&#40;</span>template_values<span style="color: black;">&#41;</span></pre></div></div>

<p>This sets up an array of base values that will be passed into the template.<br />
In other methods that use base request, we will add other objects to this array. So our html templates can process data.<br />
The last line values.update is where the two arrays gets merged.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">        path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>directory,<span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'templates'</span>,template_name<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>In this application I created a templates folder to separate the html from the code. This line just adds the template_name to the /templates path.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #008000;">self</span>.<span style="color: black;">response</span>.<span style="color: black;">out</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>template.<span style="color: black;">render</span><span style="color: black;">&#40;</span>path,values,debug=_DEBUG<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>And finally<br />
Write the request out.</p>
<p>Using the BaseRequestHandler</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> MainPage<span style="color: black;">&#40;</span>BaseRequestHandler<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
&nbsp;
        <span style="color: #808080; font-style: italic;">#Get all of the businesses</span>
        businesses = Business.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">order</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'-created'</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">generate</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'index.html'</span>, <span style="color: black;">&#123;</span>
                                     <span style="color: #483d8b;">'businesses'</span>: businesses
                                     <span style="color: black;">&#125;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Here is a simple example of querying all of the businesses ordered by created date.<br />
We then call the generate method on the BaseRequestHandler, passing in our additional objects, along with the template name.</p>
<p><strong>Using Templates</strong><br />
The Google App Engine uses the Django templating engine.  W00t</p>
<p>The for loop</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> <span style="color: #ff7700;font-weight:bold;">for</span> athlete <span style="color: #ff7700;font-weight:bold;">in</span> athlete_list <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
    <span style="color: #66cc66;">&lt;</span>li<span style="color: #66cc66;">&gt;</span><span style="color: black;">&#123;</span><span style="color: black;">&#123;</span> athlete.<span style="color: black;">name</span> <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span><span style="color: #66cc66;">&lt;</span>/li<span style="color: #66cc66;">&gt;</span>
<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> endfor <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span></pre></div></div>

<p>The if statement(there are several varieties)</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> <span style="color: #ff7700;font-weight:bold;">if</span> athlete_list <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
    Number of athletes: <span style="color: black;">&#123;</span><span style="color: black;">&#123;</span> athlete_list|length <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
    No athletes.
<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> endif <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
&nbsp;
<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> ifequal <span style="color: #dc143c;">user</span>.<span style="color: #008000;">id</span> comment.<span style="color: black;">user_id</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
    ...
<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> endifequal <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span></pre></div></div>

<p>In the spirit of python, there are a lot of functions that Django gives you.<br />
Examples:<br />
timesince: Formats a date as the time since that date (e.g., “4 days, 6 hours”).<br />
phone2numeric: Converts a phone number (possibly containing letters) to its numerical equivalent. For example, '800-COLLECT' will be converted to '800-2655328'.</p>
<p><a href="http://www.djangoproject.com/documentation/templates/">More Information on Django templates</a></p>
<p>In order to display a list of businesses I am just using a simple for loop and creating a row each time.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;table&quot;&gt;
	&lt;table&gt;
		&lt;tr&gt;
			&lt;th&gt;Business Name&lt;/th&gt;
			&lt;th&gt;Address(Address, City, State)&lt;/th&gt;
			&lt;th&gt;Description&lt;/th&gt;
			&lt;th&gt;Url&lt;/th&gt;
			&lt;th&gt;Location&lt;/th&gt;
			&lt;th&gt;Latitude&lt;/th&gt;
			&lt;th&gt;Longitude&lt;/th&gt;
		&lt;/tr&gt;
		&lt;/tr&gt;
		{% for business in businesses %}
		&lt;tr id=&quot;row{{ forloop.counter}}&quot;&gt;
		&lt;td class=&quot;main&quot; 
		  &lt;div class=&quot;name&quot;&gt;{{ business.name }}&lt;/div&gt;
		&lt;/td&gt;
		&lt;td class=&quot;members&quot;&gt;
			{{ business.address }}
		&lt;/td&gt;
		&lt;td class=&quot;members&quot;&gt;
			{{ business.description }}
		&lt;/td&gt;
		&lt;td class=&quot;members&quot;&gt;
			{{ business.url }}
		&lt;/td&gt;
		&lt;td class=&quot;members&quot;&gt;
			{{ business.location }}
		&lt;/td&gt;
		&lt;td class=&quot;members&quot;&gt;
			{{ business.latitude }}
		&lt;/td&gt;
		&lt;td class=&quot;members&quot;&gt;
			{{ business.longitude }}
		&lt;/td&gt;
&nbsp;
		&lt;/tr&gt;
		{% endfor %}
	&lt;/table&gt;
&lt;/div&gt;</pre></div></div>

<p><strong>Entering Data</strong></p>
<p>Two pieces of code were necessary for this<br />
Plumbing in the python file</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> BusinessSignup<span style="color: black;">&#40;</span>webapp.<span style="color: black;">RequestHandler</span><span style="color: black;">&#41;</span>:    
    <span style="color: #ff7700;font-weight:bold;">def</span> post<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        business = Business<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        business.<span style="color: black;">name</span> = <span style="color: #008000;">self</span>.<span style="color: black;">request</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;txtBusinessName&quot;</span><span style="color: black;">&#41;</span>
        business.<span style="color: black;">address</span> = <span style="color: #008000;">self</span>.<span style="color: black;">request</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'txtAddress'</span><span style="color: black;">&#41;</span>
        business.<span style="color: black;">description</span> = <span style="color: #008000;">self</span>.<span style="color: black;">request</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'txtDescription'</span><span style="color: black;">&#41;</span>
        business.<span style="color: black;">url</span> = <span style="color: #008000;">self</span>.<span style="color: black;">request</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'txtUrl'</span><span style="color: black;">&#41;</span>
        business.<span style="color: black;">location</span> = <span style="color: #008000;">self</span>.<span style="color: black;">request</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'txtLocation'</span><span style="color: black;">&#41;</span>
        business.<span style="color: black;">latitude</span> = <span style="color: #008000;">self</span>.<span style="color: black;">request</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'txtLatitude'</span><span style="color: black;">&#41;</span>
        business.<span style="color: black;">longitude</span> = <span style="color: #008000;">self</span>.<span style="color: black;">request</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'txtLongitude'</span><span style="color: black;">&#41;</span>
&nbsp;
        business.<span style="color: black;">put</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">redirect</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This just grabs from the data from the request and sets each property on our business object.<br />
Then calls put.<br />
put is an instance method that saves the data to the database.<br />
delete, to_xml, is_saved, are a couple of other useful instance methods.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    &lt;form action=&quot;/createbusiness.do&quot; method=&quot;post&quot; id=&quot;businessform&quot;&gt;</pre></div></div>

<p>Tells the form to post to the specified address.</p>
<p><strong>Bringing it all together</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">application: yourapplication
version: 1
runtime: python
api_version: 1
&nbsp;
handlers:
- url: /static
  static_dir: static
&nbsp;
- url: /.*
  script: localbusinesslocator.py</pre></div></div>

<p>The app.yaml is where your external url mapping occurs.<br />
If you wanted to use several python files, this is where that would happen.<br />
More Info can be found <a href="http://code.google.com/appengine/docs/configuringanapp.html">here</a></p>
<p>Testing the application<br />
usr/local/google_appengine/dev_appserver.py /sourcedirectory/</p>
<p>Hopefully this fills in some gaps left by <a href="http://code.google.com/appengine/docs/gettingstarted/">Googles tutorial.</a></p>
<p>The next installment of the series will go over displaying the data in the GeoRss format and displaying it on google maps.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonshern.com/2008/05/18/getting-started-with-the-google-app-engine/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

