U
    [±ddò@  ã                   @   sJ  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% 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„ Z/dd„ Z0d<dd„Z1dd„ 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:G d,d-„ d-e	ƒZ;G d.d/„ d/e	ƒZ<G d0d1„ d1e	ƒZ=G d2d3„ d3e	ƒZ>G d4d5„ d5e	ƒZ?G d6d7„ d7e	ƒZ@G d8d9„ d9e	ƒZAG d:d;„ d;e	ƒZBd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)Ú*)ÚCategoryModel)ÚChannelModel)ÚWorkspaceUser)ÚPrdSKUModel)Úmodel_to_dict)ÚQc                 C   s\   t j t¡st  t¡ t |¡}tj|tj	d�}t
 |t
j¡}t
 t j t| ¡t |¡ d S )N)Údtype)ÚosÚpathÚexistsÚG_ENV_PIM_CATEGORY_DATA_STRAGEÚmkdirÚbase64Ú	b64decodeÚnpÚ
frombufferÚuint8Úcv2ÚimdecodeÚIMREAD_COLORÚimwriteÚjoinÚG_PIM_CATEGORY_IMAGE_FILE_EXT)Úctgry_cdZ
base64_imgÚimage_b64dataZimage_binaryÚ
image_data© r.   ú=C:\Users\offic\Desktop\PIM\XCMS_PIM\app\views\CategoryView.pyÚsaveCategoryImage'   s    

r0   c                 C   s,   t j t| ¡t }t j |¡r(t  |¡ d S ©N)r   r   r)   r   r*   r   Úremove)r+   Z	lFilePathr.   r.   r/   ÚremoveCategoryImage1   s    r3   c           
      C   s’   t  ¡ }| d }|d krtn|}t| ddƒ}t| ddƒ}t| ddƒ}tt|d�}d| krr| d dkrrt|| d ƒ t|||||d	|||d
d�
}	|	S )NÚnameÚlabelsÚ Úorderi'  Únote©ÚcodeÚimgÚ0Ú1)
r:   r7   r4   r8   r5   Ú
is_deletedÚ
created_byÚ
updated_byÚparentÚis_leaf)ÚulidÚnewÚSYSTEM_CATEGORY_CODEÚgetOrDefaultÚget_or_noner   r0   )
ÚdataÚuserÚparent_coder:   r4   r5   r7   r8   rA   Úcategoryr.   r.   r/   ÚgetCategoryObject7   s,    örL   c                 C   sb   t |d| jƒ| _t |d| jƒ| _t |d| jƒ| _t |d| jƒ| _|| _d|kr^t| j|d ƒ | S )Nr4   r5   r8   r7   r;   )rF   r4   r5   r8   r7   r@   r0   r:   )rK   rH   rI   r.   r.   r/   ÚsetCategoryObjectP   s    rM   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategoryListViewc                 C   s4   t jjddd�}t| ¡ ƒ}d|dœ}t|dtd�S )NTr<   )Úparent__isnullr>   Úsuccess©ÚresultrH   F©ÚsafeÚstatus)r   ÚobjectsÚfilterÚlistÚvaluesr   r   ©ÚselfÚrequestÚ
categoriesrH   ÚresponseDatar.   r.   r/   Úget_   s    þzCategoryListView.getN©Ú__name__Ú
__module__Ú__qualname__r   Úauthentication_classesr   Úpermission_classesr_   r.   r.   r.   r/   rN   [   s   rN   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategoryRegisterViewc                 C   s”   t  |j¡}d|krttd�S |jj}tt|d�}d|krB|d nd }|d k	rdt	j
j|d�jdd� t|||ƒ}t	j
 |¡ d|ji}t|d	td
�S )Nr4   ©rU   ©Úuser_uidrJ   r9   r<   ©rB   ÚcdFrS   )ÚjsonÚloadsÚbodyr   r   rI   ri   rG   r   r   rV   rW   ÚupdaterL   Úcreater:   r   r   )r[   r\   rH   Úuser_idrI   rJ   rK   r^   r.   r.   r/   Úpostm   s    
 ÿzCategoryRegisterView.postN©ra   rb   rc   r   rd   r   re   rr   r.   r.   r.   r/   rf   i   s   rf   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategoryListRegisterViewc                    sœ   t  |j¡}d|krttd�S d|kr.|d nd ‰ |d }|jj}tt|d�‰ˆ d k	rlt	j
jˆ d�jdd� ‡ ‡fdd	„|D ƒ}t	j
 |¡ ttƒ d
td�S )Nr]   rg   rJ   rh   r9   r<   rj   c                    s    g | ]}d |krt |ˆˆ ƒ‘qS )r4   )rL   ©Ú.0rK   ©rJ   rI   r.   r/   Ú
<listcomp>Œ   s      z1CategoryListRegisterView.post.<locals>.<listcomp>FrS   )rl   rm   rn   r   r   rI   ri   rG   r   r   rV   rW   ro   Úbulk_creater   ÚgetSuccessResponseDatar   )r[   r\   rH   r]   rq   r.   rw   r/   rr   ‚   s    
zCategoryListRegisterView.postNrs   r.   r.   r.   r/   rt   ~   s   rt   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategoryUpdateViewc                 C   sb   t  | j¡}| jj}tt|d�}tt|d�}|d kr>tt	d�S t
|||ƒ}| ¡  ttƒ dtd�S )Nrh   r9   rg   FrS   )rl   rm   rn   rI   ri   rG   r   r   r   r   rM   Úsaver   rz   r   )r\   r:   rH   rq   rI   rK   r.   r.   r/   rr   •   s    
zCategoryUpdateView.postNrs   r.   r.   r.   r/   r{   ‘   s   r{   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategoryListUpdateViewc                    s¬   t  |j¡}d|krttd�S |d }dd„ |D ƒ}dd„ |D ƒ‰ |jj}tt|d�‰t	j
j|d�}‡ ‡fd	d„|D ƒ}d
ddddg}t	j
 ||¡ d|dœ}t|dtd�S )Nr]   rg   c                 S   s   g | ]}d |kr|d  ‘qS r9   r.   ru   r.   r.   r/   rx   ª   s      z/CategoryListUpdateView.post.<locals>.<listcomp>c                 S   s   i | ]}|d  |“qS r9   r.   ru   r.   r.   r/   Ú
<dictcomp>«   s      z/CategoryListUpdateView.post.<locals>.<dictcomp>rh   ©Zcode__inc                    s   g | ]}t |ˆ |j ˆƒ‘qS r.   )rM   r:   ru   ©ZcategoryDataMaprI   r.   r/   rx   ¯   s     r4   r5   r8   r7   r@   rP   )rR   Zupdated_category_codesFrS   )rl   rm   rn   r   r   rI   ri   rG   r   r   rV   rW   Úbulk_updater   r   )r[   r\   rH   r]   ZcategoryCodesrq   Zfields_to_updater^   r.   r€   r/   rr   ¥   s     
þzCategoryListUpdateView.postNrs   r.   r.   r.   r/   r}   ¡   s   r}   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategoryDeleteViewc           	      C   sš   t t|d�}|d krttd�S d}| ¡ }|D ]"}||j ¡  ¡ 7 }|dkr. qRq.|dkrnd|_d}| 	¡  nd}|ddœ}t
|d	td
�S t
tƒ d	td
�S )Nr9   rg   r   r=   rP   ÚfailureuQ   ã�“ã�®ã‚«ãƒ†ã‚´ãƒªãƒ¼ã�¯å•†å“�ã�«ç´�ã�¥ã�„ã�¦ã�„ã‚‹ã�Ÿã‚�å‰Šé™¤ã�§ã��ã�¾ã�›ã‚“ã€‚)rR   ÚmessageFrS   )rG   r   r   r   Úget_leaf_categoriesÚproductsÚallÚcountr>   r|   r   r   rz   )	r[   r\   r:   rK   Úbinded_product_countZleavesÚleafÚlResultÚlResponseDatar.   r.   r/   rr   ½   s&    

þzCategoryDeleteView.postNrs   r.   r.   r.   r/   r‚   ¹   s   r‚   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategoryListDeleteViewc                 C   sL   t  |j¡}d|krttd�S |d }tjj|d�}| ¡  t	t
ƒ dtd�S )NÚcategory_codesrg   r   FrS   )rl   rm   rn   r   r   r   rV   rW   Údeleter   rz   r   )r[   r\   rH   rŽ   r]   r.   r.   r/   rr   Ú   s    
zCategoryListDeleteView.postNrs   r.   r.   r.   r/   r�   Ö   s   r�   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategoryProductRegisterViewc                 C   s~   t  |j¡}d|krttd�S tt|d�}|d kr<ttd�S |d }tt|d�}|d krbttd�S |j	 
|¡ ttƒ dtd�S )NÚproduct_coderg   r9   FrS   )rl   rm   rn   r   r   rG   r   r
   r   r†   Úaddr   rz   r   )r[   r\   r:   rH   rK   r‘   Úproductr.   r.   r/   rr   è   s    


z CategoryProductRegisterView.postNrs   r.   r.   r.   r/   r�   ä   s   r�   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategoryProductListDeleteViewc                 C   sn   t  |j¡}d|krttd�S tt|d�}|d kr<ttd�S |d }tj	j
|d�}|jj|Ž  ttƒ dtd�S ©NÚproduct_codesrg   r9   )Úprd_mng_id__inFrS   )rl   rm   rn   r   r   rG   r   r
   r   rV   rW   r†   r2   r   rz   r   ©r[   r\   r:   rH   rK   r–   r†   r.   r.   r/   rr   û   s    

z"CategoryProductListDeleteView.postNrs   r.   r.   r.   r/   r”   ÷   s   r”   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategoryProductListRegisterViewc                 C   sn   t  |j¡}d|krttd�S tt|d�}|d kr<ttd�S |d }tj	j
|d�}|jj|Ž  ttƒ dtd�S r•   )rl   rm   rn   r   r   rG   r   r
   r   rV   rW   r†   r’   r   rz   r   r˜   r.   r.   r/   rr     s    

z$CategoryProductListRegisterView.postNrs   r.   r.   r.   r/   r™     s   r™   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategoryProductListViewc                 C   s\   t t|d�}|d krttd�S |jjtd� d¡}dd„ | ¡ D ƒ}d|dœ}t	|d	t
d
�S )Nr9   rg   ©Úprd_sku_flgr7   c                 S   s   g | ]}|‘qS r.   r.   )rv   r“   r.   r.   r/   rx   "  s     z/CategoryProductListView.get.<locals>.<listcomp>rP   rQ   FrS   )rG   r   r   r
   r†   rW   ÚG_PIM_PRD_SKU_FLG_SKUÚorder_byrY   r   r   ©r[   r\   r:   rK   r†   rH   r^   r.   r.   r/   r_     s    
þzCategoryProductListView.getNr`   r.   r.   r.   r/   rš     s   rš   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategorySeriesListViewc                 C   sP   t t|d�}|d krttd�S |jjtd�}t| ¡ ƒ}d|dœ}t	|dt
d�S )Nr9   rg   r›   rP   rQ   FrS   )rG   r   r   r
   r†   rW   ÚG_PIM_PRD_SKU_FLG_SERIESrX   rY   r   r   rŸ   r.   r.   r/   r_   .  s    
þzCategorySeriesListView.getNr`   r.   r.   r.   r/   r    *  s   r    c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategoryChildrenListViewc                 C   s@   t t|dd�}|d kr ttd�S | ¡ }d|dœ}t|dtd�S )Nr<   )r:   r>   rg   rP   rQ   FrS   )rG   r   r   r
   Úget_subtreer   r   )r[   r\   r:   rK   Úcategory_treer^   r.   r.   r/   r_   ?  s    
þzCategoryChildrenListView.getNr`   r.   r.   r.   r/   r¢   ;  s   r¢   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategorySystemListViewc                 C   s8   t jjdd�}dd„ | ¡ D ƒ}d|dœ}t|dtd�S )	NT)rO   c                 S   s   g | ]}|‘qS r.   r.   ru   r.   r.   r/   rx   Q  s     z.CategorySystemListView.get.<locals>.<listcomp>rP   rQ   FrS   )r   rV   rW   rY   r   r   rZ   r.   r.   r/   r_   O  s    þzCategorySystemListView.getNr`   r.   r.   r.   r/   r¥   K  s   r¥   c                   @   s    e Zd ZefZefZdd„ ZdS )ÚCategoryLeafListViewc                 C   s6   t jjddd�}dd„ |D ƒ}d|dœ}t|dtd	�S )
Nr=   r<   )rB   r>   c                 S   s   g | ]}|  ¡ ‘qS r.   )Úget_parent_category_treeru   r.   r.   r/   rx   _  s     z,CategoryLeafListView.get.<locals>.<listcomp>rP   rQ   FrS   )r   rV   rW   r   r   rZ   r.   r.   r/   r_   ]  s    þzCategoryLeafListView.getNr`   r.   r.   r.   r/   r¦   Y  s   r¦   c                   @   s0   e Zd ZefZefZdZdZdd„ Z	dd„ Z
dS )ÚCategoryTreeSearchViewÚchannelrK   c                    sè   ‡ fdd„‰ ‡ fdd„}||ƒ}|j |d�}|D ]²}| ¡ }| |j¡ d}|}	d}
|
t|ƒk rŠ||
 |	krŠ|	||
  }	|
d7 }
|d7 }qT||d … D ]J}tt|d�}t|d	gd
�}t| t	 |d< d|rÎ|nd i|	|< |	| }	q–q0d S )Nc                    s,   | j  ¡ }t|ƒ}|D ]}|ˆ |ƒ7 }q|S r1   )Úchildrenr‡   rX   )Úparent_categoryZimmediate_child_categoriesZall_child_categoriesZchild_category©Úget_all_child_categoriesr.   r/   r­   n  s
    
zICategoryTreeSearchView.process_category.<locals>.get_all_child_categoriesc                    s8   ˆ | ƒ}dd„ |D ƒ}|  | j¡ tj t|d�¡}|S )Nc                 S   s   g | ]
}|j ‘qS r.   r9   ru   r.   r.   r/   rx   w  s     zZCategoryTreeSearchView.process_category.<locals>.get_parent_categories.<locals>.<listcomp>r   )Úappendr:   r   rV   rW   r   )r«   Zchild_categoriesZchild_category_idsZsearch_resultsr¬   r.   r/   Úget_parent_categoriesu  s
    zFCategoryTreeSearchView.process_category.<locals>.get_parent_categories)Zname__icontainsr   é   r9   r†   )Úexcluder;   Úentry)
rW   Úget_parent_codesr®   r:   ÚlenrG   r   r   ÚG_ENV_PIM_CATEGORY_DATA_ADDRr*   )r[   r«   ÚsearchÚtreer¯   r]   rK   Úparent_codesZstart_code_indexZsubtreeÚir:   r²   Úcategory_dictr.   r¬   r/   Úprocess_categorym  s,    
 
ÿz'CategoryTreeSearchView.process_categoryc                    sœ   d|j krttd�S |j d ‰ |ˆjkr4tt|d�n
tt|d�}i ‰|ˆjkr^ˆ |ˆ ˆ¡ n&|ˆjkr„‡ ‡‡fdd„|j	 
¡ D ƒ dˆdœ}t|d	td
�S )Nr¶   rg   r9   )rk   c                    s   g | ]}ˆ  |ˆ ˆ¡‘qS r.   )r»   ru   ©r¶   r[   r·   r.   r/   rx   ž  s     z.CategoryTreeSearchView.get.<locals>.<listcomp>rP   rQ   FrS   )ÚGETr   r   ÚCATEGORYrG   r   r   r»   ÚCHANNELr]   r‡   r   r   )r[   r\   ÚtypeZ	parent_cdrA   r^   r.   r¼   r/   r_   ‘  s     


ÿ
ý

þzCategoryTreeSearchView.getN)ra   rb   rc   r   rd   r   re   r¿   r¾   r»   r_   r.   r.   r.   r/   r¨   g  s   $r¨   )N)CÚ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   r   rl   rC   r    r%   Únumpyr"   ÚXCMS_PIM.pim_cnstsÚapp.utils.utilsÚapp.models.category_modelr   Úapp.models.channel_modelr   ZXCMS_PIM.modelsr   Úapp.models.prd_sku_modelr   Údjango.forms.modelsr   r   r0   r3   rL   rM   rN   rf   rt   r{   r}   r‚   r�   r�   r”   r™   rš   r    r¢   r¥   r¦   r¨   r.   r.   r.   r/   Ú<module>   sX   

