Ejemplo n.º 1
0
def login(context, request):
    login_url = resource_url(request.context, request, 'login')
    referrer = request.url
    if referrer == login_url:
        referrer = '/' # never use the login form itself as came_from
    came_from = request.params.get('came_from', referrer)

    tpl = 'finlin:templates/login_form.pt'
    tpl_vars = { 
        'main': get_renderer('finlin:templates/master.pt').implementation(),
        'save_url': login_url,
        'came_from': came_from,
        'submit_label': 'Login'} 

    if 'form.submitted' in request.params:
        schema = LoginForm() 
        try:
            params = schema.to_python(request.params, request)
        except (formencode.Invalid), e:
            htmlfilled = htmlfill.render(   
                                   render(tpl, tpl_vars, request),
                                   defaults=e.value,
                                   errors=e.error_dict) 
            return Response(htmlfilled)
        try:
            doc = collection.find_one({'username':params['username']})
            if doc is None:
                raise Exception
            result = User.check_password(doc['password'], value)
            if result is False:
                raise Exception
        except (ValueError, TypeError, Exception): 
            htmlfilled = htmlfill.render(
                                render(tpl, tpl_vars, request),
                                defaults=params)
            message = ['The username or password you ',
                       'provided does not match our records.']
            request.session.flash(''.join(message))
            return Response(htmlfilled)
        else:
           headers = remember(request, username)
           return HTTPFound(location = came_from, headers = headers)
Ejemplo n.º 2
0
def add_account(context, request):
    tpl = 'finlin:templates/login_form.pt'
    tpl_vars = { 
        'main': get_renderer('finlin:templates/master.pt').implementation(),
        'save_url': resource_url(context, request, self.__name__),
        'submit_label': 'create account'} 

    if 'form.submitted' in request.params:
        schema = CreateAccountForm() 
        try:
            params = schema.to_python(request.params, request)
        except formencode.Invalid, e:
            html = htmlfill.render(
                                render(tpl, tpl_vars, request),
                                defaults=e.value,
                                errors=e.error_dict) 
            return Response(html)
        else:
            params['created'] = datetime.datetime.now()
            params['password']= User.set_password(params['password']) 
            request.db.user.save(params)
            request.session.flash('Welcome ' + params['username'])
            return HTTPFound(location = request.application_url)