o
    e                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ zddlm	Z	 W n e
y3   ddlm	Z	 Y nw ddlmZ G dd	 d	eZG d
d deejZG dd deejZG dd deZdS )zAutocomplete widgets bases.    N)forward)VERSION)forms)reverse)	mark_safec                       s   e Zd ZdZd fdd	Z fddZdd Zed	d
 Zdd Z	 fddZ
d fdd	Zd fdd	Zdd Zdd ZeeeZ  ZS )WidgetMixinac  Base mixin for autocomplete widgets.

    .. py:attribute:: url

        Absolute URL to the autocomplete view for the widget. It can be set to
        a URL name, in which case it will be reversed when the attribute is
        accessed.

    .. py:attribute:: forward

        List of field names to forward to the autocomplete view, useful to
        filter results using values of other fields in the form.

        Items of the list must be one of the following:
         - string (e. g. "some_field"): forward a value from
           the field with named "some_field";
         - ``dal.forward.Field("some_field")``: the same as above;
         - ``dal.forward.Field("some_field", "dst_field")``: forward a value from
           the field with named "some_field" as "dst_field";
         - ``dal.forward.Const("some_value", "dst_field")``: forward a constant
           value "some_value" as "dst_field".

    .. py:attribute:: autocomplete_function

        Identifier of the javascript callback that should be
        executed when such a widget is loaded in the DOM,
        either on page load or dynamically.
    Nc                    s>   || _ |pg | _|di d| _tt| j|i | dS )z@Instanciate a widget with a URL and a list of fields to forward.attrszdata-placeholderN)urlr   getplaceholdersuperr   __init__)selfr	   r   argskwargs	__class__ W/var/www/html/Testing_prj/Navya-Bakers/venv/lib/python3.10/site-packages/dal/widgets.pyr   /   s   
zWidgetMixin.__init__c                    sJ   t t| j|i |}| jdur| j|d< t| dd}|r#|d| |S )z%Build HTML attributes for the widget.Nzdata-autocomplete-light-urlautocomplete_functionz data-autocomplete-light-function)r   r   build_attrsr	   getattr
setdefault)r   r   r   r   r   r   r   r   r   6   s   

zWidgetMixin.build_attrsc                    s    fdd| j D | _ dS )z+Replace self.choices with selected_choices.c                    s    g | ]}t |d   v r|qS )r   str.0cselected_choicesr   r   
<listcomp>E   s    z8WidgetMixin.filter_choices_to_render.<locals>.<listcomp>N)choicesr   r   r   r   r   filter_choices_to_renderC   s   z$WidgetMixin.filter_choices_to_renderc                 C   s:   t | trt|  S t | tjr|  S td| )zConvert forward declaration to a dictionary.

        A returned dictionary will be dumped to JSON while rendering widget.
        z Cannot use {} as forwarded value)
isinstancer   r   Fieldto_dictForward	TypeErrorformat)fr   r   r   _make_forward_dictH   s
   
zWidgetMixin._make_forward_dictc                    s<    j rddj|d d t fdd j D  d S dS )	z+Render forward configuration for the field.z3<div style="display:none" class="dal-forward-conf" zid="dal-forward-conf-for_{id}")idz&><script type="text/dal-forward-conf">c                    s   g | ]}  |qS r   )r+   )r   r*   r   r   r   r    ^   s    z3WidgetMixin.render_forward_conf.<locals>.<listcomp>z</script></div> )r   r)   jsondumps)r   r,   r   r-   r   render_forward_confU   s   
zWidgetMixin.render_forward_confc                    st   t dk rdnd}dd || D }t| j}| jr | | n| js-| jr-| jdd tt	| j
| }|| _|S )z
        Django-compatibility method for option rendering.

        Should only render selected options, by setting self.choices before
        calling the parent method.

        Remove this code when dropping support for Django<1.10.
        )   
   r2   r   c                 S      g | ]}|rt |qS r   r   r   r   r   r   r    q   s    z.WidgetMixin.render_options.<locals>.<listcomp>Nr.   )r   copyr!   r	   r#   allow_multiple_selectedr   insertr   r   render_options)r   r   selected_choices_argr   all_choiceshtmlr   r   r   r9   e   s   	zWidgetMixin.render_optionsc                    sd   dd |D }t  | j}| jr| | n| js#| jr#| jdd tt| 	|||}|| _|S )zr
        Exclude unselected self.choices before calling the parent method.

        Used by Django>=1.10.
        c                 S   r4   r   r   r   r   r   r   r       s    z)WidgetMixin.optgroups.<locals>.<listcomp>r   r5   )
r6   r!   r	   r#   r7   r   r8   r   r   	optgroups)r   namevaluer   r   r;   resultr   r   r   r=      s   zWidgetMixin.optgroupsc           	   	      sZ   t t| j|||fi |}z|d }W n ttfy!   |}Y nw | |}t|| S )z7Call Django render together with `render_forward_conf`.r,   )r   r   renderKeyErrorr(   r1   r   )	r   r>   r?   r   rendererr   widgetfield_idconfr   r   r   rA      s   
zWidgetMixin.renderc                 C   s(   | j d u rd S d| j v r| j S t| j S )N/)_urlr   r-   r   r   r   _get_url   s
   


zWidgetMixin._get_urlc                 C   s
   || _ d S N)rH   )r   r	   r   r   r   _set_url   s   
zWidgetMixin._set_url)NNrJ   )__name__
__module____qualname____doc__r   r   r#   staticmethodr+   r1   r9   r=   rA   rI   rK   propertyr	   __classcell__r   r   r   r   r      s    

	r   c                   @      e Zd ZdZdS )Selectz@Replacement for Django's Select to render only selected choices.NrL   rM   rN   rO   r   r   r   r   rT          rT   c                   @   rS   )SelectMultiplez;Replacement SelectMultiple to render only selected choices.NrU   r   r   r   r   rW      rV   rW   c                   @   s   e Zd ZdZdd ZdS )QuerySetSelectMixinzQuerySet support for choices.c                 C   s:   z| j jjdd |D d| j _W dS  ty   Y dS w )z8Filter out un-selected choices if choices is a QuerySet.c                 S   s   g | ]}|r|qS r   r   r   r   r   r   r       s    z@QuerySetSelectMixin.filter_choices_to_render.<locals>.<listcomp>)pk__inN)r!   querysetfilter
ValueErrorr"   r   r   r   r#      s   z,QuerySetSelectMixin.filter_choices_to_renderN)rL   rM   rN   rO   r#   r   r   r   r   rX      s    rX   )rO   r6   r/   dalr   djangor   r   django.urlsr   ImportErrordjango.core.urlresolversdjango.utils.safestringr   objectr   rT   rW   rX   r   r   r   r   <module>   s"     