U
    mf5;                     @   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 ddddg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 k	r|| _n| j| _d S N)nameviolation_error_messagedefault_violation_error_message)selfr   r    r   @/tmp/pip-unpacked-wheel-siwebuq3/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 k	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||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   

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   s     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   )rP   Z
_check_sqlr   r   r!   r"   r8   r   r   r   r#   N   s    zCheckConstraint.constraint_sqlc                 C   s   |  ||}||| j|S r   )rP   Z_create_check_sqlr   rQ   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   sN   |j |j|d}z$t| jj||ds2t|  W n tk
rH   Y nX 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   rP   r#   r$   r%   r   r)   rY   r[   r1   __classcell__r   r   r<   r   r   >   s   	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   rY   |   s    zDeferrable.__repr__N)r-   r,   r5   ZDEFERREDZ	IMMEDIATErY   r   r   r   r   r   w   s   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|rt d|r|rt d	t|td tfst d
t|td ttfst dt|ttfst d|rt|t|krt 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   )rZ   strr   )rC   
expressionr   r   r   rF      s   z,UniqueConstraint.__init__.<locals>.<genexpr>r7   )
ValueErrorrZ   typer   r   listrJ   lenrc   rd   re   rf   rg   expressionsr;   r   )	r   rc   r   rd   re   rf   rg   r   rn   r<   r   r   r      sZ    

zUniqueConstraint.__init__c                 C   s
   t | jS r   )boolrn   r   r   r   r   r      s    z%UniqueConstraint.contains_expressionsc                    s`   | j d k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      s     z6UniqueConstraint._get_condition_sql.<locals>.<genexpr>)rd   r   rG   rH   r@   rI   rJ   rK   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>   )rn   r	   Zset_wrapper_classesr@   appendr   Zresolve_expressionr   )r   r!   r"   Zindex_expressionsri   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   rV   	get_fieldrC   
field_namer!   r   r   
<listcomp>   s     z3UniqueConstraint.constraint_sql.<locals>.<listcomp>c                    s   g | ]} j |jqS r   rV   rt   columnru   rw   r   r   rx      s    rd   re   rf   rg   rn   )rc   rf   rp   rr   Z_unique_sqlr   re   rg   r   r!   r"   rc   rf   rd   rn   r   rw   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   rs   ru   rw   r   r   rx      s     z/UniqueConstraint.create_sql.<locals>.<listcomp>c                    s   g | ]} j |jqS r   ry   ru   rw   r   r   rx      s    r{   )rc   rf   rp   rr   Z_create_unique_sqlr   re   rg   r|   r   rw   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   ry   ru   rw   r   r   rx      s    z/UniqueConstraint.remove_sql.<locals>.<listcomp>r{   )rp   rf   rr   Z_delete_unique_sqlr   re   rg   )r   r!   r"   rd   rf   rn   r   rw   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 krNdnd| j | jd krddnd| j | jsvdndt| j | j	sd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   rc   rX   rn   r   rd   re   rf   rg   r   r   r   r   rY     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   )rZ   r   r   rc   rd   re   rf   rg   rn   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 )Nrc   rd   re   rf   rg   )r;   r1   rc   rd   re   rf   rg   rn   r^   r<   r   r   r1   %  s    




zUniqueConstraint.deconstructc                    s  |j |}| jri }| jD ]Z}|r2||kr2 d S |j|}t||j}	|	d ksf|	dkrlt| jj	rl d S |	||j
< q|jf |}n|r| jD ]T}
t|
dr|
 D ] }t|tr|j
|kr  d S qqt|
tr|
j
|kr d S q|j|j|d  fdd| jD }|j| }||j}|jjs@|d k	r@|j|d}| js| r| jrft|  | D ]2\}}|D ]"}|| krzt||| jqzqnnZ|j|j|d}z2| jt|| j@ j||drt|  W n tk
r   Y nX d S )Nr}   flattenrR   c                    s   g | ]}t || qS r   )r
   Zreplace_references)rC   exprZreplacement_mapr   r   rx   R  s   z-UniqueConstraint.validate.<locals>.<listcomp>)pkrT   )Z_default_managerr(   rc   rV   rt   r9   Zattnamer   featuresZ!interprets_empty_strings_as_nullsr   filterrn   hasattrr~   rZ   r   rU   Z_get_pk_val_stateZaddingr'   rd   existsr   r*   Zget_constraintsZunique_error_messager   r8   r   )r   r!   r&   r'   r(   ZquerysetZlookup_kwargsrv   fieldZlookup_valueri   r   rn   Zmodel_class_pkconstraints
constraintrW   r   r   r   r)   3  sl    



 



 zUniqueConstraint.validate)r-   r,   r5   r   r6   r   rp   rr   r#   r$   r%   rY   r[   r1   r   r)   r_   r   r   r<   r   r      s&   =
	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	