为plone.recipe.zope2instance命名实例运行脚本入口点
项目描述
在您的包中某个模块中实现脚本,作为命名函数,接受上下文和请求作为参数
def whoami(context, request):
from AccessControl.SecurityManagement import getSecurityManager
user = getSecurityManager().getUser()
return {
'absolute_url': context.absolute_url(),
'context': context.__repr__(),
'user': user.__repr__(),
'getId': user.getId(),
'getUserName': user.getUserName(),
'getRoles': user.getRoles(),
'getRolesInContext': user.getRolesInContext(context)
}
请记住,当您的脚本修改数据库时,要包括事务提交(上面的例子中没有)
import transaction
transaction.commit()
在您的包的setup.py中将您的函数注册为名为collective.runhook*的setuptools入口点
from setuptools import setup
setup(
# ...
entry_points="""
# -*- Entry points: -*-
# ...
[collective.runhook]
whoami = my.package:whoami
"""
)
将collective.runhook作为您的包的依赖项,或在您的buildout实例部分中包含它
[buildout]
parts = instance
# ...
[instance]
recipe = plone.recipe.zope2instance
# ...
eggs =
Plone
# ...
collective.runhook
运行buildout并执行您的脚本
$ bin/instance runhook whoami
...
{'absolute_url': 'http://nohost/Plone',
'context': '<Application at >',
'getId': None,
'getRoles': ('manage', 'Authenticated'),
'getRolesInContext': ['manage', 'Authenticated'],
'getUserName': 'System Processes',
'user': "<UnrestrictedUser 'System Processes'>"}
collective.runhook遵循与运行命令相同的实例脚本参数
$ bin/instance -OPlone runhook whoami
...
{'absolute_url': 'http://nohost/Plone',
'context': '<PloneSite at /Plone>',
'getId': None,
'getRoles': ('manage', 'Authenticated'),
'getRolesInContext': ['manage', 'Authenticated'],
'getUserName': 'System Processes',
'user': "<UnrestrictedUser 'System Processes'>"}
作为额外功能,collective.runhook可以使用ZOPE_USER环境变量认证脚本为任何现有用户(但请注意,认证仅在-O-traverse之后执行)
$ ZOPE_USER=datakurre bin/instance -OPlone runhook whoami
...
{'absolute_url': 'http://nohost/Plone',
'context': '<PloneSite at /Plone>',
'getId': 'datakurre',
'getRoles': ['Member', 'Reviewer', 'Site Administrator', 'Authenticated'],
'getRolesInContext': ['Member',
'Reviewer',
'Site Administrator',
'Authenticated'],
'getUserName': 'datakurre',
'user': "<PloneUser 'datakurre'>"}
我们还支持带有VirtualHostBase的URL
$ ZOPE_USER=datakurre bin/instance -O/VirtualHostBase/http://example.com:80/Plone/VirtualHostRoot/Plone runhook whoami
...
{'absolute_url': 'http://example.com',
'context': '<PloneSite at /Plone>',
'getId': 'datakurre',
'getRoles': ['Member', 'Reviewer', 'Site Administrator', 'Authenticated'],
'getRolesInContext': ['Member',
'Reviewer',
'Site Administrator',
'Authenticated'],
'getUserName': 'datakurre',
'user': "<PloneUser 'datakurre'>"}
变更日志
0.9.5 (2014-09-28)
修复遍历所有同名的入口点的修复,以支持具有相同钩子名称的多个脚本 [datakurre]
修复将脚本返回值打印为pprint,而无需脚本自行打印 [datakurre]
0.9.4 (2014-09-26)
修复打印钩子结果(如果非False)[datakurre]
0.9.3 (2014-09-25)
添加对VirtualHostMonster URL的支持 [datakurre]
0.9.2 (2014-09-25)
修复从入口点读取错误属性的问题 [datakurre]
0.9.1 (2014-09-24)
更新README [datakurre]
0.9.0 (2014-09-24)
首次发布。
项目详情
关闭
collective.runhook-0.9.5.zip的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 3cd3f1620f9739ff312dd0884795a0a0b2e83970e32d144639a0a1b4c280365b |
|
MD5 | a492f221f46723bec682da36eb500cb1 |
|
BLAKE2b-256 | db9675e8921ff7438249883577664cd15299157913942a98893eabe6492ffce6 |