
    >Tf_                        d dl Z d dlmZmZmZ d dlmZ ddlmZm	Z	 ddl
mZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZmZ  eee	          d
             Z eee	          d             Z eee	          d             Z eee	          d             Z eee	          dd            Z eee	          d             Z eee	          d             Z  eee	          d             Z!dS )    N)	post_savepost_delete
pre_delete)receiver   )CableEndChoicesLinkStatusChoices)
Cable	CablePathCableTerminationDevice	FrontPortPathEndpoint
PowerPanelRackLocationVirtualChassis)trace_paths)create_cablepathrebuild_paths)senderc                    |s>|                                                      | j                   |                      d                              dd          }t          j                            |                              | j                   t          j                            |                              | j                   t          j                            |                              | j                   t          j                            |                              | j                   d	S d	S )
z
    Update child objects if Site assignment has changed. We intentionally recurse through each child
    object instead of calling update() on the QuerySet to ensure the proper change records get created for each.
    )siteT)include_selfpk)flat)location__in)_location__in)_siteN)
get_descendantsupdater   values_listr   objectsfilterr   r   r   )instancecreatedkwargs	locationss       1/var/www/html/netbox-4.1.3/netbox/dcim/signals.pyhandle_location_site_changer*      s      ]  ""))x})===,,$,??KKDW[K\\	33:::NNN955<<(-<PPP!!y!99@@hm@TTT ''i'@@GGhmG\\\\\] ]    c                     |sAt           j                            |                               | j        | j                   dS dS )zJ
    Update child Devices if Site or Location assignment has changed.
    )rack)r   locationN)r   r#   r$   r!   r   r.   )r%   r&   r'   s      r)   handle_rack_site_changer/   !   sQ    
  d8,,33QYQb3cccccd dr+   c                     |rU| j         rPt          j                            | j         j                  }| |_        d|_        |                                 dS dS dS )zf
    When a VirtualChassis is created, automatically assign its master device (if any) to the VC.
    r   r   N)masterr   r#   getr   virtual_chassisvc_positionsave)r%   r&   r'   r2   s       r)   assign_virtualchassis_masterr7   .   sg    
  8? ##x'9#::!)	   r+   c                     t           j                            | j                  }|D ]$}d|_        d|_        |                                 %dS )zp
    When a VirtualChassis is deleted, nullify the vc_position and vc_priority fields of its prior members.
    )r4   N)r   r#   r$   r   r5   vc_priorityr6   )r%   r'   devicesdevices       r)   clear_virtualchassis_membersr<   :   sV    
 n##HK#@@G  !! r+   Fc                    t          j        d          }|r|                    d|             dS | j        rg }g }| j                                        D ]L}|j        t          j        k    r|	                    |j
                   2|	                    |j
                   M||fD ]?}|st          |d         t                    rt          |           0t          |           @dS | j        | j        k    r]| j        t"          j        k    r6t&          j                            |                               d           dS t          | g           dS dS )zX
    When a Cable is saved with new terminations, retrace any affected cable paths.
    znetbox.dcim.cablez-Skipping endpoint updates for imported cable Nr   _nodes__containsF)	is_active)logging	getLoggerdebug_terminations_modifiedterminationsall	cable_endr   SIDE_Aappendtermination
isinstancer   r   r   status_orig_statusr	   STATUS_CONNECTEDr   r#   r$   r!   )	r%   r&   rawr'   loggera_terminationsb_terminationstnodess	            r)   update_connected_endpointsrU   J   s   
 233F
 OXOOPPP & &&**,, 	5 	5A{o444%%am4444%%am4444$n5 	% 	%E %(L11 % ''''e$$$$	% 	% 
H1	1	1?/@@@$$h$??FFQVFWWWWW8*%%%%%	 
2	1r+   c                 t    t           j                            |           D ]}|                                 dS )zO
    When a Cable is deleted, check for and update its connected endpoints
    r>   N)r   r#   r$   retrace)r%   r'   	cablepaths      r)   retrace_cable_pathsrY   n   sF    
 &--x-HH  	 r+   c                 t   | j                                         }|j                            | j                                      dd           t          j                            | j                  D ]C}| j        |j	        v r|j	        
                    | j                   |                                 DdS )zb
    Disassociate the Cable from the termination object, and retrace any affected CablePaths.
    r1   N )cablerG   r>   )termination_typemodel_classr#   r$   termination_idr!   r   r\   rJ   originsremoverW   )r%   r'   modelrX   s       r)   nullify_connected_endpointsrc   w   s    
 %1133E	MH344;;$RT;UUU&--x~-NN  	9#444$$X%9:::	 r+   c                     |r@|s@| j         }t          j                            |          D ]}|                                 dS dS dS )zl
    When a new FrontPort is created, add it to any CablePaths which end at its corresponding RearPort.
    r>   N)	rear_portr   r#   r$   rW   )r%   r&   rO   r'   rearportrX   s         r)   extend_rearport_cable_pathsrg      sq    
   s  %"*1181LL 	  	 I       	  	 r+   )F)"rA   django.db.models.signalsr   r   r   django.dispatchr   choicesr   r	   modelsr
   r   r   r   r   r   r   r   r   r   models.cablesr   utilsr   r   r*   r/   r7   r<   rU   rY   rc   rg    r+   r)   <module>ro      si    G G G G G G G G G G $ $ $ $ $ $ 7 7 7 7 7 7 7 7                        ' & & & & & 2 2 2 2 2 2 2 2 
)H%%%] ] &%] 
)D!!!d d "!d 
)N+++  ,+ 
*^,,,  -, 
+e$$$ &  &  & %$ &F 
+e$$$  %$ 
+.///  0/ 
)I&&&    '&     r+   