o
    lf<                     @   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
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OrderBy)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|| _d S | j| _d S N)nameviolation_error_messagedefault_violation_error_message)selfr   r    r   `/var/www/ticemtrilhas/avaliacao_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_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   r   r   r    r"   r   r   r   
create_sql"   r&   zBaseConstraint.create_sqlc                 C   r   r   r    r"   r   r   r   
remove_sql%   r&   zBaseConstraint.remove_sqlc                 C   r   r   r    )r   r#   instanceexcludeusingr   r   r   validate(   r&   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r%| j| jkr%| 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   )r4   r.   )r   _argsr3   r   r   r   clone9   s   zBaseConstraint.cloner   )r0   r/   __qualname__r5   r   r   r   propertyr   r%   r'   r(   r   r,   r-   r4   r7   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 )NconditionalFzACheckConstraint.check must be a Q instance or boolean expression.r:   )checkgetattr	TypeErrorsuperr   )r   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r#   
alias_cols
connectionc                 3       | ]}  |V  qd S r   quote_value.0pr$   r   r   	<genexpr>L       z1CheckConstraint._get_check_sql.<locals>.<genexpr>)r   build_wherer<   get_compilerrD   as_sqltupler   r#   r$   querywherecompilersqlparamsr   rK   r   _get_check_sqlG   s
   zCheckConstraint._get_check_sqlc                 C   s   |  ||}|| j|S r   )rX   
_check_sqlr   r   r#   r$   r<   r   r   r   r%   N   s   zCheckConstraint.constraint_sqlc                 C   s   |  ||}||| j|S r   )rX   _create_check_sqlr   rZ   r   r   r   r'   R   s   zCheckConstraint.create_sqlc                 C   s   | || jS r   )_delete_check_sqlr   r"   r   r   r   r(   V   s   zCheckConstraint.remove_sqlc                 C   sL   |j |j|d}zt| jj||dst|  W d S  ty%   Y d S w )Nmetar*   r+   )_get_field_value_map_metar   r<   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.   r8   r<   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   r<   r   r?   __eq__r   otherr@   r   r   rf   h   s   


zCheckConstraint.__eq__c                    s$   t   \}}}| j|d< |||fS )Nr<   )r?   r4   r<   r   r2   r6   r3   r@   r   r   r4   q   s   

zCheckConstraint.deconstruct)r0   r/   r8   r   rX   r%   r'   r(   r   r,   rd   rf   r4   __classcell__r   r   r@   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.   r8   _name_r   r   r   r   rd   |   s   zDeferrable.__repr__N)r0   r/   r8   DEFERRED	IMMEDIATErd   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          	         sT  |st d|s|st d|r|rt dt|td tfs#t d|r+|r+t d|r3|r3t d|r;|r;t d|rC|rCt d|rK|rKt d	t|td tfsXt d
t|td ttfsft dt|ttfsqt d|rt|t|krt 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.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   )re   strr   )rI   
expressionr   r   r   rL      s
    
z,UniqueConstraint.__init__.<locals>.<genexpr>r:   )
ValueErrorre   typer   r   listrQ   lenrq   rr   rs   rt   ru   expressionsr?   r   )	r   rq   r   rr   rs   rt   ru   r   r|   r@   r   r   r      sZ   

zUniqueConstraint.__init__c                 C   s
   t | jS r   )boolr|   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 )NFrA   rC   c                 3   rE   r   rF   rH   rK   r   r   rL      rM   z6UniqueConstraint._get_condition_sql.<locals>.<genexpr>)rr   r   rN   rO   rD   rP   rQ   rR   r   rK   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)rB   )r|   r
   set_wrapper_classesrD   appendr   resolve_expressionr   )r   r#   r$   index_expressionsrw   index_expressionr   r   r   _get_index_expressions   s   

z'UniqueConstraint._get_index_expressionsc              
      `    fdd| j D } fdd| jD }|  |}|  |}|j || j|| j|| j|dS )Nc                       g | ]} j |qS r   ra   	get_fieldrI   
field_namer#   r   r   
<listcomp>       z3UniqueConstraint.constraint_sql.<locals>.<listcomp>c                       g | ]	} j |jqS r   ra   r   columnr   r   r   r   r          rr   rs   rt   ru   r|   )rq   rt   r~   r   _unique_sqlr   rs   ru   r   r#   r$   rq   rt   rr   r|   r   r   r   r%          
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   )rq   rt   r~   r   _create_unique_sqlr   rs   ru   r   r   r   r   r'      r   zUniqueConstraint.create_sqlc              	      sJ   |   |} fdd| jD }|  |}|j | j|| j|| j|dS )Nc                    r   r   r   r   r   r   r   r      r   z/UniqueConstraint.remove_sql.<locals>.<listcomp>r   )r~   rt   r   _delete_unique_sqlr   rs   ru   )r   r#   r$   rr   rt   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	sHdf S 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.   r8   rq   rc   r|   r   rr   rs   rt   ru   r   r   r   r   rd     s   zUniqueConstraint.__repr__c                    sv   t |tr5| j|jko4| j|jko4| j|jko4| j|jko4| j|jko4| j|jko4| j|jko4| j	|j	kS t
 |S r   )re   r   r   rq   rr   rs   rt   ru   r|   r   r?   rf   rg   r@   r   r   rf     s"   








zUniqueConstraint.__eq__c                    sl   t   \}}}| jr| j|d< | jr| j|d< | jr | j|d< | jr(| j|d< | jr0| j|d< || j|fS )Nrq   rr   rs   rt   ru   )r?   r4   rq   rr   rs   rt   ru   r|   ri   r@   r   r   r4   %  s   




zUniqueConstraint.deconstructc                 C   s*  |j |}| jrEi }| jD ]-}|r||v r d S |j|}t||j}	|	d u s3|	dkr6t| jj	r6 d S |	||j
< q|jdi |}n_|ru| jD ]*}
t|
drg|
 D ]}t|tre|j
|v re  d S qUqJt|
trt|
j
|v rt d S qJd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|  |  D ]\}}|D ]}|| u rt|!|| jqqd S d S |j|j|d}z| jt"|| j@ j#||drt|  W d S  t$y   Y d S w )	Nr   flattenc                 S   s   i | ]	\}}t ||qS r   )r   )rI   fieldvaluer   r   r   
<dictcomp>O  s    z-UniqueConstraint.validate.<locals>.<dictcomp>r]   )pkr_   r   )%_default_managerr+   rq   ra   r   r=   attnamer   features!interprets_empty_strings_as_nullsr   filterr|   hasattrr   re   r   r`   itemsr	   rw   r   r   replace_expressions_get_pk_val_stateaddingr*   rr   existsr   r-   get_constraintsunique_error_messager   r<   r   )r   r#   r)   r*   r+   querysetlookup_kwargsr   r   lookup_valuerw   exprreplacementsr|   model_class_pkconstraints
constraintrb   r   r   r   r,   3  s   






zUniqueConstraint.validate)r0   r/   r8   r   r9   r   r~   r   r%   r'   r(   rd   rf   r4   r   r,   rj   r   r   r@   r   r      s(    =
	r   N)enumr   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.translationr   r5   __all__r   r   r   r   r   r   r   r   <module>   s    .9	