
    >Tf                        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Z G d	 d
ej                  Z G d dej                  Z G d d          Z G d deej                  Z G d deej                  ZdS )    N)forms)settings)
BoundField)reverse)widgets)get_viewname)DynamicChoiceFieldDynamicModelChoiceFieldDynamicModelMultipleChoiceFieldDynamicMultipleChoiceFieldc                       e Zd Zd ZdS )r	   c                     t          || |          }|                                fd| j        D             | _        ng | _        |S )Nc                 ,    g | ]}|d          k    |S r    .0choicedatas     C/var/www/html/netbox-4.1.3/netbox/utilities/forms/fields/dynamic.py
<listcomp>z6DynamicChoiceField.get_bound_field.<locals>.<listcomp>   s.       !VAY$5F5F5F5F5F    r   valuechoicesselfform
field_namebound_fieldr   s       @r   get_bound_fieldz"DynamicChoiceField.get_bound_field   sh     tZ88  ""   %)\  DLL DLr   N__name__
__module____qualname__r!   r   r   r   r	   r	      s#            r   r	   c                       e Zd Zd ZdS )r   c                     t          || |          }|                                fd| j        D             | _        |S )Nc                 8    g | ]}|d          r|d          v |S r   r   r   s     r   r   z>DynamicMultipleChoiceField.get_bound_field.<locals>.<listcomp>-   s;       !VAYCI!9PTCTCTCTCTCTr   r   r   s       @r   r!   z*DynamicMultipleChoiceField.get_bound_field(   s_     tZ88  ""   %)\  DL r   Nr"   r   r   r   r   r   &   s#        	 	 	 	 	r   r   c                   Z     e Zd ZdZej        Zej        Z	ddddddd fd
Z
d Zd Z xZS )DynamicModelChoiceMixina  
    Override `get_bound_field()` to avoid pre-populating field choices with a SQL query. The field will be
    rendered only with choices set via bound data. Choices are populated on-demand via the APISelect widget.

    Attributes:
        query_params: A dictionary of additional key/value pairs to attach to the API request
        initial_params: A dictionary of child field references to use for selecting a parent field's initial value
        null_option: The string used to represent a null selection (if any)
        disabled_indicator: The name of the field which, if populated, will disable selection of the
            choice (DEPRECATED: pass `context={'disabled': '$fieldname'}` instead)
        context: A mapping of <option> template variables to their API data keys (optional; see below)
        selector: Include an advanced object selection widget to assist the user in identifying the desired object

    Context keys:
        value: The name of the attribute which contains the option's value (default: 'id')
        label: The name of the attribute used as the option's human-friendly label (default: 'display')
        description: The name of the attribute to use as a description (default: 'description')
        depth: The name of the attribute which indicates an object's depth within a recursive hierarchy; must be a
            positive integer (default: '_depth')
        disabled: The name of the attribute which, if true, signifies that the option should be disabled
        parent: The name of the attribute which represents the object's parent object (e.g. device for an interface)
        count: The name of the attribute which contains a numeric count of related objects
    NF)query_paramsinitial_paramsnull_optiondisabled_indicatorcontextselectorc                    |j         | _         |pi | _        |pi | _        || _        || _        |pi | _        || _         t                      j        |fi | d S )N)	modelr+   r,   r-   r.   r/   r0   super__init__)
r   querysetr+   r,   r-   r.   r/   r0   kwargs	__class__s
            r   r4   z DynamicModelChoiceMixin.__init__S   so     ^
(.B,2&"4}" ,,V,,,,,r   c                    i }| j         
| j         |d<   | j                                        D ]\  }}||d| d<   t          | j                  dk    r|                    | j                   | j        r| j        j        j	        |d<   |S )Nzdata-null-optionzts-z-fieldr   r0   )
r-   r/   itemslenr+   add_query_paramsr0   r2   _metalabel_lower)r   widgetattrsvaraccessors        r   widget_attrsz$DynamicModelChoiceMixin.widget_attrsi   s     '(,(8E$% "\//11 	0 	0MC'/E####$$ t !!A%%##D$5666 = 	= $
 0 <E*r   c                    t          || |          }| j        s| j        ri }| j                                        D ]9\  }}|j                            |                    d                    }|r|||<   :|r) | j        j        d	i |                                | _        |	                                }|rt          |j        t                    rt          |          t          ur|g}t          | d          pd}|                     |          }		 |	                    | j        |          | _        nS# t           t"          f$ r! | j                                        | _        Y n"w xY w| j                                        | _        |j        j        }
|
j                            d          s3t+          | j        j        dd          }t/          |          |
j        d<   |S )
N$to_field_namepk)r   zdata-urllistT)actionrest_apir   )r   initialr,   r9   getlstripr5   filterfirstr   
isinstancefieldr   typerG   getattr	TypeError
ValueErrornoner>   r?   r   r2   r   )r   r   r   r    filter_kwargskwargchild_fieldr   r   rM   r>   viewnames               r   r!   z'DynamicModelChoiceMixin.get_bound_field~   s    tZ88 | 	M 3 	MM&*&9&?&?&A&A 1 1"{((););C)@)@AA 1+0M%( M3t}3DDmDDJJLL   "" 	1++-LMM VZ[_V`V`dhVhVhv 77?4J[[J[77F5 &dmT B Bz* 5 5 5 $ 2 2 4 45 !M..00DM ")|
++ 	9#DM$7QUVVVH'.x'8'8FL$s    D3 3/E%$E%)r#   r$   r%   __doc__django_filtersModelChoiceFilterrM   r   	APISelectr>   r4   rB   r!   __classcell__r7   s   @r   r*   r*   8   s         . -FF #- - - - - - -,  *& & & & & & &r   r*   c                   "     e Zd ZdZ fdZ xZS )r
   zS
    Dynamic selection field for a single object, backed by NetBox's REST API.
    c                 x    | j         |t          j        k    rdS t                                          |          S )z
        When null option is enabled and "None" is sent as part of a form to be submitted, it is sent as the
        string 'null'.  This will check for that condition and gracefully handle the conversion to a NoneType.
        Nr-   r   FILTERS_NULL_CHOICE_VALUEr3   cleanr   r   r7   s     r   rd   zDynamicModelChoiceField.clean   s6    
 'EX5W,W,W4ww}}U###r   )r#   r$   r%   rZ   rd   r^   r_   s   @r   r
   r
      sB         $ $ $ $ $ $ $ $ $r   r
   c                   >     e Zd ZdZej        Zej        Z	 fdZ
 xZS )r   zA
    A multiple-choice version of `DynamicModelChoiceField`.
    c                     |pg }| j         >t          j        |v r0d |D             }d gt                                          |          S t                                          |          S )Nc                 2    g | ]}|t           j        k    |S r   )r   rc   )r   vs     r   r   z9DynamicModelMultipleChoiceField.clean.<locals>.<listcomp>   s%    QQQ1h.P)P)PQ)P)P)Pr   rb   re   s     r   rd   z%DynamicModelMultipleChoiceField.clean   sl     'H,NRW,W,WQQQQQE0577==//00ww}}U###r   )r#   r$   r%   rZ   r[   ModelMultipleChoiceFilterrM   r   APISelectMultipler>   rd   r^   r_   s   @r   r   r      sT          5F&F	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$r   r   )r[   djangor   django.confr   django.formsr   django.urlsr   utilities.formsr   utilities.viewsr   __all__ChoiceFieldr	   MultipleChoiceFieldr   r*   ModelChoiceFieldr
   ModelMultipleChoiceFieldr   r   r   r   <module>rw      sy                         # # # # # #       # # # # # # ( ( ( ( ( (    *        !:   $l l l l l l l l^$ $ $ $ $5u7M $ $ $$ $ $ $ $&=u?] $ $ $ $ $r   