o
    "7h=                     @   s  d dl Z d dlZd dlmZ d dlmZ d dlmZ ejr&d dl	m
Z
mZ nd dlm
Z
mZ edZejr8e jneZeeeeeefZe
efZe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d Zdd ZG dd deZG dd deZdS )    N)
Serializer)Field)SerializationError)IterableMappingc                   @   s   e Zd ZdZeejZdS )	CharFieldz: Field that converts the attribute's value into a string. N)__name__
__module____qualname____doc__staticmethodsix	text_typeto_value r   r   s/var/www/epreuve.sigeris.cm/public_html/epreuve/venv/lib/python3.10/site-packages/django_rest/serializers/fields.pyr   $   s    r   c                   @      e Zd ZdZeeZdS )IntegerFieldz< Field that converts the attribute's value into an integer. N)r   r	   r
   r   r   intr   r   r   r   r   r   *       r   c                   @   r   )
FloatFieldz9 Field that converts the attribute's value into a float. N)r   r	   r
   r   r   floatr   r   r   r   r   r   0   s    r   c                   @   r   )BooleanFieldz; Field that converts the attribute's value into a boolean. N)r   r	   r
   r   r   boolr   r   r   r   r   r   8   r   r   c                 C   s0   t | tr	J d|  sJ d| jjd S )NzgCannot call `ListField()` with a Serializer object. An option `many=True` is available for serializers.zR`ListField()` can only be called with primitive-types fields. Given field type: {})
isinstancer   _is_to_value_overriddenformat	__class__r   field_instancer   r   r   _is_valid_field_instance>   s   r    c                    sb   t    j}td|j|fd|ji} fdd jD } fdd}t||_|di |S )	a  Allows to apply the Field.to_value() method on an iterable of values,
    instead of a single value. The same purpose could be achieved with `MethodField()`,
    but it'll be just too annoying.
    Example:

        class Subscription:
            status = ("active", "trialing", "canceled")

        class SubscriptionSerializer(Serializer):
            status = ListField(CharField(label="allowed_status", required=True))

        serialized_obj = SubscriptionSerializer(subscription).data
        # serialized_obj = {"allowed_status": ["active", "trialing", "canceled"]}

    :param field_instance: a Field instance that represents the type of a single element
        of the iterable to be passed to `to_value()` method.
        Note that the `field_instance` cannot be a `MethodField()`, a `ConstantField()`
        or a `Serializer` (which has the option `many=True` for the same purpose).
    The `ListField` generates a new field class (and instance) during the declaration
    (before the runtime), for perfomance purpose.
    zList{}r   c                    s   i | ]}|t  |qS r   )getattr).0namer   r   r   
<dictcomp>i   s    zListField.<locals>.<dictcomp>c                    s    fdd| D S )Nc                    s   g | ]}  |qS r   )r   )r"   vr   r   r   
<listcomp>l   s    z/ListField.<locals>.to_value.<locals>.<listcomp>r   )valuer   r   r   r   k   s   zListField.<locals>.to_valueNr   )	r    r   typer   r   r   	__slots__r   r   )r   field_classListFieldClassattrsr   r   r   r   	ListFieldK   s   

r-   c                   @   s2   e Zd ZdZdZdddZdd Zed	d
 ZdS )ConstantFielda=  
    A Field that allows to constant data injection into the serialized object, without
    the need to declare an (serialized) object method, or a serializer's `MethodField()`
    to return a constant.
    For example:

        foo_const = "Foo"
        class FooSerializer(Serializer):
            foo = ConstantField(label="foo_constant", required=False, constant=foo_const)
            bar = IntegerField()
        foo = Foo(bar=5)
        serialized_foo = FooSerializer(foo).data
        # serialized_foo = {'foo': "Foo", 'bar': 5}


    :param str label: Same as other fields.
    :param bool required: Same as other fields.
    :param constant: The constant to be added in the serialized object. Should be
        a combination (`list`, `dict` or single value) of primitive type (int, float,
        str, bool, None).
    )labelrequiredconstant_is_primitiveNTc                 C   sN   || _ || _d | _d| _| || _| js"| jr"td||jj	|| _
d S )NFzOnly primitive types are accepted in `ConstantField` (int, float, str, bool, None, unicode) and iterables/dict of primitive types. type({}) = {}.)r/   r0   	attr_namecall_is_primitive_constr2   r   r   r   r   r1   )selfr1   r/   r0   r   r   r   __init__   s   
zConstantField.__init__c                    s   j   fdd}|S )Nc                    s"   j sjstd jj S )Nz4Non-required field: Non primitive constant given: {})r2   r0   	TypeErrorr   r   r   )objr1   r6   r   r   getter   s   z'ConstantField.as_getter.<locals>.getter)r1   )r6   serializer_field_nameserializer_clsr;   r   r:   r   	as_getter   s   zConstantField.as_getterc                    sp   t |tsdS t |trdS t |tr-tdd | D }|o,t fdd| D S t fdd|D S )NFTc                 s   s    | ]
}t |ttfV  qd S N)r   strUnicodeType)r"   keyr   r   r   	<genexpr>   s    
z4ConstantField._is_primitive_const.<locals>.<genexpr>c                 3       | ]}  |V  qd S r?   r5   )r"   r'   clsr   r   rC      s    

c                 3   rD   r?   rE   )r"   elementrF   r   r   rC      s    )r   ALLOWED_CONSTANT_TYPESPRIMITIVE_TYPESr   allkeysvalues)rG   r1   keys_are_primitiver   rF   r   r5      s   


z!ConstantField._is_primitive_const)NNT)	r   r	   r
   r   r)   r7   r>   classmethodr5   r   r   r   r   r.   r   s    
r.   c                       s2   e Zd ZdZdZdZd	 fdd	Zdd Z  ZS )
MethodFielda  A Field class that calls a method on the `Serializer` class.

    This is useful if a Field needs to serialize a value that may come from multiple
    attributes on the same object. For example:

        class FooSerializer(Serializer):
            plus = MethodField()
            minus = MethodField('do_minus')

            def get_plus(self, foo_obj):
                return foo_obj.bar + foo_obj.baz

            def do_minus(self, foo_obj):
                return foo_obj.bar - foo_obj.baz

        foo = Foo(bar=5, baz=10)
        serialized_obj = FooSerializer(foo).data
        # serialized_obj = {'plus': 15, 'minus': -5}

    :param str method_name: The serializer's method name to be called. Defaults to
        `get_<field name>`.
    )r/   r0   method_nameTNc                    s    t t| jdi | || _d S )Nr   )superrP   r7   rQ   )r6   rQ   kwargsr   r   r   r7      s   
zMethodField.__init__c                 C   s"   | j }|d u rd|}t||S )Nzget_{0})rQ   r   r!   )r6   r<   r=   rQ   r   r   r   r>      s   

zMethodField.as_getterr?   )	r   r	   r
   r   r)   getter_needs_serializer_as_argr7   r>   __classcell__r   r   rT   r   rP      s    rP   ) typesr   #django_rest.serializers.serializersr   django_rest.serializers.baser   "django_rest.serializers.exceptionsr   PY34collections.abcr   r   collectionsr(   NoneTypePY2rA   r@   r   r   r   rJ   ITERABLE_TYPESrI   r   r   r   r   r    r-   r.   rP   r   r   r   r   <module>   s:   	'N