3
>^4                 @   s   d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	m
Z
 G dd deZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZdS )    )unicode_literals)OrderedDict)
force_text)MutableMappingunicode_to_repr)jsonc                   s8   e Zd ZdZ fddZdd Zdd Zdd	 Z  ZS )

ReturnDictz
    Return object from `serializer.data` for the `Serializer` class.
    Includes a backlink to the serializer instance for renderers
    to use if they need richer field information.
    c                s"   |j d| _tt| j|| d S )N
serializer)popr	   superr   __init__)selfargskwargs)	__class__ V/tmp/pip-build-8app2_gc/djangorestframework/rest_framework/utils/serializer_helpers.pyr      s    zReturnDict.__init__c             C   s   t | | jdS )N)r	   )r   r	   )r   r   r   r   copy   s    zReturnDict.copyc             C   s
   t j| S )N)dict__repr__)r   r   r   r   r      s    zReturnDict.__repr__c             C   s   t t | ffS )N)r   )r   r   r   r   
__reduce__   s    zReturnDict.__reduce__)	__name__
__module____qualname____doc__r   r   r   r   __classcell__r   r   )r   r   r      s
   r   c                   s0   e Zd ZdZ fddZdd Zdd Z  ZS )
ReturnListz
    Return object from `serializer.data` for the `SerializerList` class.
    Includes a backlink to the serializer instance for renderers
    to use if they need richer field information.
    c                s"   |j d| _tt| j|| d S )Nr	   )r
   r	   r   r   r   )r   r   r   )r   r   r   r   )   s    zReturnList.__init__c             C   s
   t j| S )N)listr   )r   r   r   r   r   -   s    zReturnList.__repr__c             C   s   t t | ffS )N)r   )r   r   r   r   r   0   s    zReturnList.__reduce__)r   r   r   r   r   r   r   r   r   r   )r   r   r   "   s   r   c               @   s>   e Zd ZdZdddZdd Zedd Zd	d
 Zdd Z	dS )
BoundFieldz
    A field object that also includes `.value` and `.error` properties.
    Returned when iterating over a serializer instance,
    providing an API similar to Django forms and form fields.
     c             C   s(   || _ || _|| _|| _|| j | _d S )N)_field_prefixvalueerrors
field_namename)r   fieldr"   r#   prefixr   r   r   r   =   s
    zBoundField.__init__c             C   s   t | j|S )N)getattrr    )r   	attr_namer   r   r   __getattr__D   s    zBoundField.__getattr__c             C   s   | j jS )N)r    r   )r   r   r   r   _proxy_classG   s    zBoundField._proxy_classc             C   s   t d| jj| j| jf S )Nz<%s value=%s errors=%s>)r   r   r   r"   r#   )r   r   r   r   r   K   s    zBoundField.__repr__c             C   s4   | j d ks| j dkrdn| j }| j| j|| j| jS )NFr   )r"   r   r    r#   r!   )r   r"   r   r   r   as_form_fieldP   s    zBoundField.as_form_fieldN)r   )
r   r   r   r   r   r*   propertyr+   r   r,   r   r   r   r   r   6   s   
r   c               @   s   e Zd Zdd ZdS )JSONBoundFieldc             C   sX   | j }t|ddsBytj| j ddd}W n ttfk
r@   Y nX | j| j|| j| j	S )NZis_json_stringFT   )	sort_keysindent)
r"   r(   r   dumps	TypeError
ValueErrorr   r    r#   r!   )r   r"   r   r   r   r,   V   s    zJSONBoundField.as_form_fieldN)r   r   r   r,   r   r   r   r   r.   U   s   r.   c                   s:   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Z  ZS )NestedBoundFieldz
    This `BoundField` additionally implements __iter__ and __getitem__
    in order to support nested bound fields. This class is the type of
    `BoundField` that is used for serializer fields.
    r   c                s.   |d ks|dkri }t t| j|||| d S )Nr   )r   r5   r   )r   r&   r"   r#   r'   )r   r   r   r   i   s    zNestedBoundField.__init__c             c   s$   x| j j D ]}| |j V  qW d S )N)fieldsvaluesr$   )r   r&   r   r   r   __iter__n   s    zNestedBoundField.__iter__c             C   sr   | j | }| jr| jj|nd }t| jtr8| jj|nd }t|dr\t|||| jd dS t	|||| jd dS )Nr6   .)r'   )
r6   r"   get
isinstancer#   r   hasattrr5   r%   r   )r   keyr&   r"   errorr   r   r   __getitem__r   s    

zNestedBoundField.__getitem__c             C   sj   i }xN| j j D ]@\}}t|ttfr0|||< q|d ks@|dkrDdnt|||< qW | j| j|| j| j	S )NFr   )
r"   itemsr;   r   r   r   r   r    r#   r!   )r   r7   r=   r"   r   r   r   r,   z   s    
$zNestedBoundField.as_form_field)r   )	r   r   r   r   r   r8   r?   r,   r   r   r   )r   r   r5   b   s
   r5   c               @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )BindingDictz
    This dict-like object is used to store fields on a serializer.

    This ensures that whenever fields are added to the serializer we call
    `field.bind()` so that the `field_name` and `parent` attributes
    can be set correctly.
    c             C   s   || _ t | _d S )N)r	   r   r6   )r   r	   r   r   r   r      s    zBindingDict.__init__c             C   s   || j |< |j|| jd d S )N)r$   parent)r6   bindr	   )r   r=   r&   r   r   r   __setitem__   s    
zBindingDict.__setitem__c             C   s
   | j | S )N)r6   )r   r=   r   r   r   r?      s    zBindingDict.__getitem__c             C   s   | j |= d S )N)r6   )r   r=   r   r   r   __delitem__   s    zBindingDict.__delitem__c             C   s
   t | jS )N)iterr6   )r   r   r   r   r8      s    zBindingDict.__iter__c             C   s
   t | jS )N)lenr6   )r   r   r   r   __len__   s    zBindingDict.__len__c             C   s   t j| jS )N)r   r   r6   )r   r   r   r   r      s    zBindingDict.__repr__N)r   r   r   r   r   rD   r?   rE   r8   rH   r   r   r   r   r   rA      s   rA   N)
__future__r   collectionsr   Zdjango.utils.encodingr   Zrest_framework.compatr   r   Zrest_framework.utilsr   r   r   r   objectr   r.   r5   rA   r   r   r   r   <module>   s   "