
    >Tf                        d dl Z d dl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 d dlmZmZ d dlmZ ddlmZ dZ  ej!        d          Z" G d dee          Z# G d dej$        %                    e                    Z& G d deee          Z' eee'          d             Z(dS )    N)cached_property)GenericRelation)models)Q)	post_save)receiverreverse)gettext_lazy)ManagedFileRootPathChoices)ManagedFile)	is_script)	JobsMixinEventRulesMixin)RestrictedQuerySet   )PythonModuleMixin)ScriptScriptModuleznetbox.data_backendsc                   N    e Zd Z ej         ed          dd          Z ej        dej        dd          Z	 ej
        d ed	          d
          Z eddd          Zd Z ej                    Z G d d          Zd Zed             Zed             Zd fd	Z xZS )r   nameO   F)verbose_name
max_lengtheditablezextras.ScriptModulescripts)to	on_deleterelated_namer   Tzis executable)defaultr   r   extras.EventRuleaction_object_typeaction_object_id)content_type_fieldobject_id_fieldc                     | j         S Nr   selfs    :/var/www/html/netbox-4.1.3/netbox/extras/models/scripts.py__str__zScript.__str__3   s
    y    c                   d    e Zd ZdZ ej        dd          fZ ed          Z ed          Z	dS )Script.Meta)moduler   )r   r0    extras_script_unique_name_module)fieldsr   scriptr   N)
__name__
__module____qualname__orderingr   UniqueConstraintconstraints_r   verbose_name_plural r-   r+   Metar/   8   sV        %#F#)7  
 q{{a	llr-   r=   c                 0    t          d| j        g          S )Nzextras:script)args)r
   pkr)   s    r+   get_absolute_urlzScript.get_absolute_urlC   s    dgY7777r-   c                 ~    | j                                                             d                                          S )Nz-created)jobsallorder_byfirstr)   s    r+   resultzScript.resultF   s,    y}}''
3399;;;r-   c                 J    | j         j                            | j                  S r'   )r0   module_scriptsgetr   r)   s    r+   python_classzScript.python_classJ   s    {)--di888r-   c                     |r6| j                                         rd| _        |                                  d S  t	                      j        di | d | _        d S )NFr<   )rC   existsis_executablesavesuperdeleteid)r*   soft_deletekwargs	__class__s      r+   rQ   zScript.deleteN   s`     	49++-- 	!&DIIKKKKKEGGN$$V$$$DGGGr-   )F)r4   r5   r6   r   	CharFieldr:   r   
ForeignKeyCASCADEr0   BooleanFieldrN   r   eventsr,   r   
as_managerobjectsr=   rA   propertyrG   r   rK   rQ   __classcell__rU   s   @r+   r   r      sn       6QvYY  D
 V .	  F (F'Q''  M
 _/*  F   , +--G	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+8 8 8 < < X< 9 9 _9         r-   r   c                        e Zd Z fdZ xZS )ScriptModuleManagerc                     t                                                                          t          t          j                  t          t          j                  z            S )N)	file_root)rP   get_querysetfilterr   r   SCRIPTSREPORTSr*   rU   s    r+   rd   z ScriptModuleManager.get_querysetY   sT    ww##%%,,2:;;;aJdJl>m>m>mmo o 	or-   )r4   r5   r6   rd   r^   r_   s   @r+   ra   ra   W   sA        o o o o o o o o or-   ra   c                        e Zd ZdZ e            ZdZ edddd          Z G d d	          Z	d
 Z
d Zed             Zd Z fdZ fdZ xZS )r   z.
    Proxy model for script module files.
    Nr!   r"   r#   F)r   r$   r%   for_concrete_modelc                   B    e Zd ZdZdZ ed          Z ed          ZdS )ScriptModule.MetaT)rc   	file_pathzscript modulezscript modulesN)r4   r5   r6   proxyr7   r:   r   r;   r<   r-   r+   r=   rl   l   s:        -q))a 011r-   r=   c                      t          d          S )Nzextras:script_listr	   r)   s    r+   rA   zScriptModule.get_absolute_urlr   s    +,,,r-   c                     | j         S r'   )python_namer)   s    r+   r,   zScriptModule.__str__u   s    r-   c                 r   d }	 |                                  }nE# t          $ r8}|| _        t                              d| j         d|            d }Y d }~nd }~ww xY wi }t          |dg           }|D ]}|| ||          <   t          j        |t                    D ]\  }}||vr|| ||          <   |S )Nc                 F    | j                             dd          d         S )N.r   )maxsplit)	full_namesplit)clss    r+   	_get_namez.ScriptModule.module_scripts.<locals>._get_name{   s"    =&&sQ&77::r-   zFailed to load script: z error: script_order)

get_module	Exceptionerrorloggerdebugrq   getattrinspect
getmembersr   )r*   ry   r0   er   orderedrx   r   s           r+   rI   zScriptModule.module_scriptsx   s   	; 	; 	;	__&&FF 	 	 	DJLLP43CPPQPPQQQFFFFFF	
 &."55 	* 	*C&)GIIcNN## +FI>> 	. 	.ID#'!!*-		#'s    
A.AAc                    | j         r$d | j                                        D             }ni }t          |                                          }t          | j                                                  }||z
  }|D ]}||                             d           ||z
  }|D ]$}t          j        	                    | |d           %dS )z
        Syncs the file-based module to the database, adding and removing individual Script objects
        in the database as needed.
        c                     i | ]
}|j         |S r<   r(   ).0r3   s     r+   
<dictcomp>z-ScriptModule.sync_classes.<locals>.<dictcomp>   s)       (.V  r-   T)rS   )r0   r   rN   N)
rR   r   rD   setkeysrI   rQ   r   r\   create)r*   
db_classesdb_classes_setmodule_classes_setremovedr   addeds          r+   sync_classeszScriptModule.sync_classes   s   
 7 	 26,2B2B2D2D  JJ JZ__..// !4!9!9!;!;<< !#55 	6 	6Dt###5555"^3 	 	DN!!" "    	 	r-   c                 H    t                                                       d S r'   )rP   	sync_datarh   s    r+   r   zScriptModule.sync_data   s    r-   c                     t           j        | _         t                      j        |i | |                                  d S r'   )r   rf   rc   rP   rO   r   )r*   r?   rT   rU   s      r+   rO   zScriptModule.save   s@    3;d%f%%%r-   )r4   r5   r6   __doc__ra   r\   r}   r   event_rulesr=   rA   r,   r]   rI   r   r   rO   r^   r_   s   @r+   r   r   ^   s         "!##GE!//* 	  K2 2 2 2 2 2 2 2- - -        X0  8            r-   r   )senderc                 .    |                                   d S r'   )r   )instancecreatedrT   s      r+   script_module_post_save_handlerr      s    r-   ))r   logging	functoolsr   "django.contrib.contenttypes.fieldsr   	django.dbr   django.db.modelsr   django.db.models.signalsr   django.dispatchr   django.urlsr
   django.utils.translationr   r:   core.choicesr   core.modelsr   extras.utilsr   netbox.models.featuresr   r   utilities.querysetsr   mixinsr   __all__	getLoggerr~   r   Managerfrom_querysetra   r   r   r<   r-   r+   <module>r      s     % % % % % % > > > > > >             . . . . . . $ $ $ $ $ $       6 6 6 6 6 6 3 3 3 3 3 3 # # # # # # " " " " " " = = = = = = = = 2 2 2 2 2 2 % % % % % %
 
	1	2	28 8 8 8 8_i 8 8 8vo o o o o&.667IJJ o o oU U U U U$i U U Up 
)L)))  *)  r-   