3
>^                 @   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-8app2_gc/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