U
    Éad<  ã                   @   sÜ  d dl 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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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$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/ G dd„ de	ƒZ0G dd„ de	ƒZ1G dd„ de	ƒZ2G dd„ de	ƒZ3G dd„ de	ƒZ4G dd„ de	ƒZ5G d d!„ d!e	ƒZ6G d"d#„ d#e	ƒZ7G d$d%„ d%e	ƒZ8G d&d'„ d'e	ƒZ9G d(d)„ d)e	ƒZ:dS )*é    )ÚrenderÚredirectÚget_object_or_404)Úviewsets)Ú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)ÚFN)Ú*)ÚProductClassificationModelÚ#ProductClassificationAttributeModel)ÚWorkspaceUser)Úmodel_to_dict)ÚAttributesModel)ÚPrdSKUModel)ÚQc                   @   s(   e Zd ZefZefZdd„ Zdd„ ZdS )ÚProductClassificationListViewc                 C   sV   t |ddddgd�}|j}|j ¡ |d< |jjtd� ¡ |d< |j|d< |j|d	< |S )
NÚ
attributesÚproductsÚ
created_byÚ
updated_by©ÚexcludeÚattribute_count©Úprd_sku_flgÚproduct_countÚ
created_at)	r   r   r   Úcountr   ÚfilterÚG_PIM_PRD_SKU_FLG_SKUÚuser_idr&   )ÚselfÚproduct_classificationZproduct_classification_dictZcreated_user© r-   úJC:\Users\offic\Desktop\PIM\XCMS_PIM\app\views\ProductClassificationView.pyÚgetAttributeAndProductCount+   s     
ÿÿ

z9ProductClassificationListView.getAttributeAndProductCountc                    s<   t jjdd�}d}‡ fdd„|D ƒ}||dœ}t|dtd�S )	NÚ0)Ú
is_deletedÚsuccessc                    s   g | ]}ˆ   |¡‘qS r-   )r/   )Ú.0r,   ©r+   r-   r.   Ú
<listcomp>;   s   ÿz5ProductClassificationListView.get.<locals>.<listcomp>©ÚresultÚdataF©ÚsafeÚstatus)r   Úobjectsr(   r   r   )r+   ÚrequestÚproduct_classificationsÚlResultr8   ÚlResponseDatar-   r4   r.   Úget8   s    
þþz!ProductClassificationListView.getN)	Ú__name__Ú
__module__Ú__qualname__r   Úauthentication_classesr   Úpermission_classesr/   rA   r-   r-   r-   r.   r   '   s   r   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚProductClassificationDetailViewc                 C   sX   t t|d�}|d krttd�S t|ddgd�}|j ¡ |d< |j ¡ |d< t|dt	d	�S )
N©Úcd©r;   r   r   r    r"   r%   Fr9   )
Úget_or_noner   r   r
   r   r   r'   r   r   r   )r+   r=   rI   r,   Z!serialized_product_classificationr-   r-   r.   rA   J   s    
z#ProductClassificationDetailView.getN)rB   rC   rD   r   rE   r   rF   rA   r-   r-   r-   r.   rG   F   s   rG   c                   @   s    e Zd ZefZefZdd„ ZdS )Ú!ProductClassificationRegisterViewc           
      C   sˆ   t  |j¡}|jj}ddi}d|kr.ttd�S |d }tt|d�}|}|}t	j
jt ¡ |d||d�}	t|	jƒ|d< d	|d< t|d
td�S )Nr7   ÚfailedÚnamerJ   ©Úuser_uidr0   )rI   rN   r1   r   r   rI   r2   Fr9   )ÚjsonÚloadsÚbodyÚuserrP   r   r   rK   r   r   r<   ÚcreateÚulidÚnewÚstrrI   r   r   )
r+   r=   r8   r*   ÚresponseDatarN   rT   r   r   r,   r-   r-   r.   ÚpostX   s*     ÿ
    ÿz&ProductClassificationRegisterView.postN©rB   rC   rD   r   rE   r   rF   rZ   r-   r-   r-   r.   rL   T   s   rL   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚProductClassificationUpdateViewc                 C   sp   t  |j¡}|jj}tt|d�}|d kr2ttd�S t	|d|j
ƒ}tt|d�}||_
||_| ¡  ttƒ dtd�S )NrH   rJ   rN   rO   Fr9   )rQ   rR   rS   rT   rP   rK   r   r   r   ÚgetOrDefaultrN   r   r   Úsaver   ÚgetSuccessResponseDatar   )r+   r=   rI   r8   r*   r,   rN   rT   r-   r-   r.   rZ   q   s    
z$ProductClassificationUpdateView.postNr[   r-   r-   r-   r.   r\   m   s   r\   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚProductClassificationDeleteViewc                 C   s<   t t|d�}|d krttd�S d|_| ¡  ttƒ dtd�S )NrH   rJ   Ú1Fr9   )	rK   r   r   r   r1   r^   r   r_   r   )r+   r=   rI   r,   r-   r-   r.   rZ   ƒ   s    
z$ProductClassificationDeleteView.postNr[   r-   r-   r-   r.   r`      s   r`   c                   @   s(   e Zd ZefZefZdd„ Zdd„ ZdS )Ú&ProductClassificationAttributeListViewc                 C   s<   t |dd ƒ}|d |d< |d k	r,|dkr,|n|d |d< |S )NÚalternative_namerN   Úoriginal_nameÚ )r]   )r+   Ú	attributerc   r-   r-   r.   Úadjust_attribute�   s
    ÿz7ProductClassificationAttributeListView.adjust_attributec                    sX   t t|d�}|d krttd�S | ¡  ¡ }d}‡ fdd„|D ƒ}||dœ}t|dtd�S )	NrH   rJ   r2   c                    s   g | ]}ˆ   |¡‘qS r-   )rg   ©r3   rf   r4   r-   r.   r5   ž   s     z>ProductClassificationAttributeListView.get.<locals>.<listcomp>r6   Fr9   )rK   r   r   r   Úget_attributesÚvaluesr   r   )r+   r=   rI   r,   r   r?   r8   r@   r-   r4   r.   rA   ˜   s    
þz*ProductClassificationAttributeListView.getN)	rB   rC   rD   r   rE   r   rF   rg   rA   r-   r-   r-   r.   rb   Œ   s   rb   c                   @   s    e Zd ZefZefZdd„ ZdS )Ú(ProductClassificationAttributeUpdateViewc                 C   s`  t t|d�}|d krttd�S |jj}t |j¡}| 	¡ j
|d� ¡ }t|d|jƒ}t|d|jƒ}	t|d|jƒ}
t|d|jƒ}t|d|jƒ}t|d	|jƒ}t|d
|jƒ}t|d|jƒ}t|d|jƒ}t|d|jƒ}t|d|jƒ}t|d|jƒ}t t|d�}||_|	|_|
|_||_||_||_||_||_||_||_||_||_||_| ¡  ttƒ dtd�S )NrH   rJ   ©Úattribute_idrN   Ú	with_unitÚ	is_commonrc   Úcontrol_typeÚnot_nullÚ
is_privateÚis_with_unitÚ
max_lengthÚselect_listÚdefault_valueÚunitrO   Fr9   )rK   r   r   r   rT   rP   rQ   rR   rS   ri   r(   Úfirstr]   rN   rs   ro   rc   rp   rq   rr   rt   ru   rv   rw   r   r   r^   r   r_   r   )r+   r=   rI   Úattribute_coder,   r*   r8   rf   rN   rn   Úcommonrc   Zcontrolrq   rr   rs   rt   ru   rv   rw   rT   r-   r-   r.   rZ   ª   sD    
z-ProductClassificationAttributeUpdateView.postNr[   r-   r-   r-   r.   rk   ¦   s   rk   c                   @   s    e Zd ZefZefZdd„ ZdS )Ú*ProductClassificationAttributeRegisterViewc                 C   sz  t t|d�}|d krttd�S t |j¡}dd„ t|ƒD ƒ}d|d< dd„ |D ƒ}d	d„ |D ƒ}tj	j
|d
�}g }	g }
t|ƒD ]ž\}}||j }| ¡ j
|jd� ¡ }|d k	rÌ|
 |¡ ||_||j |_q~td |||j|jd||j|j|j|j|j|j|j||j |j|jd�}|	 |¡ q~dd„ |
D ƒ}| ¡  
t|d� ¡ ¡  tj	 |
ddg¡ tj	  |	¡ ddi}t!|dt"d�S )NrH   rJ   c                 S   s   i | ]\}}|d  |“qS rH   r-   )r3   ÚorderÚattribute_datar-   r-   r.   Ú
<dictcomp>Ø   s      zCProductClassificationAttributeRegisterView.post.<locals>.<dictcomp>iâÿÿÿÚPRD_CDc                 S   s   g | ]}|d  ‘qS rH   r-   ©r3   r}   r-   r-   r.   r5   Ú   s     zCProductClassificationAttributeRegisterView.post.<locals>.<listcomp>c                 S   s   i | ]}|d  |d “qS )rI   ro   r-   r€   r-   r-   r.   r~   Û   s     )Zcd__inrl   r0   )Úidr,   rf   rc   Úis_fixedr1   ro   rr   rp   rN   rs   rt   ru   rq   r|   rv   rw   c                 S   s   g | ]
}|j ‘qS r-   rl   rh   r-   r-   r.   r5   þ   s     )Zattribute_id__inro   r|   r7   r2   Fr9   )#rK   r   r   r   rQ   rR   rS   Ú	enumerater   r<   r(   rI   ri   rx   Úappendro   r|   r   rc   r‚   rr   rp   rN   rs   rt   ru   rq   rv   rw   r   ÚdeleteÚbulk_updateÚbulk_creater   r   )r+   r=   rI   r,   Zattributes_dataZattribute_orders_dictZattribute_cdsZattributes_common_flags_dictr   Ú!product_classification_attributesZ	to_remainÚirf   ro   Zexisting_attributeÚentryZto_remain_cdsÚresponser-   r-   r.   rZ   Ó   s^    
ÿ

ïz/ProductClassificationAttributeRegisterView.postNr[   r-   r-   r-   r.   r{   Ï   s   r{   c                   @   s(   e Zd ZefZefZdZdZdd„ Z	dS )Ú ProductClassificationSkuListViewé   Úascc                 C   sÞ   t t|d�}|d krttd�S t|j dd¡ƒ}t|j d| j¡ƒ}|j dd¡}|j d| j¡}t	|ƒ}|dkrv|nd	| }|j
jtd
� ¡ }|j
jtdd� |¡||| … }	d}
dd„ |	D ƒ}|
||dœ}t|dtd�S )NrH   rJ   Úoffsetr   Ú
paginationr|   rŽ   Úsortú-r#   r0   )r$   Úprd_delr2   c                 S   s   g | ]}t |ƒ‘qS r-   )Úget_basic_product)r3   Úproductr-   r-   r.   r5     s     z8ProductClassificationSkuListView.get.<locals>.<listcomp>)r7   Útotalr8   Fr9   )rK   r   r   r
   ÚintÚGETrA   Úpaginate_byÚorderingÚgetSortFieldr   r(   r)   r'   Úorder_byr   r   )r+   r=   rI   r,   r�   r�   r|   r‘   r–   r   r?   r8   r@   r-   r-   r.   rA     s.    
ÿ þýz$ProductClassificationSkuListView.getN)
rB   rC   rD   r   rE   r   rF   r™   rš   rA   r-   r-   r-   r.   rŒ     s
   rŒ   c                   @   s    e Zd ZefZefZdd„ ZdS )Ú$ProductClassificationSkuRegisterViewc                 C   sl   t t|d�}|d krttd�S t |j¡}tjj	|d�}|j
 ¡  |j
j|Ž  | ¡  ddi}t|dtd�S )NrH   rJ   )Zprd_mng_id__inr7   r2   Fr9   )rK   r   r   r   rQ   rR   rS   r   r<   r(   r   ÚclearÚaddr^   r   r   )r+   r=   rI   r,   Zproduct_cdsr   r‹   r-   r-   r.   rZ   (  s    

z)ProductClassificationSkuRegisterView.postNr[   r-   r-   r-   r.   r�   $  s   r�   c                   @   s    e Zd ZefZefZdd„ ZdS )Ú)ProductClassificationUpdateAttributeOrderc                 C   s´   t  |j¡}tt|d�}|d kr*ttd�S t|dgƒs@ttd�S |d }g }|D ]<}|d }|d }tt	||d�}	|	d kr|qP||	_
| |	¡ qPt	j |dg¡ ddi}
t|
d	td
�S )NrH   rJ   r   rI   r|   )r,   rm   r7   r2   Fr9   )rQ   rR   rS   rK   r   r   r   ÚhasAllAttributesr   r   r|   r„   r<   r†   r   r   )r+   r=   rI   r8   r,   r   Úattributes_to_updaterf   r|   Z product_classification_attributer‹   r-   r-   r.   rZ   9  s.    

  ÿz.ProductClassificationUpdateAttributeOrder.postNr[   r-   r-   r-   r.   r    5  s   r    );Údjango.shortcutsr   r   r   Úrest_frameworkr   Úrest_framework.responser   Úrest_framework.viewsr   Zrest_framework.statusr   r	   r
   r   r   Z'rest_framework_simplejwt.authenticationr   Zrest_framework.permissionsr   r   Údjango.http.responser   Údjango.views.decorators.csrfr   Údjango.db.modelsr   ÚosrQ   rV   Úbase64Úcv2ÚnumpyÚnpÚXCMS_PIM.pim_cnstsÚapp.utils.utilsÚ'app.models.product_classification_modelr   r   ZXCMS_PIM.modelsr   Údjango.forms.modelsr   Úapp.models.attributes_modelr   Úapp.models.prd_sku_modelr   r   r   rG   rL   r\   r`   rb   rk   r{   rŒ   r�   r    r-   r-   r-   r.   Ú<module>   sF   )7