
    rh3                         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 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.
    )
FieldError)	DataError)Exists)gettext_lazy)ValidationError)
smart_reprc                 X    	 | j                         S # t        t        t        f$ r Y yw xY wNF)exists	TypeError
ValueErrorr   )querysets    l/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/rest_framework/validators.py	qs_existsr      s-      z9- s    ))c                     |t        |       S 	 |t        | j                  |            z  j                  |      S # t        t
        t        t        f$ r Y yw xY wr
   )r   r   filtercheckr   r   r   r   )r   	conditionagainsts      r   qs_exists_with_conditionr      sY    "" F8??9#=>>EEgNNz9j9 s   +; AAc                 |    	  | j                   di |S # t        t        t        f$ r | j	                         cY S w xY w)N )r   r   r   r   none)r   kwargss     r   	qs_filterr   '   s=    x(((z9- }}s    $;;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)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__7   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_instanceC   '    
 ##x{{#33r$   c                     |j                   d   }t        |j                  dd       }| j                  }| j	                  |||      }| j                  ||      }t        |      rt        | j                  d      y )Nr0   uniquecode)	source_attrsgetattrparentr   r*   r1   r   r   r    )r"   r'   serializer_fieldr(   r0   r   s         r   __call__zUniqueValidator.__call__L   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__Y   s%    NN##t}}%
 	
r$   c                    t        || j                        st        S | j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j
                  |j
                  k(  xr | j                  |j                  k(  S r   )
isinstancerA   NotImplementedr    requires_contextr   r!   r"   others     r   __eq__zUniqueValidator.__eq___   sp    %0!!- 0))U-C-CC0MMU^^30 KK5<</	r$   )Nexact)rB   
__module____qualname____doc___r    rH   r#   r*   r1   r<   rD   rK   r   r$   r   r   r   .   s8    
 ,-G
4?
r$   r   c                   `    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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                 n    || _         || _        |xs | j                  | _        |g n|| _        || _        y r   )r   fieldsr    condition_fieldsr   )r"   r   rU   r    rV   r   s         r   r#   z UniqueTogetherValidator.__init__s   s7     .$,,&6&>DT"r$   c                     |j                   yg | j                  | 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requiredr6   )r0   rU   rV   sourcemissing_messager   )r"   attrs
serializerr(   missing_itemss        r   enforce_required_fieldsz/UniqueTogetherValidator.enforce_required_fieldsz   s    
 * EDd.C.CD
  ,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.
        )rU   rY   r0   r9   r   )r"   r[   r   r\   r(   sourcesrY   r)   s           r   r*   z'UniqueTogetherValidator.filter_queryset   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"   r[   r   r0   s       r   r1   z0UniqueTogetherValidator.exclude_current_instance   r2   r$   c                    | j                  |       | j                  }| j                  ||      }| j                  ||j                        }| j
                  D cg c]  }j
                  |   j                   }}j                  |D cg c]  }||   	 }}n.|D cg c]#  }||   t        j                  |      k7  r||   % }}fd| j                  D        }|D ci c]  }|||   
 }	}|rad |vr\t        || j                  |	      rDdj                  | j
                        }
| j                  j                  |
      }t        |d      y y y c c}w c c}w c c}w c c}w )Nc              3   P   K   | ]  }j                   |   j                    y wr   )rU   rY   ).0r(   r\   s     r   	<genexpr>z3UniqueTogetherValidator.__call__.<locals>.<genexpr>   s"     jjZ..z:AAjs   #&, )field_namesr5   r6   )r^   r   r*   r1   r0   rU   rY   r9   rV   r   r   joinr    formatr   )r"   r[   r\   r   r(   checked_nameschecked_valuescondition_sourcesrY   condition_kwargsrh   r    s     `         r   r<   z UniqueTogetherValidator.__call__   s   $$UJ7==''xD00*BUBUV DH;;
5?Jj)00
 
 &BOPJeJ/PNP
 #0$
0C0CZ(PP j!N  kTXTiTij@QRfFE&M1RRd.8=UV^`d`n`n  qA  >B))DKK0Kll))k)BG!'99 >B8>!

 Q Ss    EE#(E#&E(c                      dj                   j                  j                  dj                   fddD                    S )Nz<{}({})>rg   c           	   3   j   K   | ]*  }t        |      | dt        t        |              , y w)N=)r9   r   )re   attrr"   s     r   rf   z3UniqueTogetherValidator.__repr__.<locals>.<genexpr>   s>      44&2 &*WT4%89:;4s   03)r   rU   r   )rj   rA   rB   ri   rC   s   `r   rD   z UniqueTogetherValidator.__repr__   s<      NN##II 4?4 4
 	
r$   c                 D   t        || j                        st        S | j                  |j                  k(  xrj | j                  |j                  k(  xrO | j
                  |j
                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   )rF   rA   rG   r    rH   rZ   r   rU   rI   s     r   rK   zUniqueTogetherValidator.__eq__   s    %0!!- 0))U-C-CC0((E,A,AA0 MMU^^30 KK5<</		r$   )NNN)rB   rM   rN   rO   rP   r    rZ   rH   r#   r^   r*   r1   r<   rD   rK   r   r$   r   rR   rR   i   sI    
 BCG12O#B 40:6
r$   rR   c                   ,    e Zd Z ed      Z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)re   chs     r   rf   z@ProhibitSurrogateCharactersValidator.__call__.<locals>.<genexpr>   s*      $D2'-R'BF'B %' $Ds   #%)
code_pointr6   )strr    rj   ry   r   r7   )r"   r'   surrogate_characterr    s       r   r<   z-ProhibitSurrogateCharactersValidator.__call__   sO    $DU $D 	;ll))S9L5M)NG!'		::	;r$   c                     t        || j                        st        S | j                  |j                  k(  xr | j                  |j                  k(  S r   )rF   rA   rG   r    r7   rI   s     r   rK   z+ProhibitSurrogateCharactersValidator.__eq__   s@    %0!!- ,II+	r$   N)rB   rM   rN   rP   r    r7   r<   rK   r   r$   r   ru   ru      s    IJG-D;r$   ru   c                   P    e Zd ZdZ ed      ZdZddZd Zd Z	d Z
d Zd	 Zd
 Zy)BaseUniqueForValidatorNrS   Tc                 X    || _         || _        || _        |xs | j                  | _        y r   )r   field
date_fieldr    )r"   r   r   r   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.
        rX   r6   N)r   r   rZ   r   )r"   r[   r(   r]   s       r   r^   z.BaseUniqueForValidator.enforce_required_fields   s]      $zz4??;
& ,,,
 

 !-jAA 
s   Ac                     t        d      )Nz&`filter_queryset` must be implemented.)NotImplementedError)r"   r[   r   r(   date_field_names        r   r*   z&BaseUniqueForValidator.filter_queryset  s    !"JKKr$   c                 B    ||j                  |j                        S |S r,   r.   rb   s       r   r1   z/BaseUniqueForValidator.exclude_current_instance  r2   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 )Nr4   )r   r5   r6   )rU   r   r8   r   r^   r   r*   r1   r0   r   r    rj   r   )r"   r[   r\   r(   r   r   r    s          r   r<   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                 z   t        || j                        st        S | j                  |j                  k(  xr | j                  |j                  k(  xrj | j
                  |j
                  k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   )	rF   rA   rG   r    rZ   rH   r   r   r   rI   s     r   rK   zBaseUniqueForValidator.__eq__   s    %0!!- 8((E,A,AA8))U-C-CC8 MMU^^38 JJ%++-	8
 OOu'7'77	r$   c           
          d| j                   j                  dt        | j                        dt        | j                        dt        | j
                        d	S )Nr>   r?   z, field=z, date_field=r@   )rA   rB   r   r   r   r   rC   s    r   rD   zBaseUniqueForValidator.__repr__+  s=    NN##t}}%tzz"t'	
 	
r$   r   )rB   rM   rN   r    rP   rZ   rH   r#   r^   r*   r1   r<   rK   rD   r   r$   r   r   r      s=    G12O/BL 	
r$   r   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)r   r   daymonthyearr   r"   r[   r   r(   r   r'   dater)   s           r   r*   z&UniqueForDateValidator.filter_queryset7  sv    djj!T__%$)j!59XXi/127;zzkO346:iij?233]33r$   NrB   rM   rN   rP   r    r*   r   r$   r   r   r   4  s    HIG	4r$   r   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 )Nr   )r   r   r   r   r   s           r   r*   z'UniqueForMonthValidator.filter_querysetF  sP    djj!T__%$)j!7;zzkO343]33r$   Nr   r   r$   r   r   r   C  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 )Nr   )r   r   r   r   r   s           r   r*   z&UniqueForYearValidator.filter_querysetS  sP    djj!T__%$)j!6:iij?233]33r$   Nr   r   r$   r   r   r   P  s    HIG4r$   r   N)rO   django.core.exceptionsr   	django.dbr   django.db.modelsr   django.utils.translationr   rP   rest_framework.exceptionsr   #rest_framework.utils.representationr   r   r   r   r   rR   ru   r   r   r   r   r   r$   r   <module>r      s    .  # 6 5 :8 8vn nb $E
 E
P43 4
44 
4
43 
4r$   