o
    ÕèFh±J  ã                   @   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(   úU/var/www/html/loop/nvenv/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 )NÚconditionalFzACheckConstraint.check must be a Q instance or boolean expression.r   )ÚcheckÚgetattrr   Úsuperr*   )r$   rH   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   ©Úquote_value©Ú.0Úp©r3   r(   r)   Ú	<genexpr>q   ó   € z1CheckConstraint._get_check_sql.<locals>.<genexpr>)r   Úbuild_whererH   Úget_compilerrO   Úas_sqlÚtuple©r$   r2   r3   ÚqueryÚwhereÚcompilerÚsqlÚparamsr(   rV   r)   Ú_get_check_sqll   s
   zCheckConstraint._get_check_sqlc                 C   s   |   ||¡}| | j|¡S rB   )rc   Ú
_check_sqlr   ©r$   r2   r3   rH   r(   r(   r)   r4   s   s   zCheckConstraint.constraint_sqlc                 C   s   |   ||¡}| || j|¡S rB   )rc   Ú_create_check_sqlr   re   r(   r(   r)   r6   w   s   zCheckConstraint.create_sqlc                 C   s   |  || j¡S rB   )Ú_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   rH   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   rH   Ú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   rH   r   r   rJ   Ú__eq__©r$   ÚotherrK   r(   r)   rv   š   s   

ÿ
þ
üzCheckConstraint.__eq__c                    s$   t ƒ  ¡ \}}}| j|d< |||fS )NrH   )rJ   rA   rH   ©r$   r?   r%   r@   rK   r(   r)   rA   ¤   s   

zCheckConstraint.deconstruct)r   r=   rE   r*   rc   r4   r6   r7   r   r;   rt   rv   rA   Ú__classcell__r(   r(   rK   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   rE   Ú_name_r+   r(   r(   r)   rt   ¯   s   zDeferrable.__repr__N)r   r=   rE   ÚDEFERREDÚ	IMMEDIATErt   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ƒ‚|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.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   )ru   Ústrr	   )rT   Ú
expressionr(   r(   r)   rW   ð   s
   € ÿ
ÿz,UniqueConstraint.__init__.<locals>.<genexpr>r   )Ú
ValueErrorru   r   r   r   r   Úlistr\   ÚboolÚlenr�   r‚   rƒ   r„   r…   r†   ÚexpressionsrJ   r*   )r$   r�   r   r‚   rƒ   r„   r…   r†   r   r   r�   rK   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 )NFrL   rN   c                 3   rP   rB   rQ   rS   rV   r(   r)   rW     rX   z6UniqueConstraint._get_condition_sql.<locals>.<genexpr>)r‚   r   rY   rZ   rO   r[   r\   r]   r(   rV   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)rM   )r�   r   Úset_wrapper_classesrO   Úappendr   Úresolve_expressionr   )r$   r2   r3   Úindex_expressionsrˆ   Ú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(   ©rn   Ú	get_field©rT   Ú
field_name©r2   r(   r)   Ú
<listcomp>  ó    z3UniqueConstraint.constraint_sql.<locals>.<listcomp>c                    ó   g | ]	}ˆ j  |¡j‘qS r(   ©rn   r˜   Úcolumnr™   r›   r(   r)   rœ     ó    ÿ©r‚   rƒ   r„   r…   r�   r†   )	r�   r„   rŽ   r”   Ú_unique_sqlr   rƒ   r…   r†   ©r$   r2   r3   r�   r„   r‚   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¢   )	r�   r„   rŽ   r”   Ú_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”   Ú_delete_unique_sqlr   rƒ   r…   r†   )r$   r2   r3   r‚   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>rp   z
 fields=%sz expressions=%sz name=%sz condition=%sz deferrable=%rz include=%sz opclasses=%sz nulls_distinct=%rrq   rr   )r   rE   r�   rs   r�   r   r‚   rƒ   r„   r…   r†   r   r   r   r+   r(   r(   r)   rt   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   )ru   r   r   r�   r‚   rƒ   r„   r…   r�   r†   r   r   rJ   rv   rw   rK   r(   r)   rv   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 )Nr�   r‚   rƒ   r„   r…   r†   )	rJ   rA   r�   r‚   rƒ   r„   r…   r†   r�   ry   rK   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rÐt|   ¡ | j!d�‚| "¡ D ]\}}|D ]}|| u rét| #|| j¡ƒ‚qÚqÔd 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 )NFrp   Úflattenc                 S   s   i | ]	\}}t |ƒ|“qS r(   )r	   )rT   ÚfieldÚvaluer(   r(   r)   Ú
<dictcomp>¨  s    ÿÿz-UniqueConstraint.validate.<locals>.<dictcomp>rh   )Úpkrk   rj   r(   )'Ú_default_managerr:   r�   rn   r˜   rI   Úattnamer†   r   ÚfeaturesÚ!interprets_empty_strings_as_nullsr   Úfilterr�   Úhasattrr¨   ru   r	   rm   Úitemsr
   rˆ   r�   r   Úreplace_expressionsÚ_get_pk_valÚ_stateÚaddingr9   r‚   Úexistsr   r<   r   Úget_constraintsÚunique_error_messager   rH   r   )r$   r2   r8   r9   r:   ÚquerysetÚlookup_kwargsrš   r©   Úlookup_valuerˆ   ÚexprÚreplacementsr�   Úmodel_class_pkÚconstraintsÚ
constraintro   r(   r(   r)   r;   †  s–   

ÿý

€þ€ÿü


þÿÿÿ÷ÿþýÿzUniqueConstraint.validate)r   r=   rE   r*   rF   r,   rŽ   r”   r4   r6   r7   rt   rv   rA   r   r;   rz   r(   r(   rK   r)   r   ³   s,    õF
	r   )#r    Úenumr   Útypesr   Údjango.core.exceptionsr   r   Ú	django.dbr   Údjango.db.models.expressionsr   r   r	   r
   Údjango.db.models.indexesr   Údjango.db.models.lookupsr   Údjango.db.models.query_utilsr   Údjango.db.models.sql.queryr   Údjango.db.utilsr   Údjango.utils.deprecationr   Údjango.utils.translationr   rC   Ú__all__r   r   r   r   r(   r(   r(   r)   Ú<module>   s$    JM	