o
    "7h*                    @   s@  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZ d dlmZ d d	lmZ  d d
l!m"Z" d dl#m$Z$m%Z%m&Z&m'Z' d dl(m)Z) d dl*m+Z+m,Z, d dl-m.Z.m/Z/ d dl0m1Z1 d dl2m3Z4 zd dl5Z5W n e6y   dZ5Y nw d dl7m8Z8 d dl9m:Z: d dl;m<Z<mZ d dl=m>Z> d dl?m@Z@mAZAmBZBmCZC d dlDmEZE d dlFmGZG d dlHmIZI G dd dZJG dd deKZLdd ZMdd  ZNd!d" ZOd#d$ ZPdqd%d&ZQd'd( ZRG d)d* d*ZSG d+d, d,ZTG d-d. d.eKZUeVeWd/ZXd0ZYd1ZZd2Z[d3Z\d4Z]G d5d6 d6Z^G d7d8 d8e^Z_G d9d: d:e^Z`G d;d< d<e`ZaG d=d> d>e`ZbG d?d@ d@e`ZcG dAdB dBe`ZdG dCdD dDe^ZeG dEdF dFe`ZfG dGdH dHe^ZgG dIdJ dJe^ZhG dKdL dLe^ZiG dMdN dNe^ZjG dOdP dPe^ZkG dQdR dRe^ZlG dSdT dTe^ZmG dUdV dVe^ZnG dWdX dXenZoG dYdZ dZenZG d[d\ d\e^ZpG d]d^ d^epZG d_d` d`e^ZqG dadb dbe^ZrG dcdd dde^ZsG dedf dfesZtG dgdh dhe^ZuG didj dje^ZvG dkdl dle^ZwG dmdn dne^ZxG dodp dpe^ZydS )r    N)Mapping)Enum)settings)ObjectDoesNotExist)ValidationError)EmailValidatorMaxLengthValidatorMaxValueValidatorMinLengthValidatorMinValueValidatorProhibitNullCharactersValidatorRegexValidatorURLValidator)FilePathField)
ImageFieldtimezone)
parse_dateparse_datetimeparse_duration
parse_timeduration_string)is_protected_type	smart_str)localize_inputsanitize_separators)clean_ipv6_address)gettext_lazy)ISO_8601)ip_address_validators)ErrorDetailr   )api_settings)htmlhumanize_datetimejsonrepresentation)lazy_format)valid_datetime)$ProhibitSurrogateCharactersValidatorc                   @      e Zd ZdZdS )emptyz
    This class is used to represent no data being provided for a given input
    or output value.

    It is required because `None` may be a valid input or output value.
    N__name__
__module____qualname____doc__ r1   r1   j/var/www/epreuve.sigeris.cm/public_html/epreuve/venv/lib/python3.10/site-packages/rest_framework/fields.pyr+   0   s    r+   c                   @   r*   )BuiltinSignatureErrorz
    Built-in function signatures are not inspectable. This exception is raised
    so the serializer can raise a helpful error message.
    Nr,   r1   r1   r1   r2   r3   :   s    r3   c                 C   sh   t | sdS t| rtdt| s!t| s!t| tjs!dS t	| }|j
 }tdd |D S )zC
    True if the object is a callable that takes no arguments.
    FzkBuilt-in function signatures are not inspectable. Wrap the function call in a simple, pure Python function.c                 s   s4    | ]}|j |jkp|j |jkp|j|jkV  qd S N)kindVAR_POSITIONALVAR_KEYWORDdefaultr+   ).0paramr1   r1   r2   	<genexpr>T   s    


z%is_simple_callable.<locals>.<genexpr>)callableinspect	isbuiltinr3   
isfunctionismethod
isinstance	functoolspartial	signature
parametersvaluesall)objsigparamsr1   r1   r2   is_simple_callableB   s   
 

rK   c                 C   s   |D ]?}zt | tr| | } nt| |} W n ty    Y  dS w t| rAz|  } W q ttfy@ } ztd||d}~ww q| S )z
    Similar to Python's built in `getattr(instance, attr)`,
    but takes a list of nested attributes, instead of a single attribute.

    Also accepts either attribute lookup on objects or dictionary lookups.
    NzGException raised in callable attribute "{}"; original exception was: {})	rA   r   getattrr   rK   AttributeErrorKeyError
ValueErrorformat)instanceattrsattrexcr1   r1   r2   get_attribute\   s$   



	rU   c                 C   sV   i }| D ]$}t |ttfs|||< q|\}}t |ttfr$t|||< q|||< q|S )z
    Convert choices into key/value dicts.

    to_choices_dict([1]) -> {1: 1}
    to_choices_dict([(1, '1st'), (2, '2nd')]) -> {1: '1st', 2: '2nd'}
    to_choices_dict([('Group', ((1, '1st'), 2))]) -> {'Group': {1: '1st', 2: '2'}}
    )rA   listtupleto_choices_dict)choicesretchoicekeyvaluer1   r1   r2   rX   w   s   

rX   c                 C   sH   i }|   D ]\}}t|tr|  D ]\}}|||< qq|||< q|S )z
    Convert a group choices dict into a flat dict of choices.

    flatten_choices_dict({1: '1st', 2: '2nd'}) -> {1: '1st', 2: '2nd'}
    flatten_choices_dict({'Group': {1: '1st', 2: '2nd'}}) -> {1: '1st', 2: '2nd'}
    )itemsrA   dict)rY   rZ   r\   r]   sub_key	sub_valuer1   r1   r2   flatten_choices_dict   s   


rb   c                 c   s    G dd d}G dd d}G dd d}d}|   D ]C\}}|r(||kr( n8t|trT||dV  |  D ]\}	}
|rC||krC n||	|
d	V  |d
7 }q7| V  q|||d	V  |d
7 }q|rx||krz|r||j|d}|d|ddV  dS dS dS dS )zE
    Helper function for options and option groups in templates.
    c                   @   s   e Zd ZdZdZdd ZdS )z&iter_options.<locals>.StartOptionGroupTFc                 S   
   || _ d S r4   label)selfre   r1   r1   r2   __init__      
z/iter_options.<locals>.StartOptionGroup.__init__Nr-   r.   r/   start_option_groupend_option_grouprg   r1   r1   r1   r2   StartOptionGroup   s    rl   c                   @   s   e Zd ZdZdZdS )z$iter_options.<locals>.EndOptionGroupFTN)r-   r.   r/   rj   rk   r1   r1   r1   r2   EndOptionGroup   s    rm   c                   @   s   e Zd ZdZdZdddZdS )ziter_options.<locals>.OptionFc                 S   s   || _ || _|| _d S r4   r]   display_textdisabled)rf   r]   ro   rp   r1   r1   r2   rg      s   
z%iter_options.<locals>.Option.__init__NFri   r1   r1   r1   r2   Option   s    rr   r   rd   )r]   ro      )countzn/aTrn   N)r^   rA   r_   rP   )grouped_choicescutoffcutoff_textrl   rm   rr   rt   r\   r]   r`   ra   r1   r1   r2   iter_options   s,   	



rx   c                    sX   t | ddpd z| j}W n ty     fdd| jD  Y S w  fdd| D S )zf
    Given a Django ValidationError, return a list of ErrorDetail,
    with the `code` populated.
    codeNinvalidc                    8   g | ]}t |jr|j|j n|j|jr|jn d qS ry   r!   rJ   messagery   r9   errorr}   r1   r2   
<listcomp>       z$get_error_detail.<locals>.<listcomp>c                    s$   i | ]\}}| fd d|D qS )c                    r{   r|   r~   r   r}   r1   r2   r      r   z/get_error_detail.<locals>.<dictcomp>.<listcomp>r1   )r9   kerrorsr}   r1   r2   
<dictcomp>   s    z$get_error_detail.<locals>.<dictcomp>)rL   
error_dictrM   
error_listr^   )exc_infor   r1   r}   r2   get_error_detail   s   



r   c                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
CreateOnlyDefaultz
    This class may be used to provide default values that are only used
    for create operations, but that do not return any value for update
    operations.
    Tc                 C   rc   r4   r8   )rf   r8   r1   r1   r2   rg      rh   zCreateOnlyDefault.__init__c                 C   sF   |j jd u}|rt t| jr t| jddr| |S |  S | jS )Nrequires_contextF)parentrQ   	SkipFieldr<   r8   rL   )rf   serializer_field	is_updater1   r1   r2   __call__   s   

zCreateOnlyDefault.__call__c                 C   s   d| j jt| jf S )Nz%s(%s))	__class__r-   reprr8   rf   r1   r1   r2   __repr__  s   zCreateOnlyDefault.__repr__N)r-   r.   r/   r0   r   rg   r   r   r1   r1   r1   r2   r      s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )CurrentUserDefaultTc                 C   s   |j d jS )Nrequest)contextuser)rf   r   r1   r1   r2   r   	     zCurrentUserDefault.__call__c                 C   s   d| j j S )Nz%s())r   r-   r   r1   r1   r2   r     r   zCurrentUserDefault.__repr__N)r-   r.   r/   r   r   r   r1   r1   r1   r2   r     s    r   c                   @   s   e Zd ZdS )r   N)r-   r.   r/   r1   r1   r1   r2   r     s    r    z-May not set both `read_only` and `write_only`z+May not set both `read_only` and `required`z)May not set both `required` and `default`z-Field(read_only=True) should be ReadOnlyFieldzrValidationError raised by `{class_name}`, but error key `{key}` does not exist in the `error_messages` dictionary.c                       s  e Zd ZdZededdZg ZeZdZ	dddeedddddddddd	Z
d
d Zdd Zedd Zejdd Zdd Zdd Zdd Zdd Zdd Zdd ZefddZdd  Zd!d" Zd#d$ Zd%d& Zed'd( Zed)d* Z fd+d,Zd-d. Zd/d0 Z  Z S )1Fieldr   zThis field is required.zThis field may not be null.)requirednullNF)	read_only
write_onlyr   r8   initialsourcere   	help_textstyleerror_messages
validators
allow_nullc                C   sB  t j| _t  jd7  _|d u r|tu o| }|r|rJ t|r&|r&J t|r0|tur0J t|r;| jt kr;J t|| _|| _	|| _
|| _|| _|tu rQ| jn|| _|| _|| _|	d u r`i n|	| _|| _| jturr|turr|| _|d ur{t|| _d | _d | _i }t| jjD ]}|t|di  q||
pi  || _d S )Nrs   default_error_messages)r   _creation_counterr+   NOT_READ_ONLY_WRITE_ONLYNOT_READ_ONLY_REQUIREDNOT_REQUIRED_DEFAULTr   USE_READONLYFIELDr   r   r   r8   r   r   re   r   r   r   default_empty_htmlrV   r   
field_namer   reversed__mro__updaterL   r   )rf   r   r   r   r8   r   r   re   r   r   r   r   r   messagesclsr1   r1   r2   rg   +  s<   


zField.__init__c                 O   s   | S r4   r1   )r   argskwargsr1   r1   r2   __class_getitem__Z     zField.__class_getitem__c                 C   s   | j |ksJ d|| jj|jjf || _|| _| jdu r&|dd | _| j du r.|| _ | j dkr8g | _dS | j 	d| _dS )z
        Initializes the field name and parent for the field instance.
        Called when a field is added to the parent serializer instance.
        zIt is redundant to specify `source='%s'` on field '%s' in serializer '%s', because it is the same as the field name. Remove the `source` keyword argument.N_ *.)
r   r   r-   r   r   re   replace
capitalizesource_attrssplitrf   r   r   r1   r1   r2   bind]  s   	



z
Field.bindc                 C   s   t | ds
|  | _| jS )N_validators)hasattrget_validatorsr   r   r1   r1   r2   r     s   

zField.validatorsc                 C   rc   r4   )r   )rf   r   r1   r1   r2   r     s   
c                 C   s
   t | jS r4   )rV   default_validatorsr   r1   r1   r2   r     rh   zField.get_validatorsc                 C   s   t | jr	|  S | jS )z
        Return a value to use when the field is being returned as a primitive
        value, without any object instance.
        )r<   r   r   r1   r1   r2   get_initial  s   
zField.get_initialc                 C   s   t |r?| j|vrt| jddrtS | jS || j }|dkr,| jr,t| ddr*dS dS |dkr=| js=t| ddr;dS tS |S |	| jtS )z
        Given the *incoming* primitive data, return the value for this field
        that should be validated and transformed to a native value.
        rC   Fr   allow_blankN)
r#   is_html_inputr   rL   rootr+   r   r   r   get)rf   
dictionaryrZ   r1   r1   r2   	get_value  s   


zField.get_valuec              
   C   s   zt || jW S  ty' } zdj| jjj| j|jjd}t||d}~w t	t
fyl } z8| jtur?|  W  Y d}~S | jrIW Y d}~dS | jsOt djt|j| j| jjj|jj|d}t||d}~ww )z~
        Given the *outgoing* object instance, return the primitive value
        that should be used for this field.
        zField source for `{serializer}.{field}` maps to a built-in function type and is invalid. Define a property or method on the `{instance}` instance that wraps the call to the built-in function.)
serializerfieldrQ   NzGot {exc_type} when attempting to get a value for field `{field}` on serializer `{serializer}`.
The serializer field might be named incorrectly and not match any attribute or key on the `{instance}` instance.
Original exception text was: {exc}.)exc_typer   r   rQ   rT   )rU   r   r3   rP   r   r   r-   r   typerN   rM   r8   r+   get_defaultr   r   r   )rf   rQ   rT   msgr1   r1   r2   rU     s>   

zField.get_attributec                 C   sN   | j tu st| jddrt t| j r$t| j ddr |  | S |   S | j S )a2  
        Return the default value to use when validating data if no input
        is provided for this field.

        If a default has not been set for this field then this will simply
        raise `SkipField`, indicating that no value should be set in the
        validated data for this field.
        rC   Fr   )r8   r+   rL   r   r   r<   r   r1   r1   r2   r     s   	

zField.get_defaultc                 C   s   | j r	d|  fS |tu r%t| jddrt | jr| d d|  fS |du r<| js3| d d	S | j	dkr:dS d	S d|fS )
a  
        Validate empty values, and either:

        * Raise `ValidationError`, indicating invalid data.
        * Raise `SkipField`, indicating that the field should be ignored.
        * Return (True, data), indicating an empty value that should be
          returned without any further validation being applied.
        * Return (False, data), indicating a non-empty value, that should
          have validation applied as normal.
        TrC   Fr   Nr   r   )FNTN)
r   r   r+   rL   r   r   r   failr   r   rf   datar1   r1   r2   validate_empty_values  s    


zField.validate_empty_valuesc                 C   s.   |  |\}}|r|S | |}| | |S )a  
        Validate a simple representation and return the internal value.

        The provided data may be `empty` if no representation was included
        in the input.

        May raise `SkipField` if the field should not be included in the
        validated data.
        )r   to_internal_valuerun_validators)rf   r   is_empty_valuer]   r1   r1   r2   run_validation  s   


zField.run_validationc                 C   s   g }| j D ]K}zt|ddr|||  n|| W q ty8 } zt|jtr( ||j W Y d}~qd}~w tyP } z|t| W Y d}~qd}~ww |rWt|dS )z
        Test the given value against all the validators on the field,
        and either raise a `ValidationError` or simply return.
        r   FN)	r   rL   r   rA   detailr_   extendDjangoValidationErrorr   )rf   r]   r   	validatorrT   r1   r1   r2   r     s&   
zField.run_validatorsc                 C      t dj| jj| jd)zN
        Transform the *incoming* primitive data into a native value.
        z{cls}.to_internal_value() must be implemented for field {field_name}. If you do not need to support write operations you probably want to subclass `ReadOnlyField` instead.r   r   NotImplementedErrorrP   r   r-   r   r   r1   r1   r2   r   6  s   zField.to_internal_valuec                 C   r   )zL
        Transform the *outgoing* native value into primitive data.
        zE{cls}.to_representation() must be implemented for field {field_name}.r   r   rf   r]   r1   r1   r2   to_representationC  s   zField.to_representationc                 K   sX   z| j | }W n ty   | jj}tj||d}t|w |jdi |}t||d)zH
        A helper method that simply raises a validation error.
        )
class_namer\   r}   Nr1   )r   rN   r   r-   MISSING_ERROR_MESSAGErP   AssertionErrorr   )rf   r\   r   r   r   message_stringr1   r1   r2   r   N  s   z
Field.failc                 C   s"   | }|j dur|j }|j dus|S )zB
        Returns the top-level serializer for this field.
        N)r   )rf   r   r1   r1   r2   r   [  s
   

z
Field.rootc                 C   s   t | jdi S )zY
        Returns the context as passed to the root serializer on initialization.
        _context)rL   r   r   r1   r1   r2   r   e  s   zField.contextc                    s   t  | }||_||_|S )z
        When a field is instantiated, we store the arguments that were used,
        so that we can present a helpful representation of the object.
        )super__new___args_kwargs)r   r   r   rQ   r   r1   r2   r   l  s   zField.__new__c                    s8   dd | j D } fdd| j D }| j|i |S )z
        When cloning fields we instantiate using the arguments it was
        originally created with, rather than copying the complete state.
        c                 S   s$   g | ]}t |tst|n|qS r1   )rA   
REGEX_TYPEcopydeepcopyr9   itemr1   r1   r2   r   ~  s    z&Field.__deepcopy__.<locals>.<listcomp>c                    s*   i | ]\}}||d vrt | n|qS ))r   regex)r   r   )r9   r\   r]   memor1   r2   r     s    z&Field.__deepcopy__.<locals>.<dictcomp>)r   r   r^   r   )rf   r   r   r   r1   r   r2   __deepcopy__v  s   
zField.__deepcopy__c                 C   s
   t | S )z
        Fields are represented using their initial calling arguments.
        This allows us to create descriptive representations for serializer
        instances that show all the declared fields on the serializer.
        )r&   
field_reprr   r1   r1   r2   r     s   
zField.__repr__)!r-   r.   r/   r   r   r   r   r+   r   r   rg   r   r   propertyr   setterr   r   r   rU   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r1   r1   r   r2   r      sJ    /$

	)!
	

r   c                       sd   e Zd ZdediZdZdZh dZh dZh dZ	 fddZ
ed	d
 Zdd Zdd Z  ZS )BooleanFieldrz   zMust be a valid boolean.F>   1tyonyestruers   >   0fnnoofffalser   >   Nr   r   c                    s.   | ddrd | _d | _t jdi | d S )Nr   Fr1   )r   r   r   r   rg   rf   r   r   r1   r2   rg     s   zBooleanField.__init__c                 C   s   t | tr	|  S | S r4   )rA   strlowerr]   r1   r1   r2   _lower_if_str  s   
zBooleanField._lower_if_strc                 C   s   t t> | || jv r	 W d    dS | || jv r(	 W d    dS | || jv r<| jr<	 W d    d S W d    n1 sFw   Y  | jd|d d S )NTFrz   input)	
contextlibsuppress	TypeErrorr  TRUE_VALUESFALSE_VALUESNULL_VALUESr   r   r   r1   r1   r2   r     s    zBooleanField.to_internal_valuec                 C   sJ   |  || jv r
dS |  || jv rdS |  || jv r!| jr!d S t|S )NTF)r  r  r  r  r   boolr   r1   r1   r2   r     s   zBooleanField.to_representation)r-   r.   r/   r   r   r   r   r  r  r  rg   staticmethodr  r   r   r   r1   r1   r   r2   r     s    


r   c                       s^   e Zd ZededededdZdZ fddZef fd	d
	Zdd Z	dd Z
  ZS )	CharFieldzNot a valid string.zThis field may not be blank.;Ensure this field has no more than {max_length} characters.z7Ensure this field has at least {min_length} characters.)rz   blank
max_length
min_lengthr   c                    s   | dd| _| dd| _| dd | _| dd | _t jd
i | | jd ur?t| jd | jd}| j	
t| j|d | jd urYt| jd | jd	}| j	
t| j|d | j	
t  | j	
t  d S )Nr   Ftrim_whitespaceTr  r  r  r   r  r1   )popr   r   r  r  r   rg   r'   r   r   appendr   r
   r   r)   rf   r   r   r   r1   r2   rg     s"   

zCharField.__init__c                    s>   |dks| j rt| dkr| js| d dS t |S )Nr   r  )r   r  stripr   r   r   r   r   r   r1   r2   r     s
   
zCharField.run_validationc                 C   s>   t |tst |tttfs| d t|}| jr| S |S )Nrz   )rA   r  r  intfloatr   r   r'  rf   r   r]   r1   r1   r2   r     s   
zCharField.to_internal_valuec                 C      t |S r4   )r  r   r1   r1   r2   r         zCharField.to_representation)r-   r.   r/   r   r   r   rg   r+   r   r   r   r   r1   r1   r   r2   r    s    
	r  c                       (   e Zd ZdediZ fddZ  ZS )
EmailFieldrz   zEnter a valid email address.c                    2   t  jdi | t| jd d}| j| d S Nrz   r"  r1   )r   rg   r   r   r   r%  rf   r   r   r   r1   r2   rg   	     zEmailField.__init__r-   r.   r/   r   r   rg   r   r1   r1   r   r2   r.        r.  c                       r-  )
RegexFieldrz   z/This value does not match the required pattern.c                    s4   t  jdi | t|| jd d}| j| d S r0  )r   rg   r   r   r   r%  )rf   r   r   r   r   r1   r2   rg     s   zRegexField.__init__r3  r1   r1   r   r2   r5    r4  r5  c                       s0   e Zd ZededdZd fdd	Z  ZS )	SlugFieldzLEnter a valid "slug" consisting of letters, numbers, underscores or hyphens.zUEnter a valid "slug" consisting of Unicode letters, numbers, underscores, or hyphens.)rz   invalid_unicodeFc                    sd   t  jdi | || _| jrttdtj| jd d}nttd| jd d}| j	| d S )Nz	^[-\w]+\Zr7  r"  z^[-a-zA-Z0-9_]+$rz   r1   )
r   rg   allow_unicoder   recompileUNICODEr   r   r%  )rf   r8  r   r   r   r1   r2   rg      s   zSlugField.__init__rq   r3  r1   r1   r   r2   r6    s
    r6  c                       r-  )URLFieldrz   zEnter a valid URL.c                    r/  r0  )r   rg   r   r   r   r%  r1  r   r1   r2   rg   /  r2  zURLField.__init__r3  r1   r1   r   r2   r<  *  r4  r<  c                       s<   e Zd ZdZdediZ fddZdd Zdd	 Z  Z	S )
	UUIDField)hex_verbosehexr(  urnrz   zMust be a valid UUID.c                    sF   | dd| _| j| jvrtdd| jt jdi | d S )NrP   r>  z;Invalid format for uuid representation. Must be one of "{}"z", "r1   )r$  uuid_formatvalid_formatsrO   rP   joinr   rg   r  r   r1   r2   rg   <  s   zUUIDField.__init__c                 C   sx   t |tjs:z"t |trtj|dW S t |trtj|dW S | jd|d W |S  ty9   | jd|d Y |S w |S )Nr(  )r?  rz   r  )rA   uuidUUIDr(  r  r   rO   r   r1   r1   r2   r   E  s   

zUUIDField.to_internal_valuec                 C   s   | j dkr	t|S t|| j S )Nr>  )rA  r  rL   r   r1   r1   r2   r   R  s   
zUUIDField.to_representation)
r-   r.   r/   rB  r   r   rg   r   r   r   r1   r1   r   r2   r=  5  s    	r=  c                       s:   e Zd ZdZdediZd	 fdd	Z fddZ  ZS )
IPAddressFieldz5Support both IPAddressField and GenericIPAddressFieldrz   z#Enter a valid IPv4 or IPv6 address.bothc                    sD   |  | _| jdk| _t jdi | t|| j}| j| d S )NrH  r1   )r  protocolunpack_ipv4r   rg   r    r   r   )rf   rI  r   r   r   r1   r2   rg   `  s
   
zIPAddressField.__init__c                    sj   t |ts| jd|d d|v r/z| jdv rt|| jW S W n ty.   | jd|d Y nw t |S )Nrz   r  :)rH  ipv6)	rA   r  r   rI  r   rJ  r   r   r   r   r   r1   r2   r   g  s   

z IPAddressField.to_internal_value)rH  )	r-   r.   r/   r0   r   r   rg   r   r   r1   r1   r   r2   rG  Y  s    rG  c                       sX   e Zd ZededededdZdZedZ fdd	Z	d
d Z
dd Z  ZS )IntegerFieldzA valid integer is required.7Ensure this value is less than or equal to {max_value}.:Ensure this value is greater than or equal to {min_value}.String value too large.)rz   	max_value	min_valuemax_string_length  z\.0*\s*$c                       | dd | _| dd | _t jdi | | jd ur1t| jd | jd}| jt	| j|d | jd urMt| jd | jd}| jt
| j|d d S d S NrQ  rR  rQ  r"  rR  r1   r$  rQ  rR  r   rg   r'   r   r   r%  r	   r   r&  r   r1   r2   rg        

zIntegerField.__init__c              	   C   sb   t |trt|| jkr| d zt| jdt|}W |S  tt	fy0   | d Y |S w )NrS  r   rz   )
rA   r  lenMAX_STRING_LENGTHr   r(  
re_decimalsubrO   r  r   r1   r1   r2   r     s   
zIntegerField.to_internal_valuec                 C   r+  r4   rD  r   r1   r1   r2   r     r,  zIntegerField.to_representation)r-   r.   r/   r   r   r\  r9  r:  r]  rg   r   r   r   r1   r1   r   r2   rM  w  s    

rM  c                       sT   e Zd ZedededededdZdZ fdd	Zd
d Zdd Z  Z	S )
FloatFieldA valid number is required.rN  rO  rP  z+Integer value too large to convert to float)rz   rQ  rR  rS  overflowrT  c                    rU  rV  rY  r&  r   r1   r2   rg     rZ  zFloatField.__init__c              	   C   sl   t |trt|| jkr| d zt|W S  ttfy'   | d Y d S  ty5   | d Y d S w )NrS  rz   ra  )	rA   r  r[  r\  r   r)  r  rO   OverflowErrorr   r1   r1   r2   r     s   

zFloatField.to_internal_valuec                 C   r+  r4   )r)  r   r1   r1   r2   r     r,  zFloatField.to_representation)
r-   r.   r/   r   r   r\  rg   r   r   r   r1   r1   r   r2   r_    s    r_  c                       s   e Zd ZedededededededdZd	Z	
	d fdd	Z fddZdd Zdd Z	dd Z
dd Z  ZS )DecimalFieldr`  rN  rO  z@Ensure that there are no more than {max_digits} digits in total.zGEnsure that there are no more than {max_decimal_places} decimal places.zVEnsure that there are no more than {max_whole_digits} digits before the decimal point.rP  )rz   rQ  rR  
max_digitsmax_decimal_placesmax_whole_digitsrS  rT  NFc	                    sn  || _ || _|| _|| _|d ur|| _| jrd| _|| _|| _| jd ur2t| jtt	j
fs2td | jd urEt| jtt	j
fsEtd | j d urW| jd urW| j | j | _nd | _t jdi |	 | jd ur}t| jd | jd}
| jt| j|
d | jd urt| jd | jd}
| jt| j|
d |d urd	d
 tt	 D }||v sJ d||f || _d S )NTz3max_value should be an integer or Decimal instance.z3min_value should be an integer or Decimal instance.rQ  rW  r"  rR  rX  c                 S   s   g | ]\}}| d r|qS )ROUND_)
startswith)r9   r   vr1   r1   r2   r     s    z)DecimalField.__init__.<locals>.<listcomp>z=Invalid rounding option %s. Valid values for rounding are: %sr1   )rd  decimal_placeslocalizenormalize_outputcoerce_to_stringrQ  rR  rA   r(  decimalDecimalwarningswarnrf  r   rg   r'   r   r   r%  r	   r   varsr^   rounding)rf   rd  rj  rm  rQ  rR  rk  rs  rl  r   r   valid_roundingsr   r1   r2   rg     sD   






zDecimalField.__init__c                    s&   t | dkr| jrdS t |S )Nr   r   )r   r'  r   r   r   r   r   r1   r2   r     s   z"DecimalField.validate_empty_valuesc                 C   s   t | }| jrt|}t|| jkr| d zt|}W n tj	y/   | d Y nw |
 r9| d |tdtdfv rJ| d | | |S )zd
        Validate that the input is a decimal number and return a Decimal
        instance.
        rS  rz   Infz-Inf)r   r'  rk  r   r[  r\  r   rn  ro  DecimalExceptionis_nanquantizevalidate_precisionr*  r1   r1   r2   r     s   


zDecimalField.to_internal_valuec                 C   s   |  \}}}|dkrt|| }|}d}nt|t|kr-t|}|t| }t|}nt|}d}|}| jdurG|| jkrG| jd| jd | jdurY|| jkrY| jd| jd | jdurk|| jkrk| jd| jd |S )	a  
        Ensure that there are no more than max_digits in the number, and no
        more than decimal_places digits after the decimal point.

        Override this method to disable the precision validation for input
        values or to enhance it in any way you need to.
        r   Nrd  )rd  re  )re  rf  )rf  )as_tupler[  absrd  r   rj  rf  )rf   r]   sign
digittupleexponenttotal_digitswhole_digitsrj  r1   r1   r2   ry    s&   
zDecimalField.validate_precisionc                 C   sx   t | dtj}|d u r|rdS d S t|tjs tt| }| |}| j	r,|
 }|s0|S | jr7t|S d|S )Nrm  r   z{:f})rL   r"   COERCE_DECIMAL_TO_STRINGrA   rn  ro  r  r'  rx  rl  	normalizerk  r   rP   )rf   r]   rm  	quantizedr1   r1   r2   r   <  s   

zDecimalField.to_representationc                 C   sJ   | j du r|S t  }| jdur| j|_|jtd| j  | j|dS )zI
        Quantize the decimal value to the configured precision.
        Nz.1)rs  r   )	rj  rn  
getcontextr   rd  precrx  ro  rs  )rf   r]   r   r1   r1   r2   rx  T  s   

zDecimalField.quantize)NNNFNF)r-   r.   r/   r   r   r\  rg   r   r   ry  r   rx  r   r1   r1   r   r2   rc    s$    	)#rc  c                       sj   e Zd ZededededdZejjZeddf fdd	Z	d	d
 Z
dd Zdd Zdd Z  ZS )DateTimeFieldzFDatetime has wrong format. Use one of these formats instead: {format}.z#Expected a datetime but got a date.z/Invalid datetime for the timezone "{timezone}".zDatetime value out of range.)rz   date
make_awarera  Nc                    s@   |t ur|| _|d ur|| _|d ur|| _t jdi | d S Nr1   )r+   rP   input_formatsr   r   rg   )rf   rP   r  default_timezoner   r   r1   r2   rg   p  s   zDateTimeField.__init__c              
   C   s   t | dr| jn|  }|dur]t|r*z||W S  ty)   | d Y nw zt||}t|s<| jd|d |W S  t	y\ } zt
rVt|t
jjrV| jd|d |d}~ww |du rnt|rnt|tjjS |S )z
        When `self.default_timezone` is `None`, always return naive datetimes.
        When `self.default_timezone` is not `None`, always return aware datetimes.
        r   Nra  r  r   )r   r   r  is_aware
astimezonerb  r   r  r(   	ExceptionpytzrA   
exceptionsInvalidTimeError
make_naivedatetimeutc)rf   r]   field_timezonedter1   r1   r2   enforce_timezoney  s,   
zDateTimeField.enforce_timezonec                 C   s   t jrt S d S r4   )r   USE_TZr   get_current_timezoner   r1   r1   r2   r    s   zDateTimeField.default_timezonec              	   C   s   t | dtj}t|tjrt|tjs| d t|tjr#| |S |D ]A}t	t
t1 | tkrJt|}|d urJ| |W  d      S | ||}| |W  d      S 1 saw   Y  q%t|}| jd|d d S )Nr  r  rz   rP   )rL   r"   DATETIME_INPUT_FORMATSrA   r  r  r   r  r  r  rO   r  r  r   r   datetime_parserr$   datetime_formatsrf   r]   r  input_formatparsedhumanized_formatr1   r1   r2   r     s"   

&
	zDateTimeField.to_internal_valuec                 C   sr   |sd S t | dtj}|d u st|tr|S | |}| tkr4| }|	dr2|d d d }|S |
|S )NrP   z+00:00iZ)rL   r"   DATETIME_FORMATrA   r  r  r  r   	isoformatendswithstrftimerf   r]   output_formatr1   r1   r2   r     s   


zDateTimeField.to_representation)r-   r.   r/   r   r   r  strptimer  r+   rg   r  r  r   r   r   r1   r1   r   r2   r  g  s    	r  c                       sL   e Zd ZededdZejjZedf fdd	Z	dd Z
d	d
 Z  ZS )	DateFieldzBDate has wrong format. Use one of these formats instead: {format}.z#Expected a date but got a datetime.)rz   r  Nc                    2   |t ur|| _|d ur|| _t jdi | d S r  r+   rP   r  r   rg   rf   rP   r  r   r   r1   r2   rg     
   zDateField.__init__c              
   C   s   t | dtj}t|tjr| d t|tjr|S |D ]<}| tkr?zt	|}W n t
tfy5   Y qw |d ur>|  S qz| ||}W n t
tfyR   Y qw |   S t|}| jd|d d S )Nr  r  rz   r  )rL   r"   DATE_INPUT_FORMATSrA   r  r   r  r  r   r   rO   r  r  r$   date_formatsr  r1   r1   r2   r     s.   

zDateField.to_internal_valuec                 C   s^   |sd S t | dtj}|d u st|tr|S t|tjr J d| tkr*| S |	|S )NrP   zExpected a `date`, but got a `datetime`. Refusing to coerce, as this may mean losing timezone information. Use a custom read-only field and deal with timezone issues explicitly.)
rL   r"   DATE_FORMATrA   r  r  r  r   r  r  r  r1   r1   r2   r     s   
zDateField.to_representationr-   r.   r/   r   r   r  r  r  r+   rg   r   r   r   r1   r1   r   r2   r    s    r  c                       sF   e Zd ZdediZejjZedf fdd	Z	dd Z
dd	 Z  ZS )
	TimeFieldrz   zBTime has wrong format. Use one of these formats instead: {format}.Nc                    r  r  r  r  r   r1   r2   rg   
  r  zTimeField.__init__c              
   C   s   t | dtj}t|tjr|S |D ]<}| tkr4zt|}W n t	t
fy*   Y qw |d ur3|  S qz| ||}W n t	t
fyG   Y qw |   S t|}| jd|d d S )Nr  rz   r  )rL   r"   TIME_INPUT_FORMATSrA   r  timer  r   r   rO   r  r  r$   time_formatsr   r  r1   r1   r2   r     s*   
zTimeField.to_internal_valuec                 C   sb   |dv rd S t | dtj}|d u st|tr|S t|tjr"J d| tkr,| S |	|S )N)Nr   rP   zExpected a `time`, but got a `datetime`. Refusing to coerce, as this may mean losing timezone information. Use a custom read-only field and deal with timezone issues explicitly.)
rL   r"   TIME_FORMATrA   r  r  r  r   r  r  r  r1   r1   r2   r   +  s   
zTimeField.to_representationr  r1   r1   r   r2   r    s    r  c                       sJ   e Zd ZededededdZ fddZdd	 Zd
d Z  ZS )DurationFieldzFDuration has wrong format. Use one of these formats instead: {format}.rN  rO  z=The number of days must be between {min_days} and {max_days}.)rz   rQ  rR  ra  c                    rU  rV  rY  r&  r   r1   r2   rg   J  rZ  zDurationField.__init__c                 C   sn   t |tjr|S ztt|}W n ty'   | jdtjjjtjj	jd Y nw |d ur.|S | jddd d S )Nra  )min_daysmax_daysrz   z[DD] [HH:[MM:]]ss[.uuuuuu]r  )
rA   r  	timedeltar   r  rb  r   mindaysmax)rf   r]   r  r1   r1   r2   r   W  s    zDurationField.to_internal_valuec                 C   r+  r4   r   r   r1   r1   r2   r   b  r,  zDurationField.to_representation	r-   r.   r/   r   r   rg   r   r   r   r1   r1   r   r2   r  B  s    r  c                       sf   e Zd ZdediZdZedZ fddZdd Zd	d
 Z	dd Z
dd Zdd ZeeeZ  ZS )ChoiceFieldinvalid_choice "{input}" is not a valid choice.NzMore than {count} items...c                    sJ   || _ |d| j| _|d| j| _|dd| _t jdi | d S )Nhtml_cutoffhtml_cutoff_textr   Fr1   )rY   r$  r  r  r   r   rg   )rf   rY   r   r   r1   r2   rg   o  s
   zChoiceField.__init__c                 C   sh   |dkr	| j r	dS t|trt|t|jkr|j}z| jt| W S  ty3   | jd|d Y d S w )Nr   r  r  )r   rA   r   r  r]   choice_strings_to_valuesrN   r   r   r1   r1   r2   r   x  s   zChoiceField.to_internal_valuec                 C   s@   |dv r|S t |trt|t|jkr|j}| jt||S )N)r   N)rA   r   r  r]   r  r   r   r1   r1   r2   r     s
   zChoiceField.to_representationc                 C   s   t | j| j| jdS )zP
        Helper method for use with templates rendering select widgets.
        )rv   rw   )rx   ru   r  r  r   r1   r1   r2   rx     s
   zChoiceField.iter_optionsc                 C   s   | j S r4   )_choicesr   r1   r1   r2   _get_choices  s   zChoiceField._get_choicesc                 C   s,   t || _t| j| _dd | jD | _d S )Nc                 S   s<   i | ]}t |trt|t|jkrt|jnt||qS r1   )rA   r   r  r]   )r9   r\   r1   r1   r2   r     s    0z,ChoiceField._set_choices.<locals>.<dictcomp>)rX   ru   rb   r  rY   r  )rf   rY   r1   r1   r2   _set_choices  s
   
zChoiceField._set_choices)r-   r.   r/   r   r   r  r  rg   r   r   rx   r  r  r   rY   r   r1   r1   r   r2   r  h  s    	

r  c                       sT   e Zd ZedededdZg Z fddZdd Z fd	d
Zdd Z	  Z
S )MultipleChoiceFieldr  5Expected a list of items but got type "{input_type}".z This selection may not be empty.)r  
not_a_listr+   c                    s$   | dd| _t jdi | d S )Nallow_emptyTr1   )r$  r  r   rg   r  r   r1   r2   rg        zMultipleChoiceField.__init__c                 C   s@   | j |vrt| jddrtS t|r|| j S || j tS )NrC   F)r   rL   r   r+   r#   r   getlistr   rf   r   r1   r1   r2   r     s   

zMultipleChoiceField.get_valuec                    sX   t |ts
t|ds jdt|jd  js"t|dkr" d  fdd|D S )N__iter__r  
input_typer   r+   c                    s   h | ]
}t t|qS r1   )r   r  r   r   )r   rf   r1   r2   	<setcomp>  s    z8MultipleChoiceField.to_internal_value.<locals>.<setcomp>)rA   r  r   r   r   r-   r  r[  r   r   r   r2   r     s   
z%MultipleChoiceField.to_internal_valuec                        fdd|D S )Nc                    s   h | ]} j t||qS r1   )r  r   r  r   r   r1   r2   r    s    z8MultipleChoiceField.to_representation.<locals>.<setcomp>r1   r   r1   r   r2   r     s   
z%MultipleChoiceField.to_representation)r-   r.   r/   r   r   r   rg   r   r   r   r   r1   r1   r   r2   r    s    
r  c                       s.   e Zd ZdediZ		d fdd	Z  ZS )	r   r  z%"{input}" is not a valid path choice.NFTc           	         s<   t ||||||d}|j|d< ||d< t jdi | d S )N)match	recursiveallow_filesallow_foldersr   rY   r   r1   )DjangoFilePathFieldrY   r   rg   )	rf   pathr  r  r  r  r   r   r   r   r1   r2   rg     s   
zFilePathField.__init__)NFTFNr3  r1   r1   r   r2   r     s    r   c                       sP   e Zd ZedededededdZ fddZd	d
 Zdd Z  ZS )	FileFieldzNo file was submitted.zGThe submitted data was not a file. Check the encoding type on the form.z No filename could be determined.zThe submitted file is empty.zKEnsure this filename has at most {max_length} characters (it has {length}).)r   rz   no_namer+   r  c                    sF   | dd | _| dd| _d|v r| d| _t jdi | d S )Nr  allow_empty_fileFuse_urlr1   )r$  r  r  r  r   rg   r  r   r1   r2   rg     s
   zFileField.__init__c                 C   s~   z|j }|j}W n ty   | d Y nw |s| d | js(|s(| d | jr=t|| jkr=| jd| jt|d |S )Nrz   r  r+   r  )r  length)namesizerM   r   r  r  r[  )rf   r   	file_name	file_sizer1   r1   r2   r     s   



zFileField.to_internal_valuec                 C   sd   |sd S t | dtj}|r/z|j}W n
 ty   Y d S w | jdd }|d ur-||S |S |jS )Nr  r   )	rL   r"   UPLOADED_FILES_USE_URLurlrM   r   r   build_absolute_urir  )rf   r]   r  r  r   r1   r1   r2   r     s   

zFileField.to_representationr  r1   r1   r   r2   r    s    r  c                       s4   e Zd ZdediZ fddZ fddZ  ZS )r   invalid_imagezYUpload a valid image. The file you uploaded was either not an image or a corrupted image.c                    s$   | dt| _t jdi | d S )N_DjangoImageFieldr1   )r$  DjangoImageFieldr  r   rg   r  r   r1   r2   rg     r  zImageField.__init__c                    s&   t  |}|  }| j|_||S r4   )r   r   r  r   clean)rf   r   file_objectdjango_fieldr   r1   r2   r     s   
zImageField.to_internal_value)r-   r.   r/   r   r   rg   r   r   r1   r1   r   r2   r     s    r   c                       s,   e Zd Z fddZdd Zdd Z  ZS )_UnvalidatedFieldc                    s"   t  jdi | d| _d| _d S )NTr1   )r   rg   r   r   r  r   r1   r2   rg   ,  s   
z_UnvalidatedField.__init__c                 C      |S r4   r1   r   r1   r1   r2   r   1  r   z#_UnvalidatedField.to_internal_valuec                 C   r  r4   r1   r   r1   r1   r2   r   4  r   z#_UnvalidatedField.to_representation)r-   r.   r/   rg   r   r   r   r1   r1   r   r2   r  +  s    r  c                       sd   e Zd Ze Zg ZededededdZ fddZdd	 Z	d
d Z
dd Zdd Z  ZS )	ListFieldr  zThis list may not be empty.z5Ensure this field has at least {min_length} elements.z9Ensure this field has no more than {max_length} elements.)r  r+   r  r  c                    s   | dt| j| _| dd| _| dd | _| dd | _t| jr*J d| jj	d u s4J dt
 jdi | | jjd| d	 | jd ur_t| jd | jd
}| jt| j|d | jd ur{t| jd | jd}| jt| j|d d S d S )Nchildr  Tr  r  "`child` has not been instantiated.vThe `source` argument is not meaningful when applied to a `child=` field. Remove `source=` from the field declaration.r   r   r   r!  r"  r#  r1   )r$  r   r   r  r  r  r  r=   isclassr   r   rg   r   r'   r   r   r%  r   r
   r&  r   r1   r2   rg   B  s"   

zListField.__init__c                 C   sd   | j |vrt| jddrtS t|r+|| j g }t|dkr"|S tj|| j tdS |	| j tS )NrC   Fr   )prefixr8   )
r   rL   r   r+   r#   r   r  r[  parse_html_listr   )rf   r   valr1   r1   r2   r   W  s   

zListField.get_valuec                 C   sj   t |rt j|g d}t|ttfst|ds"| jdt|j	d | j
s0t|dkr0| d | |S )zY
        List of dicts of native values <- List of dicts of primitive datatypes.
        r   r  r  r  r   r+   )r#   r   r  rA   r  r   r   r   r   r-   r  r[  run_child_validationr   r1   r1   r2   r   f  s   


zListField.to_internal_valuec                    r  )zS
        List of object instances -> List of dicts of primitive datatypes.
        c                    s$   g | ]}|d ur j |nd qS r4   )r  r   r   r   r1   r2   r   v  s   $ z/ListField.to_representation.<locals>.<listcomp>r1   r   r1   r   r2   r   r  s   zListField.to_representationc                 C   s   g }i }t |D ]=\}}z|| j| W q ty. } z|j||< W Y d }~qd }~w tyE } zt|||< W Y d }~qd }~ww |sJ|S t|r4   )	enumerater%  r  r   r   r   r   r   )rf   r   resultr   idxr   r  r1   r1   r2   r  x  s   zListField.run_child_validationr-   r.   r/   r  r  r   r   r   rg   r   r   r   r  r   r1   r1   r   r2   r  8  s    r  c                       sX   e Zd Ze Zi ZededdZ fddZdd Z	dd	 Z
d
d Zdd Z  ZS )	DictFieldz;Expected a dictionary of items but got type "{input_type}".z!This dictionary may not be empty.)
not_a_dictr+   c                    sr   | dt| j| _| dd| _t| jrJ d| jjd u s&J dt j	di | | jj
d| d d S )	Nr  r  Tr  r  r   r  r1   )r$  r   r   r  r  r=   r  r   r   rg   r   r  r   r1   r2   rg     s   zDictField.__init__c                 C   s(   t |rt j|| jdS || jtS )N)r  )r#   r   parse_html_dictr   r   r+   r  r1   r1   r2   r     s   
zDictField.get_valuec                 C   sX   t |r
t |}t|ts| jdt|jd | js't	|dkr'| d | 
|S )zI
        Dicts of native values <- Dicts of primitive datatypes.
        r  r  r   r+   )r#   r   r   rA   r_   r   r   r-   r  r[  r  r   r1   r1   r2   r     s   




zDictField.to_internal_valuec                    s    fdd|  D S )Nc                    s.   i | ]\}}t ||d ur j|nd qS r4   )r  r  r   )r9   r\   r  r   r1   r2   r     s    z/DictField.to_representation.<locals>.<dictcomp>)r^   r   r1   r   r2   r     s   
zDictField.to_representationc                 C   st   i }i }|  D ])\}}t|}z
| j|||< W q ty1 } z|j||< W Y d }~qd }~ww |s6|S t|r4   )r^   r  r  r   r   r   )rf   r   r  r   r\   r]   r  r1   r1   r2   r    s   zDictField.run_child_validationr  r1   r1   r   r2   r    s    r  c                       s(   e Zd ZedddZ fddZ  ZS )HStoreFieldT)r   r   c                    s*   t  jdi | t| jtsJ dd S )NzjThe `child` argument must be an instance of `CharField`, as the hstore extension stores values as strings.r1   )r   rg   rA   r  r  r  r   r1   r2   rg     s   zHStoreField.__init__)r-   r.   r/   r  r  rg   r   r1   r1   r   r2   r    s    r  c                       sD   e Zd ZdediZdZ fddZdd Zdd	 Zd
d Z	  Z
S )	JSONFieldrz   zValue must be valid JSON.Tc                    s@   | dd| _| dd | _| dd | _t jdi | d S )NbinaryFencoderdecoderr1   )r$  r  r  r  r   rg   r  r   r1   r2   rg     s   zJSONField.__init__c                 C   s@   t |r| j|v rG dd dt}||| j S || jtS )Nc                   @   s   e Zd Zdd ZdS )z'JSONField.get_value.<locals>.JSONStringc                 S   s   t | |}d|_|S )NT)r  r   is_json_string)r   r]   rZ   r1   r1   r2   r     s   z/JSONField.get_value.<locals>.JSONString.__new__N)r-   r.   r/   r   r1   r1   r1   r2   
JSONString  s    r  )r#   r   r   r  r   r+   )rf   r   r  r1   r1   r2   r     s   zJSONField.get_valuec              	   C   sp   z&| j s
t|ddrt|tr| }tj|| jdW S tj|| j	d W |S  t
tfy7   | d Y |S w )Nr  Fr   rz   )r  rL   rA   bytesdecoder%   loadsr  dumpsr  r  rO   r   r   r1   r1   r2   r     s   
zJSONField.to_internal_valuec                 C   s"   | j rtj|| jd}| }|S )Nr  )r  r%   r  r  encoder   r1   r1   r2   r     s   zJSONField.to_representation)r-   r.   r/   r   r   _is_jsonfieldrg   r   r   r   r   r1   r1   r   r2   r    s    r  c                       s(   e Zd ZdZ fddZdd Z  ZS )ReadOnlyFieldaw  
    A read-only field that simply returns the field value.

    If the field is a method with no parameters, the method will be called
    and its return value used as the representation.

    For example, the following would call `get_expiry_date()` on the object:

    class ExampleSerializer(Serializer):
        expiry_date = ReadOnlyField(source='get_expiry_date')
    c                    s   d|d< t  jdi | d S )NTr   r1   r   rg   r  r   r1   r2   rg     s   zReadOnlyField.__init__c                 C   r  r4   r1   r   r1   r1   r2   r     r   zReadOnlyField.to_representation)r-   r.   r/   r0   rg   r   r   r1   r1   r   r2   r    s    r  c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )HiddenFieldaL  
    A hidden field does not take input from the user, or present any output,
    but it does populate a field in `validated_data`, based on its default
    value. This is particularly useful when we have a `unique_for_date`
    constraint on a pair of fields, as we need some way to include the date in
    the validated data.
    c                    s.   d|v sJ dd|d< t  jdi | d S )Nr8   zdefault is a required argument.Tr   r1   r  r  r   r1   r2   rg   !  s   zHiddenField.__init__c                 C   s   t S r4   )r+   r  r1   r1   r2   r   &     zHiddenField.get_valuec                 C   r  r4   r1   r   r1   r1   r2   r   +  r   zHiddenField.to_internal_value)r-   r.   r/   r0   rg   r   r   r   r1   r1   r   r2   r    s
    r  c                       s6   e Zd ZdZd	 fdd	Z fddZdd Z  ZS )
SerializerMethodFielda  
    A read-only field that get its representation from calling a method on the
    parent serializer class. The method called will be of the form
    "get_{field_name}", and should take a single argument, which is the
    object being serialized.

    For example:

    class ExampleSerializer(Serializer):
        extra_info = SerializerMethodField()

        def get_extra_info(self, obj):
            return ...  # Calculate some data to return.
    Nc                    s,   || _ d|d< d|d< t jdi | d S )Nr   r   Tr   r1   )method_namer   rg   )rf   r  r   r   r1   r2   rg   ?  s   zSerializerMethodField.__init__c                    s*   | j d u rdj|d| _ t || d S )Nzget_{field_name})r   )r  rP   r   r   r   r   r1   r2   r   E  s   
zSerializerMethodField.bindc                 C   s   t | j| j}||S r4   )rL   r   r  )rf   r]   methodr1   r1   r2   r   L  s   z'SerializerMethodField.to_representationr4   )r-   r.   r/   r0   rg   r   r   r   r1   r1   r   r2   r  /  s
    r  c                       sD   e Zd ZdZdediZ fddZdd Zdd	 Zd
d Z	  Z
S )
ModelFieldz
    A generic field that can be used against an arbitrary model field.

    This is used by `ModelSerializer` when dealing with custom model fields,
    that do not have a serializer field to be mapped to.
    r  r  c                    sb   || _ |dd | _t jdi | | jd ur/t| jd | jd}| jt	| j|d d S d S )Nr  r!  r"  r1   )
model_fieldr$  r  r   rg   r'   r   r   r%  r   )rf   r  r   r   r   r1   r2   rg   \  s   
zModelField.__init__c                 C   s2   | j j}|d ur|jj|j|S | j |S r4   )r  remote_fieldmodel_meta	get_fieldr   	to_python)rf   r   relr1   r1   r2   r   g  s   zModelField.to_internal_valuec                 C   r  r4   r1   )rf   rH   r1   r1   r2   rU   m  r  zModelField.get_attributec                 C   s$   | j |}t|r|S | j |S r4   )r  value_from_objectr   value_to_string)rf   rH   r]   r1   r1   r2   r   r  s   zModelField.to_representation)r-   r.   r/   r0   r   r   rg   r   rU   r   r   r1   r1   r   r2   r  Q  s    r  )NN)zr  r   r  rn  rB   r=   r9  rE  rp  collections.abcr   enumr   django.confr   django.core.exceptionsr   r   r   django.core.validatorsr   r   r	   r
   r   r   r   r   django.formsr   r  r   r  django.utilsr   django.utils.dateparser   r   r   r   django.utils.durationr   django.utils.encodingr   r   django.utils.formatsr   r   django.utils.ipv6r   django.utils.translationr   r   r  ImportErrorrest_frameworkr   rest_framework.compatr    rest_framework.exceptionsr!   rest_framework.settingsr"   rest_framework.utilsr#   r$   r%   r&   rest_framework.utils.formattingr'   rest_framework.utils.timezoner(   rest_framework.validatorsr)   r+   r  r3   rK   rU   rX   rb   rx   r   r   r   r   r   r:  r   r   r   r   r   r   r   r   r  r.  r5  r6  r<  r=  rG  rM  r_  rc  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r1   r1   r1   r2   <module>   s    (

/
  u?2$%' %ZC>&<)3Q@/"