Created
February 6, 2018 03:19
-
-
Save mx-moth/02cb81557526fbbf49e9c90583bbabac to your computer and use it in GitHub Desktop.
Some helpers for making POST data for Wagtail / Django form tests
This file contains 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 _nested_form_data(data): | |
if isinstance(data, dict): | |
items = data.items() | |
elif isinstance(data, list): | |
items = enumerate(data) | |
for key, value in items: | |
key = str(key) | |
if isinstance(value, (dict, list)): | |
for child_keys, child_value in _nested_form_data(value): | |
yield [key] + child_keys, child_value | |
else: | |
yield [key], value | |
def nested_form_data(data): | |
""" | |
Helper that translates nested form data into hyphen-separated form data: | |
>>> nested_form_data({ | |
... 'foo': 'bar', | |
... 'parent': { | |
... 'child': 'field', | |
... }, | |
... }) | |
{'foo': 'bar', 'parent-child': 'field'} | |
""" | |
return {'-'.join(key): value for key, value in _nested_form_data(data)} | |
def streamfield(items): | |
""" | |
Helper that takes a list of (block, value) tuples and turns it in to | |
StreamField form data. Works well with :func:`nested_form_data`: | |
>>> nested_form_data({'content': streamfield([ | |
... ('text', '<p>Hello, world</p>'), | |
... ])}) | |
{ | |
'content-count': '1', | |
'content-0-type': 'text', | |
'content-0-value': '<p>Hello, world</p>', | |
'content-0-deleted': '', | |
} | |
""" | |
def to_block(index, item): | |
block, value = item | |
return {'type': block, 'value': value, 'deleted': '', 'order': index} | |
data_dict = {str(index): to_block(index, item) | |
for index, item in enumerate(items)} | |
data_dict['count'] = str(len(data_dict)) | |
return data_dict | |
def inline_formset(items, initial=0, min=0, max=1000): | |
""" | |
Helper that takes a list of form data for an InlineFormset and translates | |
it in to valid POST data: | |
>>> nested_form_data({'lines': inline_formset([ | |
... {'text': 'Hello'}, | |
... {'text': 'World'}, | |
... ])}) | |
{ | |
'lines-TOTAL_FORMS': '2', | |
'lines-INITIAL_FORMS': '0', | |
'lines-MIN_NUM_FORMS': '0', | |
'lines-MAX_NUM_FORMS': '1000', | |
'lines-0-text': 'Hello', | |
'lines-0-ORDER': '0', | |
'lines-0-DELETE': '', | |
'lines-1-text': 'World', | |
'lines-1-ORDER': '1', | |
'lines-1-DELETE': '', | |
} | |
""" | |
def to_form(index, item): | |
defaults = { | |
'ORDER': str(index), | |
'DELETE': '', | |
} | |
defaults.update(item) | |
return defaults | |
data_dict = {str(index): to_form(index, item) | |
for index, item in enumerate(items)} | |
data_dict.update({ | |
'TOTAL_FORMS': str(len(data_dict)), | |
'INITIAL_FORMS': str(initial), | |
'MIN_NUM_FORMS': str(min), | |
'MAX_NUM_FORMS': str(max), | |
}) | |
return data_dict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment