3
š(ƒhž  ã               @   s\   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 edfdd„Zdd	d
„ZdS )é    )Ú
ForeignKeyÚManyToManyField)Ú	ModelForm)Úcapfirst)Ú
force_text)Úugettext_lazyFc       	         sd   d|kr|j dƒ}G ‡ ‡fdd„dˆƒ}x6|jƒ D ]*\}}tˆ |||ƒ}||j|< ||j|< q2W |S )aá  Creates a ModelForm subclass with AutoComplete fields.

    Args:
        model (type): Model class for which you are making the ModelForm
        fieldlist (dict): {field_name -> channel_name, ...}
        superclass (type): optional ModelForm superclass
        show_help_text (bool): suppress or show the widget help text

    Returns:
        ModelForm: a ModelForm suitable for use in an Admin

    Usage::

        from django.contrib import admin
        from ajax_select import make_ajax_form
        from yourapp.models import YourModel

        @admin.register(YourModel)
        class YourModelAdmin(Admin):

            form = make_ajax_form(YourModel, {
                'contacts': 'contact',  # ManyToManyField
                'author':'contact'      # ForeignKeyField
            })

    Where 'contacts' is a ManyToManyField specifying to use the lookup channel 'contact'
    and 'author' is a ForeignKeyField specifying here to also use the same lookup channel 'contact'

    Zshow_m2m_helpc                   s„   e Zd ZG dd„ dƒZeed” ƒ e”dƒr€e”jdƒrHeed”jjƒ e”jdƒrdeed”jjƒ e”jdƒr€eed”jjƒ dS )zmake_ajax_form.<locals>.TheFormc               @   s   e Zd Zg ZdS )z$make_ajax_form.<locals>.TheForm.MetaN)Ú__name__Ú
__module__Ú__qualname__Úexclude© r   r   úD/tmp/pip-install-q3hcpn_q/django-ajax-selects/ajax_select/helpers.pyÚMeta-   s   r   ÚmodelÚfieldsr   ÚwidgetsN)	r   r	   r
   r   ÚsetattrÚhasattrr   r   r   r   )r   Ú
superclassr   r   ÚTheForm+   s   
r   )ÚpopÚitemsÚmake_ajax_fieldZdeclared_fieldsZbase_fields)	r   Z	fieldlistr   Úshow_help_textÚkwargsr   Zmodel_fieldnameÚchannelÚfr   )r   r   r   Úmake_ajax_form   s     

r   c       
      K   s²   ddl m}m}m} | jj|ƒ}d|kr>ttt|j	ƒƒƒ|d< d|krV|j
rV|j
|d< d|krj|j |d< ||d< t|tƒrŠ||f|Ž}	n$t|tƒr¢||f|Ž}	n||f|Ž}	|	S )a•  Makes an AutoComplete field for use in a Form.

    Args:
        related_model (Model): model of the related object
        fieldname_on_model (str): field name on the model being edited
        channel (str): channel name of a registered LookupChannel
        show_help_text (bool): show or supress help text below the widget
            Django admin will show help text below the widget, but not for ManyToMany inside of admin inlines
            This setting will show the help text inside the widget itself.
        kwargs: optional args

            - help_text: default is the model db field's help_text.
                            None will disable all help text
            - label: default is the model db field's verbose name
            - required: default is the model db field's (not) blank

    Returns:
        (AutoCompleteField, AutoCompleteSelectField, AutoCompleteSelectMultipleField): field
    r   )ÚAutoCompleteFieldÚAutoCompleteSelectMultipleFieldÚAutoCompleteSelectFieldÚlabelÚ	help_textÚrequiredr   )Zajax_select.fieldsr   r   r    Z_metaÚ	get_fieldÚ_r   r   Zverbose_namer"   ZblankÚ
isinstancer   r   )
Zrelated_modelZfieldname_on_modelr   r   r   r   r   r    Úfieldr   r   r   r   r   B   s*    


r   N)F)Zdjango.db.models.fields.relatedr   r   Zdjango.forms.modelsr   Zdjango.utils.textr   Zdjango.utils.encodingr   Zdjango.utils.translationr   r%   r   r   r   r   r   r   Ú<module>   s   :