mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 19:43:59 +03:00
Bits of cleanup
This commit is contained in:
parent
c648f2786f
commit
01d6a0899e
|
@ -41,8 +41,9 @@ class SingleObjectBaseView(SingleObjectMixin, BaseView):
|
||||||
"""
|
"""
|
||||||
Override default to add support for object-level permissions.
|
Override default to add support for object-level permissions.
|
||||||
"""
|
"""
|
||||||
super(self, SingleObjectBaseView).get_object()
|
obj = super(SingleObjectBaseView, self).get_object()
|
||||||
self.check_permissions(self.request, self.object)
|
self.check_permissions(self.request, obj)
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
### Concrete view classes that provide method handlers ###
|
### Concrete view classes that provide method handlers ###
|
||||||
|
|
|
@ -1,3 +1,12 @@
|
||||||
|
"""
|
||||||
|
Basic building blocks for generic class based views.
|
||||||
|
|
||||||
|
We don't bind behaviour to http method handlers yet,
|
||||||
|
which allows mixin classes to be composed in interesting ways.
|
||||||
|
|
||||||
|
Eg. Use mixins to build a Resource class, and have a Router class
|
||||||
|
perform the binding of http methods to actions for us.
|
||||||
|
"""
|
||||||
from djangorestframework import status
|
from djangorestframework import status
|
||||||
from djangorestframework.response import Response
|
from djangorestframework.response import Response
|
||||||
|
|
||||||
|
@ -68,6 +77,8 @@ class MetadataMixin(object):
|
||||||
"""
|
"""
|
||||||
Return a dicitonary of view metadata.
|
Return a dicitonary of view metadata.
|
||||||
Should be mixed in with any `BaseView`.
|
Should be mixed in with any `BaseView`.
|
||||||
|
|
||||||
|
This mixin is typically used for the HTTP 'OPTIONS' method.
|
||||||
"""
|
"""
|
||||||
def metadata(self, request, *args, **kwargs):
|
def metadata(self, request, *args, **kwargs):
|
||||||
content = {
|
content = {
|
||||||
|
|
|
@ -13,10 +13,12 @@ API_SETTINGS = {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
This module provides the `api_setting` object, that is used to access
|
||||||
|
REST framework settings, checking for user settings first, then falling
|
||||||
|
back to the defaults.
|
||||||
"""
|
"""
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils import importlib
|
from django.utils import importlib
|
||||||
from djangorestframework.compat import yaml
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULTS = {
|
DEFAULTS = {
|
||||||
|
@ -35,17 +37,16 @@ DEFAULTS = {
|
||||||
),
|
),
|
||||||
'DEFAULT_PERMISSIONS': (),
|
'DEFAULT_PERMISSIONS': (),
|
||||||
'DEFAULT_THROTTLES': (),
|
'DEFAULT_THROTTLES': (),
|
||||||
|
|
||||||
'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
|
'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
|
||||||
'UNAUTHENTICATED_TOKEN': None,
|
'UNAUTHENTICATED_TOKEN': None,
|
||||||
|
|
||||||
'FORM_METHOD_OVERRIDE': '_method',
|
'FORM_METHOD_OVERRIDE': '_method',
|
||||||
'FORM_CONTENT_OVERRIDE': '_content',
|
'FORM_CONTENT_OVERRIDE': '_content',
|
||||||
'FORM_CONTENTTYPE_OVERRIDE': '_content_type',
|
'FORM_CONTENTTYPE_OVERRIDE': '_content_type',
|
||||||
'URL_ACCEPT_OVERRIDE': '_accept'
|
'URL_ACCEPT_OVERRIDE': '_accept'
|
||||||
}
|
}
|
||||||
|
|
||||||
if yaml:
|
|
||||||
DEFAULTS['DEFAULT_RENDERERS'] += ('djangorestframework.renderers.YAMLRenderer', )
|
|
||||||
|
|
||||||
|
|
||||||
# List of settings that may be in string import notation.
|
# List of settings that may be in string import notation.
|
||||||
IMPORT_STRINGS = (
|
IMPORT_STRINGS = (
|
||||||
|
@ -97,7 +98,8 @@ class APISettings(object):
|
||||||
from djangorestframework.settings import api_settings
|
from djangorestframework.settings import api_settings
|
||||||
print api_settings.DEFAULT_RENDERERS
|
print api_settings.DEFAULT_RENDERERS
|
||||||
|
|
||||||
Any setting with string import paths will be resolved.
|
Any setting with string import paths will be automatically resolved
|
||||||
|
and return the class, rather than the string literal.
|
||||||
"""
|
"""
|
||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr):
|
||||||
if attr not in DEFAULTS.keys():
|
if attr not in DEFAULTS.keys():
|
||||||
|
|
|
@ -1,15 +1,19 @@
|
||||||
from django.conf.urls.defaults import url
|
from django.conf.urls.defaults import url
|
||||||
|
from djangorestframework.settings import api_settings
|
||||||
|
|
||||||
|
|
||||||
def format_suffix_patterns(urlpatterns, suffix_required=False):
|
def format_suffix_patterns(urlpatterns, suffix_required=False, suffix_kwarg=None):
|
||||||
"""
|
"""
|
||||||
Supplement existing urlpatterns with corrosponding patterns that also
|
Supplement existing urlpatterns with corrosponding patterns that also
|
||||||
include a '.format' suffix. Retains urlpattern ordering.
|
include a '.format' suffix. Retains urlpattern ordering.
|
||||||
"""
|
"""
|
||||||
|
suffix_kwarg = suffix_kwarg or api_settings.FORMAT_SUFFIX_KWARG
|
||||||
|
suffix_pattern = '.(?P<%s>[a-z]+)$' % suffix_kwarg
|
||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
for urlpattern in urlpatterns:
|
for urlpattern in urlpatterns:
|
||||||
# Form our complementing '.format' urlpattern
|
# Form our complementing '.format' urlpattern
|
||||||
regex = urlpattern.regex.pattern.rstrip('$') + '.(?P<format>[a-z]+)$'
|
regex = urlpattern.regex.pattern.rstrip('$') + suffix_pattern
|
||||||
view = urlpattern._callback or urlpattern._callback_str
|
view = urlpattern._callback or urlpattern._callback_str
|
||||||
kwargs = urlpattern.default_args
|
kwargs = urlpattern.default_args
|
||||||
name = urlpattern.name
|
name = urlpattern.name
|
||||||
|
|
Loading…
Reference in New Issue
Block a user