这个装饰器如何调用'register'方法?

时间:2010-02-22 19:50:32

标签: python django decorator

我正在试图了解装饰器@not_authenticated中发生了什么。 TraceRoute的下一步是方法' register ',它也位于django_authopenid / views.py中,我只是不明白,因为我没有看到任何地方甚至提到了寄存器登入()

如何调用方法'注册'?

def not_authenticated(func):  
    """ decorator that redirect user to next page if he is already logged."""  
    def decorated(request, *args, **kwargs):  
        if request.user.is_authenticated():  
            next = request.GET.get("next", "/")  
            return HttpResponseRedirect(next)  
        return func(request, *args, **kwargs)  
    return decorated  

@not_authenticated  
def signin(request,newquestion=False,newanswer=False):  
    """  
    signin page. It manage the legacy authentification (user/password)   
    and authentification with openid.  

    url: /signin/  

    template : authopenid/signin.htm  
    """  
    request.encoding = 'UTF-8'  
    on_failure = signin_failure  
    next = clean_next(request.GET.get('next'))  

    form_signin = OpenidSigninForm(initial={'next':next})  
    form_auth = OpenidAuthForm(initial={'next':next})  

    if request.POST:     

        if 'bsignin' in request.POST.keys() or 'openid_username' in request.POST.keys():  

            form_signin = OpenidSigninForm(request.POST)  
            if form_signin.is_valid():  
                next = clean_next(form_signin.cleaned_data.get('next'))  
                sreg_req = sreg.SRegRequest(optional=['nickname', 'email'])  
                redirect_to = "%s%s?%s" % (  
                        get_url_host(request),  
                        reverse('user_complete_signin'),   
                        urllib.urlencode({'next':next})  
                )  
                return ask_openid(request,   
                        form_signin.cleaned_data['openid_url'],   
                        redirect_to,   
                        on_failure=signin_failure,   
                        sreg_request=sreg_req)  

        elif 'blogin' in request.POST.keys():  
            # perform normal django authentification  
            form_auth = OpenidAuthForm(request.POST)  
            if form_auth.is_valid():  
                user_ = form_auth.get_user()  
                login(request, user_)  
                next = clean_next(form_auth.cleaned_data.get('next'))  
                return HttpResponseRedirect(next)  

    question = None  
    if newquestion == True:  
        from forum.models import AnonymousQuestion as AQ  
        session_key = request.session.session_key  
        qlist = AQ.objects.filter(session_key=session_key).order_by('-added_at')  
        if len(qlist) > 0:  
            question = qlist[0]  
    answer = None  
    if newanswer == True:  
        from forum.models import AnonymousAnswer as AA  
        session_key = request.session.session_key  
        alist = AA.objects.filter(session_key=session_key).order_by('-added_at')  
        if len(alist) > 0:  
            answer = alist[0]  

    return render('authopenid/signin.html', {  
        'question':question,  
        'answer':answer,  
        'form1': form_auth,  
        'form2': form_signin,  
        'msg':  request.GET.get('msg',''),  
        'sendpw_url': reverse('user_sendpw'),  
    }, context_instance=RequestContext(request))  

查看请求,似乎account / register /确实使用'PATH_INFO'引用了寄存器方法:u'/ account / register /'

以下是请求:

<WSGIRequest GET:<QueryDict: {}>, POST:<QueryDict: {u'username': [u'BryanWheelock'], u'email': [u'bryan.wheck@gmail.com'], u'bnewaccount': [u'Signup']}>, COOKIES:{'__utma': '127460431.1218630960.1266769637.1266769637.1266864494.2', '__utmb': '127460431.3.10.1266864494', '__utmc': '127460431', '__utmz': '127460431.1266769637.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)', 'sessionid': 'fb15ee538320170a22d3a3a324aad968'}, META:{'CONTENT_LENGTH': '74', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'DOCUMENT_ROOT': '/usr/local/apache2/htdocs', 'GATEWAY_INTERFACE': 'CGI/1.1', 'HTTP_ACCEPT': 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5', 'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8', 'HTTP_CACHE_CONTROL': 'max-age=0', 'HTTP_CONNECTION': 'close', 'HTTP_COOKIE': '__utmz=127460431.1266769637.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=127460431.1218630960.1266769637.1266769637.1266864494.2; __utmc=127460431; __utmb=127460431.3.10.1266864494; sessionid=fb15ee538320170a22d3a3a324aad968', 'HTTP_HOST': 'workproject.com', 'HTTP_ORIGIN': 'http://workproject.com', 'HTTP_REFERER': 'http://workproject.com/account/signin/complete/?next=%2F&janrain_nonce=2010-02-22T18%3A49%3A53ZG2KXci&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=id_res&openid.op_endpoint=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fud&openid.response_nonce=2010-02-22T18%3A49%3A53Znxxxxxxxxxw&openid.return_to=http%3A%2F%2Fworkproject.com%2Faccount%2Fsignin%2Fcomplete%2F%3Fnext%3D%252F%26janrain_nonce%3D2010-02-22T18%253A49%253A53ZG2KXci&openid.assoc_handle=AOQobUepU4xs-kGg5LiyLzfN3RYv0I0Jocgjf_1odT4RR9zfMFpQVpMg&openid.signed=op_endpoint%2Cclaimed_id%2Cidentity%2Creturn_to%2Cresponse_nonce%2Cassoc_handle&openid.sig=Jf76i2RNhqpLTJMjeQ0nnQz6fgA%3D&openid.identity=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fid%3Fid%3DAItxxxxxxxxxs9CxHQ3PrHw_N5_3j1HM&openid.claimed_id=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fid%3Fid%3DAItOaxxxxxxxxxxx4s9CxHQ3PrHw_N5_3j1HM', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/532.9 (KHTML, like Gecko) Chrome/5.0.307.7 Safari/532.9', 'HTTP_X_FORWARDED_FOR': '96.8.31.235', 'PATH': '/usr/bin:/bin', 'PATH_INFO': u'/account/register/', 'PATH_TRANSLATED': '/home/spirituality/webapps/work/spirit_app.wsgi/account/register/', 'QUERY_STRING': '', 'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': '59956', 'REQUEST_METHOD': 'POST', 'REQUEST_URI': '/account/register/', 'SCRIPT_FILENAME': '/home/spirituality/webapps/spirituality/spirit_app.wsgi', 'SCRIPT_NAME': u'', 'SERVER_ADDR': '127.0.0.1', 'SERVER_ADMIN': '[no address given]', 'SERVER_NAME': 'workproject.com', 'SERVER_PORT': '80', 'SERVER_PROTOCOL': 'HTTP/1.0', 'SERVER_SIGNATURE': '', 'SERVER_SOFTWARE': 'Apache/2.2.12 (Unix) mod_wsgi/2.5 Python/2.5.4', 'mod_wsgi.application_group': 'www.workProject.com|', 'mod_wsgi.callable_object': 'application', 'mod_wsgi.listener_host': '', 'mod_wsgi.listener_port': '25931', 'mod_wsgi.process_group': '', 'mod_wsgi.reload_mechanism': '0', 'mod_wsgi.script_reloading': '1', 'mod_wsgi.version': (2, 5), 'wsgi.errors': <mod_wsgi.Log object at 0xb7ce0038>, 'wsgi.file_wrapper': <built-in method file_wrapper of mod_wsgi.Adapter object at 0xb7e94b18>, 'wsgi.input': <mod_wsgi.Input object at 0x999cc78>, 'wsgi.multiprocess': True, 'wsgi.multithread': False, 'wsgi.run_once': False, 'wsgi.url_scheme': 'http', 'wsgi.version': (1, 0)}>

3 个答案:

答案 0 :(得分:0)

不试图了解所有这些,为什么不插入:

import pdb; pdb.set_trace()

这个“注册”方法在哪里?

比击中“bt”

答案 1 :(得分:0)

答案 2 :(得分:0)

浏览django_authopenid / views.py后,似乎正在调用注册的部分是登录的一部分:

return ask_openid(request,   
                  form_signin.cleaned_data['openid_url'],   
                  redirect_to,   
                  on_failure=signin_failure,   
                  sreg_request=sreg_req)

我并不特别了解可以触发此注册请求的所有方法,但这是调用注册的部分。跟踪你的踪迹,阅读ask_openid的代码,了解你如何到达那里。