Saturday, October 1, 2011

Creating an application in Google App Engine

I have explained you about the Google appengine. Now I’m going to show you how I developed my Google appengine project. I have implemented an url shortening application in appengine. We always see long and bulky urls. For example look at the url of this post. If we paste this url in twitter or try to send this link to someone using SMS, it will be difficult to convey the link and message. So you can use my application to shorten your bulky large url.

In appengine, I used python as the back end. Webapp framework is used here. The database used here is Google’s datastore. It is little bit different from the traditional databases. It serves an object oriented database management system.

First I downloaded Google appengine SDK for python. I extracted it to home folder in my Ubuntu system. Then created a folder with name, “teenyurl”, which is the name of my application. Then created a app.yaml file in that folder. Then added following lines to it,

application: teenyurl
version: 1
runtime: python
api_version: 1
- url: /static
  static_dir: static
- url: /.*

The application name is set to teenyurl and it is the first version. Python is used as the back end. Static folder is defined so that we can add the css and image files to that folder. Then I created file. We have to add the python code in that file.
Different from others, we need to create the table by defining a class.

class Data(db.Model):
      url = db.StringProperty()
      code = db.StringProperty()
      turl = db.StringProperty()

The above code creates a table with name Data and attributes url, code and turl with varchar type. If we want to insert values, we must create an object of class Data.

data = Data()
data.url = url
data.code = word
data.turl = turl

The above code creates an object ‘data’ of class ‘Data’ and the inserts the value. put() is used to commit the values.

x = db.GqlQuery("SELECT * FROM Data WHERE code=:1" , path)

The above code returns the list of matching fields in the SELECT query. you may noticed the word code=:1",path). :1 is replaced by the first argument, here it is path.

Here in my application I have only 2 major classes. One is to create the short url (Home class) and the other is to redirect to the original url (TeenyURL class), when the short url is used. In the get() of Home class we are rendering the home page of the application. We have the home page file “index.html”.

I will explain more about these 2 classes in the next post. You can download the source code also. Click here.



No comments:

Post a Comment