當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。