a
    ‘ÐOc5;  ã                   @   sÎ   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
 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 )é    )ÚEnum)Ú
FieldErrorÚValidationError)Úconnections)ÚExistsÚExpressionListÚF)ÚIndexExpression)ÚExact)ÚQ)ÚQuery)ÚDEFAULT_DB_ALIAS)Úgettext_lazy)ÚBaseConstraintÚCheckConstraintÚ
DeferrableÚUniqueConstraintc                   @   sl   e Zd ZedƒZdZ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.Nc                 C   s"   || _ |d ur|| _n| j| _d S ©N)ÚnameÚviolation_error_messageÚdefault_violation_error_message)Úselfr   r   © r   úS/var/www/html/gsl.tools/lib/python3.9/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   s   t dƒ‚d S ©Nz.This method must be implemented by a subclass.©ÚNotImplementedError©r   ÚmodelÚschema_editorr   r   r   Úconstraint_sql   s    zBaseConstraint.constraint_sqlc                 C   s   t dƒ‚d S r   r   r    r   r   r   Ú
create_sql"   s    zBaseConstraint.create_sqlc                 C   s   t dƒ‚d S r   r   r    r   r   r   Ú
remove_sql%   s    zBaseConstraint.remove_sqlc                 C   s   t dƒ‚d S r   r   )r   r!   ÚinstanceÚexcludeÚusingr   r   r   Úvalidate(   s    zBaseConstraint.validatec                 C   s   | j d| ji S )Nr   )r   r   r   r   r   r   Úget_violation_error_message+   s    z*BaseConstraint.get_violation_error_messagec                 C   sT   d| j j| j jf }| dd¡}d| ji}| jd urJ| j| jkrJ| j|d< |d|fS )Nz%s.%szdjango.db.models.constraintszdjango.db.modelsr   r   r   )Ú	__class__Ú
__module__Ú__name__Úreplacer   r   r   )r   ÚpathÚkwargsr   r   r   Údeconstruct.   s    
ÿ
þ
zBaseConstraint.deconstructc                 C   s   |   ¡ \}}}| j|i |¤ŽS r   )r1   r+   )r   Ú_Úargsr0   r   r   r   Úclone9   s    zBaseConstraint.clone)N)r-   r,   Ú__qualname__r2   r   r   r   Úpropertyr   r#   r$   r%   r   r)   r*   r1   r4   r   r   r   r   r      s   

r   c                       sp   e Zd Z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   c                   s.   || _ t|ddƒstdƒ‚tƒ j||d� d S )NZconditionalFzACheckConstraint.check must be a Q instance or boolean expression.r7   )ÚcheckÚgetattrÚ	TypeErrorÚsuperr   )r   r8   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©r!   Ú
alias_cols©Ú
connectionc                 3   s   | ]}ˆ   |¡V  qd S r   ©Zquote_value©Ú.0Úp©r"   r   r   Ú	<genexpr>L   ó    z1CheckConstraint._get_check_sql.<locals>.<genexpr>)r   Úbuild_wherer8   Úget_compilerr@   Úas_sqlÚtuple©r   r!   r"   ÚqueryÚwhereÚcompilerZsqlÚparamsr   rE   r   Ú_get_check_sqlG   s
    zCheckConstraint._get_check_sqlc                 C   s   |   ||¡}| | j|¡S r   )rQ   Z
_check_sqlr   ©r   r!   r"   r8   r   r   r   r#   N   s    zCheckConstraint.constraint_sqlc                 C   s   |   ||¡}| || j|¡S r   )rQ   Z_create_check_sqlr   rR   r   r   r   r$   R   s    zCheckConstraint.create_sqlc                 C   s   |  || j¡S r   )Z_delete_check_sqlr   r    r   r   r   r%   V   s    zCheckConstraint.remove_sqlc                 C   sL   |j |j|d�}z$t| jƒj||d�s2t|  ¡ ƒ‚W n tyF   Y n0 d S )N©Úmetar'   ©r(   )Ú_get_field_value_mapÚ_metar   r8   r   r*   r   )r   r!   r&   r'   r(   Úagainstr   r   r   r)   Y   s    zCheckConstraint.validatec                 C   s   d| j j| jt| jƒf S )Nz<%s: check=%s name=%s>)r+   r5   r8   Úreprr   r   r   r   r   Ú__repr__a   s
    ýzCheckConstraint.__repr__c                    s:   t |tƒr.| j|jko,| j|jko,| j|jkS tƒ  |¡S r   )Ú
isinstancer   r   r8   r   r;   Ú__eq__©r   Úotherr<   r   r   r\   h   s    

ÿ
ýzCheckConstraint.__eq__c                    s$   t ƒ  ¡ \}}}| j|d< |||fS )Nr8   )r;   r1   r8   ©r   r/   r3   r0   r<   r   r   r1   q   s    
zCheckConstraint.deconstruct)r-   r,   r5   r   rQ   r#   r$   r%   r   r)   rZ   r\   r1   Ú__classcell__r   r   r<   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+   r5   Ú_name_r   r   r   r   rZ   |   s    zDeferrable.__repr__N)r-   r,   r5   ZDEFERREDZ	IMMEDIATErZ   r   r   r   r   r   w   s   r   c                       s�   e Zd Z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Ú	opclassesr   c          	         sV  |st dƒ‚|s|st dƒ‚|r,|r,t dƒ‚t|td ƒtfƒsFt dƒ‚|rV|rVt dƒ‚|rf|rft dƒ‚|rv|rvt dƒ‚|r†|r†t dƒ‚|r–|r–t d	ƒ‚t|td ƒtfƒs°t d
ƒ‚t|td ƒttfƒsÌt dƒ‚t|ttfƒsât dƒ‚|rþt|ƒt|ƒkrþt dƒ‚t|ƒ| _|| _	|| _
|�r"t|ƒ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.fields and UniqueConstraint.opclasses must have the same number of elements.r   c                 s   s$   | ]}t |tƒrt|ƒn|V  qd S r   )r[   Ústrr   )rC   Ú
expressionr   r   r   rF   ¸   s   ÿz,UniqueConstraint.__init__.<locals>.<genexpr>r7   )Ú
ValueErrorr[   Útyper   r   ÚlistrK   Úlenrd   re   rf   rg   rh   Úexpressionsr;   r   )	r   rd   r   re   rf   rg   rh   r   ro   r<   r   r   r   �   sZ    ÿÿÿÿÿ
þ
zUniqueConstraint.__init__c                 C   s
   t | jƒS r   )Úboolro   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 )NFr=   r?   c                 3   s   | ]}ˆ   |¡V  qd S r   rA   rB   rE   r   r   rF   É   rG   z6UniqueConstraint._get_condition_sql.<locals>.<genexpr>)re   r   rH   rI   r@   rJ   rK   rL   r   rE   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>   )ro   r	   Zset_wrapper_classesr@   Úappendr   Zresolve_expressionr   )r   r!   r"   Zindex_expressionsrj   Zindex_expressionr   r   r   Ú_get_index_expressionsË   s    

ÿz'UniqueConstraint._get_index_expressionsc              
      s`   ‡ fdd„| j D ƒ}‡ fdd„| jD ƒ}|  ˆ |¡}|  ˆ |¡}|jˆ || j|| j|| j|d�S )Nc                    s   g | ]}ˆ j  |¡‘qS r   ©rW   Ú	get_field©rC   Ú
field_name©r!   r   r   Ú
<listcomp>Ø   rG   z3UniqueConstraint.constraint_sql.<locals>.<listcomp>c                    s   g | ]}ˆ j  |¡j‘qS r   ©rW   ru   Úcolumnrv   rx   r   r   ry   Ù   s   ©re   rf   rg   rh   ro   )rd   rg   rq   rs   Z_unique_sqlr   rf   rh   ©r   r!   r"   rd   rg   re   ro   r   rx   r   r#   ×   s     
ÿøzUniqueConstraint.constraint_sqlc              
      s`   ‡ fdd„| j D ƒ}‡ fdd„| jD ƒ}|  ˆ |¡}|  ˆ |¡}|jˆ || j|| j|| j|d�S )Nc                    s   g | ]}ˆ j  |¡‘qS r   rt   rv   rx   r   r   ry   ê   rG   z/UniqueConstraint.create_sql.<locals>.<listcomp>c                    s   g | ]}ˆ j  |¡j‘qS r   rz   rv   rx   r   r   ry   ë   s   r|   )rd   rg   rq   rs   Z_create_unique_sqlr   rf   rh   r}   r   rx   r   r$   é   s     
ÿøzUniqueConstraint.create_sqlc              	      sJ   |   ˆ |¡}‡ fdd„| jD ƒ}|  ˆ |¡}|jˆ | j|| j|| j|d�S )Nc                    s   g | ]}ˆ j  |¡j‘qS r   rz   rv   rx   r   r   ry   ý   s   z/UniqueConstraint.remove_sql.<locals>.<listcomp>r|   )rq   rg   rs   Z_delete_unique_sqlr   rf   rh   )r   r!   r"   re   rg   ro   r   rx   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 rNdnd| j | jd u rddnd| j | jsvdndt| jƒ | j	sŒdnd	t| j	ƒ f S )
Nz<%s:%s%s%s%s%s%s%s>Ú z
 fields=%sz expressions=%sz name=%sz condition=%sz deferrable=%rz include=%sz opclasses=%s)
r+   r5   rd   rY   ro   r   re   rf   rg   rh   r   r   r   r   rZ     s    øzUniqueConstraint.__repr__c                    sv   t |tƒrj| j|jkoh| j|jkoh| j|jkoh| j|jkoh| j|jkoh| j|jkoh| j|jkoh| j	|j	kS t
ƒ  |¡S r   )r[   r   r   rd   re   rf   rg   rh   ro   r   r;   r\   r]   r<   r   r   r\     s"    

ÿ
þ
ý
ü
û
ú
ø
zUniqueConstraint.__eq__c                    sl   t ƒ  ¡ \}}}| jr | j|d< | jr0| j|d< | jr@| j|d< | jrP| j|d< | jr`| j|d< || j|fS )Nrd   re   rf   rg   rh   )r;   r1   rd   re   rf   rg   rh   ro   r_   r<   r   r   r1   %  s    




zUniqueConstraint.deconstructc                    s  |j  |¡}| jrŠi }| jD ]Z}|r2||v r2 d S |j |¡}t||jƒ}	|	d u sf|	dkrlt| jj	rl d S |	||j
< q|jf i |¤Ž}nŽ|rê| jD ]T}
t|
dƒrÎ|
 ¡ D ] }t|tƒrª|j
|v rª  d S qªq”t|
tƒr”|
j
|v r” d S q”|j|j|d�‰ ‡ fdd„| jD ƒ}|j|Ž }| |j¡}|jj�sD|d u�rD|j|d�}| j�s¨| ¡ �r | j�rjt|  ¡ ƒ‚| ¡ D ]2\}}|D ]"}|| u �r~t| || j¡ƒ‚�q~�qrnX|j|j|d�}z2| jt| | j¡ƒ@ j||d��rèt|  ¡ ƒ‚W n t�yþ   Y n0 d S )Nr~   ÚflattenrS   c                    s   g | ]}t || ˆ ¡ƒ‘qS r   )r
   Zreplace_references)rC   Úexpr©Zreplacement_mapr   r   ry   R  s   ÿz-UniqueConstraint.validate.<locals>.<listcomp>)ÚpkrU   )Z_default_managerr(   rd   rW   ru   r9   Zattnamer   ÚfeaturesZ!interprets_empty_strings_as_nullsr   Úfilterro   Úhasattrr   r[   r   rV   Z_get_pk_valÚ_stateZaddingr'   re   Úexistsr   r*   Zget_constraintsZunique_error_messager   r8   r   )r   r!   r&   r'   r(   ZquerysetZlookup_kwargsrw   ÚfieldZlookup_valuerj   r€   ro   Zmodel_class_pkÚconstraintsÚ
constraintrX   r   r�   r   r)   3  sh    
ÿ
þ

ÿ
þ


ÿÿzUniqueConstraint.validate)r-   r,   r5   r   r6   r   rq   rs   r#   r$   r%   rZ   r\   r1   r   r)   r`   r   r   r<   r   r   €   s&   ÷=
	r   N)Úenumr   Zdjango.core.exceptionsr   r   Z	django.dbr   Zdjango.db.models.expressionsr   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.translationr   r2   Ú__all__r   r   r   r   r   r   r   r   Ú<module>   s   .9	