
    >Tf%                        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 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 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/m0Z0 d dl1m2Z2 ddl3m4Z4 ddl5m6Z6  G d de          Z7 G d d e.          Z8 G d! d"e.          Z9 G d# d$e.          Z: G d% d&e.          Z; G d' d(e.          Z< G d) d*e(e.          Z= G d+ d,e.          Z> G d- d.e.          Z? G d/ d0e.          Z@ G d1 d2e.          ZA G d3 d4e.          ZB G d5 d6e.          ZC G d7 d8e.          ZD G d9 d:e.          ZE G d; d<e(e.          ZF G d= d>e(e6e.          ZG e
 e	e4jH        ?           e	e4jH        ?          @           G dA dBe                      ZI G dC dDe          ZJ G dE dFe          ZKdGS )H    )Http404)get_object_or_404)import_string)get_connection)extend_schemaextend_schema_viewstatus)action)PermissionDenied)RetrieveUpdateDestroyAPIView)JSONRenderer)Response)APIRootView)ModelViewSetReadOnlyModelViewSet)Worker)
ObjectType)
filtersets)*)	ScriptJob)!IsAuthenticatedOrLoginNotRequired)SyncedDataMixin)ContentTypeMetadata)TextRenderer)NetBoxModelViewSet)RQWorkerNotRunningException)copy_safe_request   )serializers)ConfigTemplateRenderMixinc                       e Zd ZdZd ZdS )ExtrasRootViewz
    Extras API root view
    c                     dS )NExtras selfs    5/var/www/html/netbox-4.1.3/netbox/extras/api/views.pyget_view_namezExtrasRootView.get_view_name#   s    x    N)__name__
__module____qualname____doc__r*   r&   r+   r)   r#   r#      s-             r+   r#   c                   `    e Zd ZeZej                                        Ze	j
        Zej        ZdS )EventRuleViewSetN)r,   r-   r.   r   metadata_class	EventRuleobjectsallquerysetr    EventRuleSerializerserializer_classr   EventRuleFilterSetfilterset_classr&   r+   r)   r1   r1   +   s8        (N $$&&H"6 3OOOr+   r1   c                   `    e Zd ZeZej                                        Ze	j
        Zej        ZdS )WebhookViewSetN)r,   r-   r.   r   r2   Webhookr4   r5   r6   r    WebhookSerializerr8   r   WebhookFilterSetr:   r&   r+   r)   r<   r<   6   s7        (N""$$H"4 1OOOr+   r<   c                   b    e Zd ZeZej                            d          Ze	j
        Zej        ZdS )CustomFieldViewSet
choice_setN)r,   r-   r.   r   r2   CustomFieldr4   select_relatedr6   r    CustomFieldSerializerr8   r   CustomFieldFilterSetr:   r&   r+   r)   rA   rA   A   s:        (N"11,??H"8 5OOOr+   rA   c                       e Zd Zej                                        Zej        Z	e
j        Z ed          d             ZdS )CustomFieldChoiceSetViewSetT)detailc                 0   t          | j        |          }|j        }|j                            d          xr"                                fd|D             }|                     |          x}rd |D             }ng }|                     |          S )zO
        Provides an endpoint to iterate through each choice in a set.
        pkqc                     g | ]<}|d                                           v s|d                                          v :|=S )r   r   )lower).0crM   s     r)   
<listcomp>z7CustomFieldChoiceSetViewSet.choices.<locals>.<listcomp>X   sE    TTTQQ!A$**,,->->!qtzz||BSBSqBSBSBSr+   c                 0    g | ]}|d          |d         dS )r   r   )iddisplayr&   )rP   rQ   s     r)   rR   z7CustomFieldChoiceSetViewSet.choices.<locals>.<listcomp>\   s5       23qt!--  r+   )r   r6   choicesGETgetrO   paginate_querysetget_paginated_response)r(   requestrL   	choicesetrV   pagedatarM   s          @r)   rV   z#CustomFieldChoiceSetViewSet.choicesM   s    
 &dm;;;	# $$$1 	U		ATTTT'TTTG ))'2224 	 7;  DD D**4000r+   N)r,   r-   r.   CustomFieldChoiceSetr4   r5   r6   r    CustomFieldChoiceSetSerializerr8   r   CustomFieldChoiceSetFilterSetr:   r   rV   r&   r+   r)   rH   rH   H   s`        #+//11H"A >OV41 1 1 1 1r+   rH   c                   `    e Zd ZeZej                                        Ze	j
        Zej        ZdS )CustomLinkViewSetN)r,   r-   r.   r   r2   
CustomLinkr4   r5   r6   r    CustomLinkSerializerr8   r   CustomLinkFilterSetr:   r&   r+   r)   rc   rc   i   s8        (N!%%''H"7 4OOOr+   rc   c                   `    e Zd ZeZej                                        Ze	j
        Zej        ZdS )ExportTemplateViewSetN)r,   r-   r.   r   r2   ExportTemplater4   r5   r6   r    ExportTemplateSerializerr8   r   ExportTemplateFilterSetr:   r&   r+   r)   rh   rh   t   s8        (N%))++H"; 8OOOr+   rh   c                   `    e Zd ZeZej                                        Ze	j
        Zej        ZdS )SavedFilterViewSetN)r,   r-   r.   r   r2   SavedFilterr4   r5   r6   r    SavedFilterSerializerr8   r   SavedFilterFilterSetr:   r&   r+   r)   rm   rm      s8        (N"&&((H"8 5OOOr+   rm   c                   `    e Zd ZeZej                                        Ze	j
        Zej        ZdS )BookmarkViewSetN)r,   r-   r.   r   r2   Bookmarkr4   r5   r6   r    BookmarkSerializerr8   r   BookmarkFilterSetr:   r&   r+   r)   rr   rr      s8        (N##%%H"5 2OOOr+   rr   c                   R    e Zd ZeZej                                        Ze	j
        ZdS )NotificationViewSetN)r,   r-   r.   r   r2   Notificationr4   r5   r6   r    NotificationSerializerr8   r&   r+   r)   rw   rw      2        (N#''))H"9r+   rw   c                   N    e Zd Zej                                        Zej        Z	dS )NotificationGroupViewSetN)
r,   r-   r.   NotificationGroupr4   r5   r6   r    NotificationGroupSerializerr8   r&   r+   r)   r|   r|      s-         (,,..H">r+   r|   c                   R    e Zd ZeZej                                        Ze	j
        ZdS )SubscriptionViewSetN)r,   r-   r.   r   r2   Subscriptionr4   r5   r6   r    SubscriptionSerializerr8   r&   r+   r)   r   r      rz   r+   r   c                   \    e Zd Zej                                        Zej        Z	e
j        ZdS )
TagViewSetN)r,   r-   r.   Tagr4   r5   r6   r    TagSerializerr8   r   TagFilterSetr:   r&   r+   r)   r   r      s0        {  H"0 -OOOr+   r   c                   `    e Zd ZeZej                                        Ze	j
        Zej        ZdS )ImageAttachmentViewSetN)r,   r-   r.   r   r2   ImageAttachmentr4   r5   r6   r    ImageAttachmentSerializerr8   r   ImageAttachmentFilterSetr:   r&   r+   r)   r   r      s8        (N&**,,H"< 9OOOr+   r   c                   `    e Zd ZeZej                                        Ze	j
        Zej        ZdS )JournalEntryViewSetN)r,   r-   r.   r   r2   JournalEntryr4   r5   r6   r    JournalEntrySerializerr8   r   JournalEntryFilterSetr:   r&   r+   r)   r   r      s8        (N#''))H"9 6OOOr+   r   c                   \    e Zd Zej                                        Zej        Z	e
j        ZdS )ConfigContextViewSetN)r,   r-   r.   ConfigContextr4   r5   r6   r    ConfigContextSerializerr8   r   ConfigContextFilterSetr:   r&   r+   r)   r   r      s3        $((**H": 7OOOr+   r   c                       e Zd Zej                                        Zej        Z	e
j        Z eddgeeg          d             ZdS )ConfigTemplateViewSetTpost)rI   methodsrenderer_classesc                 f    |                                  }|j        }|                     |||          S )z
        Render a ConfigTemplate using the context data provided (if any). If the client requests "text/plain" data,
        return the raw rendered content, rather than serialized JSON.
        )
get_objectr^   render_configtemplate)r(   r[   rL   configtemplatecontexts        r)   renderzConfigTemplateViewSet.render   s2     **,))'>7KKKr+   N)r,   r-   r.   ConfigTemplater4   r5   r6   r    ConfigTemplateSerializerr8   r   ConfigTemplateFilterSetr:   r   r   r   r   r&   r+   r)   r   r      sq        %))++H"; 8OV4&\<<XYYYL L ZYL L Lr+   r   )r[   )updatepartial_updatec                   |    e Zd ZegZej                                        Ze	j
        Zej        ZdZdZd Zd Zd ZdS )ScriptViewSetTz[^/]+c                     |                                 rt          | j        |          S 	 |                    dd          \  }}n# t          $ r t
          w xY wt          | j        | d|          S )NrK   .r   )maxsplitz.py)module__file_pathname)	isnumericr   r6   split
ValueErrorr   )r(   rL   module_namescript_names       r)   _get_scriptzScriptViewSet._get_script   s    <<>> 	;$T]r::::	')xxax'@'@$K 	 	 	M	 [BUBUBU\ghhhhs   A Ac                     |                      |          }t          j        |d|i          }t          |j                  S )Nr[   r   )r   r    ScriptDetailSerializerr   r^   )r(   r[   rL   script
serializers        r)   retrievezScriptViewSet.retrieve  s@    !!"%% 7T[H\]]]

(((r+   c                    |j                             d          st          d          |                     |          }t	          j        |j        d|i          }t          j        t          d                    st                      |                                rt          j        ||j         |j        d         t          |          |j        d         |j        j        |j                            d          |j                            d	          
           t	          j        |d|i          }t)          |j                  S t)          |j        t,          j                  S )zu
        Run a Script identified by its numeric PK or module & name and return the pending Job as the result
        zextras.run_scriptz2This user does not have permission to run scripts.r   )r^   r   defaultr^   commitschedule_atinterval)instanceuserr^   r[   r   job_timeoutr   r   r[   r   r	   )r   has_permr   r   r    ScriptInputSerializerr^   r   countr   r   is_validr   enqueuer   python_classr   validated_datarX   r   r   errorsr
   HTTP_400_BAD_REQUEST)r(   r[   rL   r   input_serializerr   s         r)   r   zScriptViewSet.post  s\    |$$%899 	Y"#WXXX!!"%%&<v&
 
 
 |N95566 	0-///$$&& 	-\%*62)'22',X6"/;,;??NN)8<<ZHH	 	 	 	 %;FYX_L`aaaJJO,,,(/8STTTTr+   N)r,   r-   r.   r   permission_classesScriptr4   r5   r6   r    ScriptSerializerr8   r   ScriptFilterSetr:   _ignore_model_permissionslookup_value_regexr   r   r   r&   r+   r)   r   r      s        
 <<~!!##H"3 0O $ 
i 
i 
i) ) ) U  U  U  U  Ur+   r   c                   j    e Zd ZdZegZej                            dd          Z	e
j        Zej        ZdS )ObjectTypeViewSetz(
    Read-only list of ObjectTypes.
    	app_labelmodelN)r,   r-   r.   r/   r   r   r   r4   order_byr6   r    ObjectTypeSerializerr8   r   ObjectTypeFilterSetr:   r&   r+   r)   r   r   /  sL          <<!**;@@H"7 4OOOr+   r   c                   T    e Zd Zej                                        Zej        Z	d Z
dS )DashboardViewc                 z    t           j                            | j        j                                                  S )N)r   )	Dashboardr4   filterr[   r   firstr'   s    r)   r   zDashboardView.get_objectA  s-     ''T\->'??EEGGGr+   N)r,   r-   r.   r   r4   r5   r6   r    DashboardSerializerr8   r   r&   r+   r)   r   r   =  sE         $$&&H"6H H H H Hr+   r   N)Ldjango.httpr   django.shortcutsr   django.utils.module_loadingr   django_rq.queuesr   drf_spectacular.utilsr   r   rest_frameworkr
   rest_framework.decoratorsr   rest_framework.exceptionsr   rest_framework.genericsr   rest_framework.renderersr   rest_framework.responser   rest_framework.routersr   rest_framework.viewsetsr   r   rqr   core.modelsr   extrasr   extras.modelsextras.jobsr   netbox.api.authenticationr   netbox.api.featuresr   netbox.api.metadatar   netbox.api.renderersr   netbox.api.viewsetsr   utilities.exceptionsr   utilities.requestr    r    mixinsr!   r#   r1   r<   rA   rH   rc   rh   rm   rr   rw   r|   r   r   r   r   r   r   r   r   r   r   r&   r+   r)   <module>r      s         . . . . . . 5 5 5 5 5 5 + + + + + + C C C C C C C C ! ! ! ! ! ! , , , , , , 6 6 6 6 6 6 @ @ @ @ @ @ 1 1 1 1 1 1 , , , , , , . . . . . . F F F F F F F F       " " " " " "           ! ! ! ! ! ! G G G G G G / / / / / / 3 3 3 3 3 3 - - - - - - 2 2 2 2 2 2 < < < < < < / / / / / /       - - - - - -    [   4 4 4 4 4) 4 4 42 2 2 2 2' 2 2 26 6 6 6 6+ 6 6 61 1 1 1 1"4 1 1 1B5 5 5 5 5* 5 5 59 9 9 9 9O-? 9 9 96 6 6 6 6+ 6 6 63 3 3 3 3( 3 3 3: : : : :, : : :? ? ? ? ?1 ? ? ?
: : : : :, : : :. . . . .# . . .: : : : :/ : : :7 7 7 7 7, 7 7 78 8 8 8 8?,> 8 8 8L L L L LO-FHZ L L L* =!BCCC =)JKKK  ;U ;U ;U ;U ;UL ;U ;U	 ;UD5 5 5 5 5, 5 5 5H H H H H0 H H H H Hr+   