1.请求的封装

class HttpRequest(object):
def __init__(self):
pass @propery
def GET(self):
pass @propery
def POST(self):
pass @propery
def body(self):
pass

class Request(object):
def __init__(self,request):
self._request = request def data(self):
if content-type == "application/json"
reutrn json.loads(self._request.body.decode('urf-8'))
elif content-type == "x-www-...":
return self._request.POST def query_params(self):
return self._reqeust.GET

req = HttpRequest()
request = Request(req)

request.data
request.query_prams
request._request.GET
request._request.POST
request._request.body

drf入口请求流程:

  • 路由

    urlpatterns = [
      url(r'^order/$', views.OrderView.as_view()),
    ]
  • 视图关系

    class View(object):
    @classonlymethod
       def as_view(cls, **initkwargs):
           def view(request, *args, **kwargs):
               return self.dispatch(request, *args, **kwargs)
           return view class APIView(View): @classmethod
       def as_view(cls, **initkwargs):
           view = super().as_view(**initkwargs)
           return csrf_exempt(view)
    def dispatch(self, request, *args, **kwargs):
           # 新request内部包含老request(_reuqest=老request)
           request = self.initialize_request(request, *args, **kwargs)
           self.request = request
           
           self.initial(request, *args, **kwargs)
           
           # 通过反射执行“get”方法,并传入新的request
           handler = getattr(self, request.method.lower())
           response = handler(request, *args, **kwargs) # get(requst)
           return self.response
       
    class OrderView(APIView):    def get(self,request,*args,**kwargs):
           return Response('海狗') ​

2.版本

from django.conf.urls import url,include
from django.contrib import admin
from . import views
urlpatterns = [
  url(r'^order/$', views.OrderView.as_view()),
]
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.request import Request

class OrderView(APIView):
  def get(self,request,*args,**kwargs):
      print(request.version)
      print(request.versioning_scheme)
      return Response('...')

  def post(self,request,*args,**kwargs):
      return Response('post')

源码流程:



class APIView(View):
   versioning_class = api_settings.DEFAULT_VERSIONING_CLASS
   
def dispatch(self, request, *args, **kwargs):
     
       # ###################### 第一步 ###########################
       """
      request,是django的request,它的内部有:request.GET/request.POST/request.method
      args,kwargs是在路由中匹配到的参数,如:
          url(r'^order/(\d+)/(?P<version>\w+)/$', views.OrderView.as_view()),
          http://www.xxx.com/order/1/v2/
      """
       self.args = args
       self.kwargs = kwargs

       """
      request = 生成了一个新的request对象,此对象的内部封装了一些值。
      request = Request(request)
          - 内部封装了 _request = 老的request
      """
       request = self.initialize_request(request, *args, **kwargs)
       self.request = request

       self.headers = self.default_response_headers  # deprecate?

       try:
           # ###################### 第二步 ###########################
           self.initial(request, *args, **kwargs)

           执行视图函数。。 def initial(self, request, *args, **kwargs):
     
       # ############### 2.1 处理drf的版本 ##############
       version, scheme = self.determine_version(request, *args, **kwargs)
       request.version, request.versioning_scheme = version, scheme
...
       
   def determine_version(self, request, *args, **kwargs):
       if self.versioning_class is None:
           return (None, None)
       scheme = self.versioning_class() # obj = XXXXXXXXXXXX()
       return (scheme.determine_version(request, *args, **kwargs), scheme)
       
class OrderView(APIView):
   versioning_class = URLPathVersioning
   def get(self,request,*args,**kwargs):
       print(request.version)
       print(request.versioning_scheme)
       return Response('...')

   def post(self,request,*args,**kwargs):
       return Response('post')
class URLPathVersioning(BaseVersioning):
   """
  urlpatterns = [
      url(r'^(?P<version>[v1|v2]+)/users/$', users_list, name='users-list'),
     
  ]
  """
   invalid_version_message = _('Invalid version in URL path.')

   def determine_version(self, request, *args, **kwargs):
       version = kwargs.get(self.version_param, self.default_version)
       if version is None:
           version = self.default_version

       if not self.is_allowed_version(version):
           raise exceptions.NotFound(self.invalid_version_message)
       return version

使用(局部)

  • url中写version

    url(r'^(?P<version>[v1|v2]+)/users/$', users_list, name='users-list'),
  • 在视图中应用

    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.request import Request
    from rest_framework.versioning import URLPathVersioning


    class OrderView(APIView):

       versioning_class = URLPathVersioning
       def get(self,request,*args,**kwargs):
           print(request.version)
           print(request.versioning_scheme)
           return Response('...')

       def post(self,request,*args,**kwargs):
           return Response('post')
  • 在settings中配置

    REST_FRAMEWORK = {
       "PAGE_SIZE":2,
       "DEFAULT_PAGINATION_CLASS":"rest_framework.pagination.PageNumberPagination",
       "ALLOWED_VERSIONS":['v1','v2'],
       'VERSION_PARAM':'version'
    }

使用(全局)推荐

  • url中写version

    url(r'^(?P<version>[v1|v2]+)/users/$', users_list, name='users-list'),

    url(r'^(?P<version>\w+)/users/$', users_list, name='users-list'),
  • 在视图中应用

    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.request import Request
    from rest_framework.versioning import URLPathVersioning


    class OrderView(APIView):
       def get(self,request,*args,**kwargs):
           print(request.version)
           print(request.versioning_scheme)
           return Response('...')

       def post(self,request,*args,**kwargs):
           return Response('post')
  • 在settings中配置

    REST_FRAMEWORK = {
       "PAGE_SIZE":2,
       "DEFAULT_PAGINATION_CLASS":"rest_framework.pagination.PageNumberPagination",
       "DEFAULT_VERSIONING_CLASS":"rest_framework.versioning.URLPathVersioning",
       "ALLOWED_VERSIONS":['v1','v2'],
       'VERSION_PARAM':'version'
    }

####

3.认证(面试)

from django.conf.urls import url,include
from django.contrib import admin
from . import views
urlpatterns = [
  url(r'^login/$', views.LoginView.as_view()),
  url(r'^order/$', views.OrderView.as_view()),
  url(r'^user/$', views.UserView.as_view()),
]
import uuid
from django.shortcuts import render
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from rest_framework.versioning import URLPathVersioning
from rest_framework.views import APIView
from rest_framework.response import Response

from . import models

class LoginView(APIView):

   def post(self,request,*args,**kwargs):
       user_object = models.UserInfo.objects.filter(**request.data).first()
       if not user_object:
           return Response('登录失败')
       random_string = str(uuid.uuid4())
       user_object.token = random_string
       user_object.save()
       return Response(random_string)

class MyAuthentication:
   def authenticate(self, request):
       """
      Authenticate the request and return a two-tuple of (user, token).
      """
       token = request.query_params.get('token')
       user_object = models.UserInfo.objects.filter(token=token).first()
       if user_object:
           return (user_object,token)
       return (None,None)

class OrderView(APIView):
   authentication_classes = [MyAuthentication, ]
   def get(self,request,*args,**kwargs):
       print(request.user)
       print(request.auth)
       return Response('order')

class UserView(APIView):
   authentication_classes = [MyAuthentication,]
   def get(self,request,*args,**kwargs):
       print(request.user)
       print(request.auth)
       return Response('user')

源码分析

class Request:

   def __init__(self, request,authenticators=None):
       self._request = request
       self.authenticators = authenticators or ()
       
@property
   def user(self):
       """
      Returns the user associated with the current request, as authenticated
      by the authentication classes provided to the request.
      """
       if not hasattr(self, '_user'):
           with wrap_attributeerrors():
               self._authenticate()
       return self._user
   
   def _authenticate(self):
       """
      Attempt to authenticate the request using each authentication instance
      in turn.
      """
       for authenticator in self.authenticators:
           try:
               user_auth_tuple = authenticator.authenticate(self)
           except exceptions.APIException:
               self._not_authenticated()
               raise

           if user_auth_tuple is not None:
               self._authenticator = authenticator
               self.user, self.auth = user_auth_tuple
               return

       self._not_authenticated()
       
@user.setter
   def user(self, value):
       """
      Sets the user on the current request. This is necessary to maintain
      compatibility with django.contrib.auth where the user property is
      set in the login and logout functions.

      Note that we also set the user on Django's underlying `HttpRequest`
      instance, ensuring that it is available to any middleware in the stack.
      """
       self._user = value
       self._request.user = value

class APIView(View):
   authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES
   
def dispatch(self, request, *args, **kwargs):
       """
      `.dispatch()` is pretty much the same as Django's regular dispatch,
      but with extra hooks for startup, finalize, and exception handling.
      """
       # ###################### 第一步 ###########################
       """
      request,是django的request,它的内部有:request.GET/request.POST/request.method
      args,kwargs是在路由中匹配到的参数,如:
          url(r'^order/(\d+)/(?P<version>\w+)/$', views.OrderView.as_view()),
          http://www.xxx.com/order/1/v2/
      """
       self.args = args
       self.kwargs = kwargs


       """
      request = 生成了一个新的request对象,此对象的内部封装了一些值。
      request = Request(request)
          - 内部封装了 _request = 老的request
          - 内部封装了 authenticators = [MyAuthentication(), ]
      """
       request = self.initialize_request(request, *args, **kwargs)
       self.request = request def initialize_request(self, request, *args, **kwargs):
       """
      Returns the initial request object.
      """
       parser_context = self.get_parser_context(request)

       return Request(
           request,
           parsers=self.get_parsers(),
           authenticators=self.get_authenticators(), # [MyAuthentication(),]
           negotiator=self.get_content_negotiator(),
           parser_context=parser_context
      )

   def get_authenticators(self):
       """
      Instantiates and returns the list of authenticators that this view can use.
      """
       return [ auth() for auth in self.authentication_classes ]
   
class LoginView(APIView):
   authentication_classes = []
   def post(self,request,*args,**kwargs):
       user_object = models.UserInfo.objects.filter(**request.data).first()
       if not user_object:
           return Response('登录失败')
       random_string = str(uuid.uuid4())
       user_object.token = random_string
       user_object.save()
       return Response(random_string)

class OrderView(APIView):
   # authentication_classes = [TokenAuthentication, ]
   def get(self,request,*args,**kwargs):
       print(request.user)
       print(request.auth)
       if request.user:
           return Response('order')
       return Response('滚')

class UserView(APIView):
   同上

总结

当用户发来请求时,找到认证的所有类并实例化成为对象列表,然后将对象列表封装到新的request对象中。

以后在视同中调用request.user

在内部会循环认证的对象列表,并执行每个对象的authenticate方法,该方法用于认证,他会返回两个值分别会赋值给
request.user/request.auth

作业:将认证的功能添加到呼啦圈中。

  • 登录表

  • 登录视图

  • 写认证类

  • 应用认证类:全局应用

  • Login视图不应用认证

4.权限

from rest_framework.permissions import BasePermission
from rest_framework import exceptions

class MyPermission(BasePermission):
  message = {'code': 10001, 'error': '你没权限'}
  def has_permission(self, request, view):
      """
      Return `True` if permission is granted, `False` otherwise.
      """
      if request.user:
          return True

      # raise exceptions.PermissionDenied({'code': 10001, 'error': '你没权限'})
      return False

  def has_object_permission(self, request, view, obj):
      """
      Return `True` if permission is granted, `False` otherwise.
      """
      return False
class OrderView(APIView):
  permission_classes = [MyPermission,]
  def get(self,request,*args,**kwargs):
      return Response('order')


class UserView(APIView):
  permission_classes = [MyPermission, ]
  def get(self,request,*args,**kwargs):
      return Response('user')
REST_FRAMEWORK = {
  "PAGE_SIZE":2,
  "DEFAULT_PAGINATION_CLASS":"rest_framework.pagination.PageNumberPagination",
  "DEFAULT_VERSIONING_CLASS":"rest_framework.versioning.URLPathVersioning",
  "ALLOWED_VERSIONS":['v1','v2'],
  'VERSION_PARAM':'version',
  "DEFAULT_AUTHENTICATION_CLASSES":["kka.auth.TokenAuthentication",]
}

源码分析

class APIView(View):
   permission_classes = api_settings.DEFAULT_PERMISSION_CLASSES
   
   def dispatch(self, request, *args, **kwargs):
       封装request对象
       self.initial(request, *args, **kwargs)
       通过反射执行视图中的方法

def initial(self, request, *args, **kwargs):
       版本的处理
       # 认证
       self.perform_authentication(request)        # 权限判断
       self.check_permissions(request)
       
       
       self.check_throttles(request)
       
   def perform_authentication(self, request):
       request.user    def check_permissions(self, request):
       # [对象,对象,]
       for permission in self.get_permissions():
           if not permission.has_permission(request, self):
               self.permission_denied(request, message=getattr(permission, 'message', None))
   def permission_denied(self, request, message=None):
       if request.authenticators and not request.successful_authenticator:
           raise exceptions.NotAuthenticated()
       raise exceptions.PermissionDenied(detail=message)
       
   def get_permissions(self):
       return [permission() for permission in self.permission_classes]
   
class UserView(APIView):
   permission_classes = [MyPermission, ]
   
   def get(self,request,*args,**kwargs):
       return Response('user')

实现呼啦圈

  • 视图:ListAPIView

  • 序列化:多个序列化

  • 分页:给列表页面做分页

  • 版本:版本设置

  • 认证:认证流程 request.user赋值

  • 权限:能否访问

drf(请求封装/认证/权限/节流)的更多相关文章

  1. drf:restful概念,类继承关系,drf请求封装,drf请求流程,版本控制组件,认证组件(token),权限组件

    1.restful规范 resfful规范的概念最重要: 是一套规范,规则,用于程序之间进行数据交换的约定. 他规定了一些协议,对我们感受最直接的就是,以前写增删改查的时候需要些四个视图寒素,rest ...

  2. (四) DRF认证, 权限, 节流

    一.Token 认证的来龙去脉 摘要 Token 是在服务端产生的.如果前端使用用户名/密码向服务端请求认证,服务端认证成功,那么在服务端会返回 Token 给前端.前端可以在每次请求的时候带上 To ...

  3. day74:drf:drf其他功能:认证/权限/限流/过滤/排序/分页/异常处理&自动生成接口文档

    目录 1.django-admin 2.认证:Authentication 3.权限:Permissions 4.限流:Throttling 5.过滤:Filtering 6.排序:OrderingF ...

  4. 8) drf 三大认证 认证 权限 频率

    一.三大认证功能分析 1)APIView的 dispath(self, request, *args, **kwargs) 2)dispath方法内 self.initial(request, *ar ...

  5. DRF 版本、认证、权限、限制、解析器和渲染器

    目录 一.DRF之版本控制 为什么要有版本控制? DRF提供的版本控制方案 版本的使用 全局配置 局部配置(使用较少) 二.DRF之认证 内置的认证 步骤 三.DRF之权限 1.自定义一个权限类 2. ...

  6. drf框架中认证与权限工作原理及设置

    0909自我总结 drf框架中认证与权限工作原理及设置 一.概述 1.认证 工作原理 返回None => 游客 返回user,auth => 登录用户 抛出异常 => 非法用户 前台 ...

  7. 实战-DRF快速写接口(认证权限频率)

    实战-DRF快速写接口 开发环境 Python3.6 Pycharm专业版2021.2.3 Sqlite3 Django 2.2 djangorestframework3.13 测试工具 Postma ...

  8. 【DRF框架】认证组件

    DRF框架的认证组件 核心代码:       self.perform_authentication(request)  框架自带模块:    from rest_framework import a ...

  9. 07 drf源码剖析之节流

    07 drf源码剖析之节流 目录 07 drf源码剖析之节流 1. 节流简述 2. 节流使用 3. 源码剖析 总结: 1. 节流简述 节流类似于权限,它确定是否应授权请求.节流指示临时状态,并用于控制 ...

随机推荐

  1. testNG报告优化,testNG-xslt

    一.在使用testNG自动化框架执行测试用例后,会自动生成HTML的测试报告,但是过于简单,信息展示极少,也没有图表说明,所有我们使用testNG-xslt进行美化. 二.具体实现步骤: 1.在网站下 ...

  2. echart封装,前端简单路由,图表设置自动化

    https://github.com/cclient/EhartDemoSetByAngular 后端node.js 前端插件 echart,jquery,jqueryui,datapicker,an ...

  3. keep pace with |sixes and sevens.|Three dozen of |setting out|in despite of|appetite for|brought up|.turn to|leave behind|As can be seen|every

    Heavy but not excessive: network capacity seems to have done little more than keep pace with economi ...

  4. Leetcode刷题记录 剑指offer

    面试题3:数组中重复数字 # 使用set,时间复杂度O(n),空间复杂度O(n)class Solution(object): def findRepeatNumber(self, nums): &q ...

  5. python IO多路复用版FTP

    需求: 实现文件上传及下载功能 支持多连接并发传文件 使用select or selectors

  6. mysqldump免密备份方法

    注意:1.暂时只试验了root用户     2.暂时只试验了5.6和5.7两个版本 1.我用的root用户,先进入家目录 cd ~ 2.vim .my.cnf #在家目录添加该文件 [mysqldum ...

  7. Jekyll的_config文件配置报错

    8 April 2019 by IchiehPan Jekyll 搭建博客时, 修改完_config.yml文件后就收到了github的build error邮件. 大致内容如下: did not f ...

  8. 机器学习Week3

    分类问题(classification problems) y=0 or 1 回归分析/逻辑分析(logistic regression): 目标:令h(x)位于[0,1]之间 逻辑函数/S型函数: ...

  9. Jquery 实现原理深入学习(3)

    前言 1.总体结构 √ 2.构建函数 √ 3.each功能函数实现 √ 4.map功能函数实现 √ 5.sizzle初步学习 6.attr功能函数实现 7.toggleClass功能函数实现(好伤) ...

  10. 【RxJava Demo分析】(二)Schedulers线程调度器 · Hans Zone

    用Schedulers(调度器)实现多任务(并发,Concurrency)的例子 废话不多说我们看一下有关于RxJava的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...