当前位置: 首页>>代码示例>>Python>>正文


Python Form.data["failed"]方法代码示例

本文整理汇总了Python中pyramid_simpleform.Form.data["failed"]方法的典型用法代码示例。如果您正苦于以下问题:Python Form.data["failed"]方法的具体用法?Python Form.data["failed"]怎么用?Python Form.data["failed"]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyramid_simpleform.Form的用法示例。


在下文中一共展示了Form.data["failed"]方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: login

# 需要导入模块: from pyramid_simpleform import Form [as 别名]
# 或者: from pyramid_simpleform.Form import data["failed"] [as 别名]

#.........这里部分代码省略.........
          >>> 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)
                # Fire a ``UserLoggedIn`` event.
                request.registry.notify(events.UserLoggedIn(request, user))
                # Redirect.
                return HTTPFound(location=location, headers=headers)
        form.data["failed"] = True
    # Set ``next`` no matter what.
    if next_:
        form.data["next"] = next_
    return {"renderer": FormRenderer(form)}
开发者ID:vickyi,项目名称:pyramid_simpleauth,代码行数:104,代码来源:view.py

示例2: signup_view

# 需要导入模块: from pyramid_simpleform import Form [as 别名]
# 或者: from pyramid_simpleform.Form import data["failed"] [as 别名]
def signup_view(request):
    """Render and handle signup form.

      Setup::

          >>> from mock import Mock
          >>> from pyramid.testing import DummyRequest
          >>> from pyramid_simpleauth import model, view
          >>> _get_existing_email = model.get_existing_email
          >>> _get_existing_user = model.get_existing_user
          >>> _save = model.save
          >>> _remember = view.remember
          >>> view.remember = Mock()
          >>> model.save = Mock()
          >>> model.get_existing_user = Mock()
          >>> model.get_existing_user.return_value = None
          >>> model.get_existing_email = Mock()
          >>> model.get_existing_email.return_value = None

      If it's not a POST, renders the form::

          >>> dummy_request = DummyRequest()
          >>> return_value = signup_view(dummy_request)
          >>> return_value['renderer'].data
          {'failed': False}

      Otherwise it validates the request data against ``schema.Signup``::

          >>> dummy_request = DummyRequest(post={'foo': 'bar'})
          >>> return_value = signup_view(dummy_request)
          >>> return_value['renderer'].data
          {'failed': True, 'foo': 'bar'}

      If provided with valid data, it saves a ``User`` with related ``Email``,
      logs them in by calling ``remember`` and redirects to the user's
      profile::

          >>> valid_post = {
          ...     'username': 'thruflo',
          ...     'email': '[email protected]',
          ...     'password': 'password',
          ...     'confirm': 'password'
          ... }
          >>> dummy_request = DummyRequest(post=valid_post)
          >>> dummy_request.registry.settings = {}
          >>> dummy_request.route_url = Mock()
          >>> return_value = signup_view(dummy_request)
          >>> model.save.called
          True
          >>> view.remember.called
          True
          >>> isinstance(return_value, HTTPFound)
          True

      Teardown::

          >>> view.remember = _remember
          >>> model.save = _save
          >>> model.get_existing_user = _get_existing_user
          >>> model.get_existing_email = _get_existing_email

    """

    form = Form(request, schema=schema.Signup, defaults={"failed": False})
    if request.method == "POST":
        if form.validate():
            d = form.data
            # Determine whether to skip confirmation.
            s = request.registry.settings
            should_skip_confirmation = s.get("auth.skip_confirmation", False)
            # Instantiate the email instance.
            email = model.Email()
            email.address = d["email"]
            email.is_confirmed = should_skip_confirmation
            # Instantiate the user instance.
            user = model.User()
            user.username = d["username"]
            user.password = model.encrypt(d["password"])
            user.emails = [email]
            # Save the user and email to the db.
            model.save(user)
            # Log the user in.
            remember(request, user.canonical_id)
            # Fire a ``UserSignedUp`` event.
            request.registry.notify(events.UserSignedUp(request, user))
            # Redirect to the user's profile url.
            settings = request.registry.settings
            route_name = settings.get("simpleauth.after_signup_route", "users")
            try:
                location = request.route_url(route_name, traverse=(user.username,))
            except (KeyError, ComponentLookupError):
                location = "/"
            return HTTPFound(location=location)
        form.data["failed"] = True
    return {"renderer": FormRenderer(form)}
开发者ID:vickyi,项目名称:pyramid_simpleauth,代码行数:97,代码来源:view.py


注:本文中的pyramid_simpleform.Form.data["failed"]方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。