3
Ah`ö  ã               @   s8   d Z ddlZddlmZ dd„ Zddd„Zdd	d
„ZdS )z&
Helpers for dealing with HTML input.
é    N)ÚMultiValueDictc             C   s
   t | dƒS )NÚgetlist)Úhasattr)Ú
dictionary© r   úH/tmp/pip-build-9m32_hle/djangorestframework/rest_framework/utils/html.pyÚis_html_input	   s    r   Ú c       	         s®   i ‰ t jdt j|ƒ ƒ}xv| jƒ D ]j\}}|j|ƒ}|s:q"|jƒ \}}t|ƒ}|s\|ˆ |< q"tˆ j|ƒt	ƒrz|ˆ | |< q"t
||giƒˆ |< q"W ˆ rª‡ fdd„tˆ ƒD ƒS |S )ad  
    Used to support list values in HTML forms.
    Supports lists of primitives and/or dictionaries.

    * List of primitives.

    {
        '[0]': 'abc',
        '[1]': 'def',
        '[2]': 'hij'
    }
        -->
    [
        'abc',
        'def',
        'hij'
    ]

    * List of dictionaries.

    {
        '[0]foo': 'abc',
        '[0]bar': 'def',
        '[1]foo': 'hij',
        '[1]bar': 'klm',
    }
        -->
    [
        {'foo': 'abc', 'bar': 'def'},
        {'foo': 'hij', 'bar': 'klm'}
    ]

    :returns a list of objects, or the value specified in ``default`` if the list is empty
    z^%s\[([0-9]+)\](.*)$c                s   g | ]}ˆ | ‘qS r   r   )Ú.0Úitem)Úretr   r   ú
<listcomp>B   s    z#parse_html_list.<locals>.<listcomp>)ÚreÚcompileÚescapeÚitemsÚmatchÚgroupsÚintÚ
isinstanceÚgetÚdictr   Úsorted)	r   ÚprefixÚdefaultÚregexÚfieldÚvaluer   ÚindexÚkeyr   )r   r   Úparse_html_list   s    #

r    c             C   s^   t ƒ }tjdtj|ƒ ƒ}x>| D ]6}|j|ƒ}|s4q |jƒ d }| j|ƒ}|j||ƒ q W |S )a#  
    Used to support dictionary values in HTML forms.

    {
        'profile.username': 'example',
        'profile.email': 'example@example.com',
    }
        -->
    {
        'profile': {
            'username': 'example',
            'email': 'example@example.com'
        }
    }
    z
^%s\.(.+)$r   )r   r   r   r   r   r   r   Zsetlist)r   r   r   r   r   r   r   r   r   r   r   Úparse_html_dictE   s    


r!   )r	   N)r	   )Ú__doc__r   Zdjango.utils.datastructuresr   r   r    r!   r   r   r   r   Ú<module>   s
   
6