Never deepcopy validators. Closes #913

This commit is contained in:
Tom Christie 2013-06-06 08:56:39 +01:00
parent 181e4fddd0
commit 40e09472d8
3 changed files with 21 additions and 29 deletions

View File

@ -131,7 +131,7 @@ You may want to override this method to provide more complex behavior such as mo
For example: For example:
def get_paginate_by(self): def get_paginate_by(self):
self.request.accepted_renderer.format == 'html': if self.request.accepted_renderer.format == 'html':
return 20 return 20
return 100 return 100

View File

@ -7,25 +7,24 @@ from __future__ import unicode_literals
import copy import copy
import datetime import datetime
from decimal import Decimal, DecimalException
import inspect import inspect
import re import re
import warnings import warnings
from decimal import Decimal, DecimalException
from django import forms
from django.core import validators from django.core import validators
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.conf import settings from django.conf import settings
from django.db.models.fields import BLANK_CHOICE_DASH from django.db.models.fields import BLANK_CHOICE_DASH
from django import forms
from django.forms import widgets from django.forms import widgets
from django.utils.encoding import is_protected_type from django.utils.encoding import is_protected_type
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from rest_framework import ISO_8601 from rest_framework import ISO_8601
from rest_framework.compat import (timezone, parse_date, parse_datetime, from rest_framework.compat import (
parse_time) timezone, parse_date, parse_datetime, parse_time, BytesIO, six, smart_text,
from rest_framework.compat import BytesIO force_text, is_non_str_iterable
from rest_framework.compat import six )
from rest_framework.compat import smart_text, force_text, is_non_str_iterable
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
@ -256,6 +255,12 @@ class WritableField(Field):
widget = widget() widget = widget()
self.widget = widget self.widget = widget
def __deepcopy__(self, memo):
result = copy.copy(self)
memo[id(self)] = result
result.validators = self.validators[:]
return result
def validate(self, value): def validate(self, value):
if value in validators.EMPTY_VALUES and self.required: if value in validators.EMPTY_VALUES and self.required:
raise ValidationError(self.error_messages['required']) raise ValidationError(self.error_messages['required'])
@ -428,13 +433,6 @@ class SlugField(CharField):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(SlugField, self).__init__(*args, **kwargs) super(SlugField, self).__init__(*args, **kwargs)
def __deepcopy__(self, memo):
result = copy.copy(self)
memo[id(self)] = result
#result.widget = copy.deepcopy(self.widget, memo)
result.validators = self.validators[:]
return result
class ChoiceField(WritableField): class ChoiceField(WritableField):
type_name = 'ChoiceField' type_name = 'ChoiceField'
@ -503,13 +501,6 @@ class EmailField(CharField):
return None return None
return ret.strip() return ret.strip()
def __deepcopy__(self, memo):
result = copy.copy(self)
memo[id(self)] = result
#result.widget = copy.deepcopy(self.widget, memo)
result.validators = self.validators[:]
return result
class RegexField(CharField): class RegexField(CharField):
type_name = 'RegexField' type_name = 'RegexField'
@ -534,12 +525,6 @@ class RegexField(CharField):
regex = property(_get_regex, _set_regex) regex = property(_get_regex, _set_regex)
def __deepcopy__(self, memo):
result = copy.copy(self)
memo[id(self)] = result
result.validators = self.validators[:]
return result
class DateField(WritableField): class DateField(WritableField):
type_name = 'DateField' type_name = 'DateField'

View File

@ -341,8 +341,15 @@ class APIView(View):
Return a dictionary of metadata about the view. Return a dictionary of metadata about the view.
Used to return responses for OPTIONS requests. Used to return responses for OPTIONS requests.
""" """
# This is used by ViewSets to disambiguate instance vs list views
view_name_suffix = getattr(self, 'suffix', None)
# By default we can't provide any form-like information, however the
# generic views override this implementation and add additional
# information for POST and PUT methods, based on the serializer.
ret = SortedDict() ret = SortedDict()
ret['name'] = get_view_name(self.__class__) ret['name'] = get_view_name(self.__class__, view_name_suffix)
ret['description'] = get_view_description(self.__class__) ret['description'] = get_view_description(self.__class__)
ret['renders'] = [renderer.media_type for renderer in self.renderer_classes] ret['renders'] = [renderer.media_type for renderer in self.renderer_classes]
ret['parses'] = [parser.media_type for parser in self.parser_classes] ret['parses'] = [parser.media_type for parser in self.parser_classes]