我正在使用Pyramid 1.4a1,我有这个订阅者谓词:
class RouteName(object):
'''
Subscriber predicate to check against route name.
'''
def __init__(self, value, config):
self.value = value
self.config = config
def text(self):
return 'route_name = %s' % self.value
phash = text
def __call__(self, event):
route_name = event.request.matched_route.name
return route_name == self.value
我这样添加:
config.add_subscriber_predicate('route_name', RouteName)
但是,每当我尝试使用它时,就像这样:
@subscriber(ContextFound, route_name='user.register')
def check_invitation(event):
# Check invitation token before running registration view code ...
我得到了这个追溯:
Traceback (most recent call last):
File "/Workspace/Personal/project/.env/bin/pserve", line 8, in <module>
load_entry_point('pyramid==1.4a1', 'console_scripts', 'pserve')()
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/pyramid/scripts/pserve.py", line 47, in main
return command.run()
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/pyramid/scripts/pserve.py", line 290, in run
relative_to=base, global_conf=vars)
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/pyramid/scripts/pserve.py", line 318, in loadapp
return loadapp(app_spec, name=name, relative_to=relative_to, **kw)
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/PasteDeploy-1.5.0-py2.7.egg/paste/deploy/loadwsgi.py", line 247, in loadapp
return loadobj(APP, uri, name=name, **kw)
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/PasteDeploy-1.5.0-py2.7.egg/paste/deploy/loadwsgi.py", line 272, in loadobj
return context.create()
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/PasteDeploy-1.5.0-py2.7.egg/paste/deploy/loadwsgi.py", line 710, in create
return self.object_type.invoke(self)
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/PasteDeploy-1.5.0-py2.7.egg/paste/deploy/loadwsgi.py", line 146, in invoke
return fix_call(context.object, context.global_conf, **context.local_conf)
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/PasteDeploy-1.5.0-py2.7.egg/paste/deploy/util.py", line 56, in fix_call
val = callable(*args, **kw)
File "/Workspace/Personal/project/project/__init__.py", line 49, in main
return config.make_wsgi_app()
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/pyramid/config/__init__.py", line 955, in make_wsgi_app
self.commit()
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/pyramid/config/__init__.py", line 629, in commit
self.action_state.execute_actions(introspector=self.introspector)
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/pyramid/config/__init__.py", line 1083, in execute_actions
tb)
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/pyramid/config/__init__.py", line 1075, in execute_actions
callable(*args, **kw)
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/pyramid/config/adapters.py", line 53, in register
order, preds, phash = predlist.make(self, **predicates)
File "/Workspace/Personal/project/.env/local/lib/python2.7/site-packages/pyramid/config/util.py", line 264, in make
pred = predicate_factory(val, config)
pyramid.exceptions.ConfigurationExecutionError: <type 'exceptions.TypeError'>: object.__new__() takes no parameters
in:
Line 80 of file /Workspace/Personal/project/.env/local/lib/python2.7/site-packages/pyramid/events.py:
config.add_subscriber(wrapped, iface, **self.predicates)
我也有一个以相同方式注册的自定义视图谓词,它工作正常,所以我不知道我在这里做错了什么。
答案 0 :(得分:2)
我认为你的代码中有一个拼写错误。以下申请 在1.4a1下工作。如果你访问/它打印检查。如果你访问/另一个, 它没有:
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.events import subscriber, ContextFound
from pyramid.view import view_config
class RouteName(object):
'''
Subscriber predicate to check against route name.
'''
def __init__(self, value, config):
self.value = value
self.config = config
def text(self):
return 'route_name = %s' % self.value
phash = text
def __call__(self, event):
route_name = getattr(event.request.matched_route, 'name', None)
return route_name == self.value
@subscriber(ContextFound, route_name='home')
def check(event):
print 'checked'
@view_config(renderer='string', route_name='home')
def home(request):
return 'home'
@view_config(renderer='string', route_name='another')
def another(request):
return 'another'
if __name__ == '__main__':
config = Configurator()
config.add_subscriber_predicate('route_name', RouteName)
config.add_route('home', '/')
config.add_route('another', '/another')
config.scan()
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()