U
    ڲgn/                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlm	Z	 dd Z
dd	 ZG d
d dZG dd dZG dd dZG dd dZG dd deZG dd deZG dd deZdS )aK  
We perform uniqueness checks explicitly on the serializer class, rather
the using Django's `.full_clean()`.

This gives us better separation of concerns, allows us to use single-step
object creation, and makes it possible to switch between using the implicit
`ModelSerializer` class and an equivalent explicit `Serializer` class.
    )	DataError)gettext_lazy)ValidationError)
smart_reprc              
   C   s,   z
|   W S  tttfk
r&   Y dS X d S )NF)exists	TypeError
ValueErrorr   )queryset r
   =/tmp/pip-unpacked-wheel-11h17kvo/rest_framework/validators.py	qs_exists   s    
r   c              
   K   s6   z| j f |W S  tttfk
r0   |   Y S X d S N)filterr   r   r   none)r	   kwargsr
   r
   r   	qs_filter   s    r   c                   @   sN   e Zd ZdZedZdZdddZdd	 Zd
d Z	dd Z
dd Zdd ZdS )UniqueValidatorz
    Validator that corresponds to `unique=True` on a model field.

    Should be applied to an individual field on the serializer.
    zThis field must be unique.TNexactc                 C   s   || _ |p| j| _|| _d S r   )r	   messagelookup)selfr	   r   r   r
   r
   r   __init__*   s    zUniqueValidator.__init__c                 C   s   d|| j f |i}t|f|S )zT
        Filter the queryset to all instances matching the given attribute.
        z%s__%s)r   r   )r   valuer	   
field_namefilter_kwargsr
   r
   r   filter_queryset/   s    zUniqueValidator.filter_querysetc                 C   s   |dk	r|j |jdS |S z}
        If an instance is being updated, then do not include
        that instance itself as a uniqueness conflict.
        N)pkexcluder   )r   r	   instancer
   r
   r   exclude_current_instance6   s    z(UniqueValidator.exclude_current_instancec                 C   sR   |j d }t|jdd }| j}| |||}| ||}t|rNt| jddd S )Nr    uniquecode)	source_attrsgetattrparentr	   r   r!   r   r   r   )r   r   Zserializer_fieldr   r    r	   r
   r
   r   __call__?   s    
zUniqueValidator.__call__c                 C   s   d| j jt| jf S )Nz<%s(queryset=%s)>)	__class____name__r   r	   r   r
   r
   r   __repr__L   s    zUniqueValidator.__repr__c                 C   s@   t || jstS | j|jko>| j|jko>| j|jko>| j|jkS r   )
isinstancer*   NotImplementedr   requires_contextr	   r   r   otherr
   r
   r   __eq__R   s    


zUniqueValidator.__eq__)Nr   )r+   
__module____qualname____doc___r   r0   r   r   r!   r)   r-   r3   r
   r
   r
   r   r   !   s   
	r   c                   @   s^   e Zd ZdZedZedZdZdddZdd	 Z	d
d Z
dd Zdd Zdd Zdd ZdS )UniqueTogetherValidatorz
    Validator that corresponds to `unique_together = (...)` on a model class.

    Should be applied to the serializer class, not to an individual field.
    z0The fields {field_names} must make a unique set.This field is required.TNc                 C   s   || _ || _|p| j| _d S r   )r	   fieldsr   )r   r	   r:   r   r
   r
   r   r   f   s    z UniqueTogetherValidator.__init__c                    s:   j dk	rdS  fddjD }|r6t|dddS )z~
        The `UniqueTogetherValidator` always forces an implied 'required'
        state on the fields it applies to.
        Nc                    s$   i | ]}j | j kr|jqS r
   )r:   sourcemissing_message.0r   attrsr   
serializerr
   r   
<dictcomp>s   s    zCUniqueTogetherValidator.enforce_required_fields.<locals>.<dictcomp>requiredr$   )r    r:   r   )r   r@   rA   missing_itemsr
   r?   r   enforce_required_fieldsk   s    
z/UniqueTogetherValidator.enforce_required_fieldsc                    s^   fdd| j D }jdk	r@|D ]}| kr"tj| |< q" fdd|D }t|f|S )zU
        Filter the queryset to all instances matching the given attributes.
        c                    s   g | ]} j | jqS r
   )r:   r;   r=   )rA   r
   r   
<listcomp>   s   z;UniqueTogetherValidator.filter_queryset.<locals>.<listcomp>Nc                    s   i | ]}| | qS r
   r
   )r>   r;   )r@   r
   r   rB      s    z;UniqueTogetherValidator.filter_queryset.<locals>.<dictcomp>)r:   r    r'   r   )r   r@   r	   rA   sourcesr;   r   r
   )r@   rA   r   r   {   s    


z'UniqueTogetherValidator.filter_querysetc                 C   s   |dk	r|j |jdS |S r   r   r   r@   r	   r    r
   r
   r   r!      s    z0UniqueTogetherValidator.exclude_current_instancec                    s     |  j} ||} ||j}jd krR fdd| D }n fdd| D }|rd |krt|rd j} j	j
|d}t|ddd S )Nc                    s   g | ]\}}| j kr|qS r
   )r:   r>   fieldr   r,   r
   r   rF      s    
 z4UniqueTogetherValidator.__call__.<locals>.<listcomp>c                    s.   g | ]&\}}| j kr|tj|kr|qS r
   )r:   r'   r    rI   r   rA   r
   r   rF      s   
 z, )field_namesr#   r$   )rE   r	   r   r!   r    itemsr   joinr:   r   formatr   )r   r@   rA   r	   Zchecked_valuesrL   r   r
   rK   r   r)      s    

z UniqueTogetherValidator.__call__c                 C   s   d| j jt| jt| jf S )Nz<%s(queryset=%s, fields=%s)>)r*   r+   r   r	   r:   r,   r
   r
   r   r-      s
    z UniqueTogetherValidator.__repr__c                 C   sL   t || jstS | j|jkoJ| j|jkoJ| j|jkoJ| j|jkoJ| j|jkS r   )r.   r*   r/   r   r0   r<   r	   r:   r1   r
   r
   r   r3      s    



zUniqueTogetherValidator.__eq__)N)r+   r4   r5   r6   r7   r   r<   r0   r   rE   r   r!   r)   r-   r3   r
   r
   r
   r   r8   \   s   
	r8   c                   @   s(   e Zd ZedZdZdd Zdd ZdS )$ProhibitSurrogateCharactersValidatorz7Surrogate characters are not allowed: U+{code_point:X}.Z surrogate_characters_not_allowedc                 C   s<   dd t |D D ]$}| jjt|d}t|| jdqd S )Nc                 s   s.   | ]&}d t |  krdkrn q|V  qdS )i   i  N)ord)r>   chr
   r
   r   	<genexpr>   s
      z@ProhibitSurrogateCharactersValidator.__call__.<locals>.<genexpr>)
code_pointr$   )strr   rO   rQ   r   r%   )r   r   Zsurrogate_characterr   r
   r
   r   r)      s    z-ProhibitSurrogateCharactersValidator.__call__c                 C   s(   t || jstS | j|jko&| j|jkS r   )r.   r*   r/   r   r%   r1   r
   r
   r   r3      s
    
z+ProhibitSurrogateCharactersValidator.__eq__N)r+   r4   r5   r7   r   r%   r)   r3   r
   r
   r
   r   rP      s   rP   c                   @   sV   e Zd ZdZedZdZdddZdd Zdd	 Z	d
d Z
dd Zdd Zdd ZdS )BaseUniqueForValidatorNr9   Tc                 C   s"   || _ || _|| _|p| j| _d S r   )r	   rJ   
date_fieldr   )r   r	   rJ   rW   r   r
   r
   r   r      s    zBaseUniqueForValidator.__init__c                    s0    fddj jfD }|r,t|dddS )z
        The `UniqueFor<Range>Validator` classes always force an implied
        'required' state on the fields they are applied to.
        c                    s   i | ]}| kr|j qS r
   )r<   r=   r@   r   r
   r   rB      s    zBBaseUniqueForValidator.enforce_required_fields.<locals>.<dictcomp>rC   r$   N)rJ   rW   r   )r   r@   rD   r
   rX   r   rE      s
    
z.BaseUniqueForValidator.enforce_required_fieldsc                 C   s   t dd S )Nz&`filter_queryset` must be implemented.)NotImplementedError)r   r@   r	   r   date_field_namer
   r
   r   r      s    z&BaseUniqueForValidator.filter_querysetc                 C   s   |dk	r|j |jdS |S r   r   rH   r
   r
   r   r!      s    z/BaseUniqueForValidator.exclude_current_instancec                 C   s   |j | j jd }|j | j jd }| | | j}| ||||}| |||j}t	|r~| j
j| jd}t| j|iddd S )Nr"   )rW   r#   r$   )r:   rJ   r&   rW   rE   r	   r   r!   r    r   r   rO   r   )r   r@   rA   r   rZ   r	   r   r
   r
   r   r)      s    
 zBaseUniqueForValidator.__call__c                 C   sX   t || jstS | j|jkoV| j|jkoV| j|jkoV| j|jkoV| j|jkoV| j|jkS r   )	r.   r*   r/   r   r<   r0   r	   rJ   rW   r1   r
   r
   r   r3     s    




zBaseUniqueForValidator.__eq__c                 C   s&   d| j jt| jt| jt| jf S )Nz*<%s(queryset=%s, field=%s, date_field=%s)>)r*   r+   r   r	   rJ   rW   r,   r
   r
   r   r-     s    zBaseUniqueForValidator.__repr__)N)r+   r4   r5   r   r7   r<   r0   r   rE   r   r!   r)   r3   r-   r
   r
   r
   r   rV      s   
	rV   c                   @   s   e Zd ZedZdd ZdS )UniqueForDateValidatorz6This field must be unique for the "{date_field}" date.c                 C   sV   || j  }|| j }i }|||< |j|d| < |j|d| < |j|d| < t|f|S )Nz%s__day	%s__month%s__year)rJ   rW   daymonthyearr   r   r@   r	   r   rZ   r   dater   r
   r
   r   r   #  s    

z&UniqueForDateValidator.filter_querysetNr+   r4   r5   r7   r   r   r
   r
   r
   r   r[      s   r[   c                   @   s   e Zd ZedZdd ZdS )UniqueForMonthValidatorz7This field must be unique for the "{date_field}" month.c                 C   s:   || j  }|| j }i }|||< |j|d| < t|f|S )Nr\   )rJ   rW   r_   r   ra   r
   r
   r   r   2  s    

z'UniqueForMonthValidator.filter_querysetNrc   r
   r
   r
   r   rd   /  s   rd   c                   @   s   e Zd ZedZdd ZdS )UniqueForYearValidatorz6This field must be unique for the "{date_field}" year.c                 C   s:   || j  }|| j }i }|||< |j|d| < t|f|S )Nr]   )rJ   rW   r`   r   ra   r
   r
   r   r   ?  s    

z&UniqueForYearValidator.filter_querysetNrc   r
   r
   r
   r   re   <  s   re   N)r6   Z	django.dbr   Zdjango.utils.translationr   r7   Zrest_framework.exceptionsr   Z#rest_framework.utils.representationr   r   r   r   r8   rP   rV   r[   rd   re   r
   r
   r
   r   <module>   s   ;jH