开发者

In the python version of Google App Engine, how can you override the db.Model class to save to a temp datastore rather than big table?

开发者 https://www.devze.com 2023-01-06 20:23 出处:网络
In Singapore, we are teaching students python using Singpath (singpath.appspot.com). In addition to letting students practice writing software in python, we would like to familiarize students with the

In Singapore, we are teaching students python using Singpath (singpath.appspot.com). In addition to letting students practice writing software in python, we would like to familiarize students with the google.appengine.ext.db API used to access big table.

What is the easiest way to modify db.Model settings in an App Engine app so that any puts or gets access a local, temporary datastore rather than writing to big table? I'm trying to do something similar to how gaeunit creates a new temporary datastore each time unit tests are run.

from google.appengine.ext import db
impo开发者_开发技巧rt logging

class MyModel(db.Model):
  name = db.StringProperty()

#Configure a temp datastore to be updated instead of bigtable. 

m = MyModel()
m.put() #shouldn't update bigtable
result = MyModel.all() #should fetch from temp datastore

logging.info("There were %s models saved", result.count())


You can certainly do this in the dev server by creating a new stub datastore when you want, like gaeunit. I don't think the concept really transfers to the production environment, though. A temporary datastore would have to have some kind of backing store, either the real datastore or memcache. AFAIK there's no built-in support for either.

An alternative would be to use the real datastore with some sandboxing.

You could override db.Model.kind to prefix a session ID:

@classmethod
def kind(cls):
  return "%s_%s" % (SESSION_ID, cls.__name__)

This would give you basic namespacing for user-created entities.

If you have a session entity, you could populate it as the parent entity on any query that does not already specify one. This would force all of your user's entities into one entity group.

In either case, at the start of the session you could schedule a task to run later that would clean up entities that the user created.

0

精彩评论

暂无评论...
验证码 换一张
取 消