Skip to content

Commit

Permalink
Never deepcopy validators. Closes #913
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed Jun 6, 2013
1 parent 181e4fd commit 40e0947
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/api-guide/generic-views.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ You may want to override this method to provide more complex behavior such as mo
For example:

def get_paginate_by(self):
self.request.accepted_renderer.format == 'html':
if self.request.accepted_renderer.format == 'html':
return 20
return 100

Expand Down
39 changes: 12 additions & 27 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@

import copy
import datetime
from decimal import Decimal, DecimalException
import inspect
import re
import warnings
from decimal import Decimal, DecimalException
from django import forms
from django.core import validators
from django.core.exceptions import ValidationError
from django.conf import settings
from django.db.models.fields import BLANK_CHOICE_DASH
from django import forms
from django.forms import widgets
from django.utils.encoding import is_protected_type
from django.utils.translation import ugettext_lazy as _
from django.utils.datastructures import SortedDict
from rest_framework import ISO_8601
from rest_framework.compat import (timezone, parse_date, parse_datetime,
parse_time)
from rest_framework.compat import BytesIO
from rest_framework.compat import six
from rest_framework.compat import smart_text, force_text, is_non_str_iterable
from rest_framework.compat import (
timezone, parse_date, parse_datetime, parse_time, BytesIO, six, smart_text,
force_text, is_non_str_iterable
)
from rest_framework.settings import api_settings


Expand Down Expand Up @@ -256,6 +255,12 @@ def __init__(self, source=None, label=None, help_text=None,
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):
if value in validators.EMPTY_VALUES and self.required:
raise ValidationError(self.error_messages['required'])
Expand Down Expand Up @@ -428,13 +433,6 @@ class SlugField(CharField):
def __init__(self, *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):
type_name = 'ChoiceField'
Expand Down Expand Up @@ -503,13 +501,6 @@ def from_native(self, value):
return None
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):
type_name = 'RegexField'
Expand All @@ -534,12 +525,6 @@ def _set_regex(self, 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):
type_name = 'DateField'
Expand Down
9 changes: 8 additions & 1 deletion rest_framework/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,15 @@ def metadata(self, request):
Return a dictionary of metadata about the view.
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['name'] = get_view_name(self.__class__)
ret['name'] = get_view_name(self.__class__, view_name_suffix)
ret['description'] = get_view_description(self.__class__)
ret['renders'] = [renderer.media_type for renderer in self.renderer_classes]
ret['parses'] = [parser.media_type for parser in self.parser_classes]
Expand Down

0 comments on commit 40e0947

Please sign in to comment.