""" Demo of login/logout and saving some user-specific data. From boodebr.appspot.com. May be freely used and modified for any purpose. """ from google.appengine.api import users from google.appengine.ext import db from random import random from logging import info class DemoUser(db.Model): # the user - this is the recommended way to save a reference to # a specific user. Do NOT save by the user's email address, as it # can change. (See GAE docs for more.) user = db.UserProperty(required=True) # some data to associate with the user value = db.FloatProperty(required=True) def getpage(environ): h = u'
Login' % users.create_login_url('/logindemo') else: # load/create DemoUser. Really, I should do this in a transaction, but # keeping things simple for now. q = db.GqlQuery('select * from DemoUser where user = :1', user) uobj = q.fetch(1) if not len(uobj): # create new DemoUser uobj = DemoUser(user=user, value=random()) db.put(uobj) h += "I've picked a random number for you of %f. " % uobj.value h += "Each time you login, I will remember this number.
"
else:
# loaded existing DemoUser
uobj = uobj[0]
# show the user their info
h += 'You are logged in as %s (%s) ' % (user.nickname(), user.email())
h += '
Your random value is %f
' % uobj.value
h += '
Your database key is %s ' % uobj.key()
h += '
The app that created this key is %s ' % uobj.key().app()
h += '
The type of the data entity is %s ' % uobj.key().kind()
h += '
The id of the data entity is %s ' % uobj.key().id()
h += '
The name of the data entity is %s ' % uobj.key().name()
h += '
The parent of this data entity is %s ' % uobj.key().parent()
h += '
Logout' % users.create_logout_url('/logindemo') # show headers so user can see them change as they login/logout h += '