App Engine (Python) 数据存储区预调用 API 挂钩

2024-03-02

背景

假设我正在为 GAE 制作应用程序,并且我想使用API 钩子 http://code.google.com/appengine/articles/hooks.html.

BIG EDIT:在这个问题的原始版本中,我描述了我的用例,但有些人正确地指出它并不真正适合 API Hooks。的确!认为我有帮助。但现在我的问题是学术性的:我仍然不知道如何在实践中使用钩子,但我想这样做。我重写了我的问题,使其更加通用。


Code

所以我做了一个这样的模型:

class Model(db.Model):
    user = db.UserProperty(required=True)
    def pre_put(self):
        # Sets a value, raises an exception, whatever.  Use your imagination

然后我创建一个 db_hooks.py:

from google.appengine.api import apiproxy_stub_map

def patch_appengine(): 
    def hook(service, call, request, response):
        assert service == 'datastore_v3'
        if call == 'Put':
            for entity in request.entity_list():
                entity.pre_put()

    apiproxy_stub_map.apiproxy.GetPreCallHooks().Append('preput',
                                                        hook,
                                                        'datastore_v3')

由于 TDD 的困扰,我正在使用这一切GAEUnit http://code.google.com/p/gaeunit/,所以在 gaeunit.py 的 main 方法上方,我添加:

import db_hooks
db_hooks.patch_appengine()

然后我编写一个实例化并放置模型的测试。


Question

While patch_appengine()肯定会被调用,但钩子永远不会被调用。我缺少什么?如何让 pre_put 函数真正被调用?


对于手头的任务来说,Hook 的级别有点低。您可能想要的是自定义属性类。派生财产,来自aetycoon http://github.com/arachnid/aetycoon,只是门票。

但请记住,用户对象的“昵称”字段可能不是您想要的 - 每个the docs http://code.google.com/appengine/docs/python/users/userclass.html#User_nickname,如果他们使用 Gmail 帐户,则只是电子邮件字段的用户部分,否则就是完整的电子邮件地址。您可能想让用户设置自己的昵称。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

App Engine (Python) 数据存储区预调用 API 挂钩 的相关文章

随机推荐