a
     ldM]                     @   sn  d dl mZ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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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) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< d dl=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZF d dlGmHZH d dlImJZJ d dlKmLZLmMZMmNZNmOZOmPZP d dlQmRZR d dlSmTZTmUZU d dlVmWZWmXZXmYZY d dlZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZf d dlm
Z
mgZg eae^e]e\e[ede`ebe_eceegZhe7e9e8e:e5gZie7e9e8e:e5efgZje6gZke6efgZle<e;gZme ZnG dd  d e-ZoG d!d" d"e#e-ZpG d#d$ d$e-ZqG d%d& d&e-ZrG d'd( d(egZsG d)d* d*e-Zted+eehd,d-G d.d/ d/e"e-ZuG d0d1 d1e!e$eueqeoerZvG d2d3 d3epeqeoZwed+ee^gd,d-G d4d5 d5e"e-ZxG d6d7 d7e"e!e#e-e$Zyd8S )9    )datedatetime)settings)get_user_model)	AvgCaseCountFPrefetchQQuerySetWhenSubquery)Length)HttpResponseRedirect)method_decorator)swagger_auto_schemastatus)action)NotFound)get_object_or_404)CreateModelMixinListModelMixinRetrieveModelMixinUpdateModelMixin)AllowAnyIsAuthenticatedOrReadOnly)Request)Response)GenericViewSet)ordereddict)CompanyFilter)CompanyReviewFilter)end_date_paramis_company_profileplatform_ids_paramrating_paramsource_ids_paramstart_date_paramis_select_allcompany_id_param)	CompanyAllCategorySerializerCompanyFirstLetterSerializerCompanyListSerializerCompanyRemoveUserSerializerCompanySerializerCompanyUpdateSerializerPaymentListSerializerReviewSerializerSourceSocialSerializer)CompanyHistorySerializer)ReviewViewSet)CompanyPaymentReviewSourceSocialSubscription)ReviewSourceSerializer)PlatformReviewSource)!AllCategoriesResultsSetPaginationCompanyResultsSetPaginationReviewResultsSetPagination)category_main_paramcategory_paramdomain_paramfirst_letter_paramhas_reviews_paramis_verified_param
name_paramplatform_paramrating_star_param
user_paramhas_email_param	url_param)r	   Funcc                   @   s>   e Zd Zejddddddddd	d
	 Z
eZdS )BaseCompanyViewSetz-idusersocialsreview_sourcesZreview_sources__platform
crawled_atlast_review_datelast_synced_ataccreditationsearch_vectorN)__name__
__module____qualname__r7   objectsorder_byselect_relatedprefetch_relateddeferallquerysetr0   serializer_class rc   rc   N/var/www/html/Ranjet/backend/verify_trusted/companies/api/viewsets/__init__.pyrO      s   rO   c                       s$   e Zd Z fddZdd Z  ZS )BaseCompanyRetrieveMixinc                    s$   t   }| jdkr |  }|S )Nretrieve)superget_querysetr   with_reviews_countwith_avg_ratingselfra   	__class__rc   rd   rh      s    

z%BaseCompanyRetrieveMixin.get_querysetc                 O   s   |   }| |}|j}|jrBt|jdrBd|jjv rB|jj|d< d|d< d|d< d |d< tjj|jd	 d
	 }|d ur|j
d u rd|d< n|j
t k|d< |jd u |d< |j
|d< t|S )NnamehttplogoT	is_expireFis_trialdue_dateidcompany)
get_objectget_serializerdatarq   hasattrro   r;   r[   filterfirstrt   r   todaypaymentr   )rl   requestargskwargsinstance
serializerresponse_datasubscriptionrc   rc   rd   rf      s.    






z!BaseCompanyRetrieveMixin.retrieve)rX   rY   rZ   rh   rf   __classcell__rc   rc   rm   rd   re      s   re   c                   @   sh  e Zd ZdZeZee dddZee	 dddZ
edeejedd	ied
eddgddedddZedeejedd	ideddgddedddZedejedd	ideddgdddd Zededd	ejedd	ideddgddedddZeddeddgddeddd Zeddeddgd!dedd"d#Zd$S )%BaseCompanyReviewViewSetz\
    This view contains API for fetching `Review` &
    `ReviewSource` of
    `Company`
    returnc                 C   sD   t jd }| jjjr(| jjjr(|S |jt	j
jt	j
jgd}|S )Nsource__platform)Zsource__platform__status__in)r9   r[   r^   r`   r   rP   is_authenticatedis_superuserr|   r=   StatusACTIVEUNAVAILABLErk   rc   rc   rd   get_reviews_queryset   s    z-BaseCompanyReviewViewSet.get_reviews_querysetc                 C   sD   t j }| jjjr"| jjjr"|S |jtj	j
tj	jtj	jgd}|S )N)Zplatform__status__in)r>   r[   r`   r   rP   r   r   r|   r=   r   r   r   HIDE_REVIEWSrk   rc   rc   rd   get_review_sources_queryset   s    
z4BaseCompanyReviewViewSet.get_review_sources_querysetGETTmany)methodmanual_parameters	responsespagination_classreviewsdetailmethodsZurl_pathr   c                 O   s   | j p
| j}| j| j| i}ttjdfi |}|  jt	dt	dt
tdddd ddj|jd	d
dd}t| jj|| jdj}| jj}|tjd }	|	r|tjd }
t|	d}	|
d urt|
dnt }
|j|	|
gd}t|d	d}t|jS )Nru   source__platform__logosource__platform__statusr   display_orderZdisplay_order__gtthendefaultZplatform_logoZplatform_statussort_display_orderTZsource__company_id	is_activer   -dater   %Y-%m-%dZdate__ranger   )lookup_url_kwarglookup_fieldr   r   r7   r[   onlyr   annotater	   r   r   r|   ru   r\   r#   r   r   qsquery_paramsgetr)   ro   r$   r   strptimer   r~   r3   r   rz   )rl   r   r   r   r   filter_kwargsrw   ra   r   start_date_filterend_date_filterr   rc   rc   rd   r      sD    
	z BaseCompanyReviewViewSet.reviews)r   r   r   zreview-sourcesc           
      O   s   | j p
| j}| j| j| i}ttjdfi |}tdtj	 dddd}| 
 |djtdttddj|jd	d
}t|dd}	t|	jS )Nru   r   ratingsourcera   platformreviews__ratingcal_reviews_countcal_average_rating
company_id-average_ratingTr   )r   r   r   r   r7   r[   r   r
   r9   r`   r   r^   r   r   Roundr   r|   ru   r\   r<   r   rz   )
rl   r   r   r   r   r   rw   reviews_prefetchrR   r   rc   rc   rd   rR     s.    


z'BaseCompanyReviewViewSet.review_sources)r   r   zget-sort-reviewc           	   
   C   s   zv|   }ddg}t|jdttjjd@ }tj|	 }t
tjjttj|dddj| d d dd	}W n8 ty } z t| ttjd
W  Y d }~S d }~0 0 t|jS )Nr   r   r   Zplatform__statusru   T)source_id__inr      r   r   )rx   r   ru   r=   r   r   r>   r[   r|   r}   r3   r9   r   values_listr\   	Exceptionprintr   r   HTTP_501_NOT_IMPLEMENTEDrz   )	rl   r   pkrw   ZorderbyListZreview_source_queryrz   r   erc   rc   rd   get_sort_reviews"  s.    
z)BaseCompanyReviewViewSet.get_sort_reviewsPUT)r   request_bodyr   zsave-sorted-reviewsc                 O   s   |   }g }t|jD ]4\}}tjj|d dj|d d ||d  qtjjtt	jj|j
dddj|djd d t|jS )Nru   )ru      )r   r   )r   )Zid__in)rx   	enumeraterz   r9   r[   r|   updateappendr   r>   ru   r   excluder   )rl   r   r   r   rw   Z
review_idsireviewrc   rc   rd   save_sorted_reviews8  s    z,BaseCompanyReviewViewSet.save_sorted_reviews)r   zcheck-existsc                 O   sx   |   }|jdkrFtjj|jddd j}ttjdt	j
 d| dS |jdkrhttjdt	j
 ddS ttjd	S d S )
Nr   r   )normalized_domainr   zhttps://z	/reviews/r   rz      z/all-companyr   )rx   r   r7   r[   r|   r   urlr   HTTP_302_FOUNDr   CLIENT_NAMEHTTP_200_OK)rl   r   r   r   rw   r   rc   rc   rd   check_existW  s    

z$BaseCompanyReviewViewSet.check_existzcheck-exists-place-idc                 O   s   |   }|j}|d ur^d| v r^|d d kr^t|d dkr^d|jd< |  ttjddS |d urd| v r|d dkrd|jd< |  ttjddS d|jd< |  ttjddS d S )	NZplace_idr   r   Zplace_statusr   Z	isUpdatedTr   )rx   mapkeyslensaver   r   r   )rl   r   r   r   rw   Zcompany_maprc   rc   rd   check_exist_place_idf  s"    "
 

z-BaseCompanyReviewViewSet.check_exist_place_idN)rX   rY   rZ   __doc__r@   r   r   r9   r   r>   r   r   company_reviews_filter_paramsr   r   r3   rA   r   r   r   $company_review_sources_filter_paramsr<   rR   r   r   r   r   rc   rc   rc   rd   r      sT   'r   c                   @   s*   e Zd ZeddgddedddZdS )	BaseCompanyPaymentViewSetTr   r   r   r   c                 O   s4   |   }tjddj|d}t|dd}t|jS )Nplanrw   rv   Tr   )rx   r8   r[   r^   r|   r2   r   rz   )rl   r   r   r   rw   r   r   rc   rc   rd   r   ~  s    z!BaseCompanyPaymentViewSet.paymentN)rX   rY   rZ   r   r   r   rc   rc   rc   rd   r   }  s   r   c                   @   s   e Zd ZdZdZdS )r   ROUNDz %(function)s(%(expressions)s, 2)N)rX   rY   rZ   functiontemplaterc   rc   rc   rd   r     s   r   c                   @   sH   e Zd ZeddgddedddZeddgddedd	d
ZdS )BaseCompanyHistoryViewSetTr   	historiesr   r   c                 O   s:   |   }|jdd ddd}tt|ddjS )N
history_idhistory_datez-history_dateTr   )	rx   historyr   r`   r\   valuesr   r5   rz   )rl   r   r   r   rw   r   rc   rc   rd   r     s    z#BaseCompanyHistoryViewSet.historieszhistories/(?P<history_id>[^/]+)c                 O   s2   |   }|jj|d }|s$t tt|jS )N)r   )rx   r   r|   r}   r   r   r0   rz   )rl   r   r   r   r   rw   r   rc   rc   rd   history_diff  s
    z&BaseCompanyHistoryViewSet.history_diffN)rX   rY   rZ   r   r   r   r   rc   rc   rc   rd   r     s   
r   listr   )ro   	decoratorc                       s*   e Zd ZeZegZeZ fddZ	  Z
S )BaseCompanyListViewSetc                    sJ   | j dkrtS | j dv r@| j dkr<| jjtjd d ur<tS tS t	 
 S )NZremove_user)r   rf   r   )r   r/   r   r   r   rE   ro   r-   r.   rg   get_serializer_classrl   rm   rc   rd   r     s    

z+BaseCompanyListViewSet.get_serializer_class)rX   rY   rZ   r@   r   r   permission_classesr"   filterset_classr   r   rc   rc   rm   rd   r     s   r   c                       s8   e Zd ZdZ fddZ fddZ fddZ  ZS )CompanyViewSetz[0-9]+c                    s   | j dkrtS t  S )Nr   )r   r1   rg   r   r   rm   rc   rd   r     s    
z#CompanyViewSet.get_serializer_classc                    s(   t   }| jdv r$|j| jjd}|S )N)r   partial_update)rP   )rg   rh   r   r|   r   rP   rk   rm   rc   rd   rh     s    

zCompanyViewSet.get_querysetc                    s6   d|j v r&| jjjdu r&|j dd  t | d S )Ncurrency_symbolF)validated_datar   rP   r   poprg   perform_update)rl   r   rm   rc   rd   r    s    zCompanyViewSet.perform_update)rX   rY   rZ   lookup_value_regexr   rh   r  r   rc   rc   rm   rd   r     s   r   c                   @   s   e Zd ZdZdZegZee dddZ	ee
 dddZeegd d	d
 Zeeejeddieddd ZeeejeddidedddZdS )CompanyURLLookUpViewSetr   z[^/]+r   c                 C   s   t jdjtjjdS )Nr   )r   )r9   r[   r^   r|   r=   r   r   r   rc   rc   rd   r     s    z,CompanyURLLookUpViewSet.get_reviews_querysetc                 C   s$   t jttjjdttjjdB S )Nr   )r>   r[   r|   r   r=   r   r   r   r   rc   rc   rd   r     s    z3CompanyURLLookUpViewSet.get_review_sources_querysetr   c                 C   s   | j jd}tjj|d }| |}|d ur|j}|j	rbt
|j	drbd|j	jv rb|j	j|d< d|d< d|d	< d |d
< tjj|jd d }|d ur|jd u rd|d< n|jt k|d< |jd u |d	< |j|d
< t|S tdditjdS )Nr   r   ro   rp   rq   Trr   Frs   rt   ru   rv   r   
Not found.r   )r   r   r   r7   r[   r|   r}   ry   rz   rq   r{   ro   r;   rt   r   r~   r   r   r   HTTP_404_NOT_FOUND)rl   r   r   r   r   r   r   rc   rc   rd   get_companies_by_urls  s4    






z-CompanyURLLookUpViewSet.get_companies_by_urlsTr   )r   r   r   c           	      C   s  | j jd}tjj|d }|d ur|  jt	dt	dt
tdddd dd	j|jd
ddd}t| j j|| j dj}| j j}|tjd }|r|tjd }t|d}|d urt|dnt }|j||gd}t|d
d}t|jS tdditjdS )Nr   r  r   r   r   r   r   r   r   Tr   r   r   r   r   r   r   r   r  r   )r   r   r   r7   r[   r|   r}   r   r   r	   r   r   ru   r\   r#   r   r   r)   ro   r$   r   r   r   r~   r3   r   rz   r   r  )	rl   r   r   rw   ra   r   r   r   r   rc   rc   rd   get_company_reviews_by_urls  s>    	
z3CompanyURLLookUpViewSet.get_company_reviews_by_urls)r   r   r   c           	      O   s   | j jd}tjj|d }|d urtdtj	 
dddd}|  |djtdttd	d
j|jdd}t|dd}t|jS tdditjdS )Nr   r  r   ru   r   r   r   r   r   r   r   r   Tr   r   r  r   )r   r   r   r7   r[   r|   r}   r
   r9   r`   r   r   r^   r   r   r   r   ru   r\   r<   r   rz   r   r  )	rl   r   r   r   r   rw   r   rR   r   rc   rc   rd   "get_company_review_sources_by_urls1  s(    


z:CompanyURLLookUpViewSet.get_company_review_sources_by_urlsN)rX   rY   rZ   r   r  r   r   r   r9   r   r>   r   r   rM   r	  company_reviews_urls_paramsr   r   r3   rA   r
  !company_review_sources_url_paramsr<   r   r  rc   rc   rc   rd   r    s$   
&r  c                       s6   e Zd ZejdZeZe	gZ
eZ fddZ  ZS )CompanyCategoryViewSetcategory_mainc                    s   t   }| jdkr| jj}|tjd }|r|d d }t|	 dt|
 dB }tjd|jdddd}|S |jtddjdd	jddddS )
Nr   r   )Zcategory_main__startswithr  ZOthers)r  )Ztext_lenr   )Ztext_len__lte)rg   rh   r   r   r   r   rE   ro   r   lowerupperr7   r[   r   r|   r   distinctr\   r   r   )rl   ra   r   first_letterZfirst_letter_queryrm   rc   rd   rh   S  sD    

	z#CompanyCategoryViewSet.get_queryset)rX   rY   rZ   r7   r[   r   ra   r,   rb   r   r   r?   r   rh   r   rc   rc   rm   rd   r  J  s
   r  c                   @   s&   e Zd ZegZej dZ	e
ZdS )SourceSocialViewSetr   N)rX   rY   rZ   r   r   r:   r[   r`   r\   ra   r4   rb   rc   rc   rc   rd   r  q  s   r  N)zr   r   django.confr   django.contrib.authr   django.db.modelsr   r   r   r	   r
   r   r   r   r   django.db.models.functionsr   django.httpr   django.utils.decoratorsr   Zdrf_yasg.utilsr   rest_frameworkr   Zrest_framework.decoratorsr   rest_framework.exceptionsr   Zrest_framework.genericsr   rest_framework.mixinsr   r   r   r   rest_framework.permissionsr   r   rest_framework.requestr   rest_framework.responser   Zrest_framework.viewsetsr    Zruamel.yaml.compatr!   Z,verify_trusted.companies.api.filters.companyr"   Z+verify_trusted.companies.api.filters.reviewr#   Z#verify_trusted.companies.api.paramsr$   r%   r&   r'   r(   r)   r*   r+   Z(verify_trusted.companies.api.serializersr,   r-   r.   r/   r0   r1   r2   r3   r4   Z2verify_trusted.companies.api.serializers.changelogr5   Z,verify_trusted.companies.api.viewsets.reviewr6   Zverify_trusted.companies.modelsr7   r8   r9   r:   r;   Z&verify_trusted.reviews.api.serializersr<   verify_trusted.reviews.modelsr=   r>   Z$verify_trusted.users.api.paginationsr?   r@   rA   Zverify_trusted.users.api.paramsrB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   Zsearch_paramsr   r  r   r  Zcompany_select_all_paramsUserrO   re   r   r   r   r   r   r   r  r  r  rc   rc   rc   rd   <module>   s   ,(
,8	! J


s$
