U
    >Ke)                     @   s2   G d d dZ G dd de ZG dd deZdS )c                   @   s"   e Zd ZdZdddZdd ZdS )BaseInstanceLoaderz:
    Base abstract implementation of instance loader.
    Nc                 C   s   || _ || _d S N)resourcedataset)selfr   r    r   B/tmp/pip-unpacked-wheel-v_jw5did/import_export/instance_loaders.py__init__   s    zBaseInstanceLoader.__init__c                 C   s   t d S r   )NotImplementedErrorr   rowr   r   r   get_instance
   s    zBaseInstanceLoader.get_instance)N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   
r   c                   @   s    e Zd ZdZdd Zdd ZdS )ModelInstanceLoaderzc
    Instance loader for Django model.

    Lookup for model instance by ``import_id_fields``.
    c                 C   s
   | j  S r   )r   get_querysetr   r   r   r   r      s    z ModelInstanceLoader.get_querysetc                 C   st   zPi }| j  D ] }| j j| }||||j< q|rH|  jf |W S W d S W n | j jjj	k
rn   Y d S X d S r   )
r   get_import_id_fieldsfieldsclean	attributer   getZ_metamodelZDoesNotExist)r   r   paramskeyfieldr   r   r   r      s    
z ModelInstanceLoader.get_instanceN)r   r   r   r   r   r   r   r   r   r   r      s   r   c                       s(   e Zd ZdZ fddZdd Z  ZS )CachedInstanceLoaderz
    Loads all possible model instances in dataset avoid hitting database for
    every ``get_instance`` call.

    This instance loader work only when there is one ``import_id_fields``
    field.
    c                    s   t  j||  j d } jj|  _i  _ jjr jj	 jjd kr fdd jjD } 
 jf d jj |i} fdd|D  _d S )N    c                    s   g | ]} j |qS r   )pk_fieldr   ).0r   r   r   r   
<listcomp>9   s     z1CachedInstanceLoader.__init__.<locals>.<listcomp>z%s__inc                    s   i | ]} j ||qS r   )r   	get_value)r    instancer   r   r   
<dictcomp><   s    
 z1CachedInstanceLoader.__init__.<locals>.<dictcomp>)superr   r   r   r   r   all_instancesr   dictZcolumn_namer   filterr   )r   argskwargsZpk_field_nameidsqs	__class__r   r   r   /   s    
zCachedInstanceLoader.__init__c                 C   s   | j r| j | j|S d S r   )r&   r   r   r   r
   r   r   r   r   @   s    z!CachedInstanceLoader.get_instance)r   r   r   r   r   r   __classcell__r   r   r-   r   r   &   s   r   N)r   r   r   r   r   r   r   <module>   s   