
    >Tf                         d dl Z d dlZd dlmZ d dlZd dlmZ d dlm	Z	m
Z
 ej        ej        dZd Zd Zd Zd	 Zd
 Zd Z G d de	j                  ZdS )    N)SourceFileLoader)settings)
migrationsmodels)scriptsreportsc                 p    t           | j                 }t          j                            || j                  S )z@
    Return the full path to a ScriptModule's file on disk.
    )
ROOT_PATHS	file_rootospathjoin	file_path)scriptmodule	root_paths     H/var/www/html/netbox-4.1.3/netbox/extras/migrations/0109_script_model.pyget_full_pathr      s*     <12I7<<	<#9:::    c                     t           j                            | j                  \  }}t           j                            |          d         S )zB
    Return the Python name of a ScriptModule's file on disk.
    r   )r   r   splitr   splitext)r   r   filenames      r   get_python_namer      s9     W]]<#9::ND(7H%%a((r   c                     ddl m} ddlm} 	 t	          | |          r| |k    rdS t	          | |          r| |k    rdS n# t
          $ r Y nw xY wdS )zI
    Returns True if the passed Python object is a Script or Report.
    r   )Script)ReportTF)extras.scriptsr   extras.reportsr   
issubclass	TypeError)objr   r   s      r   	is_scriptr"   %   s     &%%%%%%%%%%%c6"" 	sf}}4c6"" 	sf}}4   5s   ? ? 
AAc                 `   d }t          t          |           t          |                     }	 |                                }n# t          $ r i cY S w xY wi }t          |dg           }|D ]}|| ||          <   t          j        |t                    D ]\  }}||vr|| ||          <   |S )z^
    Return a dictionary mapping of name and script class inside the passed ScriptModule.
    c                 F    | j                             dd          d         S )N.   )maxsplit)	full_namer   )clss    r   get_namez$get_module_scripts.<locals>.get_name:   s"    }""3"33A66r   script_order)	r   r   r   load_moduleFileNotFoundErrorgetattrinspect
getmembersr"   )r   r*   loadermoduler   orderedr)   names           r   get_module_scriptsr5   6   s    7 7 7 ol;;]<=X=XYYF##%%   			 Gfnb11G % %!$'	:: ) )	cg%(GHHSMM"Ns   A AAc                 6   |                      dd          }|                      dd          }|                      dd          }|                      dd          }|                      dd          }|j                            |d	
          }|j                            |d	
          }|j                            |d	
          }	|j                                        D ]}
t	          |
          D ]}|j                            ||
          }|j                            |j        |
j        |          	                    |j        |j                   |j                            |	j        |
j        |          	                    |j        |j                   dS )z
    Create a new Script object for each script inside each existing ScriptModule, and update any related jobs to
    reference the new Script object.
    contenttypesContentTypeextrasr   ScriptModuleReportModulecoreJobF)for_concrete_modelr4   r2   )object_type_id	object_idr4   )r@   rA   N)
	get_modelobjectsget_for_modelallr5   createfilteridpkupdate)appsschema_editorr8   r   r:   r;   r=   	script_ctscriptmodule_ctreportmodule_ctr2   script_namescripts                r   update_scriptsrR   P   s   
 ..??K^^Hh//F>>(N;;L>>(N;;L
..
'
'C#11&U1SSI!)77Y^7__O!)77Y^7__O&**,, G G-f55 	G 	GK^**  +  F K.1 )     fILFIfFFFK.1 )     fILFIfFFFF#	GG Gr   c                 P   |                      dd          }|                      dd          }|                      dd          }|                      dd          }|j                            |          }|j                            |          }|j                            |          D ]}}|j                            d          }	|j                            |j        |	d	d
i          \  }
}|j                            |j                  	                    ||
j
                   ~dS )z
    Update any existing EventRules for scripts. Change action_object_type from ScriptModule to Script, and populate
    the ID of the related Script object.
    r7   r8   r9   r   r:   	EventRule)action_object_typerP   is_executableF)	module_idr4   defaults)rI   )rU   action_object_idN)rB   rC   rD   rG   action_parametersgetget_or_createrY   rI   rJ   rH   )rK   rL   r8   r   r:   rT   rM   rN   	eventruler4   r!   createds               r   update_event_rulesr_   t   s,   
 ..??K^^Hh//F>>(N;;Lx55I#11&99I!)77EEO&---QQ p p	*..}==~330%u- 4 
 
W
 	  IL 1188Ihkhn8oooop pr   c                      e Zd ZdgZ ej        dd ej        ddd          fd ej        dd	          fd
 ej	        de
j        j        j        j        dd          fd ej        dd          fgddi           ej        d ej        dd                     ej        eej        j                   ej        eej        j                  gZdS )	Migration)r9   0108_convert_reports_to_scriptsr   rH   TF)auto_createdprimary_key	serializer4   O   )editable
max_lengthr2   r   zextras.scriptmodule)rg   	on_deleterelated_nametorV   )rg   defaultordering)r2   r4   )r4   fieldsoptionsrQ   r?    extras_script_unique_name_module)rn   r4   )
model_name
constraint)codereverse_codeN)__name__
__module____qualname__dependenciesr   CreateModelr   BigAutoField	CharField
ForeignKeydjangodbdeletionCASCADEBooleanFieldAddConstraintUniqueConstraint	RunPythonrR   noopr_   
operations r   r   ra   ra      s`        	6L
 	
*v*$Z_```a))5RHHHI,6,evyGWG`Gh  xA  F[  \  \  \  ] "5&"5ud"S"S"ST	 .	
 	
 	
 	!
 .v.6HOqrrr	
 	
 	
 	
#-2	
 	
 	
 	
##-2	
 	
 	
+JJJr   ra   )r/   r   importlib.machineryr   django.db.models.deletionr}   django.confr   	django.dbr   r   SCRIPTS_ROOTREPORTS_ROOTr
   r   r   r"   r5   rR   r_   ra   r   r   r   <module>r      s    				 0 0 0 0 0 0                     ( ( ( ( ( ( ( ( $$ 
; ; ;) ) )  "  4!G !G !GHp p p.    
$     r   