
    >Tfp                        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T 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"  e j#        d          Z$d Z%d Z&d Z'ddZ(d Z)d Z*dS )    N)defaultdict)settings)ContentType)timezone)import_string)gettext)	get_queue)*)
get_config)RQ_QUEUE_DEFAULT)registry)User)get_serializer_for_model)get_rq_retry)serialize_object   )EventRuleActionChoices)	EventRuleznetbox.events_processorc                 Z    t          | j                  }ddi} || |          }|j        S )zf
    Return a serialized representation of the given instance suitable for use in a queued event.
    requestN)context)r   	__class__data)instanceserializer_classserializer_context
serializers       2/var/www/html/netbox-4.1.3/netbox/extras/events.pyserialize_for_eventr      sB     00BCC4 "!(4FGGGJ?    c                     t          | dd           d d}|t          k    r:t          | d          r|                                 |d<   nt          |           |d<   |S )N_prechange_snapshot)	prechange
postchanger   r$   )getattrOBJECT_DELETEDhasattrr   )r   
event_type	snapshotss      r   get_snapshotsr*   &   ss    X'<dCC I ^##8/00 	A&.&?&?&A&AIl##&6x&@&@Il#r    c                    |j         j        }|j         j        }|t          d         d                             |g           vrdS |j        J | d| d|j         }|| v rWt          |          | |         d<   t          ||          d         | |         d         d<   |t          k    r|| |         d	<   dS dS t          j
                            |          |j        |t          |          t          ||          |j        |d
| |<   dS )z
    Enqueue a serialized representation of a created/updated/deleted object for the processing of
    events once the request has completed.
    model_featuresevent_rulesN.:r   r$   r)   r(   )object_type	object_idr(   r   r)   username
request_id)_meta	app_label
model_namer   getpkr   r*   r&   r   objectsget_for_modelr2   )queuer   userr3   r(   r5   r6   keys           r   enqueue_eventr>   5   s*    (I*J"23MBFFyRTUUUU;"""
3
3
3
3hk
3
3C
e||0::c
60=h
0S0ST`0ac
;-'''1E#J|$$$ (' '.<<XFF!$'11&x<<$
 
c


r    c           
         |r t           j                            |          nd }| D ]}|                    |          s|j        t
          j        k    rt                      j                            dt                    }	t          |	          }
||j        |||t          j                                                    |t                      d}|r||d<   |r||d<    |
j        	 di | |j        t
          j        k    rD|j                                        }ddlm} |                    |j        |j        ||	           |j        t
          j        k    r9|j                            ||d
         |                    d          |           lt3          t5          d                              |j                            d S )N)r2   webhook)
event_ruler6   r(   r   r)   	timestampr2   retryr)   r3   extras.webhooks.send_webhookr   )	ScriptJob)r   namer<   r   iddisplay)r0   r1   object_reprr(   z4Unknown action type for an event rule: {action_type})action_type)rD   )r   r9   r7   eval_conditionsrJ   r   WEBHOOKr   QUEUE_MAPPINGSr   r	   modelr   now	isoformatr   enqueueSCRIPTaction_objectpython_classextras.jobsrE   rF   NOTIFICATIONnotify
ValueError_format)r-   r0   r(   r   r2   r)   r3   r<   rA   
queue_namerq_queueparamsscriptrE   s                 r   process_event_rulesr_   T   s+   2:D4<X...D! > >
 ))$// 	 !%;%CCC $488DTUUJ ,,H ))/(&%\^^5577$%	 	F  0&/{# 2'1|$ H.     #'='DDD-::<<F .-----#1[	      #'='JJJ$++'t* HHY//%	 ,     QUVV]]&2 ^     y> >r    c                 T   t          t                    }| D ]}|d         }|d         }|||         vr2t          j                            |d         g|d          ||         |<   ||         |         }t          |||d         |d         |d         |d         |d         	           d
S )zO
    Flush a list of object representation to RQ for EventRule processing.
    r(   r0   T)event_types__containsobject_typesenabledr   r2   r)   r3   )r-   r0   r(   r   r2   r)   r3   N)r   dictr   r9   filterr_   )eventsevents_cacheeventr(   r0   r-   s         r   process_event_queueri      s     t$$L 
 
<(
M* l:6664=4E4L4L',\':&;( 5M 5 5L$[1
 #:.{;##\*v:&K(\*	
 	
 	
 	
 	

 
r    c                    | r}t           j        D ]r}	 t          |          } ||            # t          $ rF}t                              t          d                              ||                     Y d}~id}~ww xY wdS dS )zL
    Flush a list of object representations to RQ for event processing.
    z3Cannot import events pipeline {name} error: {error})rF   errorN)r   EVENTS_PIPELINEr   	Exceptionloggerrk   rY   rZ   )rf   rF   funces       r   flush_eventsrq      s      r, 	r 	rDr$T**V r r rQTUU\\bfno\ppqqqqqqqqrr r	r 	rs   -
A=<A88A=)NNN)+loggingcollectionsr   django.confr   "django.contrib.contenttypes.modelsr   django.utilsr   django.utils.module_loadingr   django.utils.translationr   rY   	django_rqr	   core.eventsnetbox.configr   netbox.constantsr   netbox.registryr   users.modelsr   utilities.apir   utilities.rqworkerr   utilities.serializationr   choicesr   modelsr   	getLoggerrn   r   r*   r>   r_   ri   rq    r    r   <module>r      s    # # # # # #             : : : : : : ! ! ! ! ! ! 5 5 5 5 5 5 1 1 1 1 1 1           $ $ $ $ $ $ - - - - - - $ $ $ $ $ $       2 2 2 2 2 2 + + + + + + 4 4 4 4 4 4 + + + + + +      		4	5	5
 
 
  
 
 
>A A A AH
 
 
<
r 
r 
r 
r 
rr    