FilePathField subclassing from ChoiceField

This commit is contained in:
Tom Christie 2015-08-03 10:17:41 +01:00
parent 877e964d7e
commit bf35906dc0

View File

@ -705,42 +705,6 @@ class IPAddressField(CharField):
return super(IPAddressField, self).to_internal_value(data)
class FilePathField(CharField):
default_error_messages = {
'invalid_choice': _('"{input}" is not a valid path choice.')
}
def __init__(self, path, match=None, recursive=False, allow_files=True,
allow_folders=False, required=None, **kwargs):
super(FilePathField, self).__init__(**kwargs)
# Defer to Django's FilePathField implmentation to get the
# valid set of choices.
field = DjangoFilePathField(
path, match=match, recursive=recursive, allow_files=allow_files,
allow_folders=allow_folders, required=required
)
self.choices = OrderedDict(field.choices)
self.choice_strings_to_values = dict([
(six.text_type(key), key) for key in self.choices.keys()
])
def to_internal_value(self, data):
if data == '' and self.allow_blank:
return ''
try:
return self.choice_strings_to_values[six.text_type(data)]
except KeyError:
self.fail('invalid_choice', input=data)
def to_representation(self, value):
if value in ('', None):
return value
return self.choice_strings_to_values[six.text_type(value)]
# Number types...
class IntegerField(Field):
@ -1215,6 +1179,23 @@ class MultipleChoiceField(ChoiceField):
])
class FilePathField(ChoiceField):
default_error_messages = {
'invalid_choice': _('"{input}" is not a valid path choice.')
}
def __init__(self, path, match=None, recursive=False, allow_files=True,
allow_folders=False, required=None, **kwargs):
# Defer to Django's FilePathField implmentation to get the
# valid set of choices.
field = DjangoFilePathField(
path, match=match, recursive=recursive, allow_files=allow_files,
allow_folders=allow_folders, required=required
)
kwargs['choices'] = field.choices
super(FilePathField, self).__init__(**kwargs)
# File types...
class FileField(Field):