
    >TfM%              	       :   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 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mZmZmZmZ d dlmZ d dlm Z  d dl!m"Z" dZ# G d deeee          Z$ G d dej%                  Z& G d deeeee          Z'dS )    )apps)settings)ValidationError)models)reverse)gettext_lazy)
BaseLoader)SandboxedEnvironment)ConfigContextQuerySet)
get_config)ChangeLoggedModel)CloningMixinCustomLinksMixinExportTemplatesMixinSyncedDataMixin	TagsMixin)registry)	deepmerge)DataFileLoader)ConfigContextConfigContextModelConfigTemplatec                   |    e Zd ZdZ ej         ed          dd          Z ej         ed          d          Z	 ej         ed	          d
d          Z
 ej         ed          d          Z ej        ddd          Z ej        ddd          Z ej        ddd          Z ej        ddd          Z ej        ddd          Z ej        ddd          Z ej        ddd          Z ej        ddd          Z ej        ddd          Z ej        ddd          Z ej        ddd          Z ej        ddd          Z ej        ddd          Z ej                    Z ej                    ZdZ  G d d          Z!d Z"d  Z#e$d!             Z% fd"Z&d# Z'de'_(         xZ)S )$r   aR  
    A ConfigContext represents a set of arbitrary data available to any Device or VirtualMachine matching its assigned
    qualifiers (region, site, etc.). For example, the data stored in a ConfigContext assigned to site A and tenant B
    will be available to a Device in site A assigned to tenant B. Data is stored in JSON format.
    named   T)verbose_name
max_lengthuniqueweighti  )r   defaultdescription   r   r   blankz	is activezdcim.Region+)torelated_namer$   zdcim.SiteGroupz	dcim.Sitezdcim.Locationzdcim.DeviceTypezdcim.DeviceRolezdcim.Platformzvirtualization.ClusterTypezvirtualization.ClusterGroupzvirtualization.Clusterztenancy.TenantGroupztenancy.Tenantz
extras.Tag)r   	is_activeregionssite_groupssites	locationsdevice_typesroles	platformscluster_typescluster_groupsclusterstenant_groupstenantstagsdatac                   B    e Zd ZddgZ ed          Z ed          ZdS )ConfigContext.Metar   r   zconfig contextzconfig contextsN__name__
__module____qualname__ordering_r   verbose_name_plural     :/var/www/html/netbox-4.1.3/netbox/extras/models/configs.pyMetar8      s;        f%q)**a 122rA   rC   c                     | j         S Nr   selfs    rB   __str__zConfigContext.__str__   
    yrA   c                 2    t          dd| j        i          S )Nzextras:configcontextpk)kwargsr   rL   rG   s    rB   get_absolute_urlzConfigContext.get_absolute_url   s    -tTWoFFFFrA   c                      t           j         dS )Nz!docs/models/extras/configcontext/)r   
STATIC_URLrG   s    rB   docs_urlzConfigContext.docs_url   s    %HHHHrA   c                     t                                                       t          | j                  t          ur!t          dt          d          dz   i          d S )Nr6   *JSON data must be in object form. Example: {"foo": 123})supercleantyper6   dictr   r>   rH   	__class__s    rB   rW   zConfigContext.clean   sY     	??$&&!GHH?Z[   '&rA   c                 B    | j                                         | _        dS )zQ
        Synchronize context data from the designated DataFile (if any).
        N)	data_fileget_datar6   rG   s    rB   	sync_datazConfigContext.sync_data   s     N++--			rA   )*r:   r;   r<   __doc__r   	CharFieldr>   r   PositiveSmallIntegerFieldr   r!   BooleanFieldr(   ManyToManyFieldr)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   	JSONFieldr6   r   
as_managerobjectsclone_fieldsrC   rI   rO   propertyrR   rW   r_   alters_data__classcell__r[   s   @rB   r   r      sd        
 6QvYY  D
 .V-Qx[[  F #&"Q}%%  K
 $#Q{^^  I %f$  G
 )&(  K
 #F"  E
 '&  I
 *6)  L
 #F"  E
 '&  I
 +F*'  M
 ,V+(  N
 &v%#  H
 +F*   M
 %f$  G
 "6!  D
 6D.#.00GL3 3 3 3 3 3 3 3
  G G G I I XI    . . .
 !IrA   r   c                   z     e Zd ZdZ ej        dd ed                    Z G d d          Zd Z	 fdZ
 xZS )	r   z
    A model which includes local configuration context data. This local data will override any inherited data from
    ConfigContexts.
    TzdLocal config context data takes precedence over source contexts in the final rendered config context)r$   null	help_textc                       e Zd ZdZdS )ConfigContextModel.MetaTN)r:   r;   r<   abstractr@   rA   rB   rC   rq      s        rA   rC   c                     i }t          | d          s$t          j                            | d          pg }n	| j        pg }|D ]}t          ||          }| j        rt          || j                  }|S )z
        Compile all config data, overwriting lower-weight values with higher-weight values where a collision occurs.
        Return the rendered configuration context for a device or VM.
        config_context_dataT)aggregate_data)hasattrr   rg   get_for_objectrt   r   local_context_data)rH   r6   rt   contexts       rB   get_config_contextz%ConfigContextModel.get_config_context   s    
 t233 	A"/"7"F"Ft\`"F"a"a"geg #'":"@b* 	, 	,GT7++DD " 	<T4#:;;DrA   c                     t                                                       | j        r<t          | j                  t          ur#t          dt          d          dz   i          d S d S )Nrx   rT   rU   )rV   rW   rx   rX   rY   r   r>   rZ   s    rB   rW   zConfigContextModel.clean   so     " 	tD,C'D'DD'P'P!%q)U'V'VYh'hi  	 	'P'PrA   )r:   r;   r<   r`   r   re   r>   rx   rC   rz   rW   rk   rl   s   @rB   r   r      s          *)!r
 
           .        rA   r   c            	       Z   e Zd Z ej         ed          d          Z ej         ed          dd          Z ej         ed           ed	          
          Z	 ej
         ed          dde ed                    Z G d d          Zd Zd Zd Zde_        ddZd ZdS )r   r   r   )r   r   r!   r"   Tr#   ztemplate codezJinja2 template code.)r   ro   zenvironment parameterszAny <a href="https://jinja.palletsprojects.com/en/3.1.x/api/#jinja2.Environment">additional parameters</a> to pass when constructing the Jinja2 environment.)r   r$   rn   r    ro   c                   >    e Zd ZdZ ed          Z ed          ZdS )ConfigTemplate.MetarF   zconfig templatezconfig templatesNr9   r@   rA   rB   rC   r~      s6        q*++a 233rA   rC   c                     | j         S rE   rF   rG   s    rB   rI   zConfigTemplate.__str__   rJ   rA   c                 0    t          d| j        g          S )Nzextras:configtemplate)argsrN   rG   s    rB   rO   zConfigTemplate.get_absolute_url   s    .dgY????rA   c                 (    | j         j        | _        dS )zU
        Synchronize template content from the designated DataFile (if any).
        N)r]   data_as_stringtemplate_coderG   s    rB   r_   zConfigTemplate.sync_data   s     "^:rA   Nc                 (   t                      }t          d                                         D ]V\  }}|                    |i            |D ]8}	 t	          j        ||          }|||         |j        <   )# t          $ r Y 5w xY wW||                    |           | 	                                }| j
        r |                    | j
        j                  }n|                    | j                  } |j        di |}	|	                    dd          S )z6
        Render the contents of the template.
        r   Nz

r@   )rY   r   items
setdefaultr   get_registered_modelr:   LookupErrorupdate_get_environmentr]   get_templatepathfrom_stringr   renderreplace)
rH   ry   _contextappmodel_names
model_namemodelenvironmenttemplateoutputs
             rB   r   zConfigTemplate.render   s=    66 !) 2 8 8 : : 	 	CR((()  
 5c:FFE49HSM%.11"   D	 OOG$$$ ++--> 	C"//0CDDHH"..t/ABBH ,,8,, ~~fd+++s   %A44
B Bc                 ,   | j         r<t          | j                  }|                    | j         j        | j        i           nt                      }| j        pi }t          dd|i|}|j	        
                    t                      j                   |S )zh
        Instantiate and return a Jinja2 environment suitable for rendering the ConfigTemplate.
        )data_sourceloaderr@   )r]   r   r   cache_templatesr   r   r	   environment_paramsr
   filtersr   r   JINJA2_FILTERS)rH   r   
env_paramsr   s       rB   r   zConfigTemplate._get_environment  s    
 > 	"#0@AAAF""#T%7$      \\F ,2
*GG&GJGG"":<<#>???rA   rE   )r:   r;   r<   r   ra   r>   r   r!   	TextFieldr   re   rY   r   rC   rI   rO   r_   rj   r   r   r@   rA   rB   r   r      sl       6QvYY  D #&"Q}%%  K
 %F$Q''!+,,  M *)Q/00!A
 
	 	 	4 4 4 4 4 4 4 4
  @ @ @; ; ;
 !I, , , ,>    rA   r   N)(django.appsr   django.confr   django.core.validatorsr   	django.dbr   django.urlsr   django.utils.translationr   r>   jinja2.loadersr	   jinja2.sandboxr
   extras.querysetsr   netbox.configr   netbox.modelsr   netbox.models.featuresr   r   r   r   r   netbox.registryr   utilities.datar   utilities.jinja2r   __all__r   Modelr   r   r@   rA   rB   <module>r      s                     2 2 2 2 2 2             6 6 6 6 6 6 % % % % % % / / / / / / 2 2 2 2 2 2 $ $ $ $ $ $ + + + + + + s s s s s s s s s s s s s s $ $ $ $ $ $ $ $ $ $ $ $ + + + + + +@! @! @! @! @!O\3CEV @! @! @!F. . . . . . . .j\ \ \ \ \_&68LiYj \ \ \ \ \rA   