o
    >jj~  ã                   @   sÄ   d dl Z d dlmZ d dlmZmZmZ d dlmZ d dl	m
Z
 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mZ ddlmZ ddlmZ e  e¡Zdd„ ZG dd„ dejƒZ dS )é    N)ÚRequestException)ÚgenericsÚserializersÚstatus)ÚResponse)ÚLanguage)ÚAPIKeyAuthenticationÚDashboardTokenAuthentication)ÚCurrencyé   )ÚAliExpressProductSave)ÚanalyzeProduct)ÚAliExpressUpstreamErrorÚimportProduct)ÚsaveProduct)Ú(ImportAndAnalyzeProductRequestSerializerc                 C   s’   | d | d | d | d | d |dœ}t jj|d d� d¡ ¡ }|d	u r0t jjdi |¤Ž d	S | ¡ D ]
\}}t|||ƒ q4|jt	|ƒd
� d	S )zBUpdate the current product configuration or create it when absent.ÚshipToCountryÚ	productIdÚ
currencyIdÚ
languageIdÚcollectionId)r   r   r   r   r   ÚisSavedSuccess)r   z-idN)Úupdate_fields© )
r   ÚobjectsÚfilterÚorder_byÚfirstÚcreateÚitemsÚsetattrÚsaveÚtuple)Úvalidated_dataÚis_saved_successÚproduct_save_dataÚproduct_saveÚfieldÚvaluer   r   úa/var/www/snowflake_co_dev_github/snow_flake_back_end_deploy/mama_care_api/ali_express_ds/views.pyÚstore_product_save_result   s&   ú
ÿûr*   c                   @   s@   e Zd ZdZeegZeZg Z	dd„ Z
edd„ ƒZedd„ ƒZdS )	Ú$ImportAndAnalyzeProductDashboardViewzBImport an AliExpress product, analyze it, and return the analysis.c              
      sp  d}‡fdd„|D ƒ}|rt d|dœdd�S | jˆjd�}|jd	d
� |  |jd ¡}|  |jd ¡}i |j¥|j|jdœ¥}	z|	 	¡ ‰ ‡ fdd„dD ƒ}
t
di ˆ ¤Ž}t|ƒ}W n& ttfy } zt|	dd� t dt|ƒdœtjd�W  Y d }~S d }~ww d|d< | d¡dv r¬zt|fi |
¤Ž d	|d< W n ty«   t d|jd ¡ Y nw t|	|d d� t |ƒS )N)r   r   r   r   r   c                    s0   g | ]}ˆ j  |¡d u sˆ j  |¡dkr|‘qS )NÚ )ÚdataÚget)Ú.0r'   )Úrequestr   r)   Ú
<listcomp>>   s
     þz=ImportAndAnalyzeProductDashboardView.post.<locals>.<listcomp>zMissing required fields.)ÚerrorÚmissingFieldsi�  )r   )r-   T)Úraise_exceptionr   r   )ÚtargetCurrencyÚtargetLanguagec                    s   i | ]}|ˆ   |¡“qS r   )Úpop)r/   Úkey)Úimport_datar   r)   Ú
<dictcomp>Y   s    
ÿÿz=ImportAndAnalyzeProductDashboardView.post.<locals>.<dictcomp>)r   r   r   F)r$   zAliExpress request failed.)r2   ÚdetailÚisProductSaveÚdecision)ÚSELLÚCONSIDERz-Failed to save analyzed AliExpress product %sr   r   )r   Úget_serializerr-   Úis_validÚ_get_currencyr#   Ú_get_languageÚcodeÚlanguageCodeÚcopyr   r   r   r   r*   Ústrr   ÚHTTP_502_BAD_GATEWAYr.   r   Ú	ExceptionÚloggerÚ	exception)Úselfr0   ÚargsÚkwargsÚrequired_fieldsÚmissing_fieldsÚ
serializerÚcurrencyÚlanguageÚproduct_dataÚsave_idsÚimported_productÚanalysisr2   r   )r9   r0   r)   Úpost6   sr   
þþûÿý
þþþû€ûþÿþz)ImportAndAnalyzeProductDashboardView.postc                 C   óJ   z	t jj| d�}W n t jy   t ddi¡‚w |js#t ddi¡‚|S )N©Úidr   zCurrency does not exist.zCurrency does not have a code.)r
   r   r.   ÚDoesNotExistr   ÚValidationErrorrD   )Úcurrency_idrR   r   r   r)   rB   }   ó   ÿÿÿz2ImportAndAnalyzeProductDashboardView._get_currencyc                 C   rY   )NrZ   r   zLanguage does not exist.zLanguage does not have a code.)r   r   r.   r\   r   r]   rE   )Úlanguage_idrS   r   r   r)   rC   ‹   r_   z2ImportAndAnalyzeProductDashboardView._get_languageN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   Úauthentication_classesr   Úserializer_classÚquerysetrX   ÚstaticmethodrB   rC   r   r   r   r)   r+   /   s    G
r+   )!ÚloggingÚrequests.exceptionsr   Úrest_frameworkr   r   r   Úrest_framework.responser   Úbasic.modelsr   Úcommon.viewsr   r	   Úshop.modelsr
   Úmodelsr   Úpms.analyze_productr   Úpms.import_productr   r   Úpms.save_productr   Ú
searlizersr   Ú	getLoggerra   rJ   r*   ÚGenericAPIViewr+   r   r   r   r)   Ú<module>   s    
