o
    Ö"7h  ã                   @   sp   d dl Z d dlZd dlmZmZ d dlmZ dd„ ZG dd„ deƒZ	G dd	„ d	e 
e	e¡ƒZG d
d„ deƒZdS )é    N)ÚFieldÚSerializerBase)ÚSerializationErrorc                 C   sV   |   ||¡}|d u r| | jp|¡}d }|  ¡ r| j}| jp|}|||| j| j| jfS ©N)	Ú	as_getterÚdefault_getterÚ	attr_nameÚ_is_to_value_overriddenÚto_valueÚlabelÚcallÚrequiredÚgetter_needs_serializer_as_arg)ÚfieldÚserializer_field_nameÚserializer_clsÚgetterr
   Úname© r   úx/var/www/epreuve.sigeris.cm/public_html/epreuve/venv/lib/python3.10/site-packages/django_rest/serializers/serializers.pyÚ_compile_field_to_tuple   s   
úr   c                       s4   e Zd Zedd„ ƒZedd„ ƒZ‡ fdd„Z‡  ZS )ÚSerializerMetac                 C   s>   i }|j d d d… D ]}t|tƒr| |j¡ q
| | ¡ |S )Néÿÿÿÿ)Ú__mro__Ú
issubclassr   ÚupdateÚ
_field_map)Údirect_fieldsr   Ú	field_mapÚclsr   r   r   Ú_get_fields#   s   
€
zSerializerMeta._get_fieldsc                    s   ‡ fdd„|   ¡ D ƒS )Nc                    s   g | ]
\}}t ||ˆ ƒ‘qS r   )r   )Ú.0r   r   ©r   r   r   Ú
<listcomp>/   s    
ÿÿz2SerializerMeta._compile_fields.<locals>.<listcomp>)Úitems)r   r   r   r"   r   Ú_compile_fields-   s   
þzSerializerMeta._compile_fieldsc                    s~   i }|  ¡ D ]\}}t|tƒr|||< q| ¡ D ]}||= qtt| ƒ | |||¡}|  ||¡}	|  |	|¡}
|	|_	t
|
ƒ|_|S r   )r$   Ú
isinstancer   ÚkeysÚsuperr   Ú__new__r    r%   r   ÚtupleÚ_compiled_fields)r   r   ÚbasesÚattrsr   r   r   ÚkÚreal_clsr   Úcompiled_fields©Ú	__class__r   r   r)   4   s   
€
zSerializerMeta.__new__)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr    r%   r)   Ú__classcell__r   r   r1   r   r   "   s    
	
r   c                       sD   e Zd ZdZejZd‡ fdd„	Zdd„ Zdd	„ Z	e
d
d„ ƒZ‡  ZS )Ú
SerializeraÒ  The Serializer class is used as a base for custom serializers.

    A Serializer class is also a subclass of Field class, which allows nesting
    Serializers. A new serializer is defined by subclassing the `Serializer` class,
    then adding each `Field` as a class variable.

    Example: :

        class FooSerializer(Serializer):
            foo = Field()
            bar = Field()

        foo = Foo(foo='hello', bar=5)
        serialized_obj = FooSerializer(foo).data
        # serialized_obj = {'foo': 'hello', 'bar': 5}

    :param instance: The object or iterable of objects to serialize.
    :param bool many: If `instance` is an iterable of objects, set `many` to `True`
        to serialize it as a list.
    NFc                    s,   t t| ƒjdi |¤Ž || _|| _d | _d S )Nr   )r(   r8   Ú__init__ÚinstanceÚmanyÚ_data)Úselfr:   r;   Úkwargsr1   r   r   r9   b   s   
zSerializer.__init__c                 C   s¢   i }|D ]J\}}}}}}	z|	r|| |ƒ}
n||ƒ}
|s|
d ur*|r$|
ƒ }
|r*||
ƒ}
W n t tttfyI } z|r?tt|ƒƒ‚W Y d }~qd }~ww |
||< q|S r   )ÚKeyErrorÚAttributeErrorÚ	TypeErrorÚ
ValueErrorr   Ústr)r=   r:   ÚfieldsÚserialized_valuer   r   r
   r   r   Ú	pass_selfÚresultÚer   r   r   Ú
_serializeh   s(   €ÿ€ÿ
zSerializer._serializec                    s2   | j ‰ | jr| j‰‡ ‡fdd„|D ƒS |  |ˆ ¡S )Nc                    s   g | ]}ˆ|ˆ ƒ‘qS r   r   )r!   Úo©rD   Ú	serializer   r   r#   ƒ   s    z'Serializer.to_value.<locals>.<listcomp>)r+   r;   rI   )r=   r:   r   rK   r   r
      s
   zSerializer.to_valuec                 C   s   | j du r|  | j¡| _ | j S )zoGet the serialized data from the Serializer instance. The data is cached
        for further accesses.
        N)r<   r
   r:   )r=   r   r   r   Údata†   s   
zSerializer.data)NF)r3   r4   r5   Ú__doc__ÚoperatorÚ
attrgetterr   r9   rI   r
   ÚpropertyrM   r7   r   r   r1   r   r8   I   s    r8   c                   @   s   e Zd ZdZejZdS )ÚDictSerializeraß  DictSerializer serializes python `dicts` instead of objects.

    `DictSerializer` uses `operator.itemgetter` to fetch data from the object
    to serialize, while `Serializer` class uses `operator.attrgetter`.

    Example: ::

        class FooSerializer(DictSerializer):
            foo = IntField()
            bar = FloatField()

        foo = {'foo': '5', 'bar': '2.2'}
        serialized_obj = FooSerializer(foo).data
        # serialized_obj = {'foo': 5, 'bar': 2.2}
    N)r3   r4   r5   rN   rO   Ú
itemgetterr   r   r   r   r   rR   ’   s    
rR   )rO   ÚsixÚdjango_rest.serializers.baser   r   Ú"django_rest.serializers.exceptionsr   r   Útyper   Úwith_metaclassr8   rR   r   r   r   r   Ú<module>   s   'I