mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 20:40:14 +03:00
Use properties to lazily retrieve policy settings.
Policy values are fetched at import-time, so they don't reflect changes to settings. Using a property, the values could be overriden in a subclass, changed with a setter, or reflect changes in settings by getting them lazily (which aids in testing).
This commit is contained in:
parent
6a7d34ec34
commit
fab85c4c83
|
@ -97,16 +97,6 @@ def exception_handler(exc, context):
|
||||||
|
|
||||||
class APIView(View):
|
class APIView(View):
|
||||||
|
|
||||||
# The following policies may be set at either globally, or per-view.
|
|
||||||
renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
|
|
||||||
parser_classes = api_settings.DEFAULT_PARSER_CLASSES
|
|
||||||
authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES
|
|
||||||
throttle_classes = api_settings.DEFAULT_THROTTLE_CLASSES
|
|
||||||
permission_classes = api_settings.DEFAULT_PERMISSION_CLASSES
|
|
||||||
content_negotiation_class = api_settings.DEFAULT_CONTENT_NEGOTIATION_CLASS
|
|
||||||
metadata_class = api_settings.DEFAULT_METADATA_CLASS
|
|
||||||
versioning_class = api_settings.DEFAULT_VERSIONING_CLASS
|
|
||||||
|
|
||||||
# Allow dependency injection of other settings to make testing easier.
|
# Allow dependency injection of other settings to make testing easier.
|
||||||
settings = api_settings
|
settings = api_settings
|
||||||
|
|
||||||
|
@ -238,6 +228,70 @@ class APIView(View):
|
||||||
|
|
||||||
# API policy instantiation methods
|
# API policy instantiation methods
|
||||||
|
|
||||||
|
@property
|
||||||
|
def versioning_class(self):
|
||||||
|
return getattr(self, '_versioning_class', self.settings.DEFAULT_VERSIONING_CLASS)
|
||||||
|
|
||||||
|
@versioning_class.setter
|
||||||
|
def versioning_class(self, value):
|
||||||
|
self._versioning_class = value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def metadata_class(self):
|
||||||
|
return getattr(self, '_metadata_class', self.settings.DEFAULT_METADATA_CLASS)
|
||||||
|
|
||||||
|
@metadata_class.setter
|
||||||
|
def metadata_class(self, value):
|
||||||
|
self._metadata_class = value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def content_negotiation_class(self):
|
||||||
|
return getattr(self, '_content_negotiation_class', self.settings.DEFAULT_CONTENT_NEGOTIATION_CLASS)
|
||||||
|
|
||||||
|
@content_negotiation_class.setter
|
||||||
|
def content_negotiation_class(self, value):
|
||||||
|
self._content_negotiation_class = value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def permission_classes(self):
|
||||||
|
return getattr(self, '_permission_classes', self.settings.DEFAULT_PERMISSION_CLASSES)
|
||||||
|
|
||||||
|
@permission_classes.setter
|
||||||
|
def permission_classes(self, value):
|
||||||
|
self._permission_classes = value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def throttle_classes(self):
|
||||||
|
return getattr(self, '_throttle_classes', self.settings.DEFAULT_THROTTLE_CLASSES)
|
||||||
|
|
||||||
|
@throttle_classes.setter
|
||||||
|
def throttle_classes(self, value):
|
||||||
|
self._throttle_classes = value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def authentication_classes(self):
|
||||||
|
return getattr(self, '_authentication_classes', self.settings.DEFAULT_AUTHENTICATION_CLASSES)
|
||||||
|
|
||||||
|
@authentication_classes.setter
|
||||||
|
def authentication_classes(self, value):
|
||||||
|
self._authentication_classes = value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parser_classes(self):
|
||||||
|
return getattr(self, '_parser_classes', self.settings.DEFAULT_PARSER_CLASSES)
|
||||||
|
|
||||||
|
@parser_classes.setter
|
||||||
|
def parser_classes(self, value):
|
||||||
|
self._parser_classes = value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def renderer_classes(self):
|
||||||
|
return getattr(self, '_renderer_classes', self.settings.DEFAULT_RENDERER_CLASSES)
|
||||||
|
|
||||||
|
@renderer_classes.setter
|
||||||
|
def renderer_classes(self, value):
|
||||||
|
self._renderer_classes = value
|
||||||
|
|
||||||
def get_format_suffix(self, **kwargs):
|
def get_format_suffix(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
Determine if the request includes a '.json' style format suffix
|
Determine if the request includes a '.json' style format suffix
|
||||||
|
|
Loading…
Reference in New Issue
Block a user