o
    (S-g|l  ã                   @   s  d dl Z d dlmZ d dl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mZmZmZ d d	lmZ d d
l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# 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)Úchecks)ÚFieldDoesNotExistÚ
FieldErrorÚValidationError)Úconnections)Ú
LOOKUP_SEP)ÚExistsÚExpressionListÚFÚRawSQL)ÚIndexExpression)ÚExactÚIsNull)ÚQ)ÚQuery)ÚDEFAULT_DB_ALIAS)ÚRemovedInDjango60Warning)Úgettext_lazy)ÚBaseConstraintÚCheckConstraintÚ
DeferrableÚUniqueConstraintc                   @   sˆ   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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-   ú[/var/www/html/map_library/venv/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   r2   r3   r4   r6   r-   r-   r.   Ú
create_sqlA   r:   zBaseConstraint.create_sqlc                 C   r2   r3   r4   r6   r-   r-   r.   Ú
remove_sqlD   r:   zBaseConstraint.remove_sqlc                 C   r2   r3   r4   )r)   r7   ÚinstanceÚexcludeÚusingr-   r-   r.   ÚvalidateG   r:   zBaseConstraint.validatec                 C   s   | j d| ji S )Nr   )r   r   r0   r-   r-   r.   Úget_violation_error_messageJ   s   z*BaseConstraint.get_violation_error_messagec                 C   s   g S ©Nr-   )r)   r7   Ú
connectionr-   r-   r.   Ú_checkM   s   zBaseConstraint._checkc           	   	   C   sð   g }t ƒ }|D ]e^}}|dkr| |¡ |sqz|dkr!|jj}n|j |¡}|jr0|js0|jr2W qW n	 ty<   Y qw |d }t	|dƒrlt	|dƒrl| 
|¡d u rl| |¡d u rl| tjdt |g| ¡ |dd�¡ q| | |d¡¡ |S )	NÚpkr   Úget_transformÚ
get_lookupz.'constraints' refers to the joined field '%s'.zmodels.E041)ÚobjÚidÚconstraints)ÚsetÚaddÚ_metarE   Ú	get_fieldÚis_relationÚmany_to_manyÚone_to_manyr   ÚhasattrrF   rG   Úappendr   ÚErrorr	   ÚjoinÚextendÚ_check_local_fields)	r)   r7   Ú
referencesÚerrorsÚfieldsÚ
field_nameÚlookupsÚfieldÚfirst_lookupr-   r-   r.   Ú_check_referencesP   sH   

ÿÿÿþÿüÿ€z BaseConstraint._check_referencesc                 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.   Údeconstructv   s   





zBaseConstraint.deconstructc                 C   s   |   ¡ \}}}| j|i |¤ŽS rB   )rd   r"   )r)   Ú_r*   rc   r-   r-   r.   Úcloneƒ   s   zBaseConstraint.clone)r#   r`   Ú__qualname__re   r$   r   r   r/   Úpropertyr1   r9   r;   r<   r   r@   rA   rD   r_   rd   rf   r-   r-   r-   r.   r      s"    ÿ
&r   c                       s˜   e Zd Zdddddœ‡ fdd„
Zdd„ Zdd„ ZeeeƒZ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)Ú	conditionÚcheckr   r   c                   sL   |d urt jdtdd� |}|| _t|ddƒstdƒ‚tƒ j|||d� d S )Nú=CheckConstraint.check is deprecated in favor of `.condition`.r   r   ÚconditionalFzECheckConstraint.condition must be a Q instance or boolean expression.r   )r%   r&   r   ri   Úgetattrr!   Úsuperr/   )r)   r   ri   rj   r   r   ©r"   r-   r.   r/   �   s"   	ýÿ
ýzCheckConstraint.__init__c                 C   s   t jdtdd� | jS ©Nrk   r   r   ©r%   r&   r   ri   r0   r-   r-   r.   Ú
_get_check¨   s   ýzCheckConstraint._get_checkc                 C   s   t jdtdd� || _d S rp   rq   )r)   Úvaluer-   r-   r.   Ú
_set_check°   s   ý
zCheckConstraint._set_checkc                 C   sÄ   g }|j jsd|jjv s| tj|j› d�d|dd�¡ |S |j js(d|jjvr`tƒ }| j	}t
|tƒr;| | |¡¡ tdd„ | ¡ D ƒƒrW| tjd| j›d	�d
|dd�¡ | |  ||¡¡ |S )NÚ supports_table_check_constraintsz$ does not support check constraints.úOA constraint won't be created. Silence this warning if you don't care about it.zmodels.W027©ÚhintrH   rI   c                 s   s   � | ]}t |tƒV  qd S rB   )Ú
isinstancer   )Ú.0Úexprr-   r-   r.   Ú	<genexpr>Ô   ó   € z)CheckConstraint._check.<locals>.<genexpr>zCheck constraint zS contains RawSQL() expression and won't be validated during the model full_clean().z0Silence this warning if you don't care about it.zmodels.W045)Úfeaturesru   rM   Úrequired_db_featuresrS   r   ÚWarningÚdisplay_namerK   ri   ry   r   ÚupdateÚ_get_expr_referencesÚanyÚflattenr   rV   r_   )r)   r7   rC   rY   rX   ri   r-   r-   r.   rD   º   sB   ÿ
ùÿíÿÿ
ûÿ	zCheckConstraint._checkc                    sR   t |dd�}| | j¡}|jˆ jd�}| |ˆ j¡\}}|t‡ fdd„|D ƒƒ S )NF©r7   Ú
alias_cols©rC   c                 3   ó   � | ]}ˆ   |¡V  qd S rB   ©Úquote_value©rz   Úp©r8   r-   r.   r|   æ   r}   z1CheckConstraint._get_check_sql.<locals>.<genexpr>)r   Úbuild_whereri   Úget_compilerrC   Úas_sqlÚtuple©r)   r7   r8   ÚqueryÚwhereÚcompilerÚsqlÚparamsr-   rŽ   r.   Ú_get_check_sqlá   s
   zCheckConstraint._get_check_sqlc                 C   s   |   ||¡}| | j|¡S rB   )r™   Ú
_check_sqlr   ©r)   r7   r8   rj   r-   r-   r.   r9   è   s   zCheckConstraint.constraint_sqlc                 C   s   |   ||¡}| || j|¡S rB   )r™   Ú_create_check_sqlr   r›   r-   r-   r.   r;   ì   s   zCheckConstraint.create_sqlc                 C   s   |  || j¡S rB   )Ú_delete_check_sqlr   r6   r-   r-   r.   r<   ð   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©Úmetar>   ©r?   ©Úcode)	Ú_get_field_expression_maprM   r   ri   rj   r   rA   r   r   )r)   r7   r=   r>   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: condition=%s name=%s%s%s>Ú ú violation_error_code=%rú violation_error_message=%r)r"   rg   ri   Úreprr   r   r   r$   r0   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   )ry   r   r   ri   r   r   rn   Ú__eq__©r)   Úotherro   r-   r.   rª     s   

ÿ
þ
üzCheckConstraint.__eq__c                    s$   t ƒ  ¡ \}}}| j|d< |||fS )Nri   )rn   rd   ri   ©r)   rb   r*   rc   ro   r-   r.   rd     s   

zCheckConstraint.deconstruct)r#   r`   rg   r/   rr   rt   rh   rj   rD   r™   r9   r;   r<   r   r@   r©   rª   rd   Ú__classcell__r-   r-   ro   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"   rg   Ú_name_r0   r-   r-   r.   r©   $  s   zDeferrable.__repr__N)r#   r`   rg   ÚDEFERREDÚ	IMMEDIATEr©   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dd„ Z‡ fdd„Z‡ fdd„Zdefdd„Z‡  ZS )r   r-   N)	rZ   r   ri   Ú
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   )ry   Ústrr   )rz   Ú
expressionr-   r-   r.   r|   e  s
   € ÿ
ÿz,UniqueConstraint.__init__.<locals>.<genexpr>r   )Ú
ValueErrorry   r   r   r   r!   Úlistr’   ÚboolÚlenrZ   ri   rµ   r¶   r·   r¸   Úexpressionsrn   r/   )r)   rZ   r   ri   rµ   r¶   r·   r¸   r   r   r¿   ro   r-   r.   r/   )  sh   ÿÿÿÿÿ

þ
ýzUniqueConstraint.__init__c                 C   s
   t | jƒS rB   )r½   r¿   r0   r-   r-   r.   r1   o  s   
z%UniqueConstraint.contains_expressionsc                 C   s¶  |  h | j£| j£d¡}|jj}| jd ur-|jjs-d|v s-| t	j
|j› d�d|dd�¡ | jd urJ|jjsJd|v sJ| t	j
|j› d�d|d	d�¡ | jre|jjsed
|v se| t	j
|j› d�d|dd�¡ | jr€|jjs€d|v s€| t	j
|j› d�d|dd�¡ | jd ur�|jjs�d|v s�| t	j
|j› d�d|dd�¡ tƒ }|jjs¨d|vr·t| jtƒr·| | | j¡¡ | jrÐ|jjsÂd|vrÐ| jD ]
}| | |¡¡ qÅ| |  ||¡¡ |S )NrJ   Úsupports_partial_indexesz5 does not support unique constraints with conditions.rv   zmodels.W036rw   Ú&supports_deferrable_unique_constraintsz0 does not support deferrable unique constraints.zmodels.W038Úsupports_covering_indexesz: does not support unique constraints with non-key columns.zmodels.W039Úsupports_expression_indexesz4 does not support unique constraints on expressions.zmodels.W044Ú*supports_nulls_distinct_unique_constraintsz9 does not support unique constraints with nulls distinct.zmodels.W047)rW   rZ   r¶   rM   r   ri   r~   rÀ   rS   r   r€   r�   rµ   rÁ   rÂ   r1   rÃ   r¸   rÄ   rK   ry   r   r‚   rƒ   r¿   rV   r_   )r)   r7   rC   rY   r   rX   rº   r-   r-   r.   rD   s  sš   
ÿ
øÿ
ÿ
øÿÿ
øÿÿ
øÿ
ÿ
øÿÿ
ÿÿ
zUniqueConstraint._checkc                    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   r‰   rB   rŠ   rŒ   rŽ   r-   r.   r|   Ü  r}   z6UniqueConstraint._get_condition_sql.<locals>.<genexpr>)ri   r   r�   r�   rC   r‘   r’   r“   r-   rŽ   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‡   )r¿   r   Úset_wrapper_classesrC   rS   r   Úresolve_expressionr   )r)   r7   r8   Ú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-   ©rM   rN   ©rz   r[   ©r7   r-   r.   Ú
<listcomp>ë  ó    z3UniqueConstraint.constraint_sql.<locals>.<listcomp>c                    ó   g | ]	}ˆ j  |¡j‘qS r-   ©rM   rN   ÚcolumnrÎ   rÏ   r-   r.   rÐ   ì  ó    ÿ©ri   rµ   r¶   r·   r¿   r¸   )	rZ   r¶   rÅ   rÊ   Ú_unique_sqlr   rµ   r·   r¸   ©r)   r7   r8   rZ   r¶   ri   r¿   r-   rÏ   r.   r9   ê  ó"   
ÿ÷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Ö   )	rZ   r¶   rÅ   rÊ   Ú_create_unique_sqlr   rµ   r·   r¸   rØ   r-   rÏ   r.   r;   ý  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)   r7   r8   ri   r¶   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	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>r¥   z
 fields=%sz expressions=%sz name=%sz condition=%sz deferrable=%rz include=%sz opclasses=%sz nulls_distinct=%rr¦   r§   )r"   rg   rZ   r¨   r¿   r   ri   rµ   r¶   r·   r¸   r   r   r$   r0   r-   r-   r.   r©   !  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   )ry   r   r   rZ   ri   rµ   r¶   r·   r¿   r¸   r   r   rn   rª   r«   ro   r-   r.   rª   =  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 )NrZ   ri   rµ   r¶   r·   r¸   )	rn   rd   rZ   ri   rµ   r¶   r·   r¸   r¿   r­   ro   r-   r.   rd   M  s   






zUniqueConstraint.deconstructc                 C   sJ  |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 |¤Ž}nx|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|dƒr™| ¡ }| |¡}t||ƒ}| jdu r·t|ƒtt|dƒt|dƒƒB }| |¡ qŽ|j|Ž }| |j¡}|jjsÖ|d urÖ|j|d	�}| jsô| ¡ rò| jrét | !|| j¡ƒ‚t |  "¡ | j#d
�‚d S |j|j|d�}z| jt$| | j¡ƒ@ j%||d��rt |  "¡ | j#d
�‚W d S  t&�y$   Y d S w )NFr¥   r…   c                 S   s   i | ]	\}}t |ƒ|“qS r-   )r   )rz   r]   rs   r-   r-   r.   Ú
<dictcomp>  s    ÿÿz-UniqueConstraint.validate.<locals>.<dictcomp>rž   Úget_expression_for_validationT)rE   r¡   r    r-   )'Ú_default_managerr?   rZ   rM   rN   rm   Úattnamer¸   r   r~   Ú!interprets_empty_strings_as_nullsr   Úfilterr¿   rR   r…   ry   r   r£   ÚitemsrÝ   Úreplace_expressionsr   r   r   rS   Ú_get_pk_valÚ_stateÚaddingr>   ri   Úexistsr   Úunique_error_messagerA   r   r
   rj   r   )r)   r7   r=   r>   r?   ÚquerysetÚlookup_kwargsr[   r]   Úlookup_valuerº   r{   ÚreplacementsÚfiltersÚrhsri   Úmodel_class_pkr¤   r-   r-   r.   r@   ]  s–   

ÿý

€þ€ÿü





ÿþùÿÿþýÿzUniqueConstraint.validate)r#   r`   rg   r/   rh   r1   rD   rÅ   rÊ   r9   r;   r<   r©   rª   rd   r   r@   r®   r-   r-   ro   r.   r   (  s.    õF
b	r   ))r%   Úenumr   Útypesr   Údjango.corer   Údjango.core.exceptionsr   r   r   Ú	django.dbr   Údjango.db.models.constantsr	   Údjango.db.models.expressionsr
   r   r   r   Údjango.db.models.indexesr   Údjango.db.models.lookupsr   r   Údjango.db.models.query_utilsr   Údjango.db.models.sql.queryr   Údjango.db.utilsr   Údjango.utils.deprecationr   Údjango.utils.translationr   re   Ú__all__r   r   r   r   r-   r-   r-   r.   Ú<module>   s*    s 	