2012-09-17 97 views
3

我使用金字塔1.4a1和我有這個用戶謂:金字塔用戶謂詞不工作

我添加它,像這樣:

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) 

我也有同樣的方式註冊自定義視圖謂詞,它工作得很好,所以我不知道我在做什麼錯在這裏。

回答

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() 
+0

老實說,我無法找到我哪裏錯了,但感謝。 – BDuelz