
    >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	m
Z
mZmZ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ZdZ G d de          Z G d de          Z G d de          Z G d de          ZdS )    N)OrderedDict)OpenApiSerializerFieldExtension)
AutoSchema)build_basic_typebuild_choice_fieldbuild_media_type_objectbuild_object_typeget_doc)OpenApiTypes)serializers)ManyRelatedField)ChoiceFieldSerializedPKRelatedField)WritableNestedSerializer)bulk_destroybulk_partial_updatebulk_update)PATCHPOSTPUTc                       e Zd ZdZd ZdS )FixTimeZoneSerializerFieldz5timezone_field.rest_framework.TimeZoneSerializerFieldc                 4    t          t          j                  S N)r   r   STR)selfauto_schema	directions      4/var/www/html/netbox-4.1.3/netbox/core/api/schema.pymap_serializer_fieldz/FixTimeZoneSerializerField.map_serializer_field   s     0111    N__name__
__module____qualname__target_classr     r!   r   r   r      s(        JL2 2 2 2 2r!   r   c                       e Zd ZdZd ZdS )ChoiceFieldFixznetbox.api.fields.ChoiceFieldc                 (   t          | j                  }|dk    r|S |dk    ro|}i t          t          j                  dt          t          j        | j        j        	                                                    i}t          ||d          S d S )Nrequestresponseenum)valuelabel)
properties)r   targetr   r   r   listr   fromkeyschoicesvaluesr	   )r   r   r   build_cfr.   r/   s         r   r    z#ChoiceFieldFix.map_serializer_field    s    %dk22	!!O*$$E|'(899|64H\]a]h]p]w]w]y]yHzHzC{C{||E$""    	 %$r!   Nr"   r'   r!   r   r)   r)      s(        2L    r!   r)   c                        e Zd ZdZi Zed             Z fdZdej	        f fdZ
dej	        f fdZddef fdZd	 Zd
 Z fdZ fdZd fd	Zd Zd Z xZS )NetBoxAutoSchemaa`  
    Overrides to drf_spectacular.openapi.AutoSchema to fix following issues:
        1. bulk serializers cause operation_id conflicts with non-bulk ones
        2. bulk operations should specify a list
        3. bulk operations don't have filter params
        4. bulk operations don't have pagination
        5. bulk delete should specify input
    c                 Z    t          | j        d          r| j        j        t          v rdS dS )NactionTF)hasattrviewr:   BULK_ACTIONS)r   s    r   is_bulk_actionzNetBoxAutoSchema.is_bulk_action>   s/    49h'' 	DI,<,L,L45r!   c                    | j         r|                                 }d |D             }| j        dk    r|                                 rd}n| j        j                                        }|s|                    d           t          j	        d| j
                  r|                    d           d                    ||gz             S t                                                      S )aP  
        bulk serializers cause operation_id conflicts with non-bulk ones
        bulk operations cause id conflicts in spectacular resulting in numerous:
        Warning: operationId "xxx" has collisions [xxx]. "resolving with numeral suffixes"
        code is modified from drf_spectacular.openapi.AutoSchema.get_operation_id
        c                 :    g | ]}|                     d d          S )-_)replace).0ts     r   
<listcomp>z5NetBoxAutoSchema.get_operation_id.<locals>.<listcomp>O   s&    JJJaaiiS11JJJr!   GETr2   rootz<drf_format_suffix\w*:\w+>	formattedrB   )r>   _tokenize_pathmethod_is_list_viewr<   r:   lowerappendresearch
path_regexjoinsuperget_operation_id)r   tokenized_pathr:   	__class__s      r   rT   z!NetBoxAutoSchema.get_operation_idE   s      	7!0022NJJ>JJJN{e##(:(:(<(<# )//11! .%%f---y6HH 3%%k22288NfX5666 ww'')))r!   returnc                 r   t                                                      }| j        r t          |          d          S |s| j        t
          v re|                     |          }|Nt          |d          r-|                     |j                  } ||j	        |          }n ||j	                  }|S )NTmanychild)contextr[   )r\   )
rS   get_request_serializerr>   typerK   WRITABLE_ACTIONSget_writable_classr;   r[   r\   )r   
serializerwritable_classchild_serializerrV   s       r   r]   z'NetBoxAutoSchema.get_request_serializerd   s    WW3355
 	/#4
##.... !dk5E&E&E!44Z@@N):w// L'+'>'>z?O'P'P$!/
8JRb!c!c!cJJ!/
8J!K!K!KJr!   c                     t                                                      }| j        r t          |          d          S |S )NTrY   )rS   get_response_serializersr>   r^   )r   response_serializersrV   s     r   re   z)NetBoxAutoSchema.get_response_serializersx   sF    $ww??AA 	9-4,--48888##r!   Fc                 z    t                                          |||          }t          |dd          rd| }|S )NnestedFBrief)rS   _get_serializer_namegetattr)r   ra   r   bypass_extensionsnamerV   s        r   rj   z%NetBoxAutoSchema._get_serializer_name   sE    ww++J	CTUU :x// 	"!4>>Dr!   c                     t          |dd          }t          |          j        }t          |d          r|j        }n/|}|                    d          r|dt          d                    }|S )zGet serializer's ref_name
        :param serializer: Serializer instance
        :return: Serializer's ``ref_name`` or ``None`` for inline serializer
        :rtype: str or None
        MetaNref_name
Serializer)rk   r^   r#   r;   rp   endswithlen)r   ra   serializer_metaserializer_namerp   s        r   get_serializer_ref_namez(NetBoxAutoSchema.get_serializer_ref_name   s|     "*fd;;z**3?J// 	:&/HH&H  .. :#$8s<'8'8&8$89r!   c                    i }t          |d          ri n|j        }g }|                                D ]S\  }}dt          |          v r|j        r|                    |           t          |t          t          f          rd ||<   T|sd S t          |          | j
        vrdt          |          j        z   }t          t          |          dd           }|r`d|                     |          z   }	t          |j                  }|D ]}
|                    |
           t          d|f|	|d          }||d<   t          |t          |          f|          | j
        t          |          <   | j
        t          |                   }|S )Nr[   	read_onlyWritablero   )rp   fields)r;   rz   itemsdirrx   rN   
isinstancer   r   r^   writable_serializersr#   rk   rv   r2   remove)r   ra   r0   rz   remove_fields
child_namer[   writable_name
meta_classrp   fieldwritable_metarb   s                r   r`   z#NetBoxAutoSchema.get_writable_class   s   
z733J9J!' 	. 	.Jc%jj((U_($$Z000%+/G!HII .)-
:& 	4
4#<<<&j)9)9)BBM j!1!164@@J 3%(D(DZ(P(PPj/00* ) )EMM%(((( $Vj]]c<d<d e e%2
6":>}tT^O_O_Nacm:n:nD%d:&6&6724
3C3CDr!   c                 V    | j         rg S t                                                      S r   )r>   rS   get_filter_backendsr   rV   s    r   r   z$NetBoxAutoSchema.get_filter_backends   s)     	Iww**,,,r!   c                 V    | j         rd S t                                                      S r   )r>   rS   _get_paginatorr   s    r   r   zNetBoxAutoSchema._get_paginator   s)     	4ww%%'''r!   r+   c                 P   	
  j         r j        dk    r!t                                                    S                                  	t          	t                    rog }d}	                                D ]U\  }}                     |          \  
} 	                    ||          }
8|
                    |
|f           ||z  }Vn@                     	          \  
}
d S 	
 fd                                 D             }dd |D             i}|r||d<   |S )NDELETETc           	      D    g | ]}|                     |          fS r'   )_get_examples)rD   
media_typer   request_serializerschemar   s     r   rF   z6NetBoxAutoSchema._get_request_body.<locals>.<listcomp>   sD        VT%7%78JIWa%b%bc  r!   contentc                 8    i | ]\  }}}|t          ||          S r'   )r   )rD   r   r   exampless       r   
<dictcomp>z6NetBoxAutoSchema._get_request_body.<locals>.<dictcomp>   s:       Jf*V\^f
3FHEE  r!   required)r>   rK   rS   _get_request_bodyr]   r}   dictr{   _get_request_for_media_typer   rN   map_parsers)r   r   r   request_body_requiredr   ra   partial_request_body_requiredr   request_bodyr   r   rV   s   ``       @@r   r   z"NetBoxAutoSchema._get_request_body   s   # 	8)@)@77,,Y777
 "88::($// 	G$(!*<*B*B*D*D G G&
J8<8X8XYcen8o8o55--j)ZPP>
FH=>>>%)FF%%G -1,L,LM_aj,k,k)F)~t      "&"2"2"4"4  G   jq  

 ! 	='<L$r!   c           	      &   | j         j        rt          | j         j                  S t	          | j         t	          | j         d| j                                                  d          }|r|j        rt          |          S |                                 S )z>
        Return a string description for the ViewSet.
        r:   N)r<   __doc__r
   rV   rk   rK   rM   _generate_description)r   action_or_methods     r   get_descriptionz NetBoxAutoSchema.get_description   s     9 	049./// #49gdi4;K\K\K^K^._._aeff 	- 0 8 	-+,,, ))+++r!   c                     | j         j        j        j        j        }d| j        v r| j                                         d| dS | j                                         d| dS )z{
        Generate a docstring for the method. It also takes into account whether the method is for list or detail.
        z{id}z a z object.z a list of z	 objects.)r<   querysetmodel_metaverbose_namepathrK   
capitalize)r   
model_names     r   r   z&NetBoxAutoSchema._generate_description   sp     Y'-3@
 TYk,,..GG:GGGG+((**LLzLLLLr!   )F)r+   )r#   r$   r%   r   r~   propertyr>   rT   typingAnyr]   re   strrj   rv   r`   r   r   r   r   r   __classcell__)rV   s   @r   r8   r8   2   sq           X* * * * *>
      ($&* $ $ $ $ $ $ VY        "  B- - - - -( ( ( ( ($ $ $ $ $ $L, , ,"	M 	M 	M 	M 	M 	M 	Mr!   r8   c                       e Zd ZdZd ZdS )FixSerializedPKRelatedFieldz*netbox.api.fields.SerializedPKRelatedFieldc                     |dk    r+|                     | j        j        |          }|r|j        nd S t	          t
          j                  S )Nr,   )resolve_serializerr1   ra   refr   r   INT)r   r   r   	components       r   r    z0FixSerializedPKRelatedField.map_serializer_field  sI    
""#66t{7MyYYI$-79==47#L$4555r!   Nr"   r'   r!   r   r   r     s(        ?L6 6 6 6 6r!   r   )rO   r   collectionsr   drf_spectacular.extensionsr   drf_spectacular.openapir   drf_spectacular.plumbingr   r   r   r	   r
   drf_spectacular.typesr   rest_frameworkr   rest_framework.relationsr   netbox.api.fieldsr   r   netbox.api.serializersr   r=   r_   r   r)   r8   r   r'   r!   r   <module>r      s   				  # # # # # # F F F F F F . . . . . .              / . . . . . & & & & & & 5 5 5 5 5 5 C C C C C C C C ; ; ; ; ; ; F+ 2 2 2 2 2!@ 2 2 2    4   *VM VM VM VM VMz VM VM VMr6 6 6 6 6"A 6 6 6 6 6r!   