示例#1
0
文件: views.py 项目: victorlin/ez2pay
def logout(request):
    _ = get_localizer(request)
    
    referrer = request.referrer
    my_url = request.route_url('account.logout')
    # never use the login form itself as came_from
    # or, there is no referer
    if referrer == my_url or not referrer:
        referrer = request.route_url('front.home') 
    came_from = request.params.get('came_from', referrer)
    
    session = request.db_session
    user_model = UserModel(session)
    user_id = authenticated_userid(request)
    if user_id is None:
        raise HTTPBadRequest('You are not logged in')
    
    user = user_model.get(user_id)
    if user is None:
        raise HTTPBadRequest
    
    headers = forget(request)
    name = user.display_name or user.user_name
    msg = _(u"Hope we will see you soon, ${user_name}",
            mapping=dict(user_name=name))
    
    request.add_flash(msg)
    return HTTPFound(location=came_from,
                     headers=headers)
示例#2
0
文件: views.py 项目: victorlin/ez2pay
def login(request):
    """Display login form or do login
    
    mainly borrowed from 
    https://docs.pylonsproject.org/projects/pyramid/1.1/tutorials/wiki/authorization.html?highlight=login#adding-login-and-logout-views
    
    """
    from ez2pay.models.user import BadPassword
    from ez2pay.models.user import UserNotExist
    
    _ = get_localizer(request)
    
    referrer = request.url
    my_url = request.route_url('account.login')
    if referrer == my_url:
        referrer = request.route_url('front.home')
        # never use the login form itself as came_from
    came_from = request.params.get('came_from', referrer)
    
    username_or_email = ''
    password = ''
    
    factory = FormFactory(_)
    LoginForm = factory.make_login_form()
    form = LoginForm(request.params)
    
    if request.method == 'POST' and form.validate():
        username_or_email = request.params['username_or_email']
        password = request.params['password']
        error = False
        session = request.db_session
        user_model = UserModel(session)
        try:
            user_id = user_model.authenticate_user(username_or_email, password)
        except (UserNotExist, BadPassword):
            msg = _(u'Wrong username or password')
            request.add_flash(msg, 'error')
            return dict(
                came_from=came_from,
                username_or_email=username_or_email,
                password=password,
                form=form,
            )
            
        if user_id is not None and not error:
            headers = remember(request, user_id)
            
            user = user_model.get(user_id)
            msg = _(u"Welcome back, ${user_name}", 
                    mapping=dict(user_name=user.user_name))
            request.add_flash(msg)
            return HTTPFound(location=came_from,
                             headers=headers)

    return dict(
        came_from=came_from,
        username_or_email=username_or_email,
        password=password,
        form=form,
    )