o
    hV                    @   s8  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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l2m3Z3 d dl4m5Z6 d dl7m8Z8 d dl9m:Z:m;Z;m<Z< d dl=m>Z>mZ d dl?m@Z@ d dlAmBZBmCZCmDZDmEZE d dlFmGZG d dlHmIZI G dd dZJG dd deKZLdd ZMdd  ZNd!d" ZOd#d$ ZPd%d& ZQdud'd(ZRd)d* ZSG d+d, d,ZTG d-d. d.ZUG d/d0 d0eKZVeWeXd1ZYd2ZZd3Z[d4Z\d5Z]d6Z^G d7d8 d8Z_G d9d: d:e_Z`G d;d< d<e`ZaG d=d> d>e_ZbG d?d@ d@ebZcG dAdB dBebZdG dCdD dDebZeG dEdF dFebZfG dGdH dHe_ZgG dIdJ dJebZhG dKdL dLe_ZiG dMdN dNe_ZjG dOdP dPe_ZkG dQdR dRe_ZlG dSdT dTe_ZmG dUdV dVe_ZnG dWdX dXe_ZoG dYdZ dZe_ZpG d[d\ d\epZqG d]d^ d^epZG d_d` d`e_ZrG dadb dberZG dcdd dde_ZsG dedf dfe_ZtG dgdh dhe_ZuG didj djeuZvG dkdl dle_ZwG dmdn dne_ZxG dodp dpe_ZyG dqdr dre_ZzG dsdt dte_Z{dS )v    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)utc)gettext_lazy)InvalidTimeError)ISO_8601RemovedInDRF313WarningRemovedInDRF314Warning)ErrorDetailr   )api_settings)htmlhumanize_datetimejsonrepresentation)lazy_format)$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__ r4   r4   ]/var/www/html/magazine_api/magazine_env/lib/python3.10/site-packages/rest_framework/fields.pyr.   ,   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/   r4   r4   r4   r5   r6   6   s    r6   c                 C   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.
    zkBuilt-in function signatures are not inspectable. Wrap the function call in a simple, pure Python function.Fc                 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paramr4   r4   r5   	<genexpr>M   s    


z%is_simple_callable.<locals>.<genexpr>)inspect	isbuiltinr6   
isfunctionismethod
isinstance	functoolspartial	signature
parametersvaluesall)objsigparamsr4   r4   r5   is_simple_callable>   s   
 

rM   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: {})	rC   r   getattrr   rM   AttributeErrorKeyError
ValueErrorformat)instanceattrsattrexcr4   r4   r5   get_attributeU   s$   



	rW   c                 C   sL   |s	|  | dS |dd D ]}|| vri | |< | | } 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keyr4   r4   r5   	set_valuep   s   	

r^   c                 C   sX   t  }| 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'}}
    )r   rC   listtupleto_choices_dict)choicesretchoicer]   r\   r4   r4   r5   ra      s   

ra   c                 C   sJ   t  }|  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'}
    )r   itemsrC   dict)rb   rc   r]   r\   sub_key	sub_valuer4   r4   r5   flatten_choices_dict   s   


ri   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 r7   label)selfrl   r4   r4   r5   __init__      
z/iter_options.<locals>.StartOptionGroup.__init__Nr0   r1   r2   start_option_groupend_option_grouprn   r4   r4   r4   r5   StartOptionGroup   s    rs   c                   @   s   e Zd ZdZdZdS )z$iter_options.<locals>.EndOptionGroupFTN)r0   r1   r2   rq   rr   r4   r4   r4   r5   EndOptionGroup   s    rt   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 r7   r\   display_textdisabled)rm   r\   rv   rw   r4   r4   r5   rn      s   
z%iter_options.<locals>.Option.__init__NFrp   r4   r4   r4   r5   Option   s    ry   r   rk   )r\   rv      )countzn/aTru   N)re   rC   rf   rR   )grouped_choicescutoffcutoff_textrs   rt   ry   r{   r]   r\   rg   rh   r4   r4   r5   iter_options   s,   	



r   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 r   r%   rL   messager   r<   errorr   r4   r5   
<listcomp>       z$get_error_detail.<locals>.<listcomp>c                    s$   i | ]\}}| fd d|D qS )c                    r   r   r   r   r   r4   r5   r      r   z/get_error_detail.<locals>.<dictcomp>.<listcomp>r4   )r<   kerrorsr   r4   r5   
<dictcomp>   s    z$get_error_detail.<locals>.<dictcomp>)rN   
error_dictrO   Z
error_listre   )exc_infor   r4   r   r5   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   rj   r7   r;   )rm   r;   r4   r4   r5   rn     ro   zCreateOnlyDefault.__init__c                 C   sn   |j jd u}|rt t| jr4t| jdr$tjdtdd | j	|  t
| jddr0| |S |  S | jS )Nset_contextMethod `set_context` on defaults is deprecated and will no longer be called starting with 3.13. Instead set `requires_context = True` on the class, and accept the context as an additional argument.   
stacklevelrequires_contextF)parentrS   	SkipFieldcallabler;   hasattrwarningswarnr#   r   rN   )rm   serializer_fieldZ	is_updater4   r4   r5   __call__  s   

zCreateOnlyDefault.__call__c                 C   s   d| j jt| jf S )Nz%s(%s))	__class__r0   reprr;   rm   r4   r4   r5   __repr__  s   zCreateOnlyDefault.__repr__N)r0   r1   r2   r3   r   rn   r   r   r4   r4   r4   r5   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)rm   r   r4   r4   r5   r   !     zCurrentUserDefault.__call__c                 C   s   d| j j S )Nz%s())r   r0   r   r4   r4   r5   r   $  r   zCurrentUserDefault.__repr__N)r0   r1   r2   r   r   r   r4   r4   r4   r5   r     s    r   c                   @   s   e Zd ZdS )r   N)r0   r1   r2   r4   r4   r4   r5   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   r;   initialsourcerl   	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 )Nrz   default_error_messages)r   _creation_counterr.   NOT_READ_ONLY_WRITE_ONLYNOT_READ_ONLY_REQUIREDNOT_REQUIRED_DEFAULTr   USE_READONLYFIELDr   r   r   r;   r   r   rl   r   r   r   default_empty_htmlr_   r   
field_namer   reversed__mro__rY   rN   r   )rm   r   r   r   r;   r   r   rl   r   r   r   r   r   messagesclsr4   r4   r5   rn   C  s<   


zField.__init__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   r0   r   r   rl   replace
capitalizesource_attrssplitrm   r   r   r4   r4   r5   bindq  s   	



z
Field.bindc                 C   s   t | ds
|  | _| jS )N_validators)r   get_validatorsr   r   r4   r4   r5   r     s   

zField.validatorsc                 C   rj   r7   )r   )rm   r   r4   r4   r5   r     s   
c                 C   s
   t | jS r7   )r_   default_validatorsr   r4   r4   r5   r     ro   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   r4   r4   r5   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.
        rE   Fr   allow_blankN)
r'   is_html_inputr   rN   rootr.   r   r   r   get)rm   rZ   rc   r4   r4   r5   	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fieldrS   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   rS   rV   )rW   r   r6   rR   r   r   r0   r   typerP   rO   r;   r.   get_defaultr   r   r   )rm   rS   rV   msgr4   r4   r5   rW     s>   

zField.get_attributec                 C   sv   | j tu st| jddrt t| j r8t| j dr(tjdt	dd | j 
|  t| j ddr4|  | 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.
        rE   Fr   r   r   r   r   )r;   r.   rN   r   r   r   r   r   r   r#   r   r   r4   r4   r5   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.
        TrE   Fr   Nr   r   )FNTN)
r   r   r.   rN   r   r   r   failr   r   rm   datar4   r4   r5   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)rm   r   Zis_empty_valuer\   r4   r4   r5   run_validation+  s   


zField.run_validationc                 C   s   g }| j D ]]}t|drtjdtdd ||  zt|ddr&|||  n|| W q tyJ } zt|j	t
r: ||j	 W Y d}~qd}~w tyb } z|t| W Y d}~qd}~ww |rit|dS )z
        Test the given value against all the validators on the field,
        and either raise a `ValidationError` or simply return.
        r   zMethod `set_context` on validators is deprecated and will no longer be called starting with 3.13. Instead set `requires_context = True` on the class, and accept the context as an additional argument.r   r   r   FN)r   r   r   r   r#   r   rN   r   rC   detailrf   extendDjangoValidationErrorr   )rm   r\   r   	validatorrV   r4   r4   r5   r   <  s2   


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   NotImplementedErrorrR   r   r0   r   r   r4   r4   r5   r   ^  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   rm   r\   r4   r4   r5   to_representationk  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   Nr4   )r   rP   r   r0   MISSING_ERROR_MESSAGErR   AssertionErrorr   )rm   r]   kwargsr   r   Zmessage_stringr4   r4   r5   r   v  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   )rm   r   r4   r4   r5   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)rN   r   r   r4   r4   r5   r     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   rS   r   r4   r5   r     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 r4   )rC   
REGEX_TYPEcopydeepcopyr<   itemr4   r4   r5   r     s    z&Field.__deepcopy__.<locals>.<listcomp>c                    s*   i | ]\}}||d vrt | n|qS ))r   regex)r   r   )r<   r]   r\   memor4   r5   r     s    z&Field.__deepcopy__.<locals>.<dictcomp>)r   r   re   r   )rm   r   r   r   r4   r   r5   __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   r4   r4   r5   r     s   
zField.__repr__) r0   r1   r2   r   r   r   r   r.   r   r   rn   r   propertyr   setterr   r   r   rW   r   r   r   r   r   r   r   r   r   r   r  r   __classcell__r4   r4   r   r5   r   8  sH    .$

	)!"
	

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>   ZYesrz   ONTyesyYonTrueTRUE1ZOnZYEStruet>   FALSEr   ZOFFFalseNOFfalsenZNonofZOffNoff0>   r   r   ZNULLNZNullc                 C   s`   z|| j v r	W dS || jv rW dS || jv r| jrW d S W n	 ty&   Y nw | jd|d d S )NTFr   input)TRUE_VALUESFALSE_VALUESNULL_VALUESr   	TypeErrorr   r   r4   r4   r5   r     s   

zBooleanField.to_internal_valuec                 C   s8   || j v rdS || jv rdS || jv r| jrd S t|S )NTF)r  r  r   r   boolr   r4   r4   r5   r     s   

zBooleanField.to_representationN)r0   r1   r2   r   r   r   r   r  r  r   r   r   r4   r4   r4   r5   r    s    r  c                       s    e Zd ZdZ fddZ  ZS )NullBooleanFieldNc                    s>   t jdtdd d|vsJ dd|d< t jdi | d S )NzThe `NullBooleanField` is deprecated and will be removed starting with 3.14. Instead use the `BooleanField` field and set `allow_null=True` which does the same thing.r   r   r   z#`allow_null` is not a valid option.Tr4   )r   r   r$   r   rn   rm   r   r   r4   r5   rn     s   zNullBooleanField.__init__)r0   r1   r2   r   rn   r  r4   r4   r   r5   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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)  r4   )popr   r*  r(  r)  r   rn   r+   r   r   appendr   r
   r   r,   rm   r   r   r   r4   r5   rn     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*  strstripr   r   r   r   r   r   r4   r5   r     s
   
zCharField.run_validationc                 C   s>   t |tst |tttfs| d t|}| jr| S |S )Nr   )rC   r"  r1  intfloatr   r*  r2  rm   r   r\   r4   r4   r5   r   !  s   
zCharField.to_internal_valuec                 C      t |S r7   r1  r   r4   r4   r5   r   *     zCharField.to_representation)r0   r1   r2   r   r   r   rn   r.   r   r   r   r  r4   r4   r   r5   r%    s    
	r%  c                       (   e Zd ZdediZ fddZ  ZS )
EmailFieldr   zEnter a valid email address.c                    2   t  jdi | t| jd d}| j| d S Nr   r,  r4   )r   rn   r   r   r   r/  rm   r   r   r   r4   r5   rn   3     zEmailField.__init__r0   r1   r2   r   r   rn   r  r4   r4   r   r5   r:  .      r:  c                       r9  )
RegexFieldr   z/This value does not match the required pattern.c                    s4   t  jdi | t|| jd d}| j| d S r<  )r   rn   r   r   r   r/  )rm   r   r   r   r   r4   r5   rn   >  s   zRegexField.__init__r?  r4   r4   r   r5   rA  9  r@  rA  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di | || _| jrttdtj| jd d}nttd| jd d}| j	| d S )Nz	^[-\w]+\ZrC  r,  z^[-a-zA-Z0-9_]+$r   r4   )
r   rn   allow_unicoder   recompileUNICODEr   r   r/  )rm   rD  r   r   r   r4   r5   rn   J  s   zSlugField.__init__rx   r?  r4   r4   r   r5   rB  D  s
    rB  c                       r9  )URLFieldr   zEnter a valid URL.c                    r;  r<  )r   rn   r   r   r   r/  r=  r   r4   r5   rn   Y  r>  zURLField.__init__r?  r4   r4   r   r5   rH  T  r@  rH  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hexr3  urnr   zMust be a valid UUID.c                    sF   | dd| _| j| jvrtdd| jt jdi | d S )NrR   rJ  z;Invalid format for uuid representation. Must be one of "{}"z", "r4   )r.  uuid_formatvalid_formatsrQ   rR   joinr   rn   r$  r   r4   r5   rn   f  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r3  )rK  r   r\   )rC   uuidUUIDr3  r1  r   rQ   r   r4   r4   r5   r   o  s   

zUUIDField.to_internal_valuec                 C   s   | j dkr	t|S t|| j S )NrJ  )rM  r1  rN   r   r4   r4   r5   r   |  s   
zUUIDField.to_representation)
r0   r1   r2   rN  r   r   rn   r   r   r  r4   r4   r   r5   rI  _  s    	rI  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di | t|| j\}}| j| d S )NrU  r4   )lowerprotocolunpack_ipv4r   rn   r   r   r   )rm   rW  r   r   error_messager   r4   r5   rn     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 )Nr   rQ  :)rU  Zipv6)	rC   r1  r   rW  r   rX  r   r   r   r   r   r4   r5   r     s   

z IPAddressField.to_internal_value)rU  )	r0   r1   r2   r3   r   r   rn   r   r  r4   r4   r   r5   rT    s    rT  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                       | 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 Nr`  ra  r`  r,  ra  r4   r.  r`  ra  r   rn   r+   r   r   r/  r	   r   r0  r   r4   r5   rn        

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 )Nrb  r   r   )
rC   r1  lenMAX_STRING_LENGTHr   r3  
re_decimalsubrQ   r!  r   r4   r4   r5   r     s   
zIntegerField.to_internal_valuec                 C   r6  r7   rP  r   r4   r4   r5   r     r8  zIntegerField.to_representation)r0   r1   r2   r   r   rk  rE  rF  rl  rn   r   r   r  r4   r4   r   r5   r[    s    

r[  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.r\  r]  r^  r_  rc  c                    rd  re  rh  r0  r   r4   r5   rn     ri  zFloatField.__init__c              	   C   sP   t |trt|| jkr| d zt|W S  ttfy'   | d Y d S w )Nrb  r   )rC   r1  rj  rk  r   r4  r!  rQ   r   r4   r4   r5   r     s   

zFloatField.to_internal_valuec                 C   r6  r7   )r4  r   r4   r4   r5   r     r8  zFloatField.to_representation)
r0   r1   r2   r   r   rk  rn   r   r   r  r4   r4   r   r5   rn    s    
rn  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 )DecimalFieldro  r\  r]  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.r^  )r   r`  ra  
max_digitsmax_decimal_placesmax_whole_digitsrb  rc  NFc                    s  || _ || _|| _|d ur|| _| jrd| _|| _|| _| j d ur.| jd ur.| j | j | _nd | _t jd
i | | jd urTt	| j
d | jd}	| jt| j|	d | jd urnt	| j
d | jd}	| jt| j|	d |d urdd tt D }
||
v sJ d	||
f || _d S )NTr`  rf  r,  ra  rg  c                 S   s   g | ]\}}| d r|qS )ZROUND_)
startswith)r<   r   vr4   r4   r5   r     s    z)DecimalField.__init__.<locals>.<listcomp>z=Invalid rounding option %s. Valid values for rounding are: %sr4   )rq  decimal_placeslocalizecoerce_to_stringr`  ra  rs  r   rn   r+   r   r   r/  r	   r   varsdecimalre   rounding)rm   rq  rv  rx  r`  ra  rw  r{  r   r   Zvalid_roundingsr   r4   r5   rn     s:   




zDecimalField.__init__c                    s&   t | dkr| jrdS t |S )Nr   r   )r   r2  r   r   r   r   r   r4   r5   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.
        rb  r   ZInfz-Inf)r   r2  rw  r   rj  rk  r   rz  DecimalDecimalExceptionis_nanquantizevalidate_precisionr5  r4   r4   r5   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   Nrq  )rq  rr  )rr  rs  )rs  )as_tuplerj  absrq  r   rv  rs  )rm   r\   signZ
digittupleexponentZtotal_digitsZwhole_digitsrv  r4   r4   r5   r  :  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| }| |}|s)|S | j	r0t
|S d|S )Nrx  r   z{:f})rN   r&   ZCOERCE_DECIMAL_TO_STRINGrC   rz  r|  r1  r2  r  rw  r   rR   )rm   r\   rx  Z	quantizedr4   r4   r5   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)r{  r   )	rv  rz  
getcontextr   rq  precr  r|  r{  )rm   r\   r   r4   r4   r5   r  r  s   

zDecimalField.quantize)NNNFN)r0   r1   r2   r   r   rk  rn   r   r   r  r   r  r  r4   r4   r   r5   rp    s$    	##rp  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di | d S Nr4   )r.   rR   input_formatsr   r   rn   )rm   rR   r  default_timezoner   r   r4   r5   rn     s   zDateTimeField.__init__c                 C   s   t | d|  }|dur?t|r&z||W S  ty%   | d Y nw zt||W S  ty>   | jd|d Y |S w |du rNt|rNt	|t
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   Nr  r  r   )rN   r  r   Zis_aware
astimezoneOverflowErrorr   r  r!   Z
make_naiver   )rm   r\   Zfield_timezoner4   r4   r5   enforce_timezone  s"   
zDateTimeField.enforce_timezonec                 C   s   t jrt S d S r7   )r   ZUSE_TZr   Zget_current_timezoner   r4   r4   r5   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 ]?}| t	krKzt
|}|d ur>| |W   S W q% ttfyJ   Y q%w z| ||}| |W   S  ttfyd   Y q%w t|}| jd|d d S )Nr  r  r   rR   )rN   r&   ZDATETIME_INPUT_FORMATSrC   datetimer  r   r  rV  r"   r   rQ   r!  datetime_parserr(   Zdatetime_formatsrm   r\   r  Zinput_formatparsedZhumanized_formatr4   r4   r5   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 )NrR   z+00:00iZ)rN   r&   ZDATETIME_FORMATrC   r1  r  rV  r"   	isoformatendswithstrftimerm   r\   Zoutput_formatr4   r4   r5   r     s   


zDateTimeField.to_representation)r0   r1   r2   r   r   r  strptimer  r.   rn   r  r  r   r   r  r4   r4   r   r5   r    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.)r   r  Nc                    2   |t ur|| _|d ur|| _t jdi | d S r  r.   rR   r  r   rn   rm   rR   r  r   r   r4   r5   rn     
   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  r   r  )rN   r&   ZDATE_INPUT_FORMATSrC   r  r   r  rV  r"   r   rQ   r!  r  r(   Zdate_formatsr  r4   r4   r5   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 )NrR   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.)
rN   r&   ZDATE_FORMATrC   r1  r  rV  r"   r  r  r  r4   r4   r5   r     s   
zDateField.to_representationr0   r1   r2   r   r   r  r  r  r.   rn   r   r   r  r4   r4   r   r5   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                    r  r  r  r  r   r4   r5   rn   &  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  r   r  )rN   r&   ZTIME_INPUT_FORMATSrC   r  timerV  r"   r   rQ   r!  r  r(   Ztime_formatsr   r  r4   r4   r5   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   rR   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.)
rN   r&   ZTIME_FORMATrC   r1  r  rV  r"   r  r  r  r4   r4   r5   r   G  s   
zTimeField.to_representationr  r4   r4   r   r5   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}.r\  r]  )r   r`  ra  c                    rd  re  rh  r0  r   r4   r5   rn   e  ri  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  )rC   r  	timedeltar   r1  r   )rm   r\   r  r4   r4   r5   r   r  s   zDurationField.to_internal_valuec                 C   r6  r7   r   r   r4   r4   r5   r   z  r8  zDurationField.to_representation	r0   r1   r2   r   r   rn   r   r   r  r4   r4   r   r5   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di | d S )Nhtml_cutoffhtml_cutoff_textr   Fr4   )rb   r.  r  r  r   r   rn   )rm   rb   r   r   r4   r5   rn     s
   zChoiceField.__init__c                 C   sF   |dkr	| j r	dS z| jt| W S  ty"   | jd|d Y d S w )Nr   r  r  )r   choice_strings_to_valuesr1  rP   r   r   r4   r4   r5   r     s   zChoiceField.to_internal_valuec                 C   s   |dv r|S | j t||S )N)r   Nr  r   r1  r   r4   r4   r5   r     s   zChoiceField.to_representationc                 C   s   t | j| j| jdS )zP
        Helper method for use with templates rendering select widgets.
        )r}   r~   )r   r|   r  r  r   r4   r4   r5   r     s
   zChoiceField.iter_optionsc                 C   s   | j S r7   )_choicesr   r4   r4   r5   _get_choices  s   zChoiceField._get_choicesc                 C   s,   t || _t| j| _dd | jD | _d S )Nc                 S   s   i | ]}t ||qS r4   r7  )r<   r]   r4   r4   r5   r     s    
z,ChoiceField._set_choices.<locals>.<dictcomp>)ra   r|   ri   r  rb   r  )rm   rb   r4   r4   r5   _set_choices  s
   
zChoiceField._set_choices)r0   r1   r2   r   r   r  r  rn   r   r   r   r  r  r  rb   r  r4   r4   r   r5   r    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_emptyTr4   )r.  r  r   rn   r$  r   r4   r5   rn        zMultipleChoiceField.__init__c                 C   s@   | j |vrt| jddrtS t|r|| j S || j tS )NrE   F)r   rN   r   r.   r'   r   getlistr   rm   rZ   r4   r4   r5   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  Z
input_typer   r.   c                    s   h | ]
}t t|qS r4   )r   r  r   r   )r   rm   r4   r5   	<setcomp>  s    z8MultipleChoiceField.to_internal_value.<locals>.<setcomp>)rC   r1  r   r   r   r0   r  rj  r   r   r   r5   r     s   
z%MultipleChoiceField.to_internal_valuec                        fdd|D S )Nc                    s   h | ]} j t||qS r4   r  r   r   r4   r5   r    s    z8MultipleChoiceField.to_representation.<locals>.<setcomp>r4   r   r4   r   r5   r     s   
z%MultipleChoiceField.to_representation)r0   r1   r2   r   r   r   rn   r   r   r   r  r4   r4   r   r5   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di | d S )N)match	recursiveallow_filesallow_foldersr   rb   r4   )DjangoFilePathFieldrb   r   rn   )	rm   pathr  r  r  r  r   r   r   r   r4   r5   rn     s   
zFilePathField.__init__)NFTFNr?  r4   r4   r   r5   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 r| d| _t jdi | d S )Nr(  allow_empty_fileFuse_urlr4   )r.  r(  r  r  r   rn   r$  r   r4   r5   rn     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 )Nr   r  r.   r(  )r(  length)namesizerO   r   r  r(  rj  )rm   r   	file_name	file_sizer4   r4   r5   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   )	rN   r&   ZUPLOADED_FILES_USE_URLurlrO   r   r   Zbuild_absolute_urir  )rm   r\   r  r  r   r4   r4   r5   r     s   

zFileField.to_representationr  r4   r4   r   r5   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   Z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_DjangoImageFieldr4   )r.  DjangoImageFieldr  r   rn   r$  r   r4   r5   rn   /  r  zImageField.__init__c                    s&   t  |}|  }| j|_||S r7   )r   r   r  r   clean)rm   r   Zfile_objectZdjango_fieldr   r4   r5   r   3  s   
zImageField.to_internal_value)r0   r1   r2   r   r   rn   r   r  r4   r4   r   r5   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 )NTr4   )r   rn   r   r   r$  r   r4   r5   rn   @  s   
z_UnvalidatedField.__init__c                 C      |S r7   r4   r   r4   r4   r5   r   E     z#_UnvalidatedField.to_internal_valuec                 C   r  r7   r4   r   r4   r4   r5   r   H  r  z#_UnvalidatedField.to_representation)r0   r1   r2   rn   r   r   r  r4   r4   r   r5   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-  r4   )r.  r   r   r  r  r(  r)  r?   isclassr   r   rn   r   r+   r   r   r/  r   r
   r0  r   r4   r5   rn   V  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 )NrE   Fr   )prefixr;   )
r   rN   r   r.   r'   r   r  rj  parse_html_listr   )rm   rZ   valr4   r4   r5   r   k  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  rC   r1  r   r   r   r   r0   r  rj  run_child_validationr   r4   r4   r5   r   z  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 r7   )r  r   r   r   r4   r5   r     s   $ z/ListField.to_representation.<locals>.<listcomp>r4   r   r4   r   r5   r     s   zListField.to_representationc                 C   sp   g }t  }t|D ]&\}}z|| j| W q	 ty/ } z|j||< W Y d }~q	d }~ww |s4|S t|r7   )r   	enumerater/  r  r   r   r   )rm   r   resultr   idxr   er4   r4   r5   r    s   zListField.run_child_validationr0   r1   r2   r  r  r   r   r   rn   r   r   r   r  r  r4   r4   r   r5   r  L  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  r4   )r.  r   r   r  r  r?   r  r   r   rn   r   r$  r   r4   r5   rn     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  r4   r4   r5   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  rC   rf   r   r   r0   r  rj  r  r   r4   r4   r5   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 r7   )r1  r  r   )r<   r]   r  r   r4   r5   r     s    z/DictField.to_representation.<locals>.<dictcomp>)re   r   r4   r   r5   r     s   
zDictField.to_representationc                 C   sv   i }t  }| D ])\}}t|}z
| j|||< W q	 ty2 } z|j||< W Y d }~q	d }~ww |s7|S t|r7   )r   re   r1  r  r   r   r   )rm   r   r  r   r]   r\   r  r4   r4   r5   r    s   zDictField.run_child_validationr  r4   r4   r   r5   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.r4   )r   rn   rC   r  r%  r$  r   r4   r5   rn     s   zHStoreField.__init__)r0   r1   r2   r%  r  rn   r  r4   r4   r   r5   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di | d S )NbinaryFencoderdecoderr4   )r.  r  r  r  r   rn   r$  r   r4   r5   rn     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)r1  r   is_json_string)r   r\   rc   r4   r4   r5   r     s   z/JSONField.get_value.<locals>.JSONString.__new__N)r0   r1   r2   r   r4   r4   r4   r5   
JSONString  s    r  )r'   r   r   r1  r   r.   )rm   rZ   r  r4   r4   r5   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   r   )r  rN   rC   bytesdecoder)   loadsr  dumpsr  r!  rQ   r   r   r4   r4   r5   r      s   
zJSONField.to_internal_valuec                 C   s"   | j rtj|| jd}| }|S )Nr  )r  r)   r  r  encoder   r4   r4   r5   r     s   zJSONField.to_representation)r0   r1   r2   r   r   Z_is_jsonfieldrn   r   r   r   r  r4   r4   r   r5   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   r4   r   rn   r$  r   r4   r5   rn   "  s   zReadOnlyField.__init__c                 C   r  r7   r4   r   r4   r4   r5   r   &  r  zReadOnlyField.to_representation)r0   r1   r2   r3   rn   r   r  r4   r4   r   r5   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 )Nr;   zdefault is a required argument.Tr   r4   r  r$  r   r4   r5   rn   2  s   zHiddenField.__init__c                 C   s   t S r7   )r.   r  r4   r4   r5   r   7     zHiddenField.get_valuec                 C   r  r7   r4   r   r4   r4   r5   r   <  r  zHiddenField.to_internal_value)r0   r1   r2   r3   rn   r   r   r  r4   r4   r   r5   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di | d S )Nr   r   Tr   r4   )method_namer   rn   )rm   r  r   r   r4   r5   rn   O  s   zSerializerMethodField.__init__c                    s*   | j d u rdj|d| _ t || d S )Nzget_{field_name})r   )r  rR   r   r   r   r   r4   r5   r   U  s   
zSerializerMethodField.bindc                 C   s   t | j| j}||S r7   )rN   r   r  )rm   r\   methodr4   r4   r5   r   \  s   z'SerializerMethodField.to_representationr7   )r0   r1   r2   r3   rn   r   r   r  r4   r4   r   r5   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,  r4   )
model_fieldr.  r(  r   rn   r+   r   r   r/  r   )rm   r  r   r   r   r4   r5   rn   l  s   
zModelField.__init__c                 C   s2   | j j}|d ur|jj|j|S | j |S r7   )r  Zremote_fieldmodel_meta	get_fieldr   Z	to_python)rm   r   relr4   r4   r5   r   w  s   zModelField.to_internal_valuec                 C   r  r7   r4   )rm   rJ   r4   r4   r5   rW   }  r  zModelField.get_attributec                 C   s$   | j |}t|r|S | j |S r7   )r  Zvalue_from_objectr   Zvalue_to_string)rm   rJ   r\   r4   r4   r5   r     s   zModelField.to_representation)r0   r1   r2   r3   r   r   rn   r   rW   r   r  r4   r4   r   r5   r  a  s    r  )NN)|r   r  rz  rD   r?   rE  rR  r   collectionsr   collections.abcr   Zdjango.confr   Zdjango.core.exceptionsr   r   r   Zdjango.core.validatorsr   r   r	   r
   r   r   r   r   r   Zdjango.formsr   r  r   r  Zdjango.utilsr   Zdjango.utils.dateparser   r   r   r   Zdjango.utils.durationr   Zdjango.utils.encodingr   r   Zdjango.utils.formatsr   r   Zdjango.utils.ipv6r   Zdjango.utils.timezoner   Zdjango.utils.translationr    r   Zpytz.exceptionsr!   Zrest_frameworkr"   r#   r$   Zrest_framework.exceptionsr%   Zrest_framework.settingsr&   Zrest_framework.utilsr'   r(   r)   r*   Zrest_framework.utils.formattingr+   Zrest_framework.validatorsr,   r.   	Exceptionr6   rM   rW   r^   ra   ri   r   r   r   r   r   r   rF  r   r   r   r   r   r   r   r  r#  r%  r:  rA  rB  rH  rI  rT  r[  rn  rp  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r4   r4   r4   r5   <module>   s    ,

/$
   .2$%$ XC>"9)3O@/!