gaeutilities

Utility classes to make working with appengine easier.

Session

Session is a class to manage persistent data across multiple page views. It includes user IP and User Agent validation, as well as session token regeneration to manage session fixation vulnerabilities. Only the session id is stored via cookie, all other data is stored on server side. Session data can be set using standard Python container access methods (Session()["key"] = "value").

Classes: Session Flash Event Cache

Welcome to the sessions demo. You've viewed this page 1 times during this session. You session id is 1487529658b0459b70727521ba1ae6bd1047fe02 There are 4 objects in your session data. The keys are:

  • viewCount
  • 3
  • testKey
  • testKey2
  • When the Django template library is upgraded this demo can be modified to show the values as well. For now this is enough to demonstrate iteration support.

You can View this page again, you can Set flash data, pr you can Delete your session and start over.

Memcache stats: {'hits': 618L, 'items': 7L, 'bytes': 822L, 'oldest_item_age': 193222L, 'misses': 42L, 'byte_hits': 92010L}

webapp class

class SessionPage(webapp.RequestHandler):
class SessionPage(webapp.RequestHandler):
  def get(self):
    self.sess = sessions.Session()
    if self.request.get('deleteSession') == "true":
        self.sess.delete()
        print "Location: /session\n\n"
    elif self.request.get('setflash') == "true":
        self.sess['flash'] = 'You set a flash message! <a href="/session">Refresh this page</a> and this message is gone!'
        print "Location: /session\n\n"
    else:
        self.sess['testKey'] = "test"
        self.sess['testKey2'] = "test2"
        if not 'viewCount' in self.sess:
            self.sess['viewCount'] = 1
        else:
            self.sess['viewCount'] = int(self.sess['viewCount']) + 1
        self.memcacheStats = memcache.get_stats()
        template_values = {
            'sess': self.sess,
            'memcacheStats': self.memcacheStats
        }
        path = os.path.join(os.path.dirname(__file__), 'templates/session.html')
        self.response.out.write(template.render(path, template_values))

template

{% if sess.flash %}
    <p>{{ sess.flash }}</p>
    {% endif %}
<p> Welcome to the sessions demo. You've viewed this page <strong>{{ sess.viewCount }}</strong> times during this session. You session id is <strong>{{ sess.sid }}</strong><br />
You can <a href="/session">View this page again</a>, you can <a href="/session?setflash=true">Set flash data</a>, pr you can <a href="/session?deleteSession=true">Delete your session</a> and start over.</p>