mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-12-01 05:54:01 +03:00
Drop .as_tuple()
This commit is contained in:
parent
ebbb6ac2d5
commit
db6df5ce61
|
@ -12,6 +12,7 @@ from djangorestframework import status
|
||||||
from djangorestframework.resources import Resource, FormResource, ModelResource
|
from djangorestframework.resources import Resource, FormResource, ModelResource
|
||||||
from djangorestframework.response import ErrorResponse
|
from djangorestframework.response import ErrorResponse
|
||||||
from djangorestframework.utils import as_tuple, MSIE_USER_AGENT_REGEX
|
from djangorestframework.utils import as_tuple, MSIE_USER_AGENT_REGEX
|
||||||
|
from djangorestframework.utils import MSIE_USER_AGENT_REGEX
|
||||||
from djangorestframework.utils.mediatypes import is_form_media_type, order_by_precedence
|
from djangorestframework.utils.mediatypes import is_form_media_type, order_by_precedence
|
||||||
|
|
||||||
from StringIO import StringIO
|
from StringIO import StringIO
|
||||||
|
@ -177,8 +178,7 @@ class RequestMixin(object):
|
||||||
return (None, None)
|
return (None, None)
|
||||||
|
|
||||||
parsers = as_tuple(self.parsers)
|
parsers = as_tuple(self.parsers)
|
||||||
|
for parser_cls in self.parsers:
|
||||||
for parser_cls in parsers:
|
|
||||||
parser = parser_cls(self)
|
parser = parser_cls(self)
|
||||||
if parser.can_handle_request(content_type):
|
if parser.can_handle_request(content_type):
|
||||||
return parser.parse(stream)
|
return parser.parse(stream)
|
||||||
|
|
|
@ -4,7 +4,6 @@ from django.db import models
|
||||||
|
|
||||||
from djangorestframework.response import ErrorResponse
|
from djangorestframework.response import ErrorResponse
|
||||||
from djangorestframework.serializer import Serializer, _SkipField
|
from djangorestframework.serializer import Serializer, _SkipField
|
||||||
from djangorestframework.utils import as_tuple
|
|
||||||
|
|
||||||
|
|
||||||
class BaseResource(Serializer):
|
class BaseResource(Serializer):
|
||||||
|
@ -12,9 +11,9 @@ class BaseResource(Serializer):
|
||||||
Base class for all Resource classes, which simply defines the interface
|
Base class for all Resource classes, which simply defines the interface
|
||||||
they provide.
|
they provide.
|
||||||
"""
|
"""
|
||||||
fields = None
|
fields = ()
|
||||||
include = None
|
include = ()
|
||||||
exclude = None
|
exclude = ()
|
||||||
|
|
||||||
def __init__(self, view=None, depth=None, stack=[], **kwargs):
|
def __init__(self, view=None, depth=None, stack=[], **kwargs):
|
||||||
super(BaseResource, self).__init__(depth, stack, **kwargs)
|
super(BaseResource, self).__init__(depth, stack, **kwargs)
|
||||||
|
@ -126,14 +125,16 @@ class FormResource(Resource):
|
||||||
data = data and data or {}
|
data = data and data or {}
|
||||||
files = files and files or {}
|
files = files and files or {}
|
||||||
|
|
||||||
seen_fields_set = set(data.keys())
|
seen_fields = set(data.keys())
|
||||||
form_fields_set = set(bound_form.fields.keys())
|
form_fields = set(bound_form.fields.keys())
|
||||||
allowed_extra_fields_set = set(allowed_extra_fields)
|
allowed_extra_fields = set(allowed_extra_fields)
|
||||||
|
|
||||||
# In addition to regular validation we also ensure no additional fields
|
# In addition to regular validation we also ensure no additional fields
|
||||||
# are being passed in...
|
# are being passed in...
|
||||||
unknown_fields = seen_fields_set - (form_fields_set | allowed_extra_fields_set)
|
# TODO: Hardcoded ignore_fields here is pretty icky.
|
||||||
unknown_fields = unknown_fields - set(('csrfmiddlewaretoken', '_accept', '_method')) # TODO: Ugh.
|
ignore_fields = set(('csrfmiddlewaretoken', '_accept', '_method'))
|
||||||
|
allowed_fields = form_fields | allowed_extra_fields | ignore_fields
|
||||||
|
unknown_fields = seen_fields - allowed_fields
|
||||||
|
|
||||||
# Check using both regular validation, and our stricter no additional fields rule
|
# Check using both regular validation, and our stricter no additional fields rule
|
||||||
if bound_form.is_valid() and not unknown_fields:
|
if bound_form.is_valid() and not unknown_fields:
|
||||||
|
@ -141,7 +142,7 @@ class FormResource(Resource):
|
||||||
cleaned_data = bound_form.cleaned_data
|
cleaned_data = bound_form.cleaned_data
|
||||||
|
|
||||||
# Add in any extra fields to the cleaned content...
|
# Add in any extra fields to the cleaned content...
|
||||||
for key in (allowed_extra_fields_set & seen_fields_set) - set(cleaned_data.keys()):
|
for key in (allowed_extra_fields & seen_fields) - set(cleaned_data.keys()):
|
||||||
cleaned_data[key] = data[key]
|
cleaned_data[key] = data[key]
|
||||||
|
|
||||||
return cleaned_data
|
return cleaned_data
|
||||||
|
@ -407,9 +408,9 @@ class ModelResource(FormResource):
|
||||||
model_fields = set(field.name for field in self.model._meta.fields)
|
model_fields = set(field.name for field in self.model._meta.fields)
|
||||||
|
|
||||||
if self.fields:
|
if self.fields:
|
||||||
return model_fields & set(as_tuple(self.fields))
|
return model_fields & set(self.fields)
|
||||||
|
|
||||||
return model_fields - set(as_tuple(self.exclude))
|
return model_fields - set(self.exclude)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _property_fields_set(self):
|
def _property_fields_set(self):
|
||||||
|
@ -421,6 +422,6 @@ class ModelResource(FormResource):
|
||||||
and not attr.startswith('_'))
|
and not attr.startswith('_'))
|
||||||
|
|
||||||
if self.fields:
|
if self.fields:
|
||||||
return property_fields & set(as_tuple(self.fields))
|
return property_fields & set(self.fields)
|
||||||
|
|
||||||
return property_fields.union(set(as_tuple(self.include))) - set(as_tuple(self.exclude))
|
return property_fields.union(set(self.include)) - set(self.exclude)
|
||||||
|
|
|
@ -18,24 +18,6 @@ from mediatypes import add_media_type_param, get_media_type_params, order_by_pre
|
||||||
|
|
||||||
MSIE_USER_AGENT_REGEX = re.compile(r'^Mozilla/[0-9]+\.[0-9]+ \([^)]*; MSIE [0-9]+\.[0-9]+[a-z]?;[^)]*\)(?!.* Opera )')
|
MSIE_USER_AGENT_REGEX = re.compile(r'^Mozilla/[0-9]+\.[0-9]+ \([^)]*; MSIE [0-9]+\.[0-9]+[a-z]?;[^)]*\)(?!.* Opera )')
|
||||||
|
|
||||||
def as_tuple(obj):
|
|
||||||
"""
|
|
||||||
Given an object which may be a list/tuple, another object, or None,
|
|
||||||
return that object in list form.
|
|
||||||
|
|
||||||
IE:
|
|
||||||
If the object is already a list/tuple just return it.
|
|
||||||
If the object is not None, return it in a list with a single element.
|
|
||||||
If the object is None return an empty list.
|
|
||||||
"""
|
|
||||||
if obj is None:
|
|
||||||
return ()
|
|
||||||
elif isinstance(obj, list):
|
|
||||||
return tuple(obj)
|
|
||||||
elif isinstance(obj, tuple):
|
|
||||||
return obj
|
|
||||||
return (obj,)
|
|
||||||
|
|
||||||
|
|
||||||
def url_resolves(url):
|
def url_resolves(url):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user