Wednesday, April 09, 2008

Google App Engine Adventure: The Experiment.

Yesterday google have release their App Engine. Which provides hosting, database, scaling and load balancing, and also API's. Which currently uses python. But there is more to come.

So I got an account, which probably not available anymore. Either way and spend an afternoon, write up a prototype. It is actually longer, since I need to refresh my html, and missed a wrong unclose { in the template. Then fixed up the database support. And check out a python doc, because not sure how, datetime work.

First thing first, assume you already have everything, is the configuration, app.yaml
application: testapp
version: 1
runtime: python
api_version: 1
handlers:
- url: /
script: main.
so far pretty simple, but as application getting complex, it will have more configuration. For simple apps this would do.

Second is the code.

import os
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from datetime import *
class Events(db.Model):
name=db.StringProperty(required=True)
date=db.DateTimeProperty(required=True)

class MainPage(webapp.RequestHandler):
def get(self):
day=range(1,31)
month=range(1,12)
year=range(2008,2020)
hours=range(0,23)
minutes=range(0,59)
events=Events.all().order('-date')
templ={'events':events,
'day':day,
'month':month,
'year':year,
'hours':hours,
'minutes':minutes}
path = os.path.join(os.path.dirname(__file__), 'main.html')
self.response.out.write(template.render(path,templ))

def post(self):
dt=self.request.get('year')+'-'+self.request.get('month')+'-'+\
self.request.get('day')+' '+self.request.get('hour')+':'+\
self.request.get('minute')+':00'
event=Events(name=self.request.get('name'),
date=datetime.strptime(dt,'%Y-%m-%d %H:%M:%S'),
location=self.request.get('address'),
description=self.request.get('description'))
event.put()
day=range(1,31)
month=range(1,12)
year=range(2008,2020)
hours=range(0,23)
minutes=range(0,59)
events=Events.all().order('-date')
templ={'events':events,
'day':day,
'month':month,
'year':year,
'hours':hours,
'minutes':minutes}
path = os.path.join(os.path.dirname(__file__), 'main.html')
self.response.out.write(template.render(path,templ))

def main():
apps = webapp.WSGIApplication([('/',MainPage)],debug=True)
wsgiref.handlers.CGIHandler().run(apps)

if __name__=="__name__":
main()
Basically, now I'm worried that, it will get more complex, when it got longer.

Lastly the template. Which I can't really show on the block

Then start the dev_appserver.py. Then it runs. It's ugly, but it shows a few thing to me.

No comments:

Post a Comment