Duncan
unread,Apr 15, 2008, 4:14:50 AM4/15/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Google App Engine
Here's some sample code though if anyone else is looking:
from functools import wraps
def globalmethod(f):
"""A decorator which creates a classmethod that calls the original
function
inside a transaction with the single Globals object as the first
argument.
"""
@wraps(f)
def wrapper(cls, *args, **kwds):
def inner():
obj = Globals.get_by_key_name(cls.KEY)
if not obj:
obj = cls(key_name=cls.KEY)
result = f(obj, *args, **kwds)
obj.put()
return result
return db.run_in_transaction(inner)
return classmethod(wrapper)
class Globals(db.Model):
KEY = "globalvars"
visitors = db.IntegerProperty(default=0)
@globalmethod
def countVisitor(self, n=1):
self.visitors += n
return self.visitors
then to use it:
visitorCount = Globals.countVisitor()
which increments the counter and returns the new value (call
countVisitor(0) if you just want to access it without changing it).
Extend the class as required to hold other values, remember that any
access to values should be protected by a transaction, so use the
decorator to wrap up related updates to globals in a single method.