o
    `ZhJ                     @   s   d dl Z d dlmZ d dlmZ d dlmZ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 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)
FieldErrorValidationError)connections)ExistsExpressionListFOrderBy)IndexExpression)Exact)Q)Query)DEFAULT_DB_ALIAS)RemovedInDjango60Warning)gettext_lazy)BaseConstraintCheckConstraint
DeferrableUniqueConstraintc                   @   sx   e Zd Ze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defddZdd Zdd Zdd ZdS )r   u&   Constraint “%(name)s” is violated.Nnameviolation_error_codeviolation_error_messagec                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(   S/var/www/html/lang_env/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_expressions8   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   r-   r.   r/   r1   r(   r(   r)   
create_sql?   r5   zBaseConstraint.create_sqlc                 C   r-   r.   r/   r1   r(   r(   r)   
remove_sqlB   r5   zBaseConstraint.remove_sqlc                 C   r-   r.   r/   )r$   r2   instanceexcludeusingr(   r(   r)   validateE   r5   zBaseConstraint.validatec                 C   s   | j d| ji S )Nr   )r   r   r+   r(   r(   r)   get_violation_error_messageH   s   z*BaseConstraint.get_violation_error_messagec                 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)   deconstructK   s   





zBaseConstraint.deconstructc                 C   s   |   \}}}| j|i |S N)rA   r   )r$   _r%   r@   r(   r(   r)   cloneX   s   zBaseConstraint.clone)r   r=   __qualname__rC   r   r   r   r*   propertyr,   r4   r6   r7   r   r;   r<   rA   rD   r(   r(   r(   r)   r      s    
r   c                       sr   e Zd Zddd f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)r   r   c                   s0   || _ t|ddstdt j|||d d S )NZconditionalFzACheckConstraint.check must be a Q instance or boolean expression.r   )checkgetattrr   superr*   )r$   rG   r   r   r   r   r(   r)   r*   ^   s   
zCheckConstraint.__init__c                    sR   t |dd}|| j}|j jd}|| j\}}|t fdd|D  S )NFr2   
alias_cols
connectionc                 3       | ]}  |V  qd S rB   Zquote_value.0pr3   r(   r)   	<genexpr>q       z1CheckConstraint._get_check_sql.<locals>.<genexpr>)r   build_whererG   get_compilerrN   as_sqltupler$   r2   r3   querywherecompilersqlparamsr(   rT   r)   _get_check_sqll   s
   zCheckConstraint._get_check_sqlc                 C   s   |  ||}|| j|S rB   )ra   Z
_check_sqlr   r$   r2   r3   rG   r(   r(   r)   r4   s   s   zCheckConstraint.constraint_sqlc                 C   s   |  ||}||| j|S rB   )ra   Z_create_check_sqlr   rb   r(   r(   r)   r6   w   s   zCheckConstraint.create_sqlc                 C   s   | || jS rB   )Z_delete_check_sqlr   r1   r(   r(   r)   r7   {   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metar9   r:   code)_get_field_value_map_metar   rG   r   r<   r   r   )r$   r2   r8   r9   r:   againstr(   r(   r)   r;   ~   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: check=%s name=%s%s%s>  violation_error_code=%r violation_error_message=%r)r   rE   rG   reprr   r   r   r   r+   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 rB   )
isinstancer   r   rG   r   r   rI   __eq__r$   otherrJ   r(   r)   rq      s   



zCheckConstraint.__eq__c                    s$   t   \}}}| j|d< |||fS )NrG   )rI   rA   rG   r$   r?   r%   r@   rJ   r(   r)   rA      s   

zCheckConstraint.deconstruct)r   r=   rE   r*   ra   r4   r6   r7   r   r;   ro   rq   rA   __classcell__r(   r(   rJ   r)   r   ]   s    

r   c                   @   s   e Zd ZdZdZdd ZdS )r   deferredZ	immediatec                 C   s   | j j d| j S )N.)r   rE   _name_r+   r(   r(   r)   ro      s   zDeferrable.__repr__N)r   r=   rE   ZDEFERREDZ	IMMEDIATEro   r(   r(   r(   r)   r      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 fddZ fddZdefddZ  ZS )r   r(   N)	fieldsr   	condition
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 rB   )rp   strr	   )rR   
expressionr(   r(   r)   rU      s
    
z,UniqueConstraint.__init__.<locals>.<genexpr>r   )
ValueErrorrp   r   r   r   r   listrZ   boollenry   rz   r{   r|   r}   r~   expressionsrI   r*   )r$   ry   r   rz   r{   r|   r}   r~   r   r   r   rJ   r(   r)   r*      sh   


zUniqueConstraint.__init__c                 C   s
   t | jS rB   )r   r   r+   r(   r(   r)   r,      s   
z%UniqueConstraint.contains_expressionsc                    s`   | j d u rd S t|dd}|| j }|j jd}|| j\}}|t fdd|D  S )NFrK   rM   c                 3   rO   rB   rP   rQ   rT   r(   r)   rU     rV   z6UniqueConstraint._get_condition_sql.<locals>.<genexpr>)rz   r   rW   rX   rN   rY   rZ   r[   r(   rT   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)rL   )r   r   Zset_wrapper_classesrN   appendr   Zresolve_expressionr   )r$   r2   r3   Zindex_expressionsr   Z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(   ri   	get_fieldrR   
field_namer2   r(   r)   
<listcomp>      z3UniqueConstraint.constraint_sql.<locals>.<listcomp>c                       g | ]	} j |jqS r(   ri   r   columnr   r   r(   r)   r         rz   r{   r|   r}   r   r~   )	ry   r|   r   r   Z_unique_sqlr   r{   r}   r~   r$   r2   r3   ry   r|   rz   r   r(   r   r)   r4     "   
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   )	ry   r|   r   r   Z_create_unique_sqlr   r{   r}   r~   r   r(   r   r)   r6   &  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   Z_delete_unique_sqlr   r{   r}   r~   )r$   r2   r3   rz   r|   r   r(   r   r)   r7   9  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>rk   z
 fields=%sz expressions=%sz name=%sz condition=%sz deferrable=%rz include=%sz opclasses=%sz nulls_distinct=%rrl   rm   )r   rE   ry   rn   r   r   rz   r{   r|   r}   r~   r   r   r   r+   r(   r(   r)   ro   J  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 rB   )rp   r   r   ry   rz   r{   r|   r}   r   r~   r   r   rI   rq   rr   rJ   r(   r)   rq   f  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 )Nry   rz   r{   r|   r}   r~   )	rI   rA   ry   rz   r{   r|   r}   r~   r   rt   rJ   r(   r)   rA   v  s   






zUniqueConstraint.deconstructc                 C   s@  |j |}| jrJi }| jD ]2}|r||v r d S |j|}t||j}	| jdur.|	d u s8|	dkr;t| j	j
r; d S |	||j< q|jd
i |}n_|rz| jD ]*}
t|
drl|
 D ]}t|trj|j|v rj  d S qZqOt|
try|
j|v ry d S qOdd |j|j|d D }g }| jD ]}t|tr|j}|t||| q|j| }||j}|jjs|d ur|j|d}| js| r| jrt|   | j!d|" D ]\}}|D ]}|| u rt|#|| jqqd S d S |j|j|d}z| jt$|| j@ j%||d	rt|   | j!dW d S  t&y   Y d S w )NFrk   flattenc                 S   s   i | ]	\}}t ||qS r(   )r	   )rR   fieldvaluer(   r(   r)   
<dictcomp>  s    z-UniqueConstraint.validate.<locals>.<dictcomp>rc   )pkrf   re   r(   )'Z_default_managerr:   ry   ri   r   rH   Zattnamer~   r   featuresZ!interprets_empty_strings_as_nullsr   filterr   hasattrr   rp   r	   rh   itemsr
   r   r   r   Zreplace_expressionsZ_get_pk_val_stateZaddingr9   rz   existsr   r<   r   Zget_constraintsZunique_error_messager   rG   r   )r$   r2   r8   r9   r:   ZquerysetZlookup_kwargsr   r   Zlookup_valuer   exprZreplacementsr   Zmodel_class_pkconstraints
constraintrj   r(   r(   r)   r;     s   






zUniqueConstraint.validate)r   r=   rE   r*   rF   r,   r   r   r4   r6   r7   ro   rq   rA   r   r;   ru   r(   r(   rJ   r)   r      s,    F
	r   )#r    enumr   typesr   Zdjango.core.exceptionsr   r   Z	django.dbr   Zdjango.db.models.expressionsr   r   r	   r
   Zdjango.db.models.indexesr   Zdjango.db.models.lookupsr   Zdjango.db.models.query_utilsr   Zdjango.db.models.sql.queryr   Zdjango.db.utilsr   Zdjango.utils.deprecationr   Zdjango.utils.translationr   rC   __all__r   r   r   r   r(   r(   r(   r)   <module>   s$    JM	