本文整理汇总了Python中pyramid_simpleform.Form.data['next']方法的典型用法代码示例。如果您正苦于以下问题:Python Form.data['next']方法的具体用法?Python Form.data['next']怎么用?Python Form.data['next']使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyramid_simpleform.Form
的用法示例。
在下文中一共展示了Form.data['next']方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: change_password
# 需要导入模块: from pyramid_simpleform import Form [as 别名]
# 或者: from pyramid_simpleform.Form import data['next'] [as 别名]
def change_password(request):
"""Change user password."""
# Unpack.
user = request.user
notify = request.registry.notify
# Validate the request.
form = Form(request, schema=schema.ChangePassword,
defaults={'failed': False})
location = get_redirect_location(request)
if request.method == 'POST':
if form.validate():
d = form.data
user = model.authenticate(user.username, d['old_password'])
if user:
# Save new password to the db.
user.password = model.encrypt(d['new_password'])
model.save(user)
# Notify that the password changed.
notify(events.UserChangedPassword(request, user))
# Log the user out, so that a change of password will lock out
# someone who has compromised the existing password.
headers = forget(request)
# Notify that the user is logged out.
notify(events.UserLoggedOut(request, request.user))
# Redirect.
return HTTPFound(location=location, headers=headers)
else:
form.errors['old_password'] = 'Wrong current password.'
form.data['next'] = location
return {'renderer': FormRenderer(form), 'user': request.user}
示例2: change_username
# 需要导入模块: from pyramid_simpleform import Form [as 别名]
# 或者: from pyramid_simpleform.Form import data['next'] [as 别名]
def change_username(request):
"Change username"
form = Form(request, schema=schema.ChangeUsername)
user = request.user
if request.method == 'POST':
if form.validate():
user.username = form.data['username']
model.save(user)
request.registry.notify(events.UserChangedUsername(request, user))
# Get location based on new username
location = get_redirect_location(request)
return HTTPFound(location=location)
# Get location based on unchanged username
location = get_redirect_location(request)
form.data['next'] = location
return {'renderer': FormRenderer(form), 'user': user}
示例3: change_password
# 需要导入模块: from pyramid_simpleform import Form [as 别名]
# 或者: from pyramid_simpleform.Form import data['next'] [as 别名]
def change_password(request):
"""Change user password."""
form = Form(request, schema=schema.ChangePassword,
defaults={'failed': False})
user = request.user
location = get_redirect_location(request)
if request.method == 'POST':
if form.validate():
d = form.data
user = model.authenticate(user.username, d['old_password'])
if user:
# Save new password to the db
user.password = model.encrypt(d['new_password'])
model.save(user)
request.registry.notify(
events.UserChangedPassword(request, user))
return HTTPFound(location=location)
else:
form.errors['old_password'] = 'Wrong current password.'
form.data['next'] = location
return {'renderer': FormRenderer(form), 'user': request.user}
示例4: login
# 需要导入模块: from pyramid_simpleform import Form [as 别名]
# 或者: from pyramid_simpleform.Form import data['next'] [as 别名]
def login(request):
"""Render login form. If posted a ``username`` and ``password``, attempt
to authenticate the user using the credentials provided. If
authentication if successful, redirect the user whence they came.
Setup::
>>> from mock import Mock, MagicMock
>>> from pyramid.testing import DummyRequest
>>> from pyramid import security
>>> from pyramid_simpleauth import model, view
>>> _authenticate = model.authenticate
>>> model.authenticate = Mock()
If it's not a POST, renders the form::
>>> dummy_request = DummyRequest()
>>> dummy_request.registry.settings = {}
>>> return_value = login(dummy_request)
>>> return_value['renderer'].data
{'failed': False}
Otherwise validates the request::
>>> dummy_request = DummyRequest(post={'foo': 'bar'})
>>> dummy_request.registry.settings = {}
>>> return_value = login(dummy_request)
>>> return_value['renderer'].data['failed']
True
Otherwise tries to authenticate the credentials::
>>> model.authenticate.return_value = None
>>> valid_post = {
... 'username': 'thruflo',
... 'password': 'password'
... }
>>> dummy_request = DummyRequest(post=valid_post)
>>> dummy_request.registry.settings = {}
>>> return_value = login(dummy_request)
>>> model.authenticate.assert_called_with('thruflo', 'password')
If they don't match::
>>> return_value['renderer'].data['failed']
True
If they do, redirects with the user's canonical id remembered::
>>> mock_user = Mock()
>>> mock_user.canonical_id = 'abc'
>>> model.authenticate.return_value = mock_user
>>> dummy_request = DummyRequest(post=valid_post)
>>> dummy_request.registry.settings = {}
>>> return_value = login(dummy_request)
>>> isinstance(return_value, HTTPFound)
True
>>> return_value.location
'/'
Redirecting to ``next`` if provided::
>>> data = {
... 'username': 'thruflo',
... 'password': 'password',
... 'next': '/foo/bar'
... }
>>> dummy_request = DummyRequest(post=data)
>>> dummy_request.registry.settings = {}
>>> return_value = login(dummy_request)
>>> return_value.location
'/foo/bar'
n.b.: If ``next`` is invalid, it defaults to '/' rather than failing::
>>> data['next'] = '$do.evil(h4x);'
>>> dummy_request = DummyRequest(post=data)
>>> return_value = login(dummy_request)
>>> return_value.location
'/'
Teardown::
>>> model.authenticate = _authenticate
"""
next_ = validate_next_param(request)
# Validate the rest of the user input.
form = Form(request, schema=schema.Login, defaults={'failed': False})
if request.method == 'POST':
if form.validate():
d = form.data
user = model.authenticate(d['username'], d['password'])
if user:
# Remember the logged in user.
headers = remember(request, user.canonical_id)
# Work out where to redirect to next.
location = get_redirect_location(request, user,
route_name='index', view_name=None)
#.........这里部分代码省略.........