
    >Tf*                         d dl Z d dlZd dlmZ d dlmZ d dlmZ  G d dej	                  Z
 G d dej	                  Z G d	 d
          Z G d d          Z G d d          ZdS )    N)
validatorsValidationError)gettext_lazyc                   2    e Zd ZdZ ed          ZdZd ZdS )IsEqualValidatorzB
    Employed by CustomValidator to require a specific value.
    z.Ensure this value is equal to %(limit_value)s.is_equalc                     ||k    S N selfabs      6/var/www/html/netbox-4.1.3/netbox/extras/validators.pycomparezIsEqualValidator.compare       Av    N__name__
__module____qualname____doc___messagecoder   r   r   r   r   r      sD          a@AAGD    r   r   c                   2    e Zd ZdZ ed          ZdZd ZdS )IsNotEqualValidatorzB
    Employed by CustomValidator to exclude a specific value.
    z1Ensure this value does not equal %(limit_value)s.is_not_equalc                     ||k    S r   r   r   s      r   r   zIsNotEqualValidator.compare   r   r   Nr   r   r   r   r   r      sD          aCDDGD    r   r   c                   :    e Zd ZdZ ed          ZdZddZd ZdS )	IsEmptyValidatorzA
    Employed by CustomValidator to enforce required fields.
    zThis field must be empty.is_emptyTc                     || _         d S r   _enforcer   enforces     r   __init__zIsEmptyValidator.__init__)       r   c                 j    | j         r)|t          j        vrt          | j        | j                  d S d S N)r   r&   r   EMPTY_VALUESr   r   r   r   values     r   __call__zIsEmptyValidator.__call__,   sC    = 	@U**AAA!$,TY????	@ 	@AAr   NT	r   r   r   r   r   r   r   r)   r1   r   r   r   r"   r"   "   s]          a+,,GD       @ @ @ @ @r   r"   c                   :    e Zd ZdZ ed          ZdZddZd ZdS )	IsNotEmptyValidatorzC
    Employed by CustomValidator to enforce prohibited fields.
    zThis field must not be empty.	not_emptyTc                     || _         d S r   r%   r'   s     r   r)   zIsNotEmptyValidator.__init__8   r*   r   c                 j    | j         r)|t          j        v rt          | j        | j                  d S d S r,   r-   r/   s     r   r1   zIsNotEmptyValidator.__call__;   sC    = 	@Uj&===!$,TY????	@ 	@==r   Nr2   r3   r   r   r   r5   r5   1   s]          a/00GD       @ @ @ @ @r   r5   c            
           e Zd ZdZdZeeej        ej	        ej
        ej        ej        eed	ZddZddZed             Zed             Zd	 Zd
 ZddZdS )CustomValidatoran  
    This class enables the application of user-defined validation rules to NetBox models. It can be instantiated by
    passing a dictionary of validation rules in the form {attribute: rules}, where 'rules' is a dictionary mapping
    descriptors (e.g. min_length or regex) to values.

    A CustomValidator instance is applied by calling it with the instance being validated:

        validator = CustomValidator({'name': {'min_length: 10}})
        site = Site(name='abcdef')
        validator(site)  # Raises ValidationError

    :param validation_rules: A dictionary mapping object attributes to validation rules
    request)	eqneqminmax
min_length
max_lengthregexrequired
prohibitedNc                     |pi | _         t          | j                   t          urt          t	          d                    d S )Nz/Validation rules must be passed as a dictionary)validation_rulestypedict
ValueErrorr   )r   rF   s     r   r)   zCustomValidator.__init__\   sD     0 6B%&&d22QPQQRRR 32r   c           	         | j                                         D ]\  }}|                    d          d         | j        k    r|,|                     ||          }n|                     ||          }|                                D ]j\  }}|                     ||          }	  ||           (# t          $ r6}	t          t          d          	                    ||	                    d}	~	ww xY w| 
                    ||           dS )z^
        Validate the instance and (optional) request against the validation rule(s).
        .r   Nz5Custom validation failed for {attribute}: {exception})	attribute	exception)rF   itemssplitREQUEST_TOKEN_get_request_attr_get_instance_attrget_validatorr   r   formatvalidate)
r   instancer;   	attr_pathrulesattr
descriptorr0   	validatorexcs
             r   r1   zCustomValidator.__call__a   s>    !% 5 ; ; = = 	 	Iu s##A&$*<<<?--gyAA ..xCC &+[[]] 	 	!
E ..z5AA	IdOOOO&   )QRRYY&/3 Z    		 	h(((((s   #B//
C/91C**C/c                     |                     dd          d         }	  t          j        |          |           S # t          $ r1 t	          t          d                              |                    w xY w)NrK      )maxsplitz&Invalid attribute "{name}" for requestname)rO   operator
attrgetterAttributeErrorr   r   rT   )r;   ra   s     r   rQ   z!CustomValidator._get_request_attr   s    zz#z**1-	a,8&t,,W555 	a 	a 	a!!$L"M"M"T"TZ^"T"_"_```	as	   < ;A7c                    d | j         j        D             }||v rX|t          | dg           v r| j        |         S | j        r/t          t          | |                                                    S g S 	  t          j        |          |           S # t          $ r< t          t          d                              || j        j                            w xY w)Nc                     g | ]	}|j         
S r   r`   ).0fs     r   
<listcomp>z6CustomValidator._get_instance_attr.<locals>.<listcomp>   s    HHHafHHHr   _m2m_valuesz&Invalid attribute "{name}" for {model})ra   model)_metalocal_many_to_manygetattrrj   pklistallrb   rc   rd   r   r   rT   	__class__r   )rV   ra   
m2m_fieldss      r   rR   z"CustomValidator._get_instance_attr   s     IHhn&GHHH
:wx;;;;+D11{ ;GHd337799:::I	,8&t,,X666 	 	 	!!$L"M"M"T"T(1 #U # #   	s   4B ACc                     || j         vr t          d| j        j         d| d          | j                             |          } ||          S )z
        Instantiate and return the appropriate validator based on the descriptor given. For
        example, 'min' returns MinValueValidator(value).
        zUnknown validation type for z: '')
VALIDATORSNotImplementedErrorrr   r   get)r   rZ   r0   validator_clss       r   rS   zCustomValidator.get_validator   se    
 T_,,%Xt~/FXX:XXX   ++J77}U###r   c                     dS )z
        Custom validation method, to be overridden by the user. Validation failures should
        raise a ValidationError exception.
        Nr   )r   rV   r;   s      r   rU   zCustomValidator.validate   s	    
 	r   c                 F    |t          ||i          t          |          )z~
        Raise a ValidationError exception. Associate the provided message with a form/serializer field if specified.
        r   )r   r   fields      r   failzCustomValidator.fail   s+     !5'"2333g&&&r   r   )r   r   r   r   rP   r   r   r   MinValueValidatorMaxValueValidatorMinLengthValidatorMaxLengthValidatorRegexValidatorr5   r"   rv   r)   r1   staticmethodrQ   rR   rS   rU   r}   r   r   r   r:   r:   @   s          M "++ 3 3*'&
 
JS S S S
) ) ) )> a a \a   \&
$ 
$ 
$  ' ' ' ' ' 'r   r:   )inspectrb   django.corer   django.core.exceptionsr   django.utils.translationr   r   BaseValidatorr   r   r"   r5   r:   r   r   r   <module>r      sC     " " " " " " 2 2 2 2 2 2 6 6 6 6 6 6    z/       *2   @ @ @ @ @ @ @ @@ @ @ @ @ @ @ @u' u' u' u' u' u' u' u' u' u'r   