U
    e;-dº  ã                   @   sH  d dl mZ d dlmZ d dlmZmZmZ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Zd dlZd dlZd dlZd dlZd dlZd dlZd dlT d dl T d dl!T 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eƒZ,G dd„ deƒZ-G dd„ deƒZ.dS )é    )ÚResponse)ÚAPIView)ÚHTTP_200_OKÚHTTP_401_UNAUTHORIZEDÚHTTP_204_NO_CONTENTÚHTTP_400_BAD_REQUESTÚHTTP_422_UNPROCESSABLE_ENTITY)ÚJWTAuthentication)ÚIsAuthenticated)Úpermissions)ÚJsonResponse)Úcsrf_exempt)ÚF)ÚQN)Ú*)ÚChannelModel)ÚAttributesModel)Ú*ProductClassificationChannelAttributeModel©Úmodel_to_dict)ÚWorkspaceUserc                   @   s8   e Zd ZefZefZdd„ Zdd„ Zdd„ Z	dd„ Z
d	S )
Ú&ProductClassificationChannelAttributesc                 C   s    t ||jƒ}|d krdS |dkS )NFÚD)ÚgetOrDefaultÚattribute_id©ÚselfÚ	attributeÚattribute_transformation_mapÚtransformation© r    úZC:\Users\offic\Desktop\PIM\XCMS_PIM\app\views\ProductClassificationChannelAttributeView.pyÚis_excluded*   s    z2ProductClassificationChannelAttributes.is_excludedc                 C   s    t ||jƒ}t|ƒ}||d< |S )Nr   )r   r   r   r   r    r    r!   Úcombine_attribute0   s    z8ProductClassificationChannelAttributes.combine_attributec                 C   s   t |ƒ}d|d< |S )NÚAr   r   )r   r   r    r    r!   Úset_add_attribute6   s    z8ProductClassificationChannelAttributes.set_add_attributec                    sÊ   t t|d�}t t|d�}|d ks(|d kr2ttd�S tjj||d� d¡ 	dd¡}dd„ |D ƒ‰ | 
¡ }d	d
„ ˆ  ¡ D ƒ}tjj|d�}	‡ ‡fdd
„|D ƒ}
|
 ‡fdd
„|	D ƒ¡ d|
dœ}t|dtd�S )N©Úcd©Ústatus)Úproduct_classification_idÚ
channel_idÚ
created_atr   r   c                 S   s   i | ]}|d  |d “qS )r   r   r    )Ú.0Úentityr    r    r!   Ú
<dictcomp>G   s      z>ProductClassificationChannelAttributes.get.<locals>.<dictcomp>c                 S   s   g | ]\}}|d kr|‘qS )r$   r    )r-   Úcoder   r    r    r!   Ú
<listcomp>I   s     z>ProductClassificationChannelAttributes.get.<locals>.<listcomp>)Úcd__inc                    s   g | ]}ˆ  |ˆ ¡‘qS r    )r#   ©r-   r   ©r   r   r    r!   r1   M   s   ÿc                    s   g | ]}ˆ   |¡‘qS r    )r%   r3   )r   r    r!   r1   Q   s     Úsuccess)ÚresultÚdataF©Úsafer)   )Úget_or_noneÚProductClassificationModelr   r   r   r   ÚobjectsÚfilterÚorder_byÚvaluesÚget_attributesÚitemsr   Úextendr   r   )r   ÚrequestÚproduct_classification_cdÚ
channel_cdÚproduct_classificationÚchannelÚentitiesÚ!product_classification_attributesZattribute_codes_to_addZattributes_to_addÚ
attributesÚresponser    r4   r!   Úget;   s4    
 ÿý üÿÿþ
z*ProductClassificationChannelAttributes.getN)Ú__name__Ú
__module__Ú__qualname__r	   Úauthencation_classesr
   Úpermission_classesr"   r#   r%   rL   r    r    r    r!   r   &   s   r   c                   @   s    e Zd ZefZefZdd„ ZdS )Ú,ProductClassificationChannelAttributesUpdatec              	   C   sü   |j j}tt|d�}|j}t|dgƒs0ttd�S d|kr@|d ng }g }g }	|d D ]b}
|
d }|
d }tt|||d�}|d kr t||||||d�}|	 	|¡ qT||_
||_| 	|¡ qTtj |	¡ tj |dd	g¡ tjj|||d
� ¡  ttƒ dtd�S )N)Úuser_uidrA   r(   Ú	to_deleter'   r   )r*   r+   r   )r*   r+   r   r   Ú
created_byÚ
updated_byrV   )r*   r+   Zattribute_id__inFr8   )ÚuserrS   r:   r   r7   ÚhasAllAttributesr   r   r   Úappendr   rV   r<   Úbulk_createÚbulk_updater=   Údeleter   ÚgetSuccessResponseDatar   )r   rC   rD   rE   Úuser_idrW   r7   Zitem_codes_to_deleteZitems_to_updateZitems_to_createÚitemÚattribute_codeZattribute_transformationr.   r    r    r!   ÚpostZ   sR    
üú ÿý
z1ProductClassificationChannelAttributesUpdate.postN©rM   rN   rO   r	   rP   r
   rQ   ra   r    r    r    r!   rR   V   s   rR   c                   @   s    e Zd ZefZefZdd„ ZdS )Ú,ProductClassificationChannelProductsDownloadc                 C   s    |j }t|dgƒsttd�S |d }tt|d�}tt|d�}|d ksL|d krVttd�S | ¡  ¡ }t	j
j||d� d¡ dd¡}	dd	„ |D ƒ}
d
d	„ |D ƒ}d S )NÚproduct_codesr(   r&   )rD   rE   r,   Úattribute_cdr   c                 S   s   g | ]}|d  t jkr|‘qS ©Úcontrol_type)ÚATTRIBUTE_TYPESZ
DATE_INPUTr3   r    r    r!   r1   �   s     zEProductClassificationChannelProductsDownload.post.<locals>.<listcomp>c                 S   s   g | ]}|d  t jkr|‘qS rf   )rh   ZBOOLEAN_INPUTr3   r    r    r!   r1       s     )r7   rX   r   r   r:   r;   r   r@   r?   r   r<   r=   r>   )r   rC   rD   rE   r7   rd   rF   rG   rJ   Zattributes_transformationsZdate_attributesZboolean_attributesr    r    r!   ra   Œ   s2    

 ÿý üÿÿÿz1ProductClassificationChannelProductsDownload.postNrb   r    r    r    r!   rc   ˆ   s   rc   )/Úrest_framework.responser   Úrest_framework.viewsr   Zrest_framework.statusr   r   r   r   r   Z'rest_framework_simplejwt.authenticationr	   Zrest_framework.permissionsr
   Úrest_frameworkr   Údjango.http.responser   Údjango.views.decorators.csrfr   Údjango.db.modelsr   r   ÚtimeÚosÚjsonÚulidÚbase64Úcv2ÚnumpyÚnpÚXCMS_PIM.pim_cnstsÚapp.utils.utilsZapp.utils.constsÚapp.models.channel_modelr   Úapp.models.attributes_modelr   Z9app.models.product_classification_channel_attribute_modelr   Údjango.forms.modelsr   ZXCMS_PIM.modelsr   r   rR   rc   r    r    r    r!   Ú<module>   s6   02