3
g                 @   s   d dl Z d dlmZmZ d dlmZ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 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 G dd dZG dd deejZ G dd deej!Z"G dd deej#Z$dS )    N)update_wrapperreduce)TupleDictCallable)apps)HttpResponseHttpResponseNotFound)settings)models)QuerySet)	smart_str)gettext)get_text_list)admin)ForeignKeySearchInputc                   sX   e Zd ZdZi Zi ZeeddZ fddZ	dd Z
dd	 Zd
d Z fddZ  ZS ) ForeignKeyAutocompleteAdminMixina  
    Admin class for models using the autocomplete feature.

    There are two additional fields:
       - related_search_fields: defines fields of managed model that
         have to be represented by autocomplete input, together with
         a list of target model fields that are searched for
         input string, e.g.:

         related_search_fields = {
            'author': ('first_name', 'email'),
         }

       - related_string_functions: contains optional functions which
         take target model instance as only argument and return string
         representation. By default __unicode__() method of target
         object is used.

    And also an optional additional field to set the limit on the
    results returned by the autocomplete query. You can set this integer
    value in your settings file using FOREIGNKEY_AUTOCOMPLETE_LIMIT or
    you can set this per ForeignKeyAutocompleteAdmin basis. If any value
    is set the results will not be limited.
    ZFOREIGNKEY_AUTOCOMPLETE_LIMITNc                sL   ddl m}  fdd}|d| jd jjj jjjf dgt j  S )Nr   )pathc                s    fdd}t | S )Nc                 s    j j| |S )N)Z
admin_siteZ
admin_view)argskwargs)selfview ^/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django_extensions/admin/__init__.pywrapper8   s    zHForeignKeyAutocompleteAdminMixin.get_urls.<locals>.wrap.<locals>.wrapper)r   )r   r   )r   )r   r   wrap7   s    z7ForeignKeyAutocompleteAdminMixin.get_urls.<locals>.wrapzforeignkey_autocomplete/z%s_%s_autocomplete)name)	Zdjango.urlsr   foreignkey_autocompletemodel_meta	app_label
model_namesuperget_urls)r   r   r   )	__class__)r   r   r#   4   s
    z)ForeignKeyAutocompleteAdminMixin.get_urlsc                s  |j jdd}|j jdd}|j jdd}|j jdd}|j jdd}y| j| W n tk
rp   dd Y nX |o|o|o|s|rd	d
 tj||}|jj }d}	|rVxX|j D ]L  fdd|jdD }
t	|}|j
j|j
_|jttj|
}||@ }qW | j||}|r&|j|}| jr<|d| j }djfdd|D }	n6|ry|j|d}W n tk
r   Y n
X |}	t|	ddS t S )z
        Search in the fields of the given related model and returns the
        result as a simple string to be used by the jQuery Autocomplete plugin
        qNr    r!   search_fields	object_pkc             S   s   | j  S )N)__str__)xr   r   r   <lambda>O   s    zJForeignKeyAutocompleteAdminMixin.foreignkey_autocomplete.<locals>.<lambda>c             S   sZ   | j drd| dd   S | j dr4d| dd   S | j drNd| dd   S d|  S d S )	N^z%s__istartswith   =z
%s__iexact@z
%s__searchz%s__icontains)
startswith)
field_namer   r   r   construct_searchR   s    


zRForeignKeyAutocompleteAdminMixin.foreignkey_autocomplete.<locals>.construct_search c                s(   g | ] }t jf t|t iqS r   )r   Qr   ).0r0   )bitr1   r   r   
<listcomp>c   s    zLForeignKeyAutocompleteAdminMixin.foreignkey_autocomplete.<locals>.<listcomp>,c                s"   g | ]}t d  ||jf qS )z%s|%s
)strpk)r4   f)to_string_functionr   r   r6   p   s    )r9   z
text/plain)content_type)GETgetrelated_string_functionsKeyErrorr   Z	get_modelZ_default_managerallsplitr   queryZselect_relatedfilterr   operatoror_get_related_filterautocomplete_limitjoin	Exceptionr   r	   )r   requestrC   r    r!   r&   r'   r   ZquerysetdataZ
or_queriesZother_qsZadditional_filterobjr   )r5   r1   r;   r   r   A   sF    

z8ForeignKeyAutocompleteAdminMixin.foreignkey_autocompletec             C   s   dS )z
        Given a model class and current request return an optional Q object
        that should be applied as an additional filter for autocomplete query.
        If no additional filtering is needed, this method should return
        None.
        Nr   )r   r   rK   r   r   r   rG   {   s    z3ForeignKeyAutocompleteAdminMixin.get_related_filterc             C   s6   | j j|d }|r2|t|tdd}td| S dS )Nand)r!   Z
field_listzMUse the left field to do %(model_name)s lookups in the fields %(field_list)s.r2   )related_search_fieldsr>   r   _)r   r0   r!   Zsearchable_fieldsZhelp_kwargsr   r   r   get_help_text   s    z.ForeignKeyAutocompleteAdminMixin.get_help_textc                s|   t |tjrl|j| jkrl| j|j|jjjj	}|j
drLtd|d |f }t|j| j|j |d< ||d< t j|f|S )z
        Override the default widget for Foreignkey fields if they are
        specified in the related_search_fields class attribute.
        	help_textz%s %sZwidget)
isinstancer   Z
ForeignKeyr   rO   rQ   Zremote_fieldr   r   Zobject_namer>   r8   r   r"   formfield_for_dbfield)r   Zdb_fieldr   rR   )r$   r   r   rT      s    
z6ForeignKeyAutocompleteAdminMixin.formfield_for_dbfield)__name__
__module____qualname____doc__rO   r?   getattrr
   rH   r#   r   rG   rQ   rT   __classcell__r   r   )r$   r   r      s   :	
r   c               @   s   e Zd ZdS )ForeignKeyAutocompleteAdminN)rU   rV   rW   r   r   r   r   r[      s   r[   c               @   s   e Zd ZdS )#ForeignKeyAutocompleteTabularInlineN)rU   rV   rW   r   r   r   r   r\      s   r\   c               @   s   e Zd ZdS )#ForeignKeyAutocompleteStackedInlineN)rU   rV   rW   r   r   r   r   r]      s   r]   )%rE   	functoolsr   r   typingr   r   r   Zdjango.appsr   Zdjango.httpr   r	   Zdjango.confr
   Z	django.dbr   Zdjango.db.models.queryr   Zdjango.utils.encodingr   Zdjango.utils.translationr   rP   Zdjango.utils.textr   Zdjango.contribr   Zdjango_extensions.admin.widgetsr   r   Z
ModelAdminr[   ZTabularInliner\   ZStackedInliner]   r   r   r   r   <module>   s"    