
    R
h&                     x   d 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 dd	lmZ g d
Z G d d          Z G d d          Z G d deej                  Z G d deeej                  Z G d de          Z G d deej                  Z G d deeej                  Z G d d          ZdS )z%
Agnocomplete specific form fields.

    )forms   )AgnocompleteBase)AGNOCOMPLETE_USER_ATTRIBUTE)AgnocompleteSelectAgnocompleteMultiSelect)get_agnocomplete_registry)ItemNotFound)!UnregisteredAgnocompleteException)AgnocompleteFieldAgnocompleteModelFieldAgnocompleteMultipleFieldAgnocompleteModelMultipleFieldc                   >     e Zd ZdZeZd Zd Zd Zd Z	 fdZ
 xZS )AgnocompleteMixinz?
    Handles the Agnocomplete generic handling for fields.
    c                 (    | j         | j        _         d S N)agnocompletewidgetselfs    ^/var/www/html/01_SiteInternet/03_Maps/venv/lib/python3.11/site-packages/agnocomplete/fields.py_setup_agnocomplete_widgetz,AgnocompleteMixin._setup_agnocomplete_widget   s    #'#4       c                 d   t          |t                    r<t                      }||vr"t          d                    |                    ||         }t          |t
                    s ||          }t          |t
                    r|                    |            || _        || j        _        dS )a<  
        Handling the assignation of the agnocomplete object inside the field.
        A developer may want to use a class or an instance of an
        :class:`AgnocompleteBase` class to configure her field.

        Ex::

            from agnocomplete import Fields

            class SearchForm(forms.Form):
                search_class = fields.AgnocompleteField(AgnocompleteColor)
                search_class2 = fields.AgnocompleteField('AgnocompleteColor')
                search_instance = fields.AgnocompleteField(
                    AgnocompleteColor(page_size=3))

        if it's a :class: being passed as a parameter, it'll be
        instantiated using the default parameters. If it's a string, it'll
        be instanciated also, using the name of the class as the key to
        fetch the actual class.

        z.Unregistered Agnocomplete class: {} is unknown)userN)	
isinstancestrr	   r   formatr   set_agnocomplete_fieldr   r   )r   klass_or_instancer   registrys       r   set_agnocompletez"AgnocompleteMixin.set_agnocomplete    s    . '-- 	<022H 007DKKL]^^   !)): ;+-=>> 	= 1 1t < < <')9:: 	;44T:::-!%r   c                 .    t          | t          d          S )z@
        Return the agnocomplete user variable, if set.
        N)getattrr   r   s    r   get_agnocomplete_contextz*AgnocompleteMixin.get_agnocomplete_contextH   s     t8$???r   c                 x    t          | t                    r$|                                 }|r|| j        _        |S dS )zL
        Assign the user context to the agnocomplete class, if any.
        N)hasattrr   r&   r   r   )r   r   s     r   transmit_agnocomplete_contextz/AgnocompleteMixin.transmit_agnocomplete_contextN   sJ    
 4455 	0022D .)-!&K		 	r   c                 ^    |                                    t                      j        |i |S )a  
        Potentially, these fields should validate against context-based
        queries.

        If a context variable has been transmitted to the field, it's being
        used to 'reset' the queryset and make sure the chosen item fits to
        the user context.
        )r)   superclean)r   argskwargs	__class__s      r   r,   zAgnocompleteMixin.cleanZ   s2     	**,,,uww}d-f---r   )__name__
__module____qualname____doc__r   r   r   r#   r&   r)   r,   __classcell__r/   s   @r   r   r      s           F5 5 5&& && &&P@ @ @	 	 	
. 
. 
. 
. 
. 
. 
. 
. 
.r   r   c                        e Zd Z fdZ xZS ) AgnocompleteContextQuerysetMixinc                     t                                                      }|r| j                                        | _        |S )zK
        We'll reset the current queryset only if the user is set.
        )r+   r)   r   get_querysetqueryset)r   r   r/   s     r   r)   z>AgnocompleteContextQuerysetMixin.transmit_agnocomplete_contexth   s=     ww4466 	= -::<<DMr   )r0   r1   r2   r)   r4   r5   s   @r   r7   r7   g   s8                r   r7   c                   $     e Zd ZdZd fd	Z xZS )r   z<
    Agnocomplete Field class for simple Choice fields.
    Nc                     |                      ||            t                      j        dd| j                                        i| |                                  d S )Nchoices r#   r+   __init__r   get_choicesr   r   r   r   r.   r/   s       r   r@   zAgnocompleteField.__init__v   sb    lD111KK!2!>!>!@!@KFKKK'')))))r   r   r0   r1   r2   r3   r@   r4   r5   s   @r   r   r   r   sG         * * * * * * * * * *r   r   c                   $     e Zd ZdZd fd	Z xZS )r   zQ
    Agnocomplete Field class for Choice fields based on models / querysets.
    Nc                     |                      ||            t                      j        | j                                        fi | |                                  d S r   r?   rB   s       r   r@   zAgnocompleteModelField.__init__   s_    lD111*6688CCFCCC'')))))r   r   rC   r5   s   @r   r   r   |   sG         * * * * * * * * * *r   r   c                   X     e Zd ZdZeZdZ fdZd	dZe	d             Z
 fdZd Z xZS )
AgnocompleteMultipleMixinz:
    Core mixin for multiple-selection enabled fields
    Tc                 j    t                                                       | j        | j        _        d S r   )r+   r   creater   )r   r/   s    r   r   z4AgnocompleteMultipleMixin._setup_agnocomplete_widget   s+    **,,,![r   Fc                 @    || _         t          |          p|| _        d S r   )create_fieldboolrI   )r   rI   rK   s      r   set_create_fieldz*AgnocompleteMultipleMixin.set_create_field   s"    (<((2Fr   c                     g S )z#Default empty value for this field.r>   r   s    r   empty_valuez%AgnocompleteMultipleMixin.empty_value   s	     	r   c                     |                      |          }t                                          |          }|p| j        S r   )clear_list_valuer+   	to_pythonrO   )r   valuer/   s     r   rR   z#AgnocompleteMultipleMixin.to_python   s;    %%e,,!!%(((((r   c                 L    |s| j         S | j        rd |D             }|p| j         S )zW
        Clean the argument value to eliminate None or Falsy values if needed.
        c                     g | ]}||S r>   r>   .0vs     r   
<listcomp>z>AgnocompleteMultipleMixin.clear_list_value.<locals>.<listcomp>   s    +++1+Q+++r   )rO   clean_emptyr   rS   s     r   rQ   z*AgnocompleteMultipleMixin.clear_list_value   sA    
  	$## 	,+++++E(((r   )FF)r0   r1   r2   r3   r   r   rZ   r   rM   propertyrO   rR   rQ   r4   r5   s   @r   rG   rG      s          %FK) ) ) ) )
3 3 3 3   X) ) ) ) )
) 
) 
) 
) 
) 
) 
)r   rG   c                   (     e Zd ZdZ	 	 d fd	Z xZS )r   z>
    Agnocomplete Field class for multiple Choice fields.
    NFc                     |                      ||           |                     ||            t                      j        dd| j                                        i| |                                  d S )NrI   rK   r=   r>   )r#   rM   r+   r@   r   rA   r   r   r   r   rI   rK   r.   r/   s         r   r@   z"AgnocompleteMultipleField.__init__   s{    lD111V,GGGKK!2!>!>!@!@KFKKK'')))))r   NFFrC   r5   s   @r   r   r      sO          +/,1* * * * * * * * * *r   r   c                   Z     e Zd ZdZ	 	 d
 fd	Zed             Zd Zd Zd Z	 fd	Z
 xZS )r   z>
    Field class for multiple selection on Django models.
    NFc                     |                      ||           |                     ||            t                      j        | j                                        fi | |                                  g | _        d S )Nr_   )r#   rM   r+   r@   r   rA   r   _new_valuesr`   s         r   r@   z'AgnocompleteModelMultipleField.__init__   s    lD111V,GGG*6688CCFCCC'')))r   c                 H    | j         j        j                                        S )z
        Return default empty value as a Queryset.

        This value can be added via the `|` operator, so we surely need
        a queryset and not a list.
        )r:   modelobjectsnoner   s    r   rO   z*AgnocompleteModelMultipleField.empty_value   s     }"*//111r   c                 D     | j         j        j        j        di |\  }}|S )z>
        Return a model instance created from kwargs.
        r>   )r:   rf   rg   get_or_create)r   r.   itemcreateds       r   create_itemz*AgnocompleteModelMultipleField.create_item   s.     B+3AKKFKKgr   c                     i S )z
        Return extra arguments to create the new model instance.

        You can pass context-related arguments in the dictionary, or default
        values.
        r>   r   s    r   extra_create_kwargsz2AgnocompleteModelMultipleField.extra_create_kwargs   s	     	r   c                    | j         j        }g }|                                 }| j        D ]G}| j        |i}|                    |            | j        di |}|                    |j                   H|j	        
                    |          S )zY
        Create values created by the user input. Return the model instances QS.
        )pk__inr>   )r:   rf   ro   rd   rK   updaterm   appendpkrg   filter)r   rf   pksro   rS   create_kwargsnew_items          r   create_new_valuesz0AgnocompleteModelMultipleField.create_new_values   s     #"6688% 	$ 	$E!.6M  !4555't'88-88HJJx{####}##3#///r   c                     | j         s!t                                          |          S |                     |          }d |D             }d |D             | _        t                                          |          }|S )z)
        Clean the field values.
        c                 :    g | ]}|                                 |S r>   isdigitrV   s     r   rY   z8AgnocompleteModelMultipleField.clean.<locals>.<listcomp>  s%    ///Q199;;/q///r   c                 :    g | ]}|                                 |S r>   r|   rV   s     r   rY   z8AgnocompleteModelMultipleField.clean.<locals>.<listcomp>  s%    @@@!AIIKK@A@@@r   )rI   r+   r,   rQ   rd   )r   rS   rv   qsr/   s       r   r,   z$AgnocompleteModelMultipleField.clean   s     { 	(77=='''
 %%e,, 0/%///@@u@@@WW]]3	r   ra   )r0   r1   r2   r3   r@   r\   rO   rm   ro   ry   r,   r4   r5   s   @r   r   r      s          +/,1      2 2 X2    0 0 0        r   r   c                       e Zd ZdZd ZdS )AgnocompleteUrlProxyMixinz
    This mixin can be used with a field which actually using
    :class:`agnocomplete.core.AutocompletUrlProxy`. The main purpose is to
    provide a mechanism to validate the value through the Autocomplete.
    c                 `    	 | j                             |           n# t          $ r Y dS w xY wdS )NFT)r   validater
   r[   s     r   valid_valuez%AgnocompleteUrlProxyMixin.valid_value  sJ    	&&u---- 	 	 	55	 ts    
++N)r0   r1   r2   r3   r   r>   r   r   r   r   
  s-             r   r   N)r3   djangor   corer   	constantsr   widgetsr   r   registerr	   
exceptionsr
   r   __all__r   r7   ChoiceFieldr   ModelChoiceFieldr   rG   MultipleChoiceFieldr   ModelMultipleChoiceFieldr   r   r>   r   r   <module>r      s/          " " " " " " 2 2 2 2 2 2 @ @ @ @ @ @ @ @ / / / / / / $ $ $ $ $ $ 9 9 9 9 9 9  N. N. N. N. N. N. N. N.b       * * * * *)5+< * * *	* 	* 	* 	* 	*=."3	* 	* 	*&) &) &) &) &) 1 &) &) &)R
* 
* 
* 
* 
* 9 % 9
* 
* 
*I I I I I%E%>%*%CI I IX         r   