Created
July 19, 2012 03:29
-
-
Save philipn/3140557 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def filter_value_to_python(self, value, field_name, filters=None, | |
filter_expr=None, filter_type=None): | |
""" | |
Turn the string ``value`` into a python object. | |
""" | |
# Simple values | |
if value in ['true', 'True', True]: | |
return True | |
elif value in ['false', 'False', False]: | |
return False | |
elif value in ('nil', 'none', 'None', None): | |
return None | |
# Split on ',' if not empty string and either an in or range filter. | |
if filter_type in ('in', 'range') and len(value): | |
if hasattr(filters, 'getlist'): | |
value = [] | |
for part in filters.getlist(filter_expr): | |
value.extend(part.split(',')) | |
else: | |
value = value.split(',') | |
to_python = self.filter_value_to_python | |
# We just use None to ensure the to_python call is just a | |
# string -> python object conversion. | |
return [to_python(v, field_name, None, None, None) for v in value] | |
# Attempt to use the associated Field object to convert the value to a | |
# python object. | |
try: | |
value = self.fields[field_name].convert(value) | |
except: | |
pass | |
return value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment