Wednesday, March 27, 2013
www.timetidy.com 2014 world cup schedule in local time and native languages
Tuesday, May 25, 2010
Wednesday, June 17, 2009
Template project for using Django on Google Appengine. Guido's codereview source code
This post is about how to use Django on Google Appengine with Guido van Rossum's code review source code. Now, Appengine needs to be patched to use Django. You can either use patches (Appengine helper, App-engine-patch) or patch it by yourself. Another better way is using Guido's code as a template project.
Pros include:
1. codereview is hosted on Appengine and uses Django framework.
2. one feature of Django is pluggable. Your applications can be added by modifying url configuration file urls.py.
3. this open source project is actively maintained. Both of Appengine and Django are updated frequenctly. This will provide more useful features, but new versions normally also need new patches. Since codereview is an actual appengine application, it will show you how to use new features and make them work. This poject has some contributors, which means better customer support.
4. Guido is owner. And, he has substantial knowledge about Python.
For example, main.py shows how to set a breakpoint for debugging in appengine. Rietveld.py provides some useful helper functions.
Links:
http://codereview.appspot.com/
http://code.google.com/p/rietveld/
Monday, May 11, 2009
Sample code.Python and Perl.RESTful interface.Language Ajax api. Post method. 5000. api limit.
Perl sample code:
http://search.cpan.org/dist/WebService-Google-Language-0.09/lib/WebService/Google/Language.pm
this is Perl translation module utilizing google language Ajax api.
When encoded url with translation text is less than 2000 something or Maximum URL length set by google server, this module uses GET method.
When translation test is between 2000 and 5000, this module will put encoded translation text, api version, and langpair etc in body and send by POST.
You can download Perl from (http://www.perl.org/get.html) and install this module.
Python sample code:
POST method
First please download http://code.google.com/p/python-rest-client/
############################
#POST method
############################
from restful_lib import Connection
import urllib
conn = Connection('http://ajax.googleapis.com/ajax/services/language');
query = {'v':'1.0', 'langpair':'en|de','q':'hello world'};
print conn.request_post("/translate",body=urllib.urlencode(query), headers={'referer':'http://example.com', "content-type":"application/x-www-form-urlencoded" ,"charset":"UTF-8"})
#############################
notes:
1. query needs to be encoded as a string 'q=hello+world&langpair=en%7Cde&v=1.0' with urlencode()
2. "content-type":"application/x-www-form-urlencoded" ,"charset":"UTF-8" shold be added to headers to tell server how 'body' is encoded.
Without them, you will get 'invalid version' error in response.
3. based on perl module description (http://search.cpan.org/dist/WebService-Google-Language-0.09/lib/WebService/Google/Language.pm#CONSTRUCTOR)
header 'referer':'http://example.com' is mandatory, because Google demands a "valid and accurate http referer header" in requests to their service.
4. If you want to use it one google app engine, please use 'from gae_restful_lib import GAE_Connection as Connection' instead ( http://code.google.com/p/python-rest-client/wiki/REST_for_Google_App_Engine )
5. Please try a long translation text, after you get sample code work.
BTW, here is GET method sample code
############################
#POST method
############################
from restful_lib import Connection
print conn.request_get("/translate", args={'v':'1.0','langpair':'en|de','q':'Hello'}, headers={'referer':'http://example.com','Accept':'text/json'})
#############################
useful tool for restful testing:
http://code.google.com/p/rest-client/
Chef
my ads:
U.S. Immigration Guide
http://visachoice.appspot.com
Sunday, August 17, 2008
Django tutorial polls sample App -Google App Engine port
This article explains how to create a 'Google App Engine' version port of the 'polls' sample showed in Django tutorial: (http://www.djangoproject.com/documentation/tutorial01/).
After learning Python and finishing the official Django tutorial, I am trying to revise sample web app for Google App engine. Because Django does not support the database used by Google app engine, we need to revise all the codes related to database operation to deploy original web app on Google engine. The Google app engine use nonrelation database ' Big Table' instead of relation databases (Mysql, SQLlite…). If you want to learn more about the fundamental characteristics of 'Big table' to make your web app more efficiently, here are some useful article and video
Bigtable: A Distributed Storage System for Structured Data
http://labs.google.com/papers/bigtable.html
This article written by googlers. Looks like many Google applications are based on 'Big Table', like Google map, Google earth, etc.
Google I/O: Under the Covers of the Google App Engine Datastore
http://sites.google.com/site/io/under-the-covers-of-the-google-app-engine-datastore
1.Install Google app engine Django helper
First please install Google App engine SDK. Please install Google app engine Django helper ( link ).
http://code.google.com/p/google-app-engine-django/
2.Preloaded polls
Some polls are added for testing like following:
Poll:
Question1 (key name q1 )
Choice:
Choice_q1_c1 (key name q1_c1)
Choice_q1_c2 (key name q1_c2)
Choice_q1_c3 (key name q1_c3)
Choice_q1_c4 (key name q1_c4)
When a new entity is created, its key name can be set as a specific string value, but id value will be arbitrary integer. Entity can be retrieved through Model class method get_by_key_name without query.
3.main.py
# Standard Python imports.
import os
import sys
import logging
from appengine_django import InstallAppengineHelperForDjango
InstallAppengineHelperForDjango()
# Google App Engine imports.
from google.appengine.ext.webapp import util
# Import the part of Django that we use here.
import django.core.handlers.wsgi
def main():
# Create a Django application for WSGI.
application = django.core.handlers.wsgi.WSGIHandler()
# Run the WSGI CGI handler with that application.
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
- This code is from Google app engine Django helper.
4.url patterns
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.polls.views',
(r'^polls/$', 'index'),
(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
)
from django.conf.urls.defaults import *
urlpatterns = patterns('polls.views',
(r'^polls/$', 'index'),
(r'^polls/(?P<poll_key_name>\w+)/$', 'detail'),
(r'^polls/(?P<poll_key_name>\w+)/results/$', 'results'),
(r'^polls/(?P<poll_key_name>\w+)/vote/$', 'vote'),
)
- Since poll_key_name will be string like q1,q2 ... instead of integer ,'\w' is used to capture poll_key_name.
5.models.py
from django.db import models
class Poll(models.Model):
question = models.CharField(maxlength=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(maxlength=200)
votes = models.IntegerField()
from google.appengine.ext import db
from appengine_django.models import BaseModel
class Poll(BaseModel):
question = db.TextProperty()
pub_date = db.DateTimeProperty(auto_now_add=1)
def __str__(self):
return self.question
class Choice(BaseModel):
poll = db.ReferenceProperty(Poll)
choice = db.StringProperty()
votes = db.IntegerProperty(default = 0)
- Datastore api has different datatype syntax .
- '.ReferenceProperty' is used to build one to many relationship between Poll and Choice.
You can find out more information about building models relationships form this presentation
Working with Google App Engine Models
http://sites.google.com/site/io/working-with-google-app-engine-models
6.views
6.1 index.html
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li>{{ poll.question }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li>{{ poll.question }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
- For this template, two version are the same.
6.2 function index()
from django.shortcuts import render_to_response
from mysite.polls.models import Poll
def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
return render_to_response('polls/index.html', {'latest_poll_list': latest_poll_list})
from django.shortcuts import render_to_response
from models import Poll
def index(request):
latest_poll_list = Poll.all().order('-pub_date').fetch(5)
return render_to_response('index.html', {'latest_poll_list': latest_poll_list})
- The only difference is query syntax.
6.3 detail.html
<h1>{{ poll.question }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="/polls/{{ poll.id }}/vote/" method="post">
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
<h1>{{ poll.question }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="/polls/{{ poll.key.name }}/vote/" method="post">
{% for choice in choice_list %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.key.name }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
- Looks like there is no way to achieve the same effect as 'poll.choice_set.all' in Django template without introduce parameters. Let provide 'choice_list ' instead.
6.4 function detail()
from django.http import Http404
# ...
def detail(request, poll_id):
try:
p = Poll.objects.get(pk=poll_id)
except Poll.DoesNotExist:
raise Http404
return render_to_response('polls/detail.html', {'poll': p})
from django.http import Http404
from models import Poll,Choice
# ...
def detail(request, poll_key_name):
p=Poll.get_by_key_name(poll_key_name)
if bool(p)==False:
raise Http404
else:
return render_to_response('detail.html', {'poll': p,'choice_list':p.choice_set.fetch(10)})
- Google Datastore will return 'nonetype' object when no entity exits for a given keyname. And no exception will be raised. we can take advantage of the fact that the boolean value of 'none' type is False.
- If Http404 is not raised for non-exist entity, AttributeError for Nonetype error will appear sooner or later.
6.5 function vote()
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect
from mysite.polls.models import Choice, Poll
# ...
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the poll voting form.
return render_to_response('polls/detail.html', {
'poll': p,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect('/polls/%s/results/' % p.id)
from django.http import Http404
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from models import Poll,Choice
# ...
def vote(request, poll_key_name):
p=Poll.get_by_key_name(poll_key_name)
if bool(p)==False:
raise Http404
selected_choice = Choice.get_by_key_name(request.POST['choice'])
if bool(selected_choice)==False:
# Redisplay the poll voting form.
return render_to_response('detail.html', {
'poll': p,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls.views.results', args=(p.key().name(),)))
- Please note vote default value should be given or 'selected_choice.votes += 1' will raise exception.
6.6 results.html
<h1>{{ poll.question }}</h1>
<ul>
{% for choice in poll.choice_set.all %}
<li>{{ choice.choice }} -- {{ choice.votes }} vote{{ choice.votespluralize }}</li>
{% endfor %}
</ul>
<h1>{{ poll.question }}</h1>
<ul>
{% for choice in choice_list %}
<li>{{ choice.choice }} -- {{ choice.votes }} vote{{ choice.votespluralize }}</li>
{% endfor %}
</ul>
- It is similar to detail.html. Real list of choice ('choice_list') is needed.
from django.http import Http404
from django.shortcuts import get_object_or_404, render_to_response
def results(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
return render_to_response('polls/results.html', {'poll': p})
from django.http import Http404
from django.shortcuts import render_to_response
def results(request, poll_key_name):
p=Poll.get_by_key_name(poll_key_name)
if bool(p)==False:
raise Http404
else:
return render_to_response('results.html', {'poll': p,'choice_list':p.choice_set.fetch(10)})
7. source code and live webapp
you can find live app at
http://visachoice.appspot.com/polls/
for different polls, please check
http://visachoice.appspot.com/polls/q1/
http://visachoice.appspot.com/polls/q2/
....
source code can be found at
http://code.google.com/p/djangopollsgae/downloads/list
your comment are welcome!!
I just updated an Immigration Guide website http://visachoice.appspot.com/ with Google App Engine+Django+Guido's codereview sourcecode.