【1127 | Day67】频率组件的局部配置和全局配置(源码分析)
一、局部配置频率组件
1、一个请求过来,首先进入urls.py
文件
url(r'^book_cbv/', views.Book_cbv.as_view(),name="test3"),
2、进入Book_cbv
这个类,寻找as_view
方法,这个类是我们自己的定义的,我们根本就没有写as_view方法
class Book_cbv(APIView):
def get(self,request):
query_list = models.Book.objects.all()
# bs = book_serializers(query_list,many=True)
bs = bookmodelserializer(query_list,many=True,context={'request': request})
return Response(bs.data)
def post(self,request):
bs = bookmodelserializer(data=request.data)
print(request.data)
if bs.is_valid():
print(bs.validated_data)
bs.save()
return Response(bs.data)
else:
return Response(bs.errors)
3、进入父类寻找as_view方法,父类为APIView
,父类的as_view方法实际是执行APIView类的父类的view
方法
@classmethod
def as_view(cls, **initkwargs):
"""
Store the original class on the view function.
This allows us to discover information about the view when we do URL
reverse lookups. Used for breadcrumb generation.
"""
if isinstance(getattr(cls, 'queryset', None), models.query.QuerySet):
def force_evaluation():
raise RuntimeError(
'Do not evaluate the `.queryset` attribute directly, '
'as the result will be cached and reused between requests. '
'Use `.all()` or call `.get_queryset()` instead.'
)
cls.queryset._fetch_all = force_evaluation
view = super(APIView, cls).as_view(**initkwargs)
view.cls = cls
view.initkwargs = initkwargs
4、进入APIView类的父类View类,看下as_view方法干了什么,实际执行View类的as_view方法,就是执行APIView类的dispatch方法
@classonlymethod
def as_view(cls, **initkwargs):
"""
Main entry point for a request-response process.
"""
for key in initkwargs:
if key in cls.http_method_names:
raise TypeError("You tried to pass in the %s method name as a "
"keyword argument to %s(). Don't do that."
% (key, cls.__name__))
if not hasattr(cls, key):
raise TypeError("%s() received an invalid keyword %r. as_view "
"only accepts arguments that are already "
"attributes of the class." % (cls.__name__, key))
def view(request, *args, **kwargs):
self = cls(**initkwargs)
if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get
self.request = request
self.args = args
self.kwargs = kwargs
return self.dispatch(request, *args, **kwargs)
view.view_class = cls
view.view_initkwargs = initkwargs
# take name and docstring from class
update_wrapper(view, cls, updated=())
# and possible attributes set by decorators
# like csrf_exempt from dispatch
update_wrapper(view, cls.dispatch, assigned=())
return view
5、进入APIView类的dispatch方法,其实,我们这里需要关注的是initial
方法
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.
"""
self.args = args
self.kwargs = kwargs
request = self.initialize_request(request, *args, **kwargs)
self.request = request
self.headers = self.default_response_headers # deprecate?
try:
self.initial(request, *args, **kwargs)
# Get the appropriate handler method
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(),
self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
response = handler(request, *args, **kwargs)
except Exception as exc:
response = self.handle_exception(exc)
self.response = self.finalize_response(request, response, *args, **kwargs)
return self.response
6、进入initial方法,这里就到了我们的频率组件check_throttles
7、 进入check_throttles
方法
def check_throttles(self, request):
"""
Check if request should be throttled.
Raises an appropriate exception if the request is throttled.
"""
for throttle in self.get_throttles():
if not throttle.allow_request(request, self):
self.throttled(request, throttle.wait())
8、下面看下get_throttles
方法,看到这里我们是不是很清楚了?
我们需要在视图类中定义了一个throttle_classe
的列表,列表的内容是每个频率组件类的实例对象
def get_throttles(self):
"""
Instantiates and returns the list of throttles that this view uses.
"""
return [throttle() for throttle in self.throttle_classes]
9、频率组件必须要有allow_request
的方法,如果这个方法返回true,则通过频率认证,如果返回false,则没有通过频率组件。
这里的频率说的意思是某个用户在单位时间内访问网页的次数,我们这里就先简单的实现就可以了,判断频率的逻辑不是这里的重点
class throttlerate(object):
def allow_request(self,request,view):
return True
10、在视图类中使用频率组件
class Book_cbv(APIView):
authentication_classes = []
permission_classes = [SVIPpermission(),]
throttle_classes = [throttlerate(),]
def get(self,request):
query_list = models.Book.objects.all()
# bs = book_serializers(query_list,many=True)
bs = bookmodelserializer(query_list,many=True,context={'request': request})
二、全局配置频率组件
1、全局设置频率组件和全局设置权限组件也几乎是一样的,把频率组件放在一个单独的文件中
2、确定key值
3、在settings中引入配置的频率组件
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES":(
"app1.utils.Book_auther",
),
"DEFAULT_PERMISSION_CLASSES_CLASSES": (
"app1.utils.SVIPpermission",
),
"DEFAULT_DEFAULT_THROTTLE_CLASSES_CLASSES": (
"app1.utils.throttlerate",
)
}