| 1 |
""" |
| 2 |
Helpers for dealing with HTML input. |
| 3 |
""" |
| 4 |
import re |
| 5 |
|
| 6 |
from django.utils.datastructures import MultiValueDict |
| 7 |
|
| 8 |
|
| 9 |
def is_html_input(dictionary): |
| 10 |
# MultiDict type datastructures are used to represent HTML form input, |
| 11 |
# which may have more than one value for each key. |
| 12 |
return hasattr(dictionary, 'getlist') |
| 13 |
|
| 14 |
|
| 15 |
def parse_html_list(dictionary, prefix='', default=None): |
| 16 |
""" |
| 17 |
Used to support list values in HTML forms. |
| 18 |
Supports lists of primitives and/or dictionaries. |
| 19 |
|
| 20 |
* List of primitives. |
| 21 |
|
| 22 |
{ |
| 23 |
'[0]': 'abc', |
| 24 |
'[1]': 'def', |
| 25 |
'[2]': 'hij' |
| 26 |
} |
| 27 |
--> |
| 28 |
[ |
| 29 |
'abc', |
| 30 |
'def', |
| 31 |
'hij' |
| 32 |
] |
| 33 |
|
| 34 |
* List of dictionaries. |
| 35 |
|
| 36 |
{ |
| 37 |
'[0]foo': 'abc', |
| 38 |
'[0]bar': 'def', |
| 39 |
'[1]foo': 'hij', |
| 40 |
'[1]bar': 'klm', |
| 41 |
} |
| 42 |
--> |
| 43 |
[ |
| 44 |
{'foo': 'abc', 'bar': 'def'}, |
| 45 |
{'foo': 'hij', 'bar': 'klm'} |
| 46 |
] |
| 47 |
|
| 48 |
:returns a list of objects, or the value specified in ``default`` if the list is empty |
| 49 |
""" |
| 50 |
ret = {} |
| 51 |
regex = re.compile(r'^%s\[([0-9]+)\](.*)$' % re.escape(prefix)) |
| 52 |
for field, value in dictionary.items(): |
| 53 |
match = regex.match(field) |
| 54 |
if not match: |
| 55 |
continue |
| 56 |
index, key = match.groups() |
| 57 |
index = int(index) |
| 58 |
if not key: |
| 59 |
ret[index] = value |
| 60 |
elif isinstance(ret.get(index), dict): |
| 61 |
ret[index][key] = value |
| 62 |
else: |
| 63 |
ret[index] = MultiValueDict({key: [value]}) |
| 64 |
|
| 65 |
# return the items of the ``ret`` dict, sorted by key, or ``default`` if the dict is empty |
| 66 |
return [ret[item] for item in sorted(ret)] if ret else default |
| 67 |
|
| 68 |
|
| 69 |
NOT_PROVIDED = object() |
| 70 |
|
| 71 |
|
| 72 |
def parse_html_dict(dictionary, prefix='', default=NOT_PROVIDED): |
| 73 |
""" |
| 74 |
Used to support dictionary values in HTML forms. |
| 75 |
|
| 76 |
{ |
| 77 |
'profile.username': 'example', |
| 78 |
'profile.email': '[email protected]', |
| 79 |
} |
| 80 |
--> |
| 81 |
{ |
| 82 |
'profile': { |
| 83 |
'username': 'example', |
| 84 |
'email': '[email protected]' |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
:returns a MultiValueDict of the parsed data, or the value specified in |
| 89 |
``default`` if the dict field was not present in the input |
| 90 |
""" |
| 91 |
ret = MultiValueDict() |
| 92 |
regex = re.compile(r'^%s\.(.+)$' % re.escape(prefix)) |
| 93 |
for field in dictionary: |
| 94 |
match = regex.match(field) |
| 95 |
if not match: |
| 96 |
continue |
| 97 |
key = match.groups()[0] |
| 98 |
value = dictionary.getlist(field) |
| 99 |
ret.setlist(key, value) |
| 100 |
|
| 101 |
# Left for backwards compatibility in case of external caller |
| 102 |
if default is NOT_PROVIDED: |
| 103 |
return ret |
| 104 |
|
| 105 |
return ret if ret else default |