a
    Cg3                     @   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l
mZ ddlmZ dd	 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.
    )
FieldError)	DataError)Exists)gettext_lazy)ValidationError)
smart_reprc              
   C   s*   z
|   W S  tttfy$   Y dS 0 d S NF)exists	TypeError
ValueErrorr   )queryset r   X/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/rest_framework/validators.py	qs_exists   s    
r   c                 C   sL   |d u rt | S z|t| |@ |W S  ttttfyF   Y dS 0 d S r   )r   r   filtercheckr
   r   r   r   )r   	conditionZagainstr   r   r   qs_exists_with_condition   s    r   c              
   K   s8   z| j f i |W S  tttfy2   |   Y S 0 d S N)r   r
   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__7   s    zUniqueValidator.__init__c                 C   s"   d|| j f |i}t|fi |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u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_instanceC   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__L   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__Y   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   )
isinstancer0   NotImplementedr   requires_contextr   r   r   otherr   r   r   __eq___   s    


zUniqueValidator.__eq__)Nr   )r1   
__module____qualname____doc___r   r6   r   r!   r'   r/   r3   r9   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   s4   || _ || _|p| j| _|d u r$g n|| _|| _d S r   )r   fieldsr   condition_fieldsr   )r   r   r@   r   rA   r   r   r   r   r   s   s
    z UniqueTogetherValidator.__init__c                    sF   j durdS  fddg jjR D }|rBt|dddS )z~
        The `UniqueTogetherValidator` always forces an implied 'required'
        state on the fields it applies to.
        Nc                    s$   i | ]}j | j vr|jqS r   )r@   sourcemissing_message.0r   attrsr   
serializerr   r   
<dictcomp>   s   zCUniqueTogetherValidator.enforce_required_fields.<locals>.<dictcomp>requiredr*   )r&   r@   rA   r   )r   rG   rH   missing_itemsr   rF   r   enforce_required_fieldsz   s    
z/UniqueTogetherValidator.enforce_required_fieldsc                    sb   fdd| j D }jdur@|D ]}| vr"tj| |< q" fdd|D }t|fi |S )zU
        Filter the queryset to all instances matching the given attributes.
        c                    s   g | ]} j | jqS r   r@   rB   rD   rH   r   r   
<listcomp>   s   z;UniqueTogetherValidator.filter_queryset.<locals>.<listcomp>Nc                    s   i | ]}| | qS r   r   rE   rB   rG   r   r   rI      s   z;UniqueTogetherValidator.filter_queryset.<locals>.<dictcomp>)r@   r&   r-   r   )r   rG   r   rH   sourcesrB   r    r   rG   rH   r   r!      s    


z'UniqueTogetherValidator.filter_querysetc                 C   s   |dur|j |jdS |S r"   r$   r   rG   r   r&   r   r   r   r'      s    z0UniqueTogetherValidator.exclude_current_instancec           	         s   |    | j}|  |}|  |j}fdd| jD }jd u rb fdd|D }n fdd|D } fdd| jD }|rd |vrt|| j|rd	| j}| j
j|d}t|d	d
d S )Nc                    s   g | ]} j | jqS r   rM   rD   rN   r   r   rO      s   z4UniqueTogetherValidator.__call__.<locals>.<listcomp>c                    s   g | ]} | qS r   r   rD   rQ   r   r   rO          c                    s(   g | ] } | t j|kr | qS r   )r-   r&   rD   rS   r   r   rO      s   c                    s   i | ]}| | qS r   r   rP   rQ   r   r   rI      rU   z4UniqueTogetherValidator.__call__.<locals>.<dictcomp>, )field_namesr)   r*   )rL   r   r!   r'   r&   r@   rA   r   r   joinr   formatr   )	r   rG   rH   r   Zchecked_namesZchecked_valuesZcondition_kwargsrW   r   r   rS   r   r/      s"    

z UniqueTogetherValidator.__call__c                    s$   d  jjd fdddD S )Nz<{}({})>rV   c                 3   s4   | ],}t  |d ur| dtt  | V  qd S )N=)r-   r   )rE   attrr2   r   r   	<genexpr>   s   z3UniqueTogetherValidator.__repr__.<locals>.<genexpr>)r   r@   r   )rY   r0   r1   rX   r2   r   r2   r   r3      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   )r4   r0   r5   r   r6   rC   r   r@   r7   r   r   r   r9      s    



zUniqueTogetherValidator.__eq__)NNN)r1   r:   r;   r<   r=   r   rC   r6   r   rL   r!   r'   r/   r3   r9   r   r   r   r   r>   i   s   
		r>   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)rE   chr   r   r   r\      s   z@ProhibitSurrogateCharactersValidator.__call__.<locals>.<genexpr>)
code_pointr*   )strr   rY   r^   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   )r4   r0   r5   r   r+   r7   r   r   r   r9      s
    
z+ProhibitSurrogateCharactersValidator.__eq__N)r1   r:   r;   r=   r   r+   r/   r9   r   r   r   r   r]      s   r]   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 )BaseUniqueForValidatorNr?   Tc                 C   s"   || _ || _|| _|p| j| _d S r   )r   field
date_fieldr   )r   r   rc   rd   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 | ]}| vr|j qS r   )rC   rD   rG   r   r   r   rI      s   zBBaseUniqueForValidator.enforce_required_fields.<locals>.<dictcomp>rJ   r*   N)rc   rd   r   )r   rG   rK   r   re   r   rL      s
    
z.BaseUniqueForValidator.enforce_required_fieldsc                 C   s   t dd S )Nz&`filter_queryset` must be implemented.)NotImplementedError)r   rG   r   r   date_field_namer   r   r   r!     s    z&BaseUniqueForValidator.filter_querysetc                 C   s   |dur|j |jdS |S r"   r$   rT   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(   )rd   r)   r*   )r@   rc   r,   rd   rL   r   r!   r'   r&   r   r   rY   r   )r   rG   rH   r   rg   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   )	r4   r0   r5   r   rC   r6   r   rc   rd   r7   r   r   r   r9     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)>)r0   r1   r   r   rc   rd   r2   r   r   r   r3   *  s    zBaseUniqueForValidator.__repr__)N)r1   r:   r;   r   r=   rC   r6   r   rL   r!   r'   r/   r9   r3   r   r   r   r   rb      s   
	rb   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   sZ   || j  }|| j }i }|||< |j|d| < |j|d| < |j|d| < t|fi |S )Nz%s__day	%s__month%s__year)rc   rd   daymonthyearr   r   rG   r   r   rg   r   dater    r   r   r   r!   6  s    

z&UniqueForDateValidator.filter_querysetNr1   r:   r;   r=   r   r!   r   r   r   r   rh   3  s   rh   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i |S )Nri   )rc   rd   rl   r   rn   r   r   r   r!   E  s    

z'UniqueForMonthValidator.filter_querysetNrp   r   r   r   r   rq   B  s   rq   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i |S )Nrj   )rc   rd   rm   r   rn   r   r   r   r!   R  s    

z&UniqueForYearValidator.filter_querysetNrp   r   r   r   r   rr   O  s   rr   N)r<   Zdjango.core.exceptionsr   Z	django.dbr   Zdjango.db.modelsr   Zdjango.utils.translationr   r=   Zrest_framework.exceptionsr   Z#rest_framework.utils.representationr   r   r   r   r   r>   r]   rb   rh   rq   rr   r   r   r   r   <module>   s    ;pH