U
    >Ke                     @   sN   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
d	S )
    )ObjectDoesNotExist)NOT_PROVIDED)Manager   )widgets)
FieldErrorc                   @   sf   e Zd ZdZddgZdddeddddfddZdd	 Zd
d Zdd Z	dddZ
dd ZdddZdS )Fieldaw  
    Field represent mapping between `object` field and representation of
    this field.

    :param attribute: A string of either an instance attribute or callable off
        the object.

    :param column_name: Lets you provide a name for the column that represents
        this field in the export.

    :param widget: Defines a widget that will be used to represent this
        field's data in the export, or transform the value during import.

    :param readonly: A Boolean which defines if this field will be ignored
        during import.

    :param default: This value will be returned by
        :meth:`~import_export.fields.Field.clean` if this field's widget did
        not return an adequate value.

    :param saves_null_values: Controls whether null values are saved on the object
    :param dehydrate_method: Lets you choose your own method for dehydration rather
        than using `dehydrate_{field_name}` syntax.
    :param m2m_add: changes save of this field to add the values, if they do not exist,
        to a ManyToMany field instead of setting all values.  Only useful if field is
        a ManyToMany field.
    N FTc	           	      C   s@   || _ || _|| _|st }|| _|| _|| _|| _|| _	d S )N)
	attributedefaultcolumn_namer   ZWidgetwidgetreadonlysaves_null_valuesdehydrate_methodm2m_add)	selfr
   r   r   r   r   r   r   r    r   8/tmp/pip-unpacked-wheel-v_jw5did/import_export/fields.py__init__(   s    zField.__init__c                 C   s<   d| j j| j jf }t| dd}|dk	r4d||f S d| S )zC
        Displays the module, class and name of the field.
        z%s.%sr   Nz<%s: %s>z<%s>)	__class__
__module____name__getattr)r   pathr   r   r   r   __repr__>   s
    zField.__repr__c                 K   s   z|| j  }W n* tk
r8   td| j t|f Y nX | jj|fd|i|}|| jkr~| jtkr~t| jrx|  S | jS |S )z
        Translates the value stored in the imported datasource to an
        appropriate Python object and returns it.
        z;Column '%s' not found in dataset. Available columns are: %srow)	r   KeyErrorlistr   cleanempty_valuesr   r   callable)r   datakwargsvaluer   r   r   r   H   s    

zField.cleanc              
   C   s   | j dkrdS | j d}|}|D ]@}zt||d}W n ttfk
rR   Y  dS X |dkr" dS q"t|r|t|ts|| }|S )z>
        Returns the value of the object's attribute.
        N__)r
   splitr   
ValueErrorr   r!   
isinstancer   )r   objattrsr$   attrr   r   r   	get_value_   s    

zField.get_valuec                 K   s   | j s| jd}|dd D ]}t||d}q| j|f|}|dk	sL| jr|sbt||d | n0| jr~t||d j|  nt||d 	| dS )z
        If this field is not declared readonly, the object's attribute will
        be set to the value returned by :meth:`~import_export.fields.Field.clean`.
        r%   N)
r   r
   r&   r   r   r   setattrr   addset)r   r)   r"   Zis_m2mr#   r*   r+   cleanedr   r   r   savey   s    z
Field.savec                 C   s$   |  |}|dkrdS | j||S )zd
        Returns value from the provided object converted to export
        representation.
        Nr	   )r,   r   render)r   r)   r$   r   r   r   export   s    
zField.exportc                 C   s$   d}| j s|std| j p"|| S )z{
        Returns method name to be used for dehydration of the field.
        Defaults to `dehydrate_{field_name}`
        Z
dehydrate_z6Both dehydrate_method and field_name are not supplied.)r   r   )r   
field_nameZDEFAULT_DEHYDRATE_METHOD_PREFIXr   r   r   get_dehydrate_method   s    
zField.get_dehydrate_method)F)N)r   r   __qualname____doc__r    r   r   r   r   r,   r2   r4   r6   r   r   r   r   r   	   s"   



r   N)Zdjango.core.exceptionsr   Zdjango.db.models.fieldsr   Zdjango.db.models.managerr   r	   r   
exceptionsr   r   r   r   r   r   <module>   s
   