U
    hh                     @   s*   d dl T d dlmZ G dd dejZdS )    )*)cmf_custom_field_choicec                   @   sX   e Zd ZdZejjddddg ZedddZed	d
 Z	edd Z
edddZdS )CmfCustomFieldChoiceTlist_choicecreate_choiceupdate_choicedelete_choiceNc                 C   sn   t t|}t ||d }|r,|j}t t|}	n|rDtjjjj|}	nd S |	||d}
|d k	rb||
_|
	  |
S )N)namechoice_parent_id)
getattrmodelsmodelZcmfZ	BaseModelZdpZdata_driverZgen_custom_choice_modelcust_field_conf_idsave)cls
model_name
field_namevaluer
   Zdb_choice_table_namer   
base_model
base_fieldr   
new_choice r   4./modules/settings/models/cmf_custom_field_choice.pyr      s    
z"CmfCustomFieldChoice.create_choicec                 C   sL   t t|}t ||d }|j}t t|}	|	j||dgd}
||
|< |
  |
S )Nr   )idfields)r   r   r   getr   )r   r   r   r   Zchoice_fieldr   r   r   field_model_namer   choicer   r   r   r   -   s    

z"CmfCustomFieldChoice.update_choicec                 C   sf   t t|}t ||}|j}t t|}|j|d}|  |j|d}	|	rb|	D ]}
t|||
j qLd S )N)r   )r
   )	r   r   r   r   deletelistr   r   r   )r   r   r   r   r   r   r   r   r   Zchildrencr   r   r   r   8   s    


z"CmfCustomFieldChoice.delete_choicec                 C   s\   t t|}t ||d }|sg S |j}|s,g S t t|}d|i}|jf ddddddgi|S )Nr   r   r   r	   r
   Z
cmf_hidden)r   r   r   r   )r   r   r   r   r   r   r   Zfilter_paramsr   r   r   r   G   s    

z CmfCustomFieldChoice.list_choice)NNN)N)__name__
__module____qualname__Z	api_allowr   r   Zapi_methodsclassmethodr   r   r   r   r   r   r   r   r      s   


r   N)Zcmf.includeZmodules.settings.fieldsr   r   r   r   r   r   <module>   s   