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


Python http.require_http_methods方法代码示例

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


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

示例1: project_admin_login_post_required

# 需要导入模块: from django.views.decorators import http [as 别名]
# 或者: from django.views.decorators.http import require_http_methods [as 别名]
def project_admin_login_post_required(f):
    # Wrap the function to do authorization and change arguments.
    def g(request, project_id, *args):
        # Get project, check authorization.
        project = get_object_or_404(Project, id=project_id)
        has_owner_project_portfolio_permissions = request.user.has_perm('can_grant_portfolio_owner_permission', project.portfolio)
        if request.user not in project.get_admins() and not has_owner_project_portfolio_permissions:
            return HttpResponseForbidden()

        # Call function with changed argument.
        return f(request, project)

    # Apply the require_http_methods decorator.
    g = require_http_methods(["POST"])(g)

    # Apply the login_required decorator.
    g = login_required(g)

    return g 
开发者ID:GovReady,项目名称:govready-q,代码行数:21,代码来源:views.py

示例2: test_client_credentials_grant_type

# 需要导入模块: from django.views.decorators import http [as 别名]
# 或者: from django.views.decorators.http import require_http_methods [as 别名]
def test_client_credentials_grant_type(self):
        fake_scopes_list = ['scopeone', 'scopetwo', INTROSPECTION_SCOPE]

        # Add scope for this client.
        self.client.scope = fake_scopes_list
        self.client.save()

        post_data = {
            'client_id': self.client.client_id,
            'client_secret': self.client.client_secret,
            'grant_type': 'client_credentials',
        }
        response = self._post_request(post_data)
        response_dict = json.loads(response.content.decode('utf-8'))

        # Ensure access token exists in the response, also check if scopes are
        # the ones we registered previously.
        self.assertTrue('access_token' in response_dict)
        self.assertEqual(' '.join(fake_scopes_list), response_dict['scope'])

        access_token = response_dict['access_token']

        # Create a protected resource and test the access_token.

        @require_http_methods(['GET'])
        @protected_resource_view(fake_scopes_list)
        def protected_api(request, *args, **kwargs):
            return JsonResponse({'protected': 'information'}, status=200)

        # Deploy view on some url. So, base url could be anything.
        request = self.factory.get(
            '/api/protected/?access_token={0}'.format(access_token))
        response = protected_api(request)
        response_dict = json.loads(response.content.decode('utf-8'))

        self.assertEqual(response.status_code, 200)
        self.assertTrue('protected' in response_dict)

        # Protected resource test ends here.

        # Verify access_token can be validated with token introspection

        response = self.request_client.post(
            reverse('oidc_provider:token-introspection'), data={'token': access_token},
            **self._password_grant_auth_header())
        self.assertEqual(response.status_code, 200)
        response_dict = json.loads(response.content.decode('utf-8'))
        self.assertTrue(response_dict.get('active'))

        # End token introspection test

        # Clean scopes for this client.
        self.client.scope = ''
        self.client.save()

        response = self._post_request(post_data)
        response_dict = json.loads(response.content.decode('utf-8'))

        # It should fail when client does not have any scope added.
        self.assertEqual(400, response.status_code)
        self.assertEqual('invalid_scope', response_dict['error']) 
开发者ID:juanifioren,项目名称:django-oidc-provider,代码行数:63,代码来源:test_token_endpoint.py


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