drf之分页、排序、过滤以及频率源码分析

自定义频率类

from rest_framework.throttling import BaseThrottle
class MyThrottle(BaseThrottle):
    VISIT_RECORD = {}  # 存放用户访问记录{ip1:[时间1,时间2],ip2:[时间1,时间2],'192.168.1.101':[当前时间,]}
def __init__(self):
    self.history = None
def allow_request(self, request, view):
    # 在这里写逻辑:根据ip地址判断用户是不是超过了频率限制
    # (1)取出访问者ip
    ip = request.META.get('REMOTE_ADDR')
    import time
    ctime = time.time() # 取出当前时间
    # (2)判断当前ip不在访问字典里,添加进去,并且直接返回True,表示第一次访问
    if ip not in self.VISIT_RECORD:
        self.VISIT_RECORD[ip] = [ctime, ]
        return True
    self.history = self.VISIT_RECORD.get(ip) # 当前访问者的时间列表 [时间2,]
    # (3)循环判断当前ip的时间列表,有值,并且当前时间减去列表的最后一个时间大于60s,把这种数据pop掉,这样列表中只有60s以内的访问时间,
    while self.history and -ctime + self.history[-1] < 60: #循环结束后,剩下的都是1分钟以后访问的时间
        self.history.pop()
    # (4)判断,当列表小于3,说明一分钟以内访问不足三次,把当前时间插入到列表第一个位置,返回True,顺利通过
    # (5)当大于等于3,说明一分钟内访问超过三次,返回False验证失败
    if len(self.history) < 3:
        self.history.insert(0, ctime)
        return True
    else:
        return False

def wait(self):
    import time
    ctime = time.time()
    return 60 - (ctime - self.history[-1])

频率功能源码剖析

# SimpleRateThrottle
	-源码里执行的频率类的allow_request,读SimpleRateThrottle的allow_request
    
class SimpleRateThrottle(BaseThrottle):
    cache = default_cache
    timer = time.time
    cache_format = 'throttle_%(scope)s_%(ident)s'
    scope = None
    THROTTLE_RATES = api_settings.DEFAULT_THROTTLE_RATES
    def __init__(self):  # 只要类实例化得到对象就会执行,一执行,self.rate就有值了,而且self.num_requests和self.duration
        if not getattr(self, 'rate', None): # 去频率类中反射rate属性或方法,发现没有,返回了None,这个if判断就符合,执行下面的代码
            self.rate = self.get_rate()  #返回了  '3/m'
        #  self.num_requests=3
        #  self.duration=60
        self.num_requests, self.duration = self.parse_rate(self.rate)

    def get_rate(self):
         return self.THROTTLE_RATES[self.scope] # 字典取值,配置文件中咱们配置的字典{'ss': '3/m',},根据ss取到了 '3/m'

    def parse_rate(self, rate):
        if rate is None:
            return (None, None)
        # rate:字符串'3/m'  根据 / 切分,切成了 ['3','m']
        # num=3,period=m
        num, period = rate.split('/')
        # num_requests=3  数字3
        num_requests = int(num)
        # period='m'  ---->period[0]--->'m'
        # {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]]
        # duration=60
        duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]]
        # 3     60
        return (num_requests, duration)

    def allow_request(self, request, view):
        if self.rate is None:
            return True
        # 咱们自己写的,返回什么就以什么做限制  咱们返回的是ip地址
        # self.key=当前访问者的ip地址
        self.key = self.get_cache_key(request, view)
        if self.key is None:
            return True
        # self.history 访问者的时间列表,从缓存中拿到,如果拿不到就是空列表,如果之前有 [时间2,时间1]
        self.history = self.cache.get(self.key, [])
        # 当前时间
        self.now = self.timer()
        while self.history and self.history[-1] <= self.now - self.duration:
            self.history.pop()
        if len(self.history) >= self.num_requests:
            return self.throttle_failure()
        return self.throttle_success()
    

总结:以后再写频率类,只需要继承SimpleRateThrottle,重写get_cache_key,配置类属性scope,配置文件中配置一下就可以了。

分页功能

drf中分页的使用

  • 写一个类,继承drf提供的三个分页类之一
  • 重写其中的类属性
  • 将其配置在继承GenricAPIView+ListModelMixin的子视图类上
from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination,CursorPagination

class PagePagination(PageNumberPagination):
    page_size =3
    page_query_param = 'page'  # 'p'
    page_size_query_param ='size'
    max_page_size = 5

class LimitOffsetPaginate(LimitOffsetPagination):
    default_limit = 3
    limit_query_param = 'limit'
    offset_query_param = 'offset'
    max_limit = 5

class CursorPaginate(CursorPagination):
    cursor_query_param = 'cursor'
    page_size = 3
    ordering = 'pk'

排序功能

  • 排序类from rest_framework.filters.OrderingFilter
  • 必须是继承GenericAPIView+ListModelMixin的子视图类上
  • 配置视图
# 配置排序类:
filter_backends=[OrderingFilter,]
#配置排序的字段
ordering_fields=['id','price']

# 前端的访问形式
http://127.0.0.1:8000/books/?ordering=-price,id # 先按价格的降序排,如果价格一样再按id的升序排

过滤功能

  • 过滤类from rest_framework.filters.SearchFilter
  • 必须是继承GenericAPIView+ListModelMixin的子视图类上
  • 配置视图
filter_backends=[SearchFilter]
search_fields=['title','price']   # 有源码可以该通过反射search_fields查到该视图类中有无配置

三种分页方式加过滤

# views.py
class BookView(ViewSetMixin,ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
    pagination_class = PagePagination
    # pagination_class = LimitOffsetPaginate
    # pagination_class = CursorPaginate
    filter_backends=[SearchFilter]
    search_fields=['title','price']
    # search_param=['name','price']
    
# 
from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination,CursorPagination

class PagePagination(PageNumberPagination):
    page_size =3
    page_query_param = 'page'  # 'p'
    page_size_query_param ='size'
    max_page_size = 5

class LimitOffsetPaginate(LimitOffsetPagination):
    default_limit = 3
    limit_query_param = 'limit'
    offset_query_param = 'offset'
    max_limit = 5

class CursorPaginate(CursorPagination):
    cursor_query_param = 'cursor'
    page_size = 3
    ordering = 'pk'

继承APIView,实现分页

class BooksView(APIView):
    def get(self,request):
        book_query = Book.objects.all()
        page_obj = PagePaginate(request.query_params,book_query)
        queryset=page_obj.filter_queryset()
        ser = BookSerializer(instance=queryset, many=True)
        return Response(page_obj.show_result(ser))
class PagePaginate:
    page_size=10
    page_query_param='page'
    page_size_query_param='size'
    max_page_size=10
    def __init__(self,data,book_query):
        self.data=data
        self.start=1
        self.book_query=book_query
        self.end=self.page_size
    @property
    def page_num(self):
        if not self.data.get(self.page_query_param):
            return 1
        return int(self.data.get(self.page_query_param))
    def get_start_and_end_num(self):
        if self.page_query_param in self.data:
            start=(self.page_size * (self.page_num-1))+1
            end=self.page_num*self.page_size
            return start,end
    def filter_queryset(self):
        try:
            start, end = self.get_start_and_end_num()
        except:
            start=self.start
            end=self.end
        return self.book_query.filter(pk__range=(start,end))
    def show_result(self,ser):
        url = f'http://127.0.0.1:8000/all_books/?page='
        if self.page_num == 1:
            previous = None
        else:
            previous = url + f'{(self.page_num - 1)}'
        status_code = {
            'count': self.book_query.count(),
            'next': url + f'{(self.page_num + 1)}',
            'previous': previous,
            "results": ser.data
        }
        page_count, n = divmod(self.book_query.count(), self.page_size)
        if n:
            page_count = page_count + 1
        if page_count < self.page_num:
            raise ValidationError('Invalid page.')
        return status_code
posted @ 2022-10-10 22:53  荀飞  阅读(33)  评论(0编辑  收藏  举报