
    hb(                         d Z ddlmZ ddlmZ ddlmZ ddlm	Z	 d Z
d Z G d d	      Z G d
 d      Z G d d      Z G d d      Z G d de      Z G d de      Z G d de      Zy)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                 X    	 | j                         S # t        t        t        f$ r Y yw xY w)NF)exists	TypeError
ValueErrorr   )querysets    \/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/rest_framework/validators.py	qs_existsr      s-      z9- s    ))c                 |    	  | j                   di |S # t        t        t        f$ r | j	                         cY S w xY w)N )filterr	   r
   r   none)r   kwargss     r   	qs_filterr      s=    x(((z9- }}s    $;;c                   D    e Zd ZdZ ed      ZdZd
dZd Zd Z	d Z
d	 Zy)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.TNc                 J    || _         |xs | j                  | _        || _        y N)r   messagelookup)selfr   r   r   s       r   __init__zUniqueValidator.__init__*   s      .$,,    c                 @    |d| j                   |i}t        |fi |S )zT
        Filter the queryset to all instances matching the given attribute.
        __)r   r   )r   valuer   
field_namefilter_kwargss        r   filter_querysetzUniqueValidator.filter_queryset/   s'     &0=uE3]33r   c                 B    ||j                  |j                        S |S z}
        If an instance is being updated, then do not include
        that instance itself as a uniqueness conflict.
        )pkexcluder%   )r   r   instances      r   exclude_current_instancez(UniqueValidator.exclude_current_instance6   '    
 ##x{{#33r   c                     |j                   d   }t        |j                  dd       }| j                  }| j	                  |||      }| j                  ||      }t        |      rt        | j                  d      y )Nr(   uniquecode)	source_attrsgetattrparentr   r"   r)   r   r   r   )r   r   serializer_fieldr    r(   r   s         r   __call__zUniqueValidator.__call__?   sv     &2226
+22JE==''xD008DX!$,,X>> r   c                 b    d| j                   j                  dt        | j                        dS )N<
(queryset=)>)	__class____name__r   r   r   s    r   __repr__zUniqueValidator.__repr__L   s%    NN##t}}%
 	
r   )Nexact)r:   
__module____qualname____doc___r   requires_contextr   r"   r)   r4   r<   r   r   r   r   r   !   s3    
 ,-G
4?
r   r   c                   Z    e Zd ZdZ ed      Z ed      ZdZddZd Z	d Z
d	 Zd
 Zd Zy)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                 J    || _         || _        |xs | j                  | _        y r   )r   fieldsr   )r   r   rG   r   s       r   r   z UniqueTogetherValidator.__init__]   s      .$,,r   c                     |j                   y| j                  D ci c]*  }|j                  |   j                  |vr|| j                  , }}|rt	        |d      yc c}w )z~
        The `UniqueTogetherValidator` always forces an implied 'required'
        state on the fields it applies to.
        Nrequiredr.   )r(   rG   sourcemissing_messager   )r   attrs
serializerr    missing_itemss        r   enforce_required_fieldsz/UniqueTogetherValidator.enforce_required_fieldsb   sv    
 * #kk
  ,335@ ,,,
 

 !-jAA 
s   /Ac                    | j                   D cg c]  }|j                   |   j                   }}|j                  %|D ]   }||vst        |j                  |      ||<   " |D ci c]  }|||   
 }}t	        |fi |S c c}w c c}w )zU
        Filter the queryset to all instances matching the given attributes.
        )rG   rJ   r(   r1   r   )r   rL   r   rM   r    sourcesrJ   r!   s           r   r"   z'UniqueTogetherValidator.filter_querysetr   s     #kk
 j)00
 
 *! I&$+J,?,?$HE&MI "
 E&M!
 
 3]33#

s    B&Bc                 B    ||j                  |j                        S |S r$   r&   r   rL   r   r(   s       r   r)   z0UniqueTogetherValidator.exclude_current_instance   r*   r   c                    | j                  ||       | j                  }| j                  |||      }| j                  |||j                        }|j                         D cg c]  \  }}|| j                  v s| }}}d |vrPt        |      rDdj                  | j                        }| j                  j                  |      }t        |d      y y c c}}w )Nz, )field_namesr-   r.   )rO   r   r"   r)   r(   itemsrG   r   joinr   formatr   )	r   rL   rM   r   fieldr   checked_valuesrU   r   s	            r   r4   z UniqueTogetherValidator.__call__   s    $$UJ7==''xD00*BUBUV ',kkm
"eUu7KE
 
 ~%)H*=))DKK0Kll))k)BG!'99 +>%
s   "C9Cc                     d| j                   j                  dt        | j                        dt        | j                        dS )Nr6   r7   z	, fields=r8   )r9   r:   r   r   rG   r;   s    r   r<   z UniqueTogetherValidator.__repr__   s1    NN##t}}%t{{#
 	
r   r   )r:   r>   r?   r@   rA   r   rK   rB   r   rO   r"   r)   r4   r<   r   r   r   rD   rD   S   sD    
 BCG12O/
B 40:
r   rD   c                   &    e Zd Z ed      ZdZd Zy)$ProhibitSurrogateCharactersValidatorz7Surrogate characters are not allowed: U+{code_point:X}. surrogate_characters_not_allowedc                     d t        |      D        D ]=  }| j                  j                  t        |            }t	        || j
                         y )Nc              3   N   K   | ]  }d t        |      cxk  rdk  rn n|  yw)i   i  N)ord).0chs     r   	<genexpr>z@ProhibitSurrogateCharactersValidator.__call__.<locals>.<genexpr>   s*      $D2'-R'BF'B %' $Ds   #%)
code_pointr.   )strr   rX   ra   r   r/   )r   r   surrogate_characterr   s       r   r4   z-ProhibitSurrogateCharactersValidator.__call__   sO    $DU $D 	;ll))S9L5M)NG!'		::	;r   N)r:   r>   r?   rA   r   r/   r4   r   r   r   r]   r]      s    IJG-D;r   r]   c                   J    e Zd ZdZ ed      ZdZd
dZd Zd Z	d Z
d Zd	 Zy)BaseUniqueForValidatorNrE   Tc                 X    || _         || _        || _        |xs | j                  | _        y r   )r   rY   
date_fieldr   )r   r   rY   rk   r   s        r   r   zBaseUniqueForValidator.__init__   s'     
$.$,,r   c                     | j                   | j                  fD ci c]  }||vr|| j                   }}|rt        |d      yc c}w )z
        The `UniqueFor<Range>Validator` classes always force an implied
        'required' state on the fields they are applied to.
        rI   r.   N)rY   rk   rK   r   )r   rL   r    rN   s       r   rO   z.BaseUniqueForValidator.enforce_required_fields   s]      $zz4??;
& ,,,
 

 !-jAA 
s   Ac                     t        d      )Nz&`filter_queryset` must be implemented.)NotImplementedError)r   rL   r   r    date_field_names        r   r"   z&BaseUniqueForValidator.filter_queryset   s    !"JKKr   c                 B    ||j                  |j                        S |S r$   r&   rS   s       r   r)   z/BaseUniqueForValidator.exclude_current_instance   r*   r   c                    |j                   | j                     j                  d   }|j                   | j                     j                  d   }| j	                  |       | j
                  }| j                  ||||      }| j                  |||j                        }t        |      r?| j                  j                  | j                        }t        | j                  |id      y )Nr,   )rk   r-   r.   )rG   rY   r0   rk   rO   r   r"   r)   r(   r   r   rX   r   )r   rL   rM   r    ro   r   r   s          r   r4   zBaseUniqueForValidator.__call__   s      &&tzz2??C
$++DOO<II"M$$U+==''x_U00*BUBUVXll))T__)EG!

G#  r   c           
          d| j                   j                  dt        | j                        dt        | j                        dt        | j
                        d	S )Nr6   r7   z, field=z, date_field=r8   )r9   r:   r   r   rY   rk   r;   s    r   r<   zBaseUniqueForValidator.__repr__   s=    NN##t}}%tzz"t'	
 	
r   r   )r:   r>   r?   r   rA   rK   rB   r   rO   r"   r)   r4   r<   r   r   r   ri   ri      s8    G12O/BL 
r   ri   c                   "    e Zd Z ed      Zd Zy)UniqueForDateValidatorz6This field must be unique for the "{date_field}" date.c                     || j                      }|| j                     }i }|||<   |j                  |d|z  <   |j                  |d|z  <   |j                  |d|z  <   t        |fi |S )Nz%s__day	%s__month%s__year)rY   rk   daymonthyearr   r   rL   r   r    ro   r   dater!   s           r   r"   z&UniqueForDateValidator.filter_queryset   sv    djj!T__%$)j!59XXi/127;zzkO346:iij?233]33r   Nr:   r>   r?   rA   r   r"   r   r   r   rt   rt      s    HIG	4r   rt   c                   "    e Zd Z ed      Zd Zy)UniqueForMonthValidatorz7This field must be unique for the "{date_field}" month.c                     || j                      }|| j                     }i }|||<   |j                  |d|z  <   t        |fi |S )Nrv   )rY   rk   ry   r   r{   s           r   r"   z'UniqueForMonthValidator.filter_queryset  sP    djj!T__%$)j!7;zzkO343]33r   Nr}   r   r   r   r   r     s    IJG4r   r   c                   "    e Zd Z ed      Zd Zy)UniqueForYearValidatorz6This field must be unique for the "{date_field}" year.c                     || j                      }|| j                     }i }|||<   |j                  |d|z  <   t        |fi |S )Nrw   )rY   rk   rz   r   r{   s           r   r"   z&UniqueForYearValidator.filter_queryset  sP    djj!T__%$)j!6:iij?233]33r   Nr}   r   r   r   r   r     s    HIG4r   r   N)r@   	django.dbr   django.utils.translationr   rA   rest_framework.exceptionsr   #rest_framework.utils.representationr   r   r   r   rD   r]   ri   rt   r   r   r   r   r   <module>r      sv      6 5 :/
 /
dT
 T
n; ;:
 :
z43 4
44 
4
43 
4r   