
    >TfO                         d dl mZ d dlmZmZmZmZ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mZ d dlmZ ddlmZm Z  ddl!m"Z" dZ#ddZ$d Z%d Z&d ZddZ'ddZ(d Z)dS )    )GenericForeignKey)FieldDoesNotExist
FieldErrorMultipleObjectsReturnedObjectDoesNotExistValidationError)ManyToOneRelRelatedField)reverse)import_string)gettext_lazy)
Serializer)get_view_name)HTTP_CONTENT_TYPE_JSON)GraphQLTypeNotFoundSerializerNotFound)RelatedObjectCountField   )count_relateddict_to_filter_params)title)get_annotations_for_serializerget_graphql_type_for_modelget_prefetches_for_serializerget_related_object_by_attrsget_serializer_for_modelr   is_api_request c           
          | j         j                            d          \  }}| d| | d}	 t          |          S # t          $ r t          d| d| d| d          w xY w)zI
    Return the appropriate REST API serializer for the given model.
    .z.api.serializers.r   z#Could not determine serializer for z with prefix '')_metalabelsplitr   ImportErrorr   )modelprefix	app_label
model_nameserializer_names        2/var/www/html/netbox-4.1.3/netbox/utilities/api.pyr   r      s     "K-33C88Iz"SSVSZSSSO
_--- 
 
 
 a)aajaaX^aaa
 
 	

s	   = $A!c                     | j         j                            d          \  }}| d| d}	 t          |          S # t          $ r t          d| d|           w xY w)z<
    Return the GraphQL type class for the given model.
    r    z.graphql.types.Typez Could not find GraphQL type for )r"   r#   r$   r   r%   r   )r&   r(   r)   
class_names       r+   r   r   +   s     "K-33C88Iz>>j>>>J_Z((( _ _ _!"]Y"]"]Q["]"]^^^_s	   ;  Ac                 t    t          d          }| j                            |          o| j        t          k    S )zD
    Return True of the request is being made via the REST API.
    zapi-root)r   	path_info
startswithcontent_typer   )requestapi_paths     r+   r   r   7   s6     z""H''11dg6JNd6dd    c                     t          | d          r?t          | j        j        j        j                  }t          | dd          x}r| d| }|S t          |           S )z
    Derive the view name from its associated model, if it has one. Fall back to DRF's built-in `get_view_name()`.
    This function is provided to DRF as its VIEW_NAME_FUNCTION.
    querysetsuffixN )hasattrr   r7   r&   r"   verbose_namegetattrdrf_get_view_name)viewnamer8   s      r+   r   r   ?   so    
 tZ   T](.;<<T8T2226 	&%%V%%D T"""r5   Nc                 Z   | j         j        }|s| j         j        }g }|D ]
}| j                            |          }|}|r|j        r|j        }	 |j                            |          }t          |t          t          t          f          r|                    |j                   n# t          $ r Y w xY w|rqt          t!          |          t"                    rO|j        r|j         j        nd}t)          t!          |          |          D ]}	|                    | d|	            |S )zj
    Compile and return a list of fields which should be prefetched on the queryset for a serializer.
    N__)Metar&   fields_declared_fieldsgetsourcer"   	get_field
isinstancer
   r	   r   appendr?   r   
issubclasstyper   nestedbrief_fieldsr   )
serializer_classfields_to_includer&   prefetch_fields
field_nameserializer_fieldmodel_field_namefield	subfieldssubfields
             r+   r   r   O   st    !'E  9,18O' H H
+<@@LL & 	7 0 7 	7/6	K))*:;;E%,>O!PQQ 3&&uz222  	 	 	H	
  	H$/00*== HBRBYc,1>>_c	 =dCS>T>TV_ ` ` H HH#**j+F+FH+F+FGGGGs   AB&&
B32B3c                 2   i }|s| j         j        }| j         j        }| j                                        D ]`\  }}||v rWt          |          t          u rA|j                            |j	                  j
        }t          |j        |j                  ||<   a|S )zh
    Return a mapping of field names to annotations to be applied to the queryset for a serializer.
    )rB   rC   r&   rD   itemsrK   r   r"   rG   relationrT   r   r?   )rN   rO   annotationsr&   rQ   rT   related_fields          r+   r   r   v   s     K  9,18!'E->DDFF ] ]
E***tE{{>U/U/U!K11%.AAGM&3M4GI[&\&\K
#r5   c                    |dS t          |t                    rt          |          }	  | j        d
i |S # t          $ r1 t          t          d                              |                    t          $ r1 t          t          d                              |                    t          $ r}t          |          d}~ww xY w	 t          |          }nE# t          t          f$ r1 t          t          d                              |                    w xY w	 |                     |          S # t          $ r1 t          t          d                              |	                    w xY w)z
    Return an object identified by either a dictionary of attributes or its numeric primary key (ID). This is used
    for referencing related objects when creating/updating objects via the REST API.
    Nz@Related object not found using the provided attributes: {params})paramsz8Multiple objects match the provided attributes: {params}zxRelated objects must be referenced by numeric ID or by dictionary of attributes. Received an unrecognized value: {value})value)pkz<Related object not found using the provided numeric ID: {id})id )rH   dictr   rE   r   r   _formatr   r   int	TypeError
ValueError)r7   attrsr]   er_   s        r+   r   r      s   
 }t % %&u--
	%8<))&)))! 	m 	m 	m!TUU\\dj\kkm m m& 	 	 	!LMMTT\bTcc    	% 	% 	%!!$$$	%	
ZZz" 
 
 
.  f5f!!	
 
 	

o||r|""" o o oa ^__ffjlfmmnnnos/   7 A<C3CCC AD!D7 7;E2)r   )N)*"django.contrib.contenttypes.fieldsr   django.core.exceptionsr   r   r   r   r   django.db.models.fields.relatedr	   r
   django.urlsr   django.utils.module_loadingr   django.utils.translationr   rc   rest_framework.serializersr   rest_framework.viewsr   r=   extras.constantsr   netbox.api.exceptionsr   r   netbox.api.fieldsr   queryr   r   stringr   __all__r   r   r   r   r   r   ra   r5   r+   <module>rx      s   @ @ @ @ @ @              G F F F F F F F       5 5 5 5 5 5 6 6 6 6 6 6 1 1 1 1 1 1 C C C C C C 3 3 3 3 3 3 I I I I I I I I 5 5 5 5 5 5 7 7 7 7 7 7 7 7      
 
 
 
	_ 	_ 	_e e e# # # $ $ $ $N   ('o 'o 'o 'o 'or5   