a
    xd                     @   s   d dl mZ d dlmZmZ d dlmZ d dlmZ G dd deZ	G dd de
ZG d	d
 d
ZG dd deZG dd deZG dd deZdS )    )OrderedDict)MappingMutableMapping)	force_str)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"   | d| _t j|i | d S N
serializerpopr	   super__init__selfargskwargs	__class__ _/var/www/html/Ranjet/env/lib/python3.9/site-packages/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 | S N)dict__repr__r   r   r   r   r      s    zReturnDict.__repr__c                 C   s   t t | ffS r   )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"   | d| _t j|i | d S r   r
   r   r   r   r   r   '   s    zReturnList.__init__c                 C   s
   t | S r   )listr   r   r   r   r   r   +   s    zReturnList.__repr__c                 C   s   t t | ffS r   )r"   r   r   r   r   r   .   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 r   )_field_prefixvalueerrors
field_namenamer   fieldr'   r(   prefixr   r   r   r   ;   s
    zBoundField.__init__c                 C   s   t | j|S r   )getattrr%   )r   	attr_namer   r   r   __getattr__B   s    zBoundField.__getattr__c                 C   s   | j jS r   )r%   r   r   r   r   r   _proxy_classE   s    zBoundField._proxy_classc                 C   s   d| j j| j| jf S )Nz<%s value=%s errors=%s>)r   r   r'   r(   r   r   r   r   r   I   s    zBoundField.__repr__c                 C   s4   | j d u s| j du rdn| j }| | j|| j| jS NFr$   )r'   r   r%   r(   r&   r   r'   r   r   r   as_form_fieldN   s    zBoundField.as_form_fieldN)r$   )
r   r   r   r   r   r0   propertyr1   r   r4   r   r   r   r   r#   4   s   

r#   c                   @   s   e Zd Zdd ZdS )JSONBoundFieldc              	   C   sX   | j }t|ddsBztj| j dddd}W n ttfy@   Y n0 | | j|| j| j	S )NZis_json_stringFT   ),z: )	sort_keysindent
separators)
r'   r.   r   dumps	TypeError
ValueErrorr   r%   r(   r&   r3   r   r   r   r4   T   s    
zJSONBoundField.as_form_fieldN)r   r   r   r4   r   r   r   r   r6   S   s   r6   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                    s4   |d u s|dkst |tsi }t |||| d S )Nr$   )
isinstancer   r   r   r+   r   r   r   r   l   s    zNestedBoundField.__init__c                 c   s    | j  D ]}| |j V  q
d S r   )fieldsvaluesr)   )r   r,   r   r   r   __iter__q   s    zNestedBoundField.__iter__c                 C   s   | j | }| jr| j|nd }t| jtr8| j|nd }t|dr\t|||| jd dS t	|ddr~t
|||| jd dS t|||| jd dS )NrA   .)r-   Z_is_jsonfieldF)rA   r'   getr@   r(   r   hasattrr?   r*   r.   r6   r#   )r   keyr,   r'   errorr   r   r   __getitem__u   s    

zNestedBoundField.__getitem__c                 C   sf   i }| j  D ]@\}}t|ttfr.|||< q|d u s>|du rBdnt|||< q| | j|| j| j	S r2   )
r'   itemsr@   r"   r   r   r   r%   r(   r&   )r   rB   rG   r'   r   r   r   r4      s    
"zNestedBoundField.as_form_field)r$   )	r   r   r   r   r   rC   rI   r4   r    r   r   r   r   r?   e   s
   
r?   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 r   )r	   r   rA   )r   r	   r   r   r   r      s    zBindingDict.__init__c                 C   s   || j |< |j|| jd d S )N)r)   parent)rA   bindr	   )r   rG   r,   r   r   r   __setitem__   s    
zBindingDict.__setitem__c                 C   s
   | j | S r   rA   r   rG   r   r   r   rI      s    zBindingDict.__getitem__c                 C   s   | j |= d S r   rO   rP   r   r   r   __delitem__   s    zBindingDict.__delitem__c                 C   s
   t | jS r   )iterrA   r   r   r   r   rC      s    zBindingDict.__iter__c                 C   s
   t | jS r   )lenrA   r   r   r   r   __len__   s    zBindingDict.__len__c                 C   s   t | jS r   )r   r   rA   r   r   r   r   r      s    zBindingDict.__repr__N)r   r   r   r   r   rN   rI   rQ   rC   rT   r   r   r   r   r   rK      s   rK   N)collectionsr   collections.abcr   r   Zdjango.utils.encodingr   Zrest_framework.utilsr   r   r"   r!   r#   r6   r?   rK   r   r   r   r   <module>   s   $