一、自定义频率类

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])

二、频率源码

1.源码分析

# 1、在进入源码里
class MyThrottling(SimpleRateThrottle):  # 这里进入
    scope = 'ip_1m_3' 
    
# 2、利用反射机制找属性
class SimpleRateThrottle(BaseThrottle):
    cache = default_cache  # 缓存
    timer = time.time      # 当前时间
    cache_format = 'throttle_%(scope)s_%(ident)s'
    scope = None  # 配置中key
    THROTTLE_RATES = api_settings.DEFAULT_THROTTLE_RATES  # 配置信息
    
    
    def __init__(self):
        if not getattr(self, 'rate', None):
            # 调用了get_rate方法
            self.rate = self.get_rate()    # rate:3/m
        # 将频率配置解析成次数和时间,分别 存放到self.num_requests、self.duration 
        self.num_requests, self.duration = self.parse_rate(self.rate)  # 调用了parse_rate方法
        
# 3、进入get_rate方法
    def get_rate(self):
        if not getattr(self, 'scope', None):
            msg = ("You must set either `.scope` or `.rate` for '%s' throttle" %
                   self.__class__.__name__)
            raise ImproperlyConfigured(msg)

        try:
            # 根据key取出values值,把values值返回__init__
            return self.THROTTLE_RATES[self.scope]  # scope就是配置文件中的"scope"
        except KeyError:
            msg = "No default throttle rate set for '%s' scope" % self.scope
            raise ImproperlyConfigured(msg)
            
# 4、进入parse_rate方法
    def parse_rate(self, rate):
        if rate is None:
            return (None, None)
        # 通过/把"3/m"转成了3  m,并且解压赋值
        num, period = rate.split('/')
        # 把3转为整形赋值给num_requests
        num_requests = int(num)
        # 根据字段0的索引取值,m对应'm': 60
        duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]]
        # 把3和60返回到__init__
        return (num_requests, duration)
    
# 5、走完上面几步,证明了有值,往下面走
    def allow_request(self, request, view):
        if self.rate is None:  # 如果没有值返回True
            return True
        
        # 当前登录用户的IP地址
        self.key = self.get_cache_key(request, view) # 返回的是ip地址
        # 判断是否有值,如果返回的是None,就不做限制
        if self.key is None:
            return True
        
        # 初次访问缓存为空,self.history为[],是存放时间的列表
        self.history = self.cache.get(self.key, []) # cache.get:获取缓存
        # 获取当前的时间,存放到self.now
        self.now = self.timer()
        
        # 判断self.history是否有值,并且当前时间与第一次访问时间间隔如果大于60s,第一次记录清除,不算作一次计数
        # self.now:10:56  比如
        # self.duration:[10:23,10:55]
        while self.history and self.history[-1] <= self.now - self.duration:
            self.history.pop()  # 有值,删除缓存
            
        # 用history和 限制次数3进行比较
        # history长度第一次访问是0,第二次访问是1,第三次访问是2,直到第四次访问是3为失败
        if len(self.history) >= self.num_requests:  # self.num_requests:是解压赋值中的3
            # 直接返回False,代表频率限制
            return self.throttle_failure()
        # 如果history的长度未达到限制次数,代表可以访问
        # 将当前时间插入到history列表的开头,将history列表作为数据存到缓存中
        return self.throttle_success()  # 调用了throttle_success方法
    
# 6、进入了throttle_success方法
    def throttle_success(self):
        # 把将当前时间插入到history列表的开头
        self.history.insert(0, self.now)
        # 将history列表作为数据存到缓存中,key就是'ip_1m_3': '3/m' 中的ip_1m_3
        # self.duration就是超时时间60s就会删除
        self.cache.set(self.key, self.history, self.duration) # duration:就是__init__解析的60
        return True
    
# 7、最后计算下一次访问需要等待的时间
    def wait(self):
        if self.history:
            remaining_duration = self.duration - (self.now - self.history[-1])
        else:
            remaining_duration = self.duration

        available_requests = self.num_requests - len(self.history) + 1
        if available_requests <= 0:
            return None

        return remaining_duration / float(available_requests)

2.总结

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

三、分页Pagination

1.简介

  • 查询所有的接口才需要分页
  • 分页后端写法是固定的,前端展现形式是不一样的
    • pc端的下一页的点击
    • app中,翻页是下拉加载更多

2.drf分页使用

  1. 写一个类,继承drf提供的三个分页类之一

  2. 重写某几个类属性

  3. 把它配置在继承自GenericAPIView+ListModelMixin的子视图类上

  4. 如果继承的是APIView,需要自己写

    page = MyPageNumberPagination()
    res = page.paginate_queryset(qs, request)
    

3.drf提供的三个分页类

3.1.PageNumberPagination

前端访问网址形式:

GET  http://127.0.0.1:8000/students/?page=4

可以在子类中定义的属性:

  • page_size:每页数目
  • page_query_param:前端发送的页数关键字名,默认为”page”
  • page_size_query_param:前端发送的每页数目关键字名,默认为None
  • max_page_size:前端最多能设置的每页数量

代码示例

# pagination.py
from rest_framework.pagination import PageNumberPagination


class MyPageNumberPagination(PageNumberPagination):  # 这个用的最多,其他了解
    # 写4个类属性即可
    page_size = 4  # 每页显示的条数
    page_query_param = 'page'  # /books/?page=3  查询第几页的参数
    page_size_query_param = 'size'  # /books/?page=3&size=4  # 查询第三页,每页显示4条
    max_page_size = 10  # 限制通过size查询,最大的条数
    
    
from .pagination import MyPageNumberPagination


class BookView(ViewSetMixin, ListAPIView, CreateModelMixin):
    pagination_class = MyPageNumberPagination  # 必须继承GenericAPIView才有这个类属性
    queryset = Book.objects.all()
    serializer_class = BookSerializer

3.2.LimitOffsetPagination

前端访问网址形式:

GET http://127.0.0.1/four/students/?limit=100&offset=400

可以在子类中定义的属性:

  • default_limit:默认限制,默认值与PAGE_SIZE设置一直
  • limit_query_param:limit参数名,默认’limit’
  • offset_query_param:offset参数名,默认’offset’
  • max_limit:最大limit限制,默认None

代码示例

# pagination.py
from rest_framework.pagination import LimitOffsetPagination


class MyLimitOffsetPagination(LimitOffsetPagination):
    default_limit = 3  # 每页显示的条数
    limit_query_param = 'limit'  # /books/?limit=4   这一页显示4条数据
    offset_query_param = 'offset'  # /books/?offset=3&limit=4  # 从第3条开始,取4条数据
    max_limit = 5  # 限制最多显示多少条

    
from .pagination import MyLimitOffsetPagination

        
class BookView(ViewSetMixin, ListAPIView, CreateModelMixin):
    pagination_class = MyLimitOffsetPagination  # 必须继承GenericAPIView才有这个类属性
    queryset = Book.objects.all()
    serializer_class = BookSerializer

3.3.CursorPagination

前端访问网址形式:

GET http://127.0.0.1/four/students/?cursor=cD0xNQ%3D%3D

可以在子类中定义的属性:

  • cursor_query_param:默认查询字段,不需要修改
  • page_size:每页数目
  • ordering:按什么排序,需要指定

代码示例

# pagination.py
from rest_framework.pagination import CursorPagination


class MyCursorPagination(CursorPagination): # 只能上一页和下一页,它的分页效率是最高的,高于上面所有的分页方式,大数据量的分页,建议使用这种
    cursor_query_param = 'cursor'
    page_size = 3  #每页显示条数
    ordering = 'id'  # 排序,必须是表中得字段
    
    
from .pagination import MyCursorPagination

        
class BookView(ViewSetMixin, ListAPIView, CreateModelMixin):
    pagination_class = MyCursorPaginationn  # 必须继承GenericAPIView才有这个类属性
    queryset = Book.objects.all()
    serializer_class = BookSerializer

四、排序功能

1.简介

  • 查询所有才涉及到排序,其他接口都不需要
  • 排序和分页能一起用,但是是先排序后分页

2.使用

必须是继承GenericAPIView+ListModelMixin的子视图类上

  1. 配置排序类

    filter_backends=[OrderingFilter,]
    
  2. 配置排序字段

    ordering_fields=['id','price']
    
  3. 支持前端的访问形式

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

纯自己写的,继承了APIView的,需要自己从请求地址中取出排序规则,自己排序

'price','-id'=reqeust.query_params.get('ordering').split(',')
-qs = Book.objects.all().order_by('price','-id')

五、过滤功能

内置过滤

1.简介

  • 查询所有才涉及到过滤,其它接口都不需要
  • restful规范中有一条,请求地址中带过滤条件:分页、排序、过滤的统称为过滤
  • 内置过滤类只能通过search写条件

2.使用步骤

  1. 必须是继承GenericAPIView+ListModelMixin的子视图类上

  2. 配置过滤类

    filter_backends=[SearchFilter,]
    
  3. 配置过滤字段

    ordering_fields=['name','publish']
    
  4. 支持前端的访问形式

    http://127.0.0.1:8000/books/?search=三 # 只要name中或publish中有三都能搜出来
    

作业

# 自定义频率类
class BaseThrottling(BaseThrottle):
    VISIT_RECORD = {}

    def __init__(self):
        self.history = None

    def allow_request(self, request, view):
        ip = request.META.get('REMOTE_ADDR')
        ctime = time.time()
        if ip not in self.VISIT_RECORD:
            self.VISIT_RECORD[ip] = [ctime, ]
            return True
        self.history = self.VISIT_RECORD.get(ip, [])
        while self.history and ctime - self.history[-1] > 60:
            self.history.pop()
        if len(self.history) < 3:
            self.history.insert(0, ctime)
            return True
        else:
            return False

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

pagination.py

from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination


class MyPageNumberPagination(PageNumberPagination):
    page_size = 4
    page_query_param = 'page'
    page_size_query_param = 'size'
    max_page_size = 10


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


class MyCursorPagination(CursorPagination):
    cursor_query_param = 'cursor'
    page_size = 3
    ordering = 'id'

views.py

from django.shortcuts import render

# Create your views here.
from rest_framework.filters import OrderingFilter, SearchFilter
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ViewSetMixin

from app01.models import Book
from app01.pagination import MyPageNumberPagination
from app01.serializer import BookSerializer


class BookView(ViewSetMixin, ListAPIView):
    pagination_class = MyPageNumberPagination
    queryset = Book.objects.all()
    serializer_class = BookSerializer

    filter_backends = [OrderingFilter, ]
    ordering_fields = ['id', 'price']

    # filter_backends = [SearchFilter, ]
    # search_fields = ['name']

models.py

from django.db import models


# Create your models here.
class Book(models.Model):
    name = models.CharField(max_length=32)
    price = models.IntegerField()
    publish = models.CharField(max_length=32)

serializer.py

from rest_framework import serializers

from app01.models import Book


class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = "__all__"

urls.py

from django.contrib import admin
from django.urls import path
from rest_framework.routers import SimpleRouter

from app01 import views

router = SimpleRouter()
router.register('books', views.BookView, 'books')

urlpatterns = [
    path('admin/', admin.site.urls),
]
urlpatterns += router.urls
 posted on 2022-10-10 21:41  念白SAMA  阅读(22)  评论(0编辑  收藏  举报