Drop .as_tuple()

This commit is contained in:
Tom Christie 2011-12-14 14:45:59 +00:00
parent ebbb6ac2d5
commit db6df5ce61
3 changed files with 17 additions and 34 deletions

View File

@ -12,6 +12,7 @@ from djangorestframework import status
from djangorestframework.resources import Resource, FormResource, ModelResource
from djangorestframework.response import ErrorResponse
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 StringIO import StringIO
@ -177,8 +178,7 @@ class RequestMixin(object):
return (None, None)
parsers = as_tuple(self.parsers)
for parser_cls in parsers:
for parser_cls in self.parsers:
parser = parser_cls(self)
if parser.can_handle_request(content_type):
return parser.parse(stream)

View File

@ -4,7 +4,6 @@ from django.db import models
from djangorestframework.response import ErrorResponse
from djangorestframework.serializer import Serializer, _SkipField
from djangorestframework.utils import as_tuple
class BaseResource(Serializer):
@ -12,9 +11,9 @@ class BaseResource(Serializer):
Base class for all Resource classes, which simply defines the interface
they provide.
"""
fields = None
include = None
exclude = None
fields = ()
include = ()
exclude = ()
def __init__(self, view=None, depth=None, stack=[], **kwargs):
super(BaseResource, self).__init__(depth, stack, **kwargs)
@ -126,14 +125,16 @@ class FormResource(Resource):
data = data and data or {}
files = files and files or {}
seen_fields_set = set(data.keys())
form_fields_set = set(bound_form.fields.keys())
allowed_extra_fields_set = set(allowed_extra_fields)
seen_fields = set(data.keys())
form_fields = set(bound_form.fields.keys())
allowed_extra_fields = set(allowed_extra_fields)
# In addition to regular validation we also ensure no additional fields
# are being passed in...
unknown_fields = seen_fields_set - (form_fields_set | allowed_extra_fields_set)
unknown_fields = unknown_fields - set(('csrfmiddlewaretoken', '_accept', '_method')) # TODO: Ugh.
# TODO: Hardcoded ignore_fields here is pretty icky.
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
if bound_form.is_valid() and not unknown_fields:
@ -141,7 +142,7 @@ class FormResource(Resource):
cleaned_data = bound_form.cleaned_data
# 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]
return cleaned_data
@ -407,9 +408,9 @@ class ModelResource(FormResource):
model_fields = set(field.name for field in self.model._meta.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
def _property_fields_set(self):
@ -421,6 +422,6 @@ class ModelResource(FormResource):
and not attr.startswith('_'))
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)

View File

@ -18,25 +18,7 @@ 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 )')
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):
"""
Return True if the given URL is mapped to a view in the urlconf, False otherwise.