o
    e                     @   s   d Z ddlZddlZddlmZ ddlZddlmZ ejdkr&ddlm	Z	 n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dlmZ G dd deZG dd deeZdS )z$Base views for autocomplete widgets.    N)reduce)http)   r   )lookup_spawns_duplicates)lookup_needs_distinct)get_permission_codename)ImproperlyConfiguredValidationErrorQ)HttpResponseBadRequestHttpResponseNotAllowed)render_to_string)BaseListViewc                       s$   e Zd ZdZdZ fddZ  ZS )	ViewMixina  Common methods for autocomplete views.

    It is assumed this view will be used in conjunction with a Django
    :py:class:`View` based class that will implement OPTIONS.

    .. py:attribute:: forwarded

        Dict of field values that were forwarded from the form, may be used to
        filter autocompletion results based on the form state. See
        ``linked_data`` example for reference.

    .. py:attribute:: q

        Query string as typed by the user in the autocomplete field.
    )GETPOSTc                    s   |j  | jvrt| jS ztt||j dd| _W n t	y*   t
d Y S w t| jts5t
dS |jdd| _tt| j|g|R i |S )z*Set :py:attr:`forwarded` and :py:attr:`q`.forwardz{}zInvalid JSON datazNot a JSON objectq )methodupperhttp_method_allowedr   jsonloadsgetattrget	forwarded
ValueErrorr   
isinstancedictr   r   superr   dispatch)selfrequestargskwargs	__class__ U/var/www/html/Testing_prj/Navya-Bakers/venv/lib/python3.10/site-packages/dal/views.pyr"   )   s   

zViewMixin.dispatch)__name__
__module____qualname____doc__r   r"   __classcell__r)   r)   r'   r*   r      s    r   c                       s   e Zd ZdZdZdZdZdZg ZdZ	dZ
dZdd Zdd	 Zd
d Zdd Z fddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z  ZS ) BaseQuerySetViewa^  Base view to get results from a QuerySet.

    .. py:attribute:: create_field

        Name of the field to use to create missing values. For example, if
        create_field='title', and the user types in "foo", then the
        autocomplete view will propose an option 'Create "foo"' if it can't
        find any value matching "foo". When the user does click 'Create "foo"',
        the autocomplete script should POST to this view to create the object
        and get back the newly created object id.

    .. py:attribute:: model_field_name

        Name of the Model field to run filter against.
    
   resultsnameNc                 C   s   |d r
|d   S dS )z.For widgets that have infinite-scroll feature.page_objF)has_next)r#   contextr)   r)   r*   has_moreV   s   zBaseQuerySetView.has_morec                 C   s
   t |jS )zReturn the value of a result.)strpkr#   resultr)   r)   r*   get_result_valueZ      
z!BaseQuerySetView.get_result_valuec                 C   s   | j rt| j d|iS t|S )zReturn the label of a result.r;   )templater   r8   r:   r)   r)   r*   get_result_label^   s   z!BaseQuerySetView.get_result_labelc                 C   s
   |  |S )z&Return the label of a selected result.)r?   r:   r)   r)   r*   get_selected_result_labele   r=   z*BaseQuerySetView.get_selected_result_labelc                    s    t t|  }| || j}|S )z"Filter the queryset with GET['q'].)r!   r0   get_querysetget_search_resultsr   )r#   qsr'   r)   r*   rA   i   s   zBaseQuerySetView.get_querysetc                 C   s   | j r| j S | jgS )zGet the fields to search over.)search_fieldsmodel_field_namer#   r)   r)   r*   get_search_fieldsq   s   z"BaseQuerySetView.get_search_fieldsc                 C   sV   | drd|dd  S | drd|dd  S | dr'd|dd  S d	| S )
zApply keyword searches.^z%s__istartswith   N=z
%s__iexact@z
%s__searchz%s__icontains)
startswith)r#   
field_namer)   r)   r*   _construct_searchx   s   


z"BaseQuerySetView._construct_searchc                    s     }|ra rafdd|D }jdurEg }  D ]fdd|D }|ttj| qjdkr9tjntj}|t||}n fdd|D }|ttj|}	||ra|
 }|S )z&Filter the results based on the query.c                    s   g | ]}  |qS r)   )rN   ).0search_fieldrF   r)   r*   
<listcomp>   s    
z7BaseQuerySetView.get_search_results.<locals>.<listcomp>Nc                       g | ]}t d i | iqS r)   r
   rO   
orm_lookup)wordr)   r*   rQ      s    orc                    rR   rS   r
   rT   )search_termr)   r*   rQ      s    )rG   split_wordssplitappendr   operatoror_and_filterr   distinct)r#   querysetrX   rD   orm_lookupsword_conditions
or_queriesop_r)   )rX   r#   rV   r*   rB      s&   


z#BaseQuerySetView.get_search_resultsc                    s   t  fdd|D S )z<Return True if an orm_lookup requires calling qs.distinct().c                 3   s    | ]
}t  jj|V  qd S )N)r   model_meta)rO   search_specra   r)   r*   	<genexpr>   s
    
z9BaseQuerySetView.lookup_needs_distinct.<locals>.<genexpr>)any)r#   ra   rb   r)   ri   r*   r      s   z&BaseQuerySetView.lookup_needs_distinctc                 C   s   |   jdi | j|id S )zCreate an object given a text.r   Nr)   )rA   get_or_createcreate_field)r#   textr)   r)   r*   create_object   s
   zBaseQuerySetView.create_objectc                 C   sR   t jdk r|j }n|jj}|sdS |  jj}td|}|jd|j	|f S )z:Return True if the user has the permission to add a model.)   r   r   Faddz%s.%s)
djangoVERSIONuseris_authenticatedrA   rf   rg   r   has_perm	app_label)r#   r$   authoptscodenamer)   r)   r*   has_add_permission   s   

z#BaseQuerySetView.has_add_permissionc              
   O   s   |  |s	t S | jstd|jdd}|du rt S | jrPz| 	| W n& t
yO } z| j|jv rEtt|dW  Y d}~S W Y d}~nd}~ww | |}t| || |dS )z
        Create an object given a text after checking permissions.

        Runs self.validate() if self.validate_create is True.
        zMissing "create_field"rn   N)error)idrn   )r{   r   HttpResponseForbiddenrm   r   r   r   r   validate_createvalidater	   message_dictJsonResponser    ro   r<   r@   )r#   r$   r%   r&   rn   r|   r;   r)   r)   r*   post   s*   

zBaseQuerySetView.postc                 C   s*   |   j}|di | j|i}|  dS )zo
        Validate a given text for new option creation.

        Raise ValidationError or return None.
        Nr)   )rA   rf   rm   
full_clean)r#   rn   rf   objr)   r)   r*   r      s   
zBaseQuerySetView.validate)r+   r,   r-   r.   paginate_bycontext_object_namerE   rm   rD   rY   r>   r   r7   r<   r?   r@   rA   rG   rN   rB   r   ro   r{   r   r   r/   r)   r)   r'   r*   r0   <   s.    r0   )r.   r   r\   	functoolsr   rr   r   rs   django.contrib.admin.utilsr   r   django.contrib.authr   django.core.exceptionsr   r	   django.db.modelsr   django.httpr   r   django.template.loaderr   django.views.generic.listr   objectr   r0   r)   r)   r)   r*   <module>   s"    
&