a
     ldS#                     @   s  d dl m Z  d dlmZ d dlmZmZmZ d dl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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"m#Z#m$Z$m%Z% d dl&m'Z'm(Z( d dl)m*Z*m+Z+ d dl,m-Z-m.Z. d dl/m0Z0m1Z1 d dl2m3Z3m4Z4m5Z5m6Z6m7Z7 d dl8m9Z9 e Z:ej;dej<dej=e*j>e*j?e*j@gdZAe5e6eAe#e"gZBe4e3eAe#e"gZCe$e#e"gZDedeeBe9gddG dd deZEedeeCe9gddG d d! d!eZFdS )"    )datetime)BytesIO)IterableListOrderedDictN)settings)get_user_model)HttpResponse)method_decorator)openapi)swagger_auto_schema)action)IsAuthenticated)Request)Response)ReadOnlyModelViewSet)ChangelogFilterReviewChangelogFilter)date_after_paramdate_before_paramcompany_name_paramstatus_param)CompanyHistorySerializerReviewHistorySerializer)HistoryTypeHistoryTypeSymbol)CompanyReview)"CompanyHistoryResultsSetPagination!ReviewHistoryResultsSetPagination)author_paramcompany_param
name_param
user_paramemail_config_param)#NoDjangoFilerBackendFilterInspectortypezChangelog type)descriptionr&   enumlist)manual_parametersfilter_inspectors)name	decoratorc                   @   sZ   e Zd ZeZefZeZe	Z
dZdd Zdd Zeededdgd	d
edddZdS )CompanyHistoryViewSet
history_idc                 C   sV   t jt jt jg}tjddj|dd}| j	j
}|jsR|j|jjdddd}|S )	Nuserhistory_userZhistory_type__in-history_dateidTflatid__in)r   CREATEDCHANGEDDELETEDr   historyprefetch_relatedfilterorder_byrequestr0   is_superuser	companiesvalues_listselftypesquerysetr0    rH   O/var/www/html/Ranjet/backend/verify_trusted/companies/api/viewsets/changelog.pyget_querysetZ   s    z"CompanyHistoryViewSet.get_querysetc           
      O   s   |   }| |}|j}|j|d ur0| |jnd d}|jrdt|jdkrddtj|jf |d d< |d ur|jrt|jdkrdtj|jf |d d< |d ur||}dd |j	D }	nd }	|	|d	< t
|S )
Nselected_recordprevious_recordr   zhttps://%s/media/%srL   logorM   c                 S   sX   i | ]P}|j |j d ks |j dkrFt|jjdkr8|jjnd|jjdn|j|jdqS )imagerN   r   Noldnew)fieldlenrQ   r,   urlrR   .0changerH   rH   rI   
<dictcomp>   s   z2CompanyHistoryViewSet.retrieve.<locals>.<dictcomp>diff)
get_objectget_serializerprev_recorddatarN   rT   r   AWS_S3_CUSTOM_DOMAINdiff_againstchangesr   
rE   r@   argskwargsinstance
serializerr]   responsedeltarZ   rH   rH   rI   retrievei   s4    


zCompanyHistoryViewSet.retrieve)r*   FGETzexport-excel)detailmethodsurl_path)r@   c                 O   s$  |  |  }t|ddj}g d}t }t|}| }	t|D ]\}
}|		d|
| qDt|ddD ]v\}}|	
|d|d t|d |d	 |d
 t|d t|d |d |d |d |d |d |d |d |d g qh|  tdd}dtt }d||d< |	|  |S )NT)many)Idz
History IdzHistory DateActionzHistory UserUserDomainzCompany NameZPhoneEmailZLogoDescriptionAddresszIs Verifiedr      )startr4   r/   history_datehistory_typer1   r0   rU   r,   phoneemailrN   aboutaddressis_verifiedzapplication/vnd.ms-excel)content_typezCompany Changelogs {}zattachment;filename="{}.xlsx"zContent-Disposition)filter_querysetrJ   r   r^   r   
xlsxwriterWorkbookadd_worksheet	enumeratewrite	write_rowstrcloser	   formatr   todaygetvalue)rE   r@   rc   rd   	historiesr^   headersoutputworkbook	worksheetirS   rr<   rg   filenamerH   rH   rI   export_excel   sH    




z"CompanyHistoryViewSet.export_excelN)__name__
__module____qualname__r   serializer_classr   permission_classesr   pagination_classr   filterset_classlookup_fieldrJ   ri   r   export_paramsr   r   r   rH   rH   rH   rI   r.   L   s   &r.   c                   @   s2   e Zd ZeZefZeZe	Z
dZdd Zdd ZdS )ReviewHistoryViewSetr/   c                 C   s`   t jt jt jg}tjddj|dd}| j	j
}|js\|jd ur\|j|jjdddd}|S )	Nsourcer1   r2   r3   r4   Tr5   r7   )r   r9   r:   r;   r   r<   r=   r>   r?   r@   r0   rA   rB   rC   rD   rH   rH   rI   rJ      s     z!ReviewHistoryViewSet.get_querysetc           
      O   sp   |   }| |}|j}|j|d ur0| |jnd d}|d ur\||}dd |jD }	nd }	|	|d< t|S )NrK   c                 S   s   i | ]}|j |j|jd qS )rP   )rS   rQ   rR   rV   rH   rH   rI   rY     s   z1ReviewHistoryViewSet.retrieve.<locals>.<dictcomp>rZ   )r[   r\   r]   r^   r`   ra   r   rb   rH   rH   rI   ri      s     

zReviewHistoryViewSet.retrieveN)r   r   r   r   r   r   r   r   r   r   r   r   rJ   ri   rH   rH   rH   rI   r      s   r   )Gr   ior   typingr   r   r   r   django.confr   django.contrib.authr   django.httpr	   django.utils.decoratorsr
   drf_yasgr   Zdrf_yasg.utilsr   rest_framework.decoratorsr   rest_framework.permissionsr   rest_framework.requestr   rest_framework.responser   rest_framework.viewsetsr   Z.verify_trusted.companies.api.filters.changelogr   r   #verify_trusted.companies.api.paramsr   r   r   r   2verify_trusted.companies.api.serializers.changelogr   r   &verify_trusted.companies.history_typesr   r   Zverify_trusted.companies.modelsr   r   $verify_trusted.users.api.paginationsr   r   verify_trusted.users.api.paramsr    r!   r"   r#   r$   Z$verify_trusted.utils.swagger_schemasr%   rq   	ParameterIN_QUERYTYPE_STRINGr9   r:   CLAIMEDZhistory_type_paramZhistory_search_paramsZreview_history_search_paramsr   r.   r   rH   rH   rH   rI   <module>   s   	 