a
    SicR                    @   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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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l/m0Z0 d dl1m2Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9mZ d dl:m;Z; d dl<m=Z=m>Z>m?Z?m@Z@ d dlAmBZB d dlCmDZD G dd dZEG dd deFZGdd ZHdd ZId d! ZJd"d# ZKd$d% ZLdrd&d'ZMd(d) ZNG d*d+ d+ZOG d,d- d-ZPG d.d/ d/eFZQeReSd0ZTd1ZUd2ZVd3ZWd4ZXd5ZYG d6d7 d7ZZG d8d9 d9eZZ[G d:d; d;eZZ\G d<d= d=e\Z]G d>d? d?e\Z^G d@dA dAe\Z_G dBdC dCe\Z`G dDdE dEeZZaG dFdG dGe\ZbG dHdI dIeZZcG dJdK dKeZZdG dLdM dMeZZeG dNdO dOeZZfG dPdQ dQeZZgG dRdS dSeZZhG dTdU dUeZZiG dVdW dWeZZjG dXdY dYejZkG dZd[ d[ejZG d\d] d]eZZlG d^d_ d_elZG d`da daeZZmG dbdc dceZZnG ddde deeZZoG dfdg dgeoZpG dhdi dieZZqG djdk dkeZZrG dldm dmeZZsG dndo doeZZtG dpdq dqeZZudS )s    N)OrderedDict)Mapping)settings)ObjectDoesNotExist)ValidationError)	EmailValidatorMaxLengthValidatorMaxValueValidatorMinLengthValidatorMinValueValidatorProhibitNullCharactersValidatorRegexValidatorURLValidatorip_address_validators)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)InvalidTimeError)ISO_8601)ErrorDetailr   )api_settings)htmlhumanize_datetimejsonrepresentation)lazy_format)$ProhibitSurrogateCharactersValidatorc                   @   s   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__ r0   r0   Q/var/www/html/django/DPS/env/lib/python3.9/site-packages/rest_framework/fields.pyr*   (   s   r*   c                   @   s   e Zd ZdZdS )BuiltinSignatureErrorz
    Built-in function signatures are not inspectable. This exception is raised
    so the serializer can raise a helpful error message.
    Nr+   r0   r0   r0   r1   r2   2   s   r2   c                 C   sh   t | sdS t| rtdt| sBt| sBt| tjsBd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   s2   | ]*}|j |jkp(|j |jkp(|j|jkV  qd S N)kindVAR_POSITIONALVAR_KEYWORDdefaultr*   ).0paramr0   r0   r1   	<genexpr>L   s
   
z%is_simple_callable.<locals>.<genexpr>)callableinspect	isbuiltinr2   
isfunctionismethod
isinstance	functoolspartial	signature
parametersvaluesall)objsigparamsr0   r0   r1   is_simple_callable:   s    
 

rJ   c                 C   s   |D ]}z"t | tr| | } n
t| |} W n ty@   Y  dS 0 t| rz
|  } W q ttfy } ztd||W Y d}~qd}~0 0 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: {})	r@   r   getattrr   rJ   AttributeErrorKeyError
ValueErrorformat)instanceattrsattrexcr0   r0   r1   get_attributeT   s    



(rT   c                 C   sL   |s|  | dS |dd D ]}|| vr2i | |< | | } q|| |d < dS )a1  
    Similar to Python's built in `dictionary[key] = value`,
    but takes a list of nested keys instead of a single key.

    set_value({'a': 1}, [], {'b': 2}) -> {'a': 1, 'b': 2}
    set_value({'a': 1}, ['x'], 2) -> {'a': 1, 'x': 2}
    set_value({'a': 1}, ['x', 'y'], 2) -> {'a': 1, 'x': {'y': 2}}
    N)update)
dictionarykeysvaluekeyr0   r0   r1   	set_valueo   s    	

r[   c                 C   sX   t  }| D ]H}t|ttfs&|||< q
|\}}t|ttfrJt|||< 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'}}
    )r   r@   listtupleto_choices_dict)choicesretchoicerZ   rY   r0   r0   r1   r^      s    

r^   c                 C   sJ   t  }|  D ]6\}}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'}
    )r   itemsr@   dict)r_   r`   rZ   rY   sub_key	sub_valuer0   r0   r1   flatten_choices_dict   s    

rf   c                 c   s   G dd d}G dd d}G dd d}d}|   D ]\}}|rN||krN qt|tr||dV  |  D ].\}	}
|r||kr q||	|
d	V  |d
7 }ql| V  q6|||d	V  |d
7 }q6|r||kr|r|j|d}|d|ddV  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   s
   || _ d S r3   label)selfrh   r0   r0   r1   __init__   s    z/iter_options.<locals>.StartOptionGroup.__init__Nr,   r-   r.   start_option_groupend_option_grouprj   r0   r0   r0   r1   StartOptionGroup   s   rn   c                   @   s   e Zd ZdZdZdS )z$iter_options.<locals>.EndOptionGroupFTN)r,   r-   r.   rl   rm   r0   r0   r0   r1   EndOptionGroup   s   ro   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 r3   rY   display_textdisabled)ri   rY   rq   rr   r0   r0   r1   rj      s    z%iter_options.<locals>.Option.__init__N)Frk   r0   r0   r0   r1   Option   s   rs   r   rg   )rY   rq      )countzn/aTrp   N)rb   r@   rc   rO   )grouped_choicescutoffcutoff_textrn   ro   rs   ru   rZ   rY   rd   re   r0   r0   r1   iter_options   s(    	



ry   c                    sX   t | ddpd z
| j}W n& ty@    fdd| jD  Y S 0  fdd| D S )zf
    Given a Django ValidationError, return a list of ErrorDetail,
    with the `code` populated.
    codeNinvalidc                    s8   g | ]0}t |jr|j|j n|j|jr,|jn d qS rz   r"   rI   messagerz   r8   errorr}   r0   r1   
<listcomp>   s   z$get_error_detail.<locals>.<listcomp>c                    s$   i | ]\}}| fd d|D qS )c                    s8   g | ]0}t |jr|j|j n|j|jr,|jn d qS r|   r~   r   r}   r0   r1   r      s   z/get_error_detail.<locals>.<dictcomp>.<listcomp>r0   )r8   kerrorsr}   r0   r1   
<dictcomp>   s   z$get_error_detail.<locals>.<dictcomp>)rK   
error_dictrL   
error_listrb   )exc_infor   r0   r}   r1   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   s
   || _ d S r3   r7   )ri   r7   r0   r0   r1   rj     s    zCreateOnlyDefault.__init__c                 C   sF   |j jd u}|rt t| jr@t| jddr8| |S |  S | jS )Nrequires_contextF)parentrP   	SkipFieldr;   r7   rK   )ri   serializer_fieldZ	is_updater0   r0   r1   __call__  s    

zCreateOnlyDefault.__call__c                 C   s   d| j jt| jf S )Nz%s(%s))	__class__r,   reprr7   ri   r0   r0   r1   __repr__  s    zCreateOnlyDefault.__repr__N)r,   r-   r.   r/   r   rj   r   r   r0   r0   r0   r1   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)ri   r   r0   r0   r1   r     s    zCurrentUserDefault.__call__c                 C   s   d| j j S )Nz%s())r   r,   r   r0   r0   r1   r     s    zCurrentUserDefault.__repr__N)r,   r-   r.   r   r   r   r0   r0   r0   r1   r     s   r   c                   @   s   e Zd ZdS )r   N)r,   r-   r.   r0   r0   r0   r1   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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-d. Z  ZS )/Fieldr   zThis field is required.zThis field may not be null.)requirednullNF)	read_only
write_onlyr   r7   initialsourcerh   	help_textstyleerror_messages
validators
allow_nullc                C   sF  t j| _t  jd7  _|d u r,|tu o*| }|r<|r<J t|rL|rLJ t|r`|tur`J t|rv| jt krvJ t|| _|| _	|| _
|| _|| _|tu r| jn|| _|| _|| _|	d u ri n|	| _|| _| jtur|tur|| _|d urt|| _d | _d | _i }t| jjD ]}|t|di  q||
p8i  || _d S )Nrt   default_error_messages)r   _creation_counterr*   NOT_READ_ONLY_WRITE_ONLYNOT_READ_ONLY_REQUIREDNOT_REQUIRED_DEFAULTr   USE_READONLYFIELDr   r   r   r7   r   r   rh   r   r   r   default_empty_htmlr\   r   
field_namer   reversed__mro__rV   rK   r   )ri   r   r   r   r7   r   r   rh   r   r   r   r   r   messagesclsr0   r0   r1   rj   8  s<    

zField.__init__c                 C   s   | j |ks$J d|| jj|jjf || _|| _| jdu rL|dd | _| j du r\|| _ | j dkrng | _n| 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   rh   replace
capitalizesource_attrssplitri   r   r   r0   r0   r1   bindf  s    	


z
Field.bindc                 C   s   t | ds|  | _| jS )N_validators)hasattrget_validatorsr   r   r0   r0   r1   r     s    

zField.validatorsc                 C   s
   || _ d S r3   )r   )ri   r   r0   r0   r1   r     s    c                 C   s
   t | jS r3   )r\   default_validatorsr   r0   r0   r1   r     s    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   r0   r0   r1   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rX| jrXt| ddrTdS dS |dkrz| jszt| ddrvd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.
        rB   Fr   allow_blankN)
r$   is_html_inputr   rK   rootr*   r   r   r   get)ri   rW   r`   r0   r0   r1   	get_value  s    


zField.get_valuec              
   C   s   zt || jW S  tyZ } z4dj| jjj| j|jjd}t||W Y d}~nd}~0  t	t
fy } z|| jtur|  W  Y d}~S | jrW Y d}~dS | jst djt|j| j| jjj|jj|d}t||W Y d}~n
d}~0 0 dS )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fieldrP   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   rP   rS   )rT   r   r2   rO   r   r   r,   r   typerM   rL   r7   r*   get_defaultr   r   r   )ri   rP   rS   msgr0   r0   r1   rT     s4    
 
zField.get_attributec                 C   sN   | j tu st| jddrt t| j rHt| 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.
        rB   Fr   )r7   r*   rK   r   r   r;   r   r0   r0   r1   r     s    	

zField.get_defaultc                 C   s~   | j rd|  fS |tu rJt| jddr.t | jr>| d d|  fS |du rv| jsd| d n| j	dkrrd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.
        TrB   Fr   Nr   r   )FNTN)
r   r   r*   rK   r   r   r   failr   r   ri   datar0   r0   r1   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)ri   r   is_empty_valuerY   r0   r0   r1   run_validation  s    


zField.run_validationc                 C   s   g }| j D ]}z$t|ddr(|||  n|| W q
 typ } z&t|jtrP ||j W Y d}~q
d}~0  ty } z|t| W Y d}~q
d}~0 0 q
|rt|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   rK   r   r@   detailrc   extendDjangoValidationErrorr   )ri   rY   r   	validatorrS   r0   r0   r1   r   '  s    
 &zField.run_validatorsc                 C   s   t dj| jj| jddS )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   NNotImplementedErrorrO   r   r,   r   r   r0   r0   r1   r   ?  s    zField.to_internal_valuec                 C   s   t dj| jj| jddS )zL
        Transform the *outgoing* native value into primitive data.
        zE{cls}.to_representation() must be implemented for field {field_name}.r   Nr   ri   rY   r0   r0   r1   to_representationL  s    zField.to_representationc                 K   s`   z| j | }W n0 ty>   | jj}tj||d}t|Y n0 |jf i |}t||ddS )zH
        A helper method that simply raises a validation error.
        )
class_namerZ   r}   N)r   rM   r   r,   MISSING_ERROR_MESSAGErO   AssertionErrorr   )ri   rZ   kwargsr   r   message_stringr0   r0   r1   r   W  s    z
Field.failc                 C   s   | }|j dur|j }q|S )zB
        Returns the top-level serializer for this field.
        N)r   )ri   r   r0   r0   r1   r   d  s    
z
Field.rootc                 C   s   t | jdi S )zY
        Returns the context as passed to the root serializer on initialization.
        _context)rK   r   r   r0   r0   r1   r   n  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   argsr   rP   r   r0   r1   r   u  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 r0   )r@   
REGEX_TYPEcopydeepcopyr8   itemr0   r0   r1   r     s   z&Field.__deepcopy__.<locals>.<listcomp>c                    s*   i | ]"\}}||d vr"t | n|qS ))r   regex)r   r   )r8   rZ   rY   memor0   r1   r     s   z&Field.__deepcopy__.<locals>.<dictcomp>)r   r   rb   r   )ri   r   r   r   r0   r   r1   __deepcopy__  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'   Z
field_reprr   r0   r0   r1   r     s    zField.__repr__) r,   r-   r.   r   r   r   r   r*   r   r   rj   r   propertyr   setterr   r   r   rT   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r0   r0   r   r1   r   -  sF   .$

	)!
	

r   c                   @   sH   e Zd ZdediZdZdZh dZh dZh dZ	dd Z
d	d
 ZdS )BooleanFieldr{   zMust be a valid boolean.F>   rt   ontrueYesTRUEZYEST1TrueZOntONyYyes>   r   offNoZOff0noOFFfNOFalseFALSEnNFfalse>   r   ZNullr   NNULLc                 C   s`   z:|| j v rW dS || jv r"W dS || jv r8| jr8W d S W n tyL   Y n0 | jd|d d S )NTFr{   input)TRUE_VALUESFALSE_VALUESNULL_VALUESr   	TypeErrorr   r   r0   r0   r1   r     s    


zBooleanField.to_internal_valuec                 C   s8   || j v rdS || jv rdS || jv r0| jr0d S t|S )NTF)r  r  r  r   boolr   r0   r0   r1   r     s    

zBooleanField.to_representationN)r,   r-   r.   r   r   r   r   r  r  r  r   r   r0   r0   r0   r1   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.)r{   blank
max_length
min_lengthr   c                    s   | dd| _| dd| _| dd | _| dd | _t jf 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 | j	
t  | j	
t  d S )
Nr   Ftrim_whitespaceTr  r  r  r   r  )popr   r  r  r  r   rj   r(   r   r   appendr   r
   r   r)   ri   r   r   r   r0   r1   rj     s"    

zCharField.__init__c                    s>   |dks| j r2t| dkr2| js.| d dS t |S )Nr   r  )r  strstripr   r   r   r   r   r   r0   r1   r     s
    
zCharField.run_validationc                 C   s>   t |tst |tttfs$| d t|}| jr:| S |S )Nr{   )r@   r  r$  intfloatr   r  r%  ri   r   rY   r0   r0   r1   r     s    
zCharField.to_internal_valuec                 C   s   t |S r3   r$  r   r0   r0   r1   r     s    zCharField.to_representation)r,   r-   r.   r   r   r   rj   r*   r   r   r   r   r0   r0   r   r1   r    s   
	r  c                       s(   e Zd ZdediZ fddZ  ZS )
EmailFieldr{   zEnter a valid email address.c                    s2   t  jf i | t| jd d}| j| d S Nr{   r  )r   rj   r   r   r   r"  ri   r   r   r   r0   r1   rj     s    zEmailField.__init__r,   r-   r.   r   r   rj   r   r0   r0   r   r1   r*    s   r*  c                       s(   e Zd ZdediZ fddZ  ZS )
RegexFieldr{   z/This value does not match the required pattern.c                    s4   t  jf i | t|| jd d}| j| d S r+  )r   rj   r   r   r   r"  )ri   r   r   r   r   r0   r1   rj     s    zRegexField.__init__r-  r0   r0   r   r1   r.  	  s   r.  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.)r{   invalid_unicodeFc                    sd   t  jf i | || _| jr<ttdtj| jd d}nttd| jd d}| j	| d S )Nz	^[-\w]+\Zr0  r  z^[-a-zA-Z0-9_]+$r{   )
r   rj   allow_unicoder   recompileUNICODEr   r   r"  )ri   r1  r   r   r   r0   r1   rj     s    zSlugField.__init__)Fr-  r0   r0   r   r1   r/    s   r/  c                       s(   e Zd ZdediZ fddZ  ZS )URLFieldr{   zEnter a valid URL.c                    s2   t  jf i | t| jd d}| j| d S r+  )r   rj   r   r   r   r"  r,  r   r0   r1   rj   )  s    zURLField.__init__r-  r0   r0   r   r1   r5  $  s   r5  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&  urnr{   zMust be a valid UUID.c                    sF   | dd| _| j| jvr0tdd| jt jf i | d S )NrO   r7  z;Invalid format for uuid representation. Must be one of "{}"z", ")r!  uuid_formatvalid_formatsrN   rO   joinr   rj   ri   r   r   r0   r1   rj   6  s    
zUUIDField.__init__c                 C   st   t |tjspzBt |tr&tj|dW S t |tr>tj|dW S | jd|d W n  tyn   | jd|d Y n0 |S )Nr&  )r8  r{   rY   )r@   uuidUUIDr&  r$  r   rN   r   r0   r0   r1   r   ?  s    

zUUIDField.to_internal_valuec                 C   s"   | j dkrt|S t|| j S d S )Nr7  )r:  r$  rK   r   r0   r0   r1   r   L  s    
zUUIDField.to_representation)
r,   r-   r.   r;  r   r   rj   r   r   r   r0   r0   r   r1   r6  /  s   	r6  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 GenericIPAddressFieldr{   z#Enter a valid IPv4 or IPv6 address.bothc                    sH   |  | _| jdk| _t jf i | t|| j\}}| j| d S )NrC  )lowerprotocolunpack_ipv4r   rj   r   r   r   )ri   rE  r   r   error_messager   r0   r1   rj   Z  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0 t |S )Nr{   r?  :)rC  ipv6)	r@   r$  r   rE  r   rF  r   r   r   r   r   r0   r1   r   a  s    

z IPAddressField.to_internal_value)rC  )	r,   r-   r.   r/   r   r   rj   r   r   r0   r0   r   r1   rB  S  s
   rB  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.r{   	max_value	min_valuemax_string_length  z\.0*\s*$c                    s   | dd | _| dd | _t jf i | | jd urbt| jd | jd}| jt	| j|d | jd urt| jd | jd}| jt
| j|d d S NrO  rP  rO  r  rP  r!  rO  rP  r   rj   r(   r   r   r"  r	   r   r#  r   r0   r1   rj   {  s    

zIntegerField.__init__c              	   C   sb   t |tr"t|| jkr"| d zt| jdt|}W n  tt	fy\   | d Y n0 |S )NrQ  r   r{   )
r@   r$  lenMAX_STRING_LENGTHr   r&  
re_decimalsubrN   r  r   r0   r0   r1   r     s    
zIntegerField.to_internal_valuec                 C   s   t |S r3   r>  r   r0   r0   r1   r     s    zIntegerField.to_representation)r,   r-   r.   r   r   rX  r2  r3  rY  rj   r   r   r   r0   r0   r   r1   rJ  q  s   

rJ  c                       sN   e Zd Z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.rK  rL  rM  rN  rR  c                    s   | dd | _| dd | _t jf i | | jd urbt| jd | jd}| jt	| j|d | jd urt| jd | jd}| jt
| j|d d S rS  rV  r#  r   r0   r1   rj     s    

zFloatField.__init__c              	   C   sR   t |tr"t|| jkr"| d z
t|W S  ttfyL   | d Y n0 d S )NrQ  r{   )r@   r$  rW  rX  r   r'  r  rN   r   r0   r0   r1   r     s    

zFloatField.to_internal_valuec                 C   s   t |S r3   )r'  r   r0   r0   r1   r     s    zFloatField.to_representation)
r,   r-   r.   r   r   rX  rj   r   r   r   r0   r0   r   r1   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\  rK  rL  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.rM  )r{   rO  rP  
max_digitsmax_decimal_placesmax_whole_digitsrQ  rR  NFc                    s   || _ || _|| _|d ur || _| jr,d| _|| _|| _| j d ur\| jd ur\| j | j | _nd | _t jf i | | jd urt	| j
d | jd}	| jt| j|	d | jd urt	| j
d | jd}	| jt| j|	d |d urdd tt D }
||
v sJ d	||
f || _d S )
NTrO  rT  r  rP  rU  c                 S   s   g | ]\}}| d r|qS )ZROUND_)
startswith)r8   r   vr0   r0   r1   r         z)DecimalField.__init__.<locals>.<listcomp>z=Invalid rounding option %s. Valid values for rounding are: %s)r^  decimal_placeslocalizecoerce_to_stringrO  rP  r`  r   rj   r(   r   r   r"  r	   r   varsdecimalrb   rounding)ri   r^  rd  rf  rO  rP  re  ri  r   r   Zvalid_roundingsr   r0   r1   rj     s:    



zDecimalField.__init__c                    s&   t | dkr| jrdS t |S )Nr   r   )r   r%  r   r   r   r   r   r0   r1   r     s    z"DecimalField.validate_empty_valuesc                 C   s   t | }| jrt|}t|| jkr2| d zt|}W n tj	y^   | d Y n0 |
 rr| d |tdtdfv r| d | | |S )zd
        Validate that the input is a decimal number and return a Decimal
        instance.
        rQ  r{   ZInfz-Inf)r   r%  re  r   rW  rX  r   rh  DecimalDecimalExceptionis_nanquantizevalidate_precisionr(  r0   r0   r1   r     s    


zDecimalField.to_internal_valuec                 C   s   |  \}}}|dkr,t|| }|}d}n>t|t|krZt|}|t| }t|}nt|}d}|}| jdur|| jkr| jd| jd | jdur|| jkr| jd| jd | jdur|| jkr| 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   Nr^  )r^  r_  )r_  r`  )r`  )as_tuplerW  absr^  r   rd  r`  )ri   rY   signZ
digittupleexponentZtotal_digitswhole_digitsrd  r0   r0   r1   rn  
  s&    
zDecimalField.validate_precisionc                 C   sj   t | dtj}|d u r"|rdS d S t|tjs@tt| }| |}|sR|S | j	r`t
|S d|S )Nrf  r   z{:f})rK   r#   COERCE_DECIMAL_TO_STRINGr@   rh  rj  r$  r%  rm  re  r   rO   )ri   rY   rf  Z	quantizedr0   r0   r1   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)ri  r   )	rd  rh  
getcontextr   r^  precrm  rj  ri  )ri   rY   r   r0   r0   r1   rm  B  s    

zDecimalField.quantize)NNNFN)r,   r-   r.   r   r   rX  rj   r   r   rn  r   rm  r   r0   r0   r   r1   r]    s"   	  ##r]  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.)r{   date
make_awareoverflowNc                    s@   |t ur|| _|d ur|| _|d ur*|| _t jf i | d S r3   )r*   rO   input_formatsr   r   rj   )ri   rO   r{  default_timezoner   r   r0   r1   rj   ^  s    zDateTimeField.__init__c                 C   s   t | dr| jn|  }|durt|rTz||W S  tyR   | d Y n0 zt||W S  ty   | jd|d Y q0 n"|du rt|rt	|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   Nrz  ry  r   )r   r   r|  is_aware
astimezoneOverflowErrorr   ry  r    
make_naivedatetimeutc)ri   rY   Zfield_timezoner0   r0   r1   enforce_timezoneg  s    
zDateTimeField.enforce_timezonec                 C   s   t jrt S d S r3   )r   USE_TZr   get_current_timezoner   r0   r0   r1   r|  |  s    zDateTimeField.default_timezonec              
   C   s   t | dtj}t|tjr0t|tjs0| d t|tjrF| |S |D ]}| t	krz$t
|}|d ur|| |W   S W q ttfy   Y q0 qJz| ||}| |W   S  ttfy   Y qJ0 qJt|}| jd|d d S )Nr{  rx  r{   rO   )rK   r#   DATETIME_INPUT_FORMATSr@   r  rx  r   r  rD  r!   r   rN   r  datetime_parserr%   Zdatetime_formatsri   rY   r{  input_formatparsedZhumanized_formatr0   r0   r1   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rh| }|	drd|d d d }|S |
|S )NrO   z+00:00iZ)rK   r#   DATETIME_FORMATr@   r$  r  rD  r!   	isoformatendswithstrftimeri   rY   output_formatr0   r0   r1   r     s    

zDateTimeField.to_representation)r,   r-   r.   r   r   r  strptimer  r*   rj   r  r|  r   r   r   r0   r0   r   r1   rw  U  s   	rw  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.)r{   r  Nc                    s2   |t ur|| _|d ur|| _t jf i | d S r3   r*   rO   r{  r   rj   ri   rO   r{  r   r   r0   r1   rj     s
    zDateField.__init__c              
   C   s   t | dtj}t|tjr$| d t|tjr4|S |D ]z}| tkr~zt	|}W n t
tfyj   Y q0 |d ur|  S q8z| ||}W n t
tfy   Y q80 |   S q8t|}| jd|d d S )Nr{  r  r{   r  )rK   r#   DATE_INPUT_FORMATSr@   r  r   rx  rD  r!   r   rN   r  r  r%   Zdate_formatsr  r0   r0   r1   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rT| S |	|S )NrO   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.)
rK   r#   DATE_FORMATr@   r$  r  rD  r!   r  r  r  r0   r0   r1   r     s    zDateField.to_representationr,   r-   r.   r   r   r  r  r  r*   rj   r   r   r   r0   r0   r   r1   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 )
	TimeFieldr{   zBTime has wrong format. Use one of these formats instead: {format}.Nc                    s2   |t ur|| _|d ur|| _t jf i | d S r3   r  r  r   r0   r1   rj     s
    zTimeField.__init__c              
   C   s   t | dtj}t|tjr|S |D ]z}| tkrhzt|}W n t	t
fyT   Y q0 |d ur|  S q"z| ||}W n t	t
fy   Y q"0 |   S q"t|}| jd|d d S )Nr{  r{   r  )rK   r#   TIME_INPUT_FORMATSr@   r  timerD  r!   r   rN   r  r  r%   Ztime_formatsr   r  r0   r0   r1   r     s$    

zTimeField.to_internal_valuec                 C   sb   |dv rd S t | dtj}|d u s,t|tr0|S t|tjrDJ d| tkrX| S |	|S )N)Nr   rO   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.)
rK   r#   TIME_FORMATr@   r$  r  rD  r!   r  r  r  r0   r0   r1   r     s    zTimeField.to_representationr  r0   r0   r   r1   r    s   r  c                       sD   e Zd Z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}.rK  rL  )r{   rO  rP  c                    s   | dd | _| dd | _t jf i | | jd urbt| jd | jd}| jt	| j|d | jd urt| jd | jd}| jt
| j|d d S rS  rV  r#  r   r0   r1   rj   5  s    

zDurationField.__init__c                 C   s:   t |tjr|S tt|}|d ur(|S | jddd d S )Nr{   z[DD] [HH:[MM:]]ss[.uuuuuu]r  )r@   r  	timedeltar   r$  r   )ri   rY   r  r0   r0   r1   r   B  s    zDurationField.to_internal_valuec                 C   s   t |S r3   r   r   r0   r0   r1   r   J  s    zDurationField.to_representation	r,   r-   r.   r   r   rj   r   r   r   r0   r0   r   r1   r  .  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f i | d S )Nhtml_cutoffhtml_cutoff_textr   F)r_   r!  r  r  r   r   rj   )ri   r_   r   r   r0   r1   rj   W  s
    zChoiceField.__init__c                 C   sH   |dkr| j rdS z| jt| W S  tyB   | jd|d Y n0 d S )Nr   r  r  )r   choice_strings_to_valuesr$  rM   r   r   r0   r0   r1   r   `  s    zChoiceField.to_internal_valuec                 C   s   |dv r|S | j t||S )N)r   Nr  r   r$  r   r0   r0   r1   r   i  s    zChoiceField.to_representationc                 C   s   t | j| j| jdS )zP
        Helper method for use with templates rendering select widgets.
        )rw   rx   )ry   rv   r  r  r   r0   r0   r1   ry   n  s
    zChoiceField.iter_optionsc                 C   s   | j S r3   )_choicesr   r0   r0   r1   _get_choicesx  s    zChoiceField._get_choicesc                 C   s,   t || _t| j| _dd | jD | _d S )Nc                 S   s   i | ]}t ||qS r0   r)  )r8   rZ   r0   r0   r1   r     s   z,ChoiceField._set_choices.<locals>.<dictcomp>)r^   rv   rf   r  r_   r  )ri   r_   r0   r0   r1   _set_choices{  s
    
zChoiceField._set_choices)r,   r-   r.   r   r   r  r  rj   r   r   ry   r  r  r   r_   r   r0   r0   r   r1   r  P  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f i | d S )Nallow_emptyT)r!  r  r   rj   r=  r   r0   r1   rj     s    zMultipleChoiceField.__init__c                 C   s@   | j |vrt| jddrtS t|r2|| j S || j tS )NrB   F)r   rK   r   r*   r$   r   getlistr   ri   rW   r0   r0   r1   r     s    

zMultipleChoiceField.get_valuec                    sX   t |tst|ds( jdt|jd  jsDt|dkrD d  fdd|D S )N__iter__r  
input_typer   r*   c                    s   h | ]}t t|qS r0   )r   r  r   r   )r   ri   r0   r1   	<setcomp>  s   z8MultipleChoiceField.to_internal_value.<locals>.<setcomp>)r@   r$  r   r   r   r,   r  rW  r   r   r   r1   r     s    
z%MultipleChoiceField.to_internal_valuec                    s    fdd|D S )Nc                    s   h | ]} j t||qS r0   r  r   r   r0   r1   r    s   z8MultipleChoiceField.to_representation.<locals>.<setcomp>r0   r   r0   r   r1   r     s    
z%MultipleChoiceField.to_representation)r,   r-   r.   r   r   r   rj   r   r   r   r   r0   r0   r   r1   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           	         s4   t ||||||d}|j|d< t jf i | d S )N)match	recursiveallow_filesallow_foldersr   r_   )DjangoFilePathFieldr_   r   rj   )	ri   pathr  r  r  r  r   r   r   r   r0   r1   rj     s    
zFilePathField.__init__)NFTFNr-  r0   r0   r   r1   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   r{   no_namer*   r  c                    sF   | dd | _| dd| _d|v r0| d| _t jf i | d S )Nr  allow_empty_fileFuse_url)r!  r  r  r  r   rj   r=  r   r0   r1   rj     s
    zFileField.__init__c                 C   s~   z|j }|j}W n ty,   | d Y n0 |s<| d | jsP|sP| d | jrzt|| jkrz| jd| jt|d |S )Nr{   r  r*   r  )r  length)namesizerL   r   r  r  rW  )ri   r   	file_name	file_sizer0   r0   r1   r     s    



zFileField.to_internal_valuec                 C   sd   |sd S t | dtj}|r^z
|j}W n ty8   Y d S 0 | jdd }|d urZ||S |S |jS )Nr  r   )	rK   r#   UPLOADED_FILES_USE_URLurlrL   r   r   build_absolute_urir  )ri   rY   r  r  r   r0   r0   r1   r     s    

zFileField.to_representationr  r0   r0   r   r1   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f i | d S )N_DjangoImageField)r!  DjangoImageFieldr  r   rj   r=  r   r0   r1   rj     s    zImageField.__init__c                    s&   t  |}|  }| j|_||S r3   )r   r   r  r   clean)ri   r   Zfile_objectZdjango_fieldr   r0   r1   r     s    zImageField.to_internal_value)r,   r-   r.   r   r   rj   r   r   r0   r0   r   r1   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f i | d| _d| _d S NT)r   rj   r   r   r=  r   r0   r1   rj     s    z_UnvalidatedField.__init__c                 C   s   |S r3   r0   r   r0   r0   r1   r     s    z#_UnvalidatedField.to_internal_valuec                 C   s   |S r3   r0   r   r0   r0   r1   r     s    z#_UnvalidatedField.to_representation)r,   r-   r.   rj   r   r   r   r0   r0   r   r1   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rTJ d| jj	d u shJ dt
 jf i | | jjd| d	 | jd urt| jd | jd
}| jt| j|d | jd urt| jd | jd}| jt| j|d 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   )r!  r   r   r  r  r  r  r<   isclassr   r   rj   r   r(   r   r   r"  r   r
   r#  r   r0   r1   rj   &  s     

zListField.__init__c                 C   sd   | j |vrt| jddrtS t|rV|| j g }t|dkrD|S tj|| j tdS |	| j tS )NrB   Fr   )prefixr7   )
r   rK   r   r*   r$   r   r  rW  parse_html_listr   )ri   rW   valr0   r0   r1   r   ;  s    

zListField.get_valuec                 C   sj   t |rt j|g d}t|ttfs0t|dsD| jdt|j	d | j
s`t|dkr`| d | |S )zY
        List of dicts of native values <- List of dicts of primitive datatypes.
        r   r  r  r  r   r*   )r$   r   r  r@   r$  r   r   r   r   r,   r  rW  run_child_validationr   r0   r0   r1   r   J  s    

zListField.to_internal_valuec                    s    fdd|D S )zS
        List of object instances -> List of dicts of primitive datatypes.
        c                    s$   g | ]}|d ur j |nd qS r3   )r  r   r   r   r0   r1   r   Z  rc  z/ListField.to_representation.<locals>.<listcomp>r0   r   r0   r   r1   r   V  s    zListField.to_representationc                 C   sv   g }t  }t|D ]N\}}z|| j| W q ty^ } z|j||< W Y d }~qd }~0 0 q|sj|S t|d S r3   )r   	enumerater"  r  r   r   r   )ri   r   resultr   idxr   er0   r0   r1   r  \  s    "zListField.run_child_validationr,   r-   r.   r  r  r   r   r   rj   r   r   r   r  r   r0   r0   r   r1   r    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r8J d| jjd u sLJ dt j	f i | | jj
d| d d S )Nr  r  Tr  r  r   r  )r!  r   r   r  r  r<   r  r   r   rj   r   r=  r   r0   r1   rj   s  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  r0   r0   r1   r     s    
zDictField.get_valuec                 C   sX   t |rt |}t|ts2| jdt|jd | jsNt	|dkrN| d | 
|S )zI
        Dicts of native values <- Dicts of primitive datatypes.
        r  r  r   r*   )r$   r   r  r@   rc   r   r   r,   r  rW  r  r   r0   r0   r1   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 r3   )r$  r  r   )r8   rZ   r  r   r0   r1   r     s   z/DictField.to_representation.<locals>.<dictcomp>)rb   r   r0   r   r1   r     s    
zDictField.to_representationc                 C   s|   i }t  }| D ]T\}}t|}z| j|||< W q tyd } z|j||< W Y d }~qd }~0 0 q|sp|S t|d S r3   )r   rb   r$  r  r   r   r   )ri   r   r  r   rZ   rY   r  r0   r0   r1   r    s    "zDictField.run_child_validationr  r0   r0   r   r1   r  k  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f 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.)r   rj   r@   r  r  r=  r   r0   r1   rj     s    zHStoreField.__init__)r,   r-   r.   r  r  rj   r   r0   r0   r   r1   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 )	JSONFieldr{   zValue must be valid JSON.Tc                    s@   | dd| _| dd | _| dd | _t jf i | d S )NbinaryFencoderdecoder)r!  r  r  r  r   rj   r=  r   r0   r1   rj     s    zJSONField.__init__c                 C   s@   t |r2| j|v r2G 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 r  )r$  r   is_json_string)r   rY   r`   r0   r0   r1   r     s    z/JSONField.get_value.<locals>.JSONString.__new__N)r,   r-   r.   r   r0   r0   r0   r1   
JSONString  s   r  )r$   r   r   r$  r   r*   )ri   rW   r  r0   r0   r1   r     s    zJSONField.get_valuec              	   C   sp   zJ| j st|ddr8t|tr&| }tj|| jdW S tj|| j	d W n  t
tfyj   | d Y n0 |S )Nr  Fr   r{   )r  rK   r@   bytesdecoder&   loadsr  dumpsr  r  rN   r   r   r0   r0   r1   r     s    
zJSONField.to_internal_valuec                 C   s"   | j rtj|| jd}| }|S )Nr  )r  r&   r  r  encoder   r0   r0   r1   r     s    zJSONField.to_representation)r,   r-   r.   r   r   _is_jsonfieldrj   r   r   r   r   r0   r0   r   r1   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f i | d S )NTr   r   rj   r=  r   r0   r1   rj     s    zReadOnlyField.__init__c                 C   s   |S r3   r0   r   r0   r0   r1   r     s    zReadOnlyField.to_representation)r,   r-   r.   r/   rj   r   r   r0   r0   r   r1   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f i | d S )Nr7   zdefault is a required argument.Tr   r  r=  r   r0   r1   rj     s    zHiddenField.__init__c                 C   s   t S r3   )r*   r  r0   r0   r1   r     s    zHiddenField.get_valuec                 C   s   |S r3   r0   r   r0   r0   r1   r     s    zHiddenField.to_internal_value)r,   r-   r.   r/   rj   r   r   r   r0   r0   r   r1   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(self):
        extra_info = SerializerMethodField()

        def get_extra_info(self, obj):
            return ...  # Calculate some data to return.
    Nc                    s,   || _ d|d< d|d< t jf i | d S )Nr   r   Tr   )method_namer   rj   )ri   r  r   r   r0   r1   rj     s    zSerializerMethodField.__init__c                    s*   | j d u rdj|d| _ t || d S )Nzget_{field_name})r   )r  rO   r   r   r   r   r0   r1   r   %  s    
zSerializerMethodField.bindc                 C   s   t | j| j}||S r3   )rK   r   r  )ri   rY   methodr0   r0   r1   r   ,  s    z'SerializerMethodField.to_representation)N)r,   r-   r.   r/   rj   r   r   r   r0   r0   r   r1   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                    s^   || _ |dd | _t jf i | | jd urZt| jd | jd}| jt	| j|d d S )Nr  r  r  )
model_fieldr!  r  r   rj   r(   r   r   r"  r   )ri   r  r   r   r   r0   r1   rj   <  s    
zModelField.__init__c                 C   s2   | j j}|d ur&|jj|j|S | j |S r3   )r  remote_fieldmodel_meta	get_fieldr   	to_python)ri   r   relr0   r0   r1   r   G  s    zModelField.to_internal_valuec                 C   s   |S r3   r0   )ri   rG   r0   r0   r1   rT   M  s    zModelField.get_attributec                 C   s$   | j |}t|r|S | j |S r3   )r  value_from_objectr   value_to_string)ri   rG   rY   r0   r0   r1   r   R  s    zModelField.to_representation)r,   r-   r.   r/   r   r   rj   r   rT   r   r   r0   r0   r   r1   r  1  s   r  )NN)vr   r  rh  rA   r<   r2  r@  collectionsr   collections.abcr   django.confr   django.core.exceptionsr   r   r   Zdjango.core.validatorsr   r   r	   r
   r   r   r   r   r   Z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   Zpytz.exceptionsr    rest_frameworkr!   rest_framework.exceptionsr"   rest_framework.settingsr#   rest_framework.utilsr$   r%   r&   r'   Zrest_framework.utils.formattingr(   rest_framework.validatorsr)   r*   	Exceptionr2   rJ   rT   r[   r^   rf   ry   r   r   r   r   r   r3  r   r   r   r   r   r   r   r   r  r*  r.  r/  r5  r6  rB  rJ  r[  r]  rw  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r0   r0   r0   r1   <module>   s   ,

/
  q02$%$ XC>"9)3O@/!