2011-07-01 5 views
4

사용자가 아직 인증되지 않은 경우 로그인 페이지로 리디렉션되도록 내 CherryPy 컨트롤러 클래스에서 메소드를 꾸미는 간단한 방법을 설정하려고합니다. 나는 기본적인 파이썬 꾸미기를 할 예정 이었지만, an answer here은 CherryPy Custom Tool을 대신 사용할 것을 제안했다. 그래서 나는 그것을하려고 노력하고있다. 그러나 나는 그것을 작동시킬 수 없다. 여기에 내가 가진 무엇 :사용자 인증을위한 CherryPy 사용자 정의 도구

def authenticate(): 
    user = cherrypy.session.get('user', None) 
    if not user: 
     raise cherrypy.HTTPRedirect('/?errMsg=Please%20log%20in%20first') 

cherrypy.tools.authenticate = cherrypy.Tool('on_start_resource', authenticate) 

/home 페이지는 인증 된 사용자로 제한해야 페이지입니다, 그래서이있다 : 내를 시작하려고하면이 오류를 얻을 그러나

@cherrypy.expose 
@cherrypy.tools.authenticate 
def home(self, **kwargs): 
    tmpl = TemplateDir.get_template('home.mako') 
    return tmpl.render() 

을 웹 사이트 :

Traceback (most recent call last): 
    File ".\example.py", line 3, in <module> 
    from controller.main import Root 
    File "C:\...\controller\main.py", line 9, in <module> 
    class Root(BaseModule): 
    File "C:\...\controller\main.py", line 19, in Root 
    @cherrypy.tools.authenticate 
    File "C:\Python26\lib\site-packages\cherrypy\_cptools.py", line 119, in 
    __call__ % self._name) 
TypeError: The 'authenticate' Tool does not accept positional arguments; you must 
    use keyword arguments. 

편집 : 내가 괄호를 위해 사용자 지정 도구의 내 사용을 변경하는 경우 좋아, 내가 다른 오류가 발생합니다.

@cherrypy.expose 
@cherrypy.tools.authenticate() # Magic parentheses... 
def home(self, **kwargs): 
    ... 

지금 내가 얻을 :

Traceback (most recent call last): 
    File "C:\Python26\lib\site-packages\cherrypy\_cprequest.py", line 625, in respond 
    self.hooks.run('on_start_resource') 
    File "C:\Python26\lib\site-packages\cherrypy\_cprequest.py", line 97, in run 
    hook() 
    File "C:\Python26\lib\site-packages\cherrypy\_cprequest.py", line 57, in __call__ 
    return self.callback(**self.kwargs) 
    File ".\example.py", line 40, in authenticate 
    user = cherrypy.session.get('user', None) 
AttributeError: 'module' object has no attribute 'session' 

편집 : 내가 처음 웹 방법에 내 사용자 지정 도구 장식으로 페이지를로드 할 때

cherrypy.tools.sessions.storage_type = 'file' 
cherrypy.tools.sessions.storage_path = r'%s\sessions' % curDir 
cherrypy.tools.sessions.timeout = 60 
cherrypy.tree.mount(Root(), "/", config={ 
    '/static': { 
     'tools.staticdir.on':True, 
     'tools.staticdir.dir':r'%s\static' % curDir, 
    }, 
    '/': { 
     'tools.sessions.on':True, 
    } 
}) 

: 내가 가지고있는 세션이 켜져 이 오류가 발생합니다.

AttributeError: 'module' object has no attribute 'session'

AttributeError: '_Serving' object has no attribute 'session'

편집 : 심지어 내 컨트롤러 클래스에서이 정도의 노력, 나는 여전히 '모듈 오브젝트가 속성 세션이 없습니다'라는 에러 :

내가 페이지를 다시로드 할 때

그런 다음,이 오류가 발생합니다

class Root(BaseModule): 
    _cp_config = {'tools.sessions.on': True} 
    sess = cherrypy.session # Error here 
    ... 

답변

5

잘못된 후크를 사용하고있었습니다. 변경 :

cherrypy.tools.authenticate = cherrypy.Tool('on_start_resource', authenticate) 

에 :

cherrypy.tools.authenticate = cherrypy.Tool('before_handler', authenticate) 

문제를 해결했습니다. 분명히 내 authenticate 메서드는 세션이 켜지 기 전에 호출되었으므로 cherrypy.session에 액세스 할 수 없습니다. 나는 내 컨트롤러에서 세션 켜기 기능을 필요로하지 않았다. 하지만,

@cherrypy.expose 
@cherrypy.tools.authenticate() 
def home(self, **kwargs): 
    ... 
0

대부분의 세션은 활성화되어 있지 않습니다. session wiki page에 예제 설정 파일이 있거나 tutorial #7이 있습니다.

+0

그들은 다음과 같습니다 제한된 방법 내 컨트롤러,

def authenticate(): ... cherrypy.tools.authenticate = cherrypy.Tool('before_handler', authenticate) cherrypy.tree.mount(Root(), "/", config={ "/": { 'tools.sessions.on':True, 'tools.sessions.storage_type':'file', 'tools.sessions.storage_path':r'%s\sessions' % curDir, 'tools.sessions.timeout':60 }, ... }) 

그런 : 모든 것을 필요하다고는 내 서버 시작 스크립트에 다음이었다. :(세션 설정을 보여주기 위해 업데이트 된 질문입니다. 튜토리얼 # 7 링크를 확인하고 있습니다. –

관련 문제