o
    {,h%t                     @   s  d dl 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
 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 d dlmZ d dlmZ d dlm Z  d dl!m"Z# g 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 )    N)Enum)NoneType)checks)FieldDoesNotExist
FieldErrorValidationError)connections)
LOOKUP_SEP)ExistsExpressionListFRawSQL)IndexExpression)ExactIsNull)Q)Query)DEFAULT_DB_ALIAS)RemovedInDjango60Warning)gettext_lazy)BaseConstraintCheckConstraint
DeferrableUniqueConstraintc                   @   s   e Zd ZedZdZdZdZddddddZe	dd Z
d	d
 Zdd Zdd Zedd ZdefddZdd Zdd Zdd Zdd Zdd ZdS )r   u&   Constraint “%(name)s” is violated.N)violation_error_codeviolation_error_messagenamer   r   c                G   s   |d u r|st | jj d|| _|d ur|| _|d ur!|| _n| j| _|rItjd| jj dt	dd t
|ddgD ]\}}|rHt| || q<d S d S )Nz<.__init__() missing 1 required keyword-only argument: 'name'z Passing positional arguments to z is deprecated.   
stacklevelr   r   )	TypeError	__class____name__r   r   r   default_violation_error_messagewarningswarnr   zipsetattr)selfr   r   r   argsargattr r-   R/var/www/html/rh/venv/lib/python3.10/site-packages/django/db/models/constraints.py__init__    s,   zBaseConstraint.__init__c                 C   s   dS )NFr-   r)   r-   r-   r.   contains_expressions<   s   z#BaseConstraint.contains_expressionsc                 C      t dNz.This method must be implemented by a subclass.NotImplementedErrorr)   modelschema_editorr-   r-   r.   constraint_sql@      zBaseConstraint.constraint_sqlc                 C   r2   r3   r4   r6   r-   r-   r.   
create_sqlC   r:   zBaseConstraint.create_sqlc                 C   r2   r3   r4   r6   r-   r-   r.   
remove_sqlF   r:   zBaseConstraint.remove_sqlc                 C   sR   |j j}||D ]^}}||v r dS ||}|jr&| ||j|r& dS q	dS )NTF)_meta	get_field_get_expr_references	generated_expression_refs_exclude
expression)clsr7   rB   excluder>   
field_name__fieldr-   r-   r.   rA   I   s   
z'BaseConstraint._expression_refs_excludec                 C   r2   r3   r4   )r)   r7   instancerD   usingr-   r-   r.   validateV   r:   zBaseConstraint.validatec                 C   s   | j d| ji S )Nr   )r   r   r0   r-   r-   r.   get_violation_error_messageY   s   z*BaseConstraint.get_violation_error_messagec                 C   s   g S Nr-   )r)   r7   
connectionr-   r-   r.   _check\   s   zBaseConstraint._checkc           
   	   C   s
  ddl m} g }t }|D ]l^}}|dkst|jj|r!|| |s$qz|dkr.|jj}n|j|}|jr=|j	s=|j
r?W qW n	 tyI   Y qw |d }	t|dryt|dry||	d u ry||	d u ry|tjdt|g|  |dd q|||d	 |S )
Nr   )CompositePrimaryKeypkget_transform
get_lookupz.'constraints' refers to the joined field '%s'.zmodels.E041)objidconstraints)!django.db.models.fields.compositerO   set
isinstancer=   rP   addr>   is_relationmany_to_manyone_to_manyr   hasattrrQ   rR   appendr   Errorr	   joinextend_check_local_fields)
r)   r7   
referencesrO   errorsfieldsrE   lookupsrG   first_lookupr-   r-   r.   _check_references_   sJ   

z BaseConstraint._check_referencesc                 C   sh   d| j j| j jf }|dd}d| ji}| jd ur%| j| jkr%| j|d< | jd ur/| j|d< |d|fS )Nz%s.%szdjango.db.models.constraintszdjango.db.modelsr   r   r   r-   )r"   
__module__r#   replacer   r   r$   r   )r)   pathkwargsr-   r-   r.   deconstruct   s   





zBaseConstraint.deconstructc                 C   s   |   \}}}| j|i |S rL   )rm   r"   )r)   _r*   rl   r-   r-   r.   clone   s   zBaseConstraint.clone)r#   ri   __qualname__rn   r$   r   r   non_db_attrsr/   propertyr1   r9   r;   r<   classmethodrA   r   rJ   rK   rN   rh   rm   ro   r-   r-   r-   r.   r      s(    

(r   c                       s   e Zd Zddddd fdd
Zdd Zdd ZeeeZd	d
 Zdd Z	dd Z
dd Zdd ZdefddZdd Z fddZ fddZ  ZS )r   N)	conditioncheckr   r   c                   sL   |d urt jdtdd |}|| _t|ddstdt j|||d d S )N=CheckConstraint.check is deprecated in favor of `.condition`.r   r   conditionalFzECheckConstraint.condition must be a Q instance or boolean expression.r   )r%   r&   r   rt   getattrr!   superr/   )r)   r   rt   ru   r   r   r"   r-   r.   r/      s"   	
zCheckConstraint.__init__c                 C   s   t jdtdd | jS Nrv   r   r   r%   r&   r   rt   r0   r-   r-   r.   
_get_check   s   zCheckConstraint._get_checkc                 C   s   t jdtdd || _d S r{   r|   )r)   valuer-   r-   r.   
_set_check   s   
zCheckConstraint._set_checkc                 C   s   g }|j jsd|jjv s|tj|j dd|dd |S |j js(d|jjvr`t }| j	}t
|tr;||| tdd | D rW|tjd| jd	d
|dd || || |S )N supports_table_check_constraintsz$ does not support check constraints.OA constraint won't be created. Silence this warning if you don't care about it.zmodels.W027hintrS   rT   c                 s   s    | ]}t |tV  qd S rL   )rX   r   ).0exprr-   r-   r.   	<genexpr>       z)CheckConstraint._check.<locals>.<genexpr>zCheck constraint zS contains RawSQL() expression and won't be validated during the model full_clean().z0Silence this warning if you don't care about it.zmodels.W045)featuresr   r=   required_db_featuresr^   r   Warningdisplay_namerW   rt   rX   r   updater?   anyflattenr   ra   rh   )r)   r7   rM   rd   rc   rt   r-   r-   r.   rN      sB   

	zCheckConstraint._checkc                    sR   t |dd}|| j}|j jd}|| j\}}|t fdd|D  S )NFr7   
alias_colsrM   c                 3       | ]}  |V  qd S rL   quote_valuer   pr8   r-   r.   r      r   z1CheckConstraint._get_check_sql.<locals>.<genexpr>)r   build_wherert   get_compilerrM   as_sqltupler)   r7   r8   querywherecompilersqlparamsr-   r   r.   _get_check_sql   s
   zCheckConstraint._get_check_sqlc                 C   s   |  ||}|| j|S rL   )r   
_check_sqlr   r)   r7   r8   ru   r-   r-   r.   r9      s   zCheckConstraint.constraint_sqlc                 C   s   |  ||}||| j|S rL   )r   _create_check_sqlr   r   r-   r-   r.   r;      s   zCheckConstraint.create_sqlc                 C   s   | || jS rL   )_delete_check_sqlr   r6   r-   r-   r.   r<     s   zCheckConstraint.remove_sqlc                 C   sR   |j |j|d}zt| jj||dst|  | jdW d S  ty(   Y d S w )NmetarD   rI   code)	_get_field_expression_mapr=   r   rt   ru   r   rK   r   r   )r)   r7   rH   rD   rI   againstr-   r-   r.   rJ     s   
zCheckConstraint.validatec                 C   sV   d| j j| jt| j| jd u rdnd| j | jd u s | j| jkr$df S d| j f S )Nz<%s: condition=%s name=%s%s%s>  violation_error_code=%r violation_error_message=%r)r"   rp   rt   reprr   r   r   r$   r0   r-   r-   r.   __repr__  s   

zCheckConstraint.__repr__c                    sF   t |tr| j|jko| j|jko| j|jko| j|jkS t |S rL   )rX   r   r   rt   r   r   ry   __eq__r)   otherrz   r-   r.   r      s   



zCheckConstraint.__eq__c                    s$   t   \}}}| j|d< |||fS )Nrt   )ry   rm   rt   r)   rk   r*   rl   rz   r-   r.   rm   *  s   

zCheckConstraint.deconstruct)r#   ri   rp   r/   r}   r   rr   ru   rN   r   r9   r;   r<   r   rJ   r   r   rm   __classcell__r-   r-   rz   r.   r      s$    	
'

r   c                   @   s   e Zd ZdZdZdd ZdS )r   deferred	immediatec                 C   s   | j j d| j S )N.)r"   rp   _name_r0   r-   r-   r.   r   5  s   zDeferrable.__repr__N)r#   ri   rp   DEFERRED	IMMEDIATEr   r-   r-   r-   r.   r   0  s    r   c                
       s   e Zd Zdddddddddd	 fdd
Zedd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Z fddZ fddZdefddZ  ZS )r   r-   N)	re   r   rt   
deferrableinclude	opclassesnulls_distinctr   r   c       	            sf  |st d|
s|st d|
r|rt dt|ttfs!t d|r)|r)t d|r1|r1t d|r9|r9t d|
rA|rAt d|
rI|rIt d	t|ttfsTtd
t|tttfs`tdt|ttfsktdt|ttfsvtd|rt	|t	|krt dt|| _
|| _|| _|rt|nd| _|| _|| _tdd |
D | _t j|||	d d S )Nz"A unique constraint must be named.zKAt least one field or expression is required to define a unique constraint.z?UniqueConstraint.fields and expressions are mutually exclusive.z0UniqueConstraint.condition must be a Q instance.z4UniqueConstraint with conditions cannot be deferred.z8UniqueConstraint with include fields cannot be deferred.z3UniqueConstraint with opclasses cannot be deferred.z5UniqueConstraint with expressions cannot be deferred.zrUniqueConstraint.opclasses cannot be used with expressions. Use django.contrib.postgres.indexes.OpClass() instead.z:UniqueConstraint.deferrable must be a Deferrable instance.z1UniqueConstraint.include must be a list or tuple.z3UniqueConstraint.opclasses must be a list or tuple.z/UniqueConstraint.nulls_distinct must be a bool.z]UniqueConstraint.fields and UniqueConstraint.opclasses must have the same number of elements.r-   c                 s   s&    | ]}t |trt|n|V  qd S rL   )rX   strr   r   rB   r-   r-   r.   r   v  s
    
z,UniqueConstraint.__init__.<locals>.<genexpr>r   )
ValueErrorrX   r   r   r   r!   listr   boollenre   rt   r   r   r   r   expressionsry   r/   )r)   re   r   rt   r   r   r   r   r   r   r   rz   r-   r.   r/   :  sh   


zUniqueConstraint.__init__c                 C   s
   t | jS rL   )r   r   r0   r-   r-   r.   r1     s   
z%UniqueConstraint.contains_expressionsc                 C   s  | h | j| jd}|jj}| jd ur-|jjs-d|v s-|t	j
|j dd|dd | jd urJ|jjsJd|v sJ|t	j
|j dd|d	d | jre|jjsed
|v se|t	j
|j dd|dd | jr|jjsd|v s|t	j
|j dd|dd | jd ur|jjsd|v s|t	j
|j dd|dd t }|jjsd|vrt| jtr||| j | jr|jjsd|vr| jD ]
}||| q|| || |S )NrU   supports_partial_indexesz5 does not support unique constraints with conditions.r   zmodels.W036r   &supports_deferrable_unique_constraintsz0 does not support deferrable unique constraints.zmodels.W038supports_covering_indexesz: does not support unique constraints with non-key columns.zmodels.W039supports_expression_indexesz4 does not support unique constraints on expressions.zmodels.W044*supports_nulls_distinct_unique_constraintsz9 does not support unique constraints with nulls distinct.zmodels.W047)rb   re   r   r=   r   rt   r   r   r^   r   r   r   r   r   r   r1   r   r   r   rW   rX   r   r   r?   r   ra   rh   )r)   r7   rM   rd   r   rc   rB   r-   r-   r.   rN     s   









zUniqueConstraint._checkc                    s`   | j d u rd S t|dd}|| j }|j jd}|| j\}}|t fdd|D  S )NFr   r   c                 3   r   rL   r   r   r   r-   r.   r     r   z6UniqueConstraint._get_condition_sql.<locals>.<genexpr>)rt   r   r   r   rM   r   r   r   r-   r   r.   _get_condition_sql  s   
z#UniqueConstraint._get_condition_sqlc                 C   sN   | j sd S g }| j D ]}t|}||j || q
t| t|ddS )NF)r   )r   r   set_wrapper_classesrM   r^   r   resolve_expressionr   )r)   r7   r8   index_expressionsrB   index_expressionr-   r-   r.   _get_index_expressions  s   

z'UniqueConstraint._get_index_expressionsc                    d    fdd| j D } fdd| jD }|  |}|  |}|j || j|| j|| j|| jd	S )Nc                       g | ]} j |qS r-   r=   r>   r   rE   r7   r-   r.   
<listcomp>      z3UniqueConstraint.constraint_sql.<locals>.<listcomp>c                       g | ]	} j |jqS r-   r=   r>   columnr   r   r-   r.   r         rt   r   r   r   r   r   )	re   r   r   r   _unique_sqlr   r   r   r   r)   r7   r8   re   r   rt   r   r-   r   r.   r9     "   
zUniqueConstraint.constraint_sqlc                    r   )Nc                    r   r-   r   r   r   r-   r.   r     r   z/UniqueConstraint.create_sql.<locals>.<listcomp>c                    r   r-   r   r   r   r-   r.   r     r   r   )	re   r   r   r   _create_unique_sqlr   r   r   r   r   r-   r   r.   r;     r   zUniqueConstraint.create_sqlc              
      sN   |   |} fdd| jD }|  |}|j | j|| j|| j|| jdS )Nc                    r   r-   r   r   r   r-   r.   r   #  r   z/UniqueConstraint.remove_sql.<locals>.<listcomp>r   )r   r   r   _delete_unique_sqlr   r   r   r   )r)   r7   r8   rt   r   r   r-   r   r.   r<   !  s   
zUniqueConstraint.remove_sqlc                 C   s   d| j j| js	dndt| j | jsdndt| j dt| j | jd u r'dnd| j | jd u r2dnd| j | js;dndt| j | j	sFdnd	t| j	 | j
d u rSdnd
| j
 | jd u r^dnd| j | jd u sm| j| jkrqdf S d| j f S )Nz<%s:%s%s%s%s%s%s%s%s%s%s>r   z
 fields=%sz expressions=%sz name=%sz condition=%sz deferrable=%rz include=%sz opclasses=%sz nulls_distinct=%rr   r   )r"   rp   re   r   r   r   rt   r   r   r   r   r   r   r$   r0   r-   r-   r.   r   2  s*   


zUniqueConstraint.__repr__c                    s   t |trA| j|jko@| j|jko@| j|jko@| j|jko@| j|jko@| j|jko@| j|jko@| j	|j	u o@| j
|j
ko@| j|jkS t |S rL   )rX   r   r   re   rt   r   r   r   r   r   r   r   ry   r   r   rz   r-   r.   r   N  s*   









	zUniqueConstraint.__eq__c                    s   t   \}}}| jr| j|d< | jr| j|d< | jr | j|d< | jr(| j|d< | jr0| j|d< | jd ur:| j|d< || j|fS )Nre   rt   r   r   r   r   )	ry   rm   re   rt   r   r   r   r   r   r   rz   r-   r.   rm   ^  s   






zUniqueConstraint.deconstructc                    s  j |}jri }g }jD ]I} r| v r d S j|}	|	jr8 r1|	j r1 d S ||	j	 qt
||	j}
jdurG|
d u sQ|
dkrTt| jjrT d S |
||	j	< qg }|r|jj d}|D ]+}|| }jdu rt|}tt||tt|dt|dB }|| qh|||< qh|j|i |}nZ rt fddjD rd S dd |jj d D }g }jD ].}t|d	r| }||}t||}jdu rt|tt|dt|dB }|| q|j| }|j}|jjs|jr|j |d
}j!s<|" r:jr1j#j$kr1|%j}t&||j'dt&( j)dd S |jj d}zj!t*|j!@ j+||dr_t&( j)dW d S  t,yl   Y d S w )NFr   r   Tc                 3   s    | ]
} | V  qd S rL   )rA   r   rD   r7   r)   r-   r.   r     s
    
z,UniqueConstraint.validate.<locals>.<genexpr>c                 S   s   i | ]	\}}t ||qS r-   )r   )r   rG   r~   r-   r-   r.   
<dictcomp>  s    z-UniqueConstraint.validate.<locals>.<dictcomp>get_expression_for_validation)rP   r   r   )-_default_managerrI   re   r=   r>   r@   rA   rB   r^   r   rx   attnamer   r   r   !interprets_empty_strings_as_nullsr   r   r   r   r   filterr   r   itemsr]   r   replace_expressions_get_pk_val_stateadding
_is_pk_setrD   rt   existsr   r$   unique_error_messager   r   rK   r   r
   ru   r   )r)   r7   rH   rD   rI   querysetlookup_kwargsgenerated_field_namesrE   rG   lookup_valuelookup_argsfield_expression_maprB   lhsrt   replacementsfiltersr   rhsmodel_class_pkvalidation_error_messager   r-   r   r.   rJ   n  s   










zUniqueConstraint.validate)r#   ri   rp   r/   rr   r1   rN   r   r   r9   r;   r<   r   r   rm   r   rJ   r   r-   r-   rz   r.   r   9  s.    F
b	r   ))r%   enumr   typesr   django.corer   django.core.exceptionsr   r   r   	django.dbr   django.db.models.constantsr	   django.db.models.expressionsr
   r   r   r   django.db.models.indexesr   django.db.models.lookupsr   r   django.db.models.query_utilsr   django.db.models.sql.queryr   django.db.utilsr   django.utils.deprecationr   django.utils.translationr   rn   __all__r   r   r   r   r-   r-   r-   r.   <module>   s,      	