
    >Tfm                         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	Z G d
 d          Z G d d          Z G d de          ZdS )    )TemplateError)action)JSONRenderer)Response)HTTP_400_BAD_REQUEST)TextRenderer   )ConfigTemplateSerializer)ConfigContextQuerySetMixinConfigTemplateRenderMixinRenderConfigMixinc                   "     e Zd ZdZ fdZ xZS )r   z
    Used by views that work with config context models (device and virtual machine).
    Provides a get_queryset() method which deals with adding the config context
    data annotation or not.
    c                     t                                                      }|                                 d         }| j        sd|j                            dg           v r|S |                                S )a'  
        Build the proper queryset based on the request context

        If the `brief` query param equates to True or the `exclude` query param
        includes `config_context` as a value, return the base queryset.

        Else, return the queryset annotated with config context data
        requestconfig_contextexclude)superget_querysetget_serializer_contextbriefquery_paramsgetannotate_config_context_data)selfquerysetr   	__class__s      6/var/www/html/netbox-4.1.3/netbox/extras/api/mixins.pyr   z'ConfigContextQuerySetMixin.get_queryset   sp     77''))--//	:: 	)W-A-E-EiQS-T-TTTO44666    )__name__
__module____qualname____doc__r   __classcell__)r   s   @r   r   r      sB         
7 7 7 7 7 7 7 7 7r   r   c                       e Zd ZdZd ZdS )r   zQ
    Provides a method to return a rendered ConfigTemplate as REST API data.
    c                 0   	 |                     |          }n5# t          $ r(}t          dd|j         d| id          cY d }~S d }~ww xY w|j        j        dk    rt          |          S t          |dd	|i
          }t          |j        |d          S )N)contextdetailz5An error occurred while rendering the template (line z): i  statustxtTr   )nestedr&   )configtemplatecontent)renderr   r   linenoaccepted_rendererformatr
   data)r   r   r,   r&   outputetemplate_serializers          r   render_configtemplatez/ConfigTemplateRenderMixin.render_configtemplate+   s    	#**7*;;FF 	 	 	bRSRZbb_`bb        	 $+u44F###6~d]fho\pqqq16
 
   	s    
AA AAN)r   r    r!   r"   r6    r   r   r   r   '   s-             r   r   c                   H    e Zd ZdZ eddgdeeg          d             ZdS )r   zp
    Provides a /render-config/ endpoint for REST API views whose model may have a ConfigTemplate assigned.
    Tpostzrender-config)r'   methodsurl_pathrenderer_classesc                 ^   |                                  }|j        j        }|                                }|st	          dd| dit
                    S |                                }|                    |j                   |                    ||i           | 	                    |||          S )zR
        Resolve and render the preferred ConfigTemplate for this Device.
        errorz"No config template found for this .r(   )

get_object_meta
model_nameget_config_templater   r   get_config_contextupdater2   r6   )r   r   pkinstanceobject_typer,   context_datas          r   render_configzRenderConfigMixin.render_configC   s    
 ??$$n/!5577 	,LkLLL*, , , ,
  2244GL)))[(3444))'><PPPr   N)r   r    r!   r"   r   r   r   rJ   r7   r   r   r   r   ?   s\          V4&OWceqVrsssQ Q tsQ Q Qr   r   N)jinja2.exceptionsr   rest_framework.decoratorsr   rest_framework.renderersr   rest_framework.responser   rest_framework.statusr   netbox.api.renderersr   serializersr
   __all__r   r   r   r7   r   r   <module>rS      s   + + + + + + , , , , , , 1 1 1 1 1 1 , , , , , , 6 6 6 6 6 6 - - - - - - 1 1 1 1 1 17 7 7 7 7 7 7 7,       0Q Q Q Q Q1 Q Q Q Q Qr   