U
    |h|                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ dd Zd	d
 Z	dd Z
dddZdddZdejeedddZdeeeeedddZdS )a3  
The ``assertCanCreate`` method requires page data to be passed in
the same format that the page edit form would submit. For complex
page types, it can be difficult to construct this data structure by hand;
the ``wagtail.test.utils.form_data`` module provides a set of helper
functions to assist with this.
    N)	QueryDict)get_rich_text_editor_widget   )WagtailTestUtilsc                 c   s~   t | tr|  }nt | tr&t| }|D ]N\}}t|}t |ttfrlt|D ]\}}|g| |fV  qPq*|g|fV  q*d S )N)
isinstancedictitemslist	enumeratestr_nested_form_data)datar   keyvalueZ
child_keysZchild_value r   d/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/test/utils/form_data.pyr      s    


r   c                 C   s   dd t | D S )aH  
    Translates a nested dict structure into a flat form data dict
    with hyphen-separated keys.

    .. code-block:: python

        nested_form_data({
            'foo': 'bar',
            'parent': {
                'child': 'field',
            },
        })
        # Returns: {'foo': 'bar', 'parent-child': 'field'}
    c                 S   s   i | ]\}}d  ||qS )-)join).0r   r   r   r   r   
<dictcomp>.   s      z$nested_form_data.<locals>.<dictcomp>)r   )r   r   r   r   nested_form_data   s    r   c                    s2   dd   fddt | D }tt||d< |S )a/  
    Takes a list of (block_type, value) tuples and turns it in to
    StreamField form data. Use this within a :func:`nested_form_data`
    call, with the field name as the key.

    .. code-block:: python

        nested_form_data({'content': streamfield([
            ('text', 'Hello, world'),
        ])})
        # Returns:
        # {
        #     'content-count': '1',
        #     'content-0-type': 'text',
        #     'content-0-value': 'Hello, world',
        #     'content-0-order': '0',
        #     'content-0-deleted': '',
        # }
    c                 S   s   |\}}||dt | dS )N )typer   Zdeletedorderr   )indexitemblockr   r   r   r   to_blockF   s    zstreamfield.<locals>.to_blockc                    s    i | ]\}}t | ||qS r   r   r   r   r   r   r   r   r   J   s      zstreamfield.<locals>.<dictcomp>count)r
   r   len)r   	data_dictr   r    r   streamfield1   s    r$     c                    sJ   dd   fddt | D }|tt|t|t|t|d |S )a  
    Takes a list of form data for an InlineFormset and translates
    it in to valid POST data. Use this within a :func:`nested_form_data`
    call, with the formset relation name as the key.

    .. code-block:: python

        nested_form_data({'lines': inline_formset([
            {'text': 'Hello'},
            {'text': 'World'},
        ])})
        # Returns:
        # {
        #     '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': '',
        # }
    c                 S   s   t | dd}|| |S )Nr   )ZORDERDELETE)r   update)r   r   defaultsr   r   r   to_formj   s
    
zinline_formset.<locals>.to_formc                    s    i | ]\}}t | ||qS r   r   r   r)   r   r   r   r   s      z"inline_formset.<locals>.<dictcomp>)ZTOTAL_FORMSZINITIAL_FORMSZMIN_NUM_FORMSZMAX_NUM_FORMS)r
   r'   r   r"   )r   initialminmaxr#   r   r*   r   inline_formsetO   s    
r.   defaultc                 C   s   t ||}|| S )a  
    Converts an HTML-like rich text string to the data format required by
    the currently active rich text editor.

    :param editor: An alternative editor name as defined in ``WAGTAILADMIN_RICH_TEXT_EDITORS``
    :param features: A list of features allowed in the rich text content (see :ref:`rich_text_features`)

    .. code-block:: python

        self.assertCanCreate(root_page, ContentPage, nested_form_data({
            'title': 'About us',
            'body': rich_text('<p>Lorem ipsum dolor sit amet</p>'),
        }))
    )r   Zformat_value)r   ZeditorfeaturesZwidgetr   r   r   	rich_text   s    
r1   T)formexclude_csrfreturnc           
   
   C   s  t dd}| dD ]D}|jd}|r|jdddkr|rF|dkr|jd	d||< q| jdd
ddD ]$}|jd}|rj|jd	||< qj| jddddD ]*}|jd}|r|||jd	d q| dD ] }|jd}|r| ||< q| dD ]|}|jd}|rd}|jdddD ]$}d}|||jd	|  q*|s|d}	|	r|	jd	|	 ||< q|S )NT)Zmutableinputnamer   r   )checkboxradioZcsrfmiddlewaretokenr   r8   )r   checkedr7   textareaselectFoption)selected)r   find_allattrsgetZ
appendlistZget_textfind)
r2   r3   r   r5   r6   r:   r;   Zselected_valuer<   Zfirst_optionr   r   r   _querydict_from_form   sN    

 rB   )htmlform_id
form_indexr3   r4   c                 C   s   t | }|d k	rF|jdd|id}|d kr<td| dt||S t|}t|jd|d dD ]\}}||krdt||  S qdtd| d	d S )
Nr2   id)r?   zNo form was found with id "z".r   )limitzNo form was found with index: .)r   Zget_souprA   
ValueErrorrB   intr
   r>   )rC   rD   rE   r3   Zsoupr2   r   ir   r   r   querydict_from_html   s    

rL   )r   r   r%   )r/   N)T)Nr   T)__doc__Zbs4Zdjango.httpr   Zwagtail.admin.rich_textr   Zwagtail_testsr   r   r   r$   r.   r1   TagboolrB   r   rJ   rL   r   r   r   r   <module>   s(   
0
+        