27 January, 2010

Redesign the whole system

It's not allowed to use cookie in the new test system, although I've realized many functions, I have to redesign them.
Try to use Django to pass the values between different web pages.

23 January, 2010

Eclipse+PyDev+GAE memcache error

 Using memcache in GAE like this in my code:

tenth = memcache.get('tenth') 

There is an error notification in Eclipse+PyDev, but it works well. Checked a website, found other people have the same problem, so that's just a bug of PyDev, not mine, luckily.

Reference: http://stackoverflow.com/questions/1469860/eclipsepydevgae-memcache-error

21 January, 2010

JavaScript passing value to HTML in same page

As mentioned in the previous post Generate a random number from website. There is a variable generator the JavaScript. How to pass this value to an   element of the same page? 
We use:   
document.test.result.value = generator;
test is the form name in the same page's HTML, result is an input element in this form, so this sentence means to pass the value of the variable generator to the input element result in form test.
Then, we can use Python to revoke the element's value for other use.

20 January, 2010

Using the Templates from Python

from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get(self):
      temp = os.path.join(
              os.path.dirname(__file__),
             'templates/index.htm')
      outstr = template.render(
              temp,
             {'hint': 'Good luck!'})
             self.response.out.write(outstr)
We add an import statement to make the template library available to our application.

Within the get() method, the first line looks up the full path to the template file by using the path to the currently executing file and adding templates/index.htm to the end of the current file’s path.

The actual template processing is done in the template.render() line. This takes two parameters: the first is the location of the template file from the previous step (stored in the variable temp) and the second is a Python dictionary object, which contains the strings to be placed in the template where the {{ hint }} entries are found. The results f the substitution of the variables into the template are returned as a string in the variable outstr.

Generate a random number from website

<script type="text/javascript">
function randomnumber()
{
var generator = Math.floor(Math.random()*100);
document.test.result.value = generator + ' wpm';
}
</script>

<form name="test">
<input type=button value=Finish onclick="randomnumber()">
Test Result : <input name=result readonly type=text size=6>
</form>

19 January, 2010

Check email in html form

<script type="text/javascript">

function check_email(email_id, err_id){
emailRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9]+(\.[a-z0-9]+)*(\.([a-z]){2,4})$/;
var err_mail='Email address incorrect!';
if(emailRegExp.test(document.getElementById(email_id).value)){
alert('true');
return true;
}else{
document.getElementById(err_id).innerHTML=err_mail;
alert(err_mail);
return false;
}
}
</script>



<span id="err_msg" style="color: red;"></span>
<form id="myForm" name="myForm" action="./register.php" method="post" onsubmit="return check_email('email','err_msg');">
<input type="text" name="email" id="email" size="20"/>
<input type="submit" name=send" value="Send" />
</form>

18 January, 2010

Extending Base Templates

{% extends "_base.htm" %}
{% block bodycontent %}
<h2>App Engine: About</h2>
<p>
Welcome to the site dedicated to
learning Google App Engine.
We hope you find www.appenginelearn.com useful.
</p>
{% endblock %}

The template language uses curly braces and percent signs to indicate our commands to the render engine. The first line says this page starts with the text contained in the file _base.htm. We are starting with _base.htm and then extending it.
The second line says, “When you find an area marked as the bodycontent block in the _base.htm file, replace that block with the text in between the block and endblock template commands.”

The _base.htm file is placed in the templates directory, along with all the rest of the template files.

16 January, 2010

What is ' if __name__ == "__main__": ' for?

The if __name__ == "__main__": ... trick exists in Python so that our Python files can act as either reusable modules, or as standalone programs. As a toy example, let’s say that we have two files:

$ cat mymath.py
def square(x):
return x * x
if __name__ == '__main__':
print "test: square(42) ==", square(42)

$ cat mygame.py
import mymath
print "this is mygame."
print mymath.square(17)

In this example, we’ve written mymath.py to be both used as a utility module, as well as a standalone program. We can run mymath standalone by doing this:

$ python mymath.py
test: square(42) == 1764

But we can also use mymath.py as a module; let’s see what happens when we run mygame.py:

$ python mygame.py
this is mygame.
289

Notice that here we don’t see the ‘test’ line that mymath.py had near the bottom of its code. That’s because, in this context, mymath is not the main program. That’s what the if __name__ == "__main__": ... trick is used for.

[From a post to Python Tutor by Danny Yoo]

Use google app engine webapp application

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

This bit of code is best translated as “Create a new web application, and route all the URLs that come to this application (/.*) to the MainHandler class; once the application has been created and configured with its routing, run the application.”

Google app engine-Python in Eclipse with PyDev

If you have more than one project run in Eclipse, when you want to switch one to another, you should terminate the previous project in the "Console" window in Eclipse. Otherwise, Eclipse will run the previous one instead of the one you actually want to run.

15 January, 2010

Get user IP address and put in the Datastore

x = TaggedIp()

x.IpAddress = os.environ["REMOTE_ADDR"]

x.put()

14 January, 2010

HTTP tool- Firebug

An essential tool to help you understand and debug many aspects of web programming in the browser is the Firebug plugin for Firefox, written by Joe Hewitt.

Adding Navigation Hints for Users---CSS

Change the link to the current page from blue to black and remove the underline. To do this, we add a class attribute to the link on the currently selected page.

from 'Using goole app engine' p39-p40

Web page design by CSS ---- tool

Another important tool for working with increasingly complex CSS is the Web Developer plug-in for the Firefox browser that is freely available from Chris Pedrick. Web developers have pretty much standardized on Firefox as their browser because of its support for web development. You can find and install the Web Developer plug-in at http://addons.mozilla.org/.

When the Web Developer plug-in is installed, you will have an additional toolbar when you start Firefox. There are many useful options in this toolbar. The first one we will use is under Outline→Outline Block Elements.† When you enable the outlining of block elements, you can see the structure of your web page, including which elements are contained within other elements. If you have made a mistake in the structure or nesting of your HTML, you may be able to quickly identify and fix the problem just by looking over the block elements.

from 'using google app engine'

13 January, 2010

Some terminology



1. CSS, or Cascading Styles Sheets, is a way to style HTML. Whereas the HTML is the content, the style sheet is the presentation of that document.

2. MVP, Model-view-presenter

3. GWT, Google Web Toolkit
4. The MVC pattern splits the code of a web application into three basic areas:
Controller
The code that does the thinking and decision making.
View
The  HTML,  CSS,  and  other  elements  that  make  up  the look  and  feel  of  the application.
Model
The persistent data that we keep in the datastore.






What is Memcahe Entries?

One part of the new Test Subsystem. I need to design the Memcache Entries for the system.

From book 'Developing with Google App Engine', Chapter 7, Memcache and Session Data, it shows:

What Is Memcache?
Memcache is a service that provides a key-value caching mechanism for
efficient in-memory data retrieval across multiple instances of an App
Engine application. The Memcache API enables
􀂃 A reduction in the number of Datastore queries
􀂃 A reduction in the Datastore quota usage for popular pages
􀂃 Caching of expensive query results
􀂃 Implementation of transient counters
Data in Memcache are available to every instance of an application and
only discarded due to cache exhaustion.

Checked the book ' Using Google App Engine ', Chapter 11- Memory Cache, P193-P203.

Questions in view.py

What's the meaning of " Option = "21" Access = "2" "?
A: They are the access authority of the datastore, mainly for the admin screen, nothing to do with the student account users.

Why I can log in by using Account " PIT", but cannot log in by another group of users?

10 January, 2010

About 3 years have passed!

3 years................