From db6df5ce61248d6bcf5e83d1c7fa5d495e3584ec Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 14 Dec 2011 14:45:59 +0000 Subject: [PATCH] Drop .as_tuple() --- djangorestframework/mixins.py | 4 ++-- djangorestframework/resources.py | 29 ++++++++++++++------------- djangorestframework/utils/__init__.py | 18 ----------------- 3 files changed, 17 insertions(+), 34 deletions(-) diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 93a094e6f..350af5cb1 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -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) diff --git a/djangorestframework/resources.py b/djangorestframework/resources.py index dc11c31f8..42c294a5a 100644 --- a/djangorestframework/resources.py +++ b/djangorestframework/resources.py @@ -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) diff --git a/djangorestframework/utils/__init__.py b/djangorestframework/utils/__init__.py index 04baea78f..7d693cc44 100644 --- a/djangorestframework/utils/__init__.py +++ b/djangorestframework/utils/__init__.py @@ -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.