Saturday, October 1, 2011

Two main classes in Teeny URL


I have explained that I have 2 main classes- Home and TeenyURL – in my application. Lets start with Home class.

Home class

This class has two basic functions, get() and post(). In the get(), we need to render the index.html file. When our application is executed, index file will be loaded first. We need to pass some parameters/variables to the index.html file. For that we use the following code.

template_values = {
            'title': "TeenyURL",
}

Now the template_values hold the string value “TeenyURL” which will be replaced by the variable ‘title’ in the index.html file.

<html>
<head>
<title> {{title}} </title>

This is the code in the index.html file. The ‘title’ variable is written inside {{}} so  that the compiler will understand it as a variable. The ‘title’ is replaced with “TeenyURL”. To render the index.html, we need to specify the path to the index.html and its parameters (ie., template_values). Look at the below code,

path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))

Thus our index.html file is loaded and the user will see interface to enter the long url. When the user inputs some url and then submits, the post() of the Home class works.

url = self.request.get('url')

This code is used to get the value from the form where the user has given his input. Think about a situation where a user inputs his url and captures his short url and the again inputs the same large url. In that situation we need to provide the same short url which is provided earlier. So we need to first check, whether the submitted url is already in the datastore.  The following code is used for that.

x = db.GqlQuery("SELECT * FROM Data WHERE url=:1",url)
count = x.fetch(1)
z = len(count)
if (z>0):
                turl = count[0].turl

If the large url is a new one to datastore, we need to create a new short url for it.

while y>0 :
                word = "".join(random.choice(char_array) for i in range(4))
                x = db.GqlQuery("SELECT * FROM Data WHERE code=:1",word)
                count = x.fetch(1)
                y = len(count)

Here also checks whether the random generated code is already generated before for other large urls. I stored the large url and the corresponding short url along with the 4 letter code in the datastore. We need to create an object of the Data class. The below code is used to enter the values to the datastore,

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

The below code is used to render the index.html file with original url and its corresponding short url.

template_values = {
            'title': title,
            'url' : url,
            'teenyurl' : turl,
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))

TeenyURL class

When anyone uses this short url, I need to redirect it to its original url. That is the purpose of TeenyURL class. The following code is used for that,

x = Data.all()
x = db.GqlQuery("SELECT * FROM Data WHERE code=:1" , path)
count = x.fetch(1)
ourl = count[0].url
self.redirect(ourl)

In order to efficiently call the corresponding classes, I defined the templates paths as below,

application = webapp.WSGIApplication( [('/',Home),
                              ('/(.+)',Teenyurl)],
                              debug=True)

Now I have explained about all the code. In my next post I will explain you how to run the application in local machine and how to upload it to the appengine.

Thanks

AJAY

No comments:

Post a Comment