a
    nwg!                     @   s  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Zd dlmZ ddlT d dlmZmZmZ d dlmZ d dlmZ d d
lZd dlmZ d dlmZ d d
lZd dl m!Z! d dl m"Z" d dl#m$Z$ d dl%T 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/m0Z0m1Z1m2Z2 d dl3m4Z4 d dl5m6Z6 d dl7m8Z8 e
j9Z:e
j;Z<e
j=Z>e
j?Z@e
jAZBe
jCZDe4e:e<ZEG dd deZFG dd  d eZGG d!d" d"eZHG d#d$ d$eZIG d%d& d&eZJG d'd( d(eZKG d)d* d*eZLG d+d, d,eZMG d-d. d.eZNG d/d0 d0eZOG d1d2 d2eZPG d3d4 d4eZQG d5d6 d6eZRG d7d8 d8eZSG d9d: d:eZTG d;d< d<eZUG d=d> d>eZVG d?d@ d@eZWG dAdB dBeZXG dCdD dDeZYG dEdF dFeZZG dGdH dHeZ[G dIdJ dJeZ\G dKdL dLeZ]G dMdN dNeZ^G dOdP dPeZ_G dQdR dReZ`G dSdT dTeZaG dUdV dVeZbG dWdX dXeZcd
S )Y    )render)APIView)Responsestatus)datetime)settings)RefreshToken)TokenBackend)get_authorization_headerN)
exceptions   )*)	PaginatorPageNotAnInteger	EmptyPage)Q)date)render_to_string)HttpResponse)JsonResponse)redirect)authenticated)mail)EmailMultiAlternatives)authenticate)	SyncGrant	ChatGrant
VideoGrant)Client)AccessToken)	timesincec                   @   s   e Zd Zdd ZdS )AgncyRegisterc              
   C   sl  z(|j }|d}|d}|d}|d}|d}|d}d}	|sbtdd	itjd
W S |sztdditjd
W S |stdditjd
W S |stdditjd
W S t|	|stdditjd
W S tjj	|d
 }
|
rtdditjd
W S tjj||d}
tjj|
|||||d}tjj|d | d ddd td t }td ||_|  d| }i }td |j}d}t|tj|g|d}|jdd t|
}tjj	|dd d!
 }td" |r|j}nd}|j|j|j |j|j|j!|j"|j#|d#	}t$|t$|j%|d$}td%|d&W S W n< t&yf } z"tdt$|itj'd
W  Y d }~S d }~0 0 d S )'Ncompany_name
first_name	last_nameemailpasswordloginUserTypez*^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$messagefirst_name is requiredr   last_name is requiredemail is requiredpassword is requiredzInvalid EmailusernamezThe email is already registeredr/   r'   )user	firstNamelastNamer&   r(   agency_company z1 agency has been successfully registered with us.TZnew_agency_registeredr)   is_readnotification_typezhere zhere2 9
OTP has been sent on you registered email successfully:
zhere 3Verify OTP:subject
from_emailtobodyFfail_silentlycustomer	is_active-idzhere 4)	r$   r%   companyr&   otpemail_verifiedcustomer_idhas_subscriptionsubscription_idrefreshaccessr1   Successr)   data)(rQ   getr   r   HTTP_400_BAD_REQUESTresearchUserobjectsfilterfirstHTTP_409_CONFLICTcreate_userCustomercreateAdminNotificationsprint
GernateOTPrG   saver&   r   r   EMAIL_HOST_USERsendr	   for_userAgencySubscriptionsorder_byplan_idr2   r3   r4   emailVerifiedidrJ   straccess_token	ExceptionHTTP_500_INTERNAL_SERVER_ERROR)selfrequestrQ   rF   r$   r%   r&   r'   r(   regexuserObjcustomerObjOTPr)   context	to_emailsr<   refresh_tokensub_datarg   userDataallDatae r{   1/var/www/html/Sam_Eipo/Eipo/agency_panel/views.pypost1   sx    






zAgncyRegister.postN__name__
__module____qualname__r}   r{   r{   r{   r|   r"   0   s   r"   c                   @   s   e Zd Zdd ZdS )AgencyemailVerifyOtpc                 C   sT  z|j }|d}|d}tjj|d }tjj|d }|sZtdditj	dW S |j
|krxtdditj	dW S d	|_|  t|}tjj|d	d
d }|r|j}	nd}	|j|j|j|j|j|j
|j|j|j|j|j|	d}
t|t|j|
d}td|dW S  tyN } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr&   rG   r&   r.   r)   zotp is requiredr   zInvalid OtpTrB   rE   r5   )r$   r%   rF   profile_imager&   rG   rH   isProfileCompletedphoneVerifiedrI   rJ   rK   rL   zOTP VerifiedrP   )rQ   rR   r\   rW   rX   rY   rV   r   r   rS   rG   rh   ra   r	   rd   re   rf   rg   r2   r3   r4   profileImager&   r   r   ri   rJ   rj   rk   rl   rm   )rn   ro   rQ   r&   rG   rr   rq   rv   rw   rg   rx   ry   rz   r{   r{   r|   r}   {   sJ    



zAgencyemailVerifyOtp.postNr~   r{   r{   r{   r|   r   z   s   r   c                   @   s   e Zd Zdd ZdS )AgencyregisterResendOtpc              
   C   s   zz|j }|d}tjj|d }t }||_|  d| }i }|}d}	t	|	t
j|g|d}|jdd td|d	W S  ty }
 z"td
t|
itjdW  Y d }
~
S d }
~
0 0 d S )Nr&   r   r9   r:   r;   Fr@   z6OTP has been sent on you registered email successfully)r)   rG   r)   r   )rQ   rR   r\   rW   rX   rY   r`   rG   ra   r   r   rb   rc   r   rl   rj   r   rm   )rn   ro   rQ   r&   rr   rs   r)   rt   ru   r<   rz   r{   r{   r|   r}      s     
zAgencyregisterResendOtp.postNr~   r{   r{   r{   r|   r      s   r   c                   @   s   e Zd Zdd ZdS )Agencyloginc                 C   s  z|j }|d}|d}|s6tdditjdW S |sNtdditjdW S t||d}|srtdditjdW S tjj|d		 }|stdd
itjdW S |j
dkrt }||_|  d| }i }	|j}
d}t|tj|
g|d}|jdd t|}tjj|ddd	 }|r"|j}nd}|j|j|j|j|j|j|j|j
|j|j||j |j d}t!|t!|j"|d}td|dW S  t#y } z"tdt!|itj$dW  Y d }~S d }~0 0 d S )Nr&   r'   r)   r,   r   r-   r0   zemail or password is incorrectr1   zInvalid Credentialsr   r9   r:   r;   Fr@   TrB   rE   r5   )r$   r%   rF   r&   r   r   r   rH   rI   rJ   rK   r(   rolerL   zLogin SuccessfullyrP   )%rQ   rR   r   r   rS   r   r\   rW   rX   rY   rh   r`   rG   ra   r&   r   r   rb   rc   r	   rd   re   rf   rg   r2   r3   r4   r   r   r   ri   rJ   r(   rj   rk   rl   rm   )rn   ro   rQ   r&   r'   rq   rr   rs   r)   rt   ru   r<   rv   rw   rg   rx   ry   rz   r{   r{   r|   r}      sb    



zAgencylogin.postNr~   r{   r{   r{   r|   r      s   r   c                   @   s   e Zd Zdd Zdd ZdS )AgencycompleteProfilec                 C   s.  zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 i }tjj|d	 }t
jj|ddd	 }|r|j}nd}|r||j|j|j|j|j|j|j|j|j|j|j||j|j|j|j|jd td	|d
W S  ty( } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   r   TrB   rE   r5   )r$   r%   r4   r&   rD   r   r   r   r   rI   rJ   rK   phone_number	your_selfaddresslatitude	longituderO   rP   )r   rl   r   rj   r   HTTP_401_UNAUTHORIZEDr\   rW   rX   rY   re   rf   rg   updater2   r3   r4   r&   rD   r   r(   r   r   ri   rJ   phoneNumberbior   r   r   rm   )rn   ro   uidrz   ry   rr   rw   rg   r{   r{   r|   rR      sD    .zAgencycompleteProfile.getc              
   C   s8  zzt |}W n< tyL } z$tdt|itjdW  Y d }~W S d }~0 0 |j}tjj	|d
 }|d}|d}|d}|d}	|d}
|d	}|d
}|d}|d}|d}|
stdditjdW S | stdditjdW S |stdditjdW S |s0tdditjdW S |	sJtdditjdW S |sdtdditjdW S |s~tdditjdW S ||_||_||_|	|_|
|_||_||_||_||_d|_||_|  tjj|jd |j d ddd tddiW S  ty2 } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   r   r   r   r   r   r$   r%   r&   rF   r   r   r*   r+   r,   zaddress is requiredzyour_self is requiredzcompany is requiredzphone_number is requiredTr5   z# agnecy has completed their profilecomplete_profiler6   rO   )r   rl   r   rj   r   r   rQ   r\   rW   rX   rY   rR   rS   r   r   r   r   r2   r3   r&   r   r4   r   r   ra   r^   r]   rm   )rn   ro   r   rz   rQ   rr   r   r   r   r   r$   r%   r&   rF   r   r   r{   r{   r|   r}   "  sP    .









"zAgencycompleteProfile.postNr   r   r   rR   r}   r{   r{   r{   r|   r      s   +r   c                   @   s   e Zd Zdd ZdS )AgencyPropertyRequestc              
   C   s&  zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|dd	 }t
jj|jd}g }|D ]T}tjj|j|jd}|rd}	nd}	|j|j|j|j|j|j|	|jd	}
||
 qxttjd
||jdW S  ty  } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   agencyr1   r(   )location_agency)r   property_requestTF)ri   mobile_numbernamer&   property_typer   is_requestedr   rO   )status_codestatus_messagerQ   rJ   )r   rl   r   rj   r   r   r\   rW   rX   rY   AgencyPropertyRequestsri   AgencyApprovedPropertyr   r   r&   r   property_status
start_dateappendHTTP_200_OKrJ   rm   )rn   ro   r   rz   rr   propertyObjpropertyDatapropertyZapproved_requestedr   rQ   r{   r{   r|   rR   N  s4    .
zAgencyPropertyRequest.getNr   r   r   rR   r{   r{   r{   r|   r   M  s   r   c                   @   s   e Zd Zdd ZdS )ApprovePropertyRequestc              
   C   s*  zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|dd	 }|j
d}|stdditjdW S tjj|d	 }|stdditjdW S tjj|jjd	 }tjj|j||d	 ttjd
dW S  ty$ } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   r   r   property_idzproperty_id is requiredri   zProperty request not found)	agency_idrC   r   rO   r   r   )r   rl   r   rj   r   r   r\   rW   rX   rY   rQ   rR   rS   r   rC   ri   r   r]   r   rm   )rn   ro   r   rz   rr   r   property_objZrequest_customerr{   r{   r|   r}   o  s"    .zApprovePropertyRequest.postNr~   r{   r{   r{   r|   r   n  s   r   c                   @   s   e Zd Zdd ZdS )AgencyNotificationc           	   
   C   s   zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|dd	 }t
jj|ddd}g }|D ](}|j|j|j|j|jd}|| q~td	|d
W S  t y } z*t| tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   r   r   T)r   end_date__isnullrE   )ri   r)   r8   r7   r   successrP   )r   rl   r   rj   r   r   r\   rW   rX   rY   AgencyNotificationsrf   ri   r)   r8   r7   r   r   r_   rm   )	rn   ro   r   rz   rr   notificationObjry   notifyalldatar{   r{   r|   rR     s(    .zAgencyNotification.getNr   r{   r{   r{   r|   r     s   r   c                   @   s   e Zd Zdd ZdS )AgencyDeleteNotificationsc              
   C   s  zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|dd	 }|j
}|d}|stdditjdW S |D ] }tjj|djtj d qtdd	iW S  t y } z*t| tdt|itjdW  Y d }~S d }~0 0 d S )
Nr)   r   r   r   ri   zid requiredr   )end_dater   )r   rl   r   rj   r   r   r\   rW   rX   rY   rQ   rR   rS   r   r   r   nowr_   rm   )rn   ro   r   rz   rr   rQ   notificaton_idsir{   r{   r|   r}     s     .
zAgencyDeleteNotifications.postNr~   r{   r{   r{   r|   r     s   r   c                   @   s   e Zd Zdd ZdS )AgencyReadNotificationsc           
   
   C   s  zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|dd	 }|j
}|d}|r|D ]}tjj|djdd qxn&tjjd	d}|D ]}	d|	_|	  qtdd
iW S  ty } z*t| tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   r   r   ri   r   T)r7   Fr   )r   rl   r   rj   r   r   r\   rW   rX   rY   rQ   rR   r   r   r7   ra   r_   rm   )
rn   ro   r   rz   rr   rQ   r   r   not_objr   r{   r{   r|   r}     s&    .

zAgencyReadNotifications.postNr~   r{   r{   r{   r|   r     s   r   c                   @   s   e Zd Zdd ZdS )AgencyAcceptedRequestc           	   
   C   s  zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|dd	 }t
jj|jdd}g }|D ]4}|j|j|j|j|j|jjj|jd}|| qzttjd|d	W S  t y } z"tdt|itjdW  Y d }~S d }~0 0 d S )
Nr)   r   r   r   Acceptedr   r   )ri   r   r   r&   r   rI   r   rO   r   r   rQ   )r   rl   r   rj   r   r   r\   rW   rX   rY   r   ri   r   r   r&   r   rC   r1   r   r   r   rm   )	rn   ro   r   rz   rr   r   r   r   rQ   r{   r{   r|   rR     s*    .	zAgencyAcceptedRequest.getNr   r{   r{   r{   r|   r     s   r   c                   @   s   e Zd Zdd ZdS )AgencyLoginAsUserc              
   C   s  zt |}|d u s&|dks&| dkr0tdt|tj}|d }|j}|	d}|srt
tjddtjdW S tjj|d	 }|st
d
ditjdW S t|}t|t|jdd}	t
tjd|	dW S  ty
 }
 z"t
d
t|
itjdW  Y d }
~
S d }
~
0 0 d S )Nnullr5   z;Authorization Header or Token is missing on Request Headersuser_idri   zid is requiredr   r   r   r)   no user foundr   )rM   rN   login_byr   r   )r   stripr   AuthenticationFailedjwtdecoder   
SECRET_KEYrQ   rR   r   r   rS   r\   rW   rX   rY   r	   rd   rj   rk   r   rl   rm   )rn   ro   tokendecodedr   rQ   rI   customer_objrv   ry   rz   r{   r{   r|   r}     s*    


zAgencyLoginAsUser.postNr~   r{   r{   r{   r|   r     s   r   c                   @   s   e Zd Zdd ZdS )AgencyChangePasswordc              
   C   s  z`zt |}W n< tyL } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|dd	 }|sztdditj
dW S |jd}|stdditj
dW S |jd}|stdd	itj
dW S |jd
}|stdditj
dW S |j|dkrttj
ddtj
dW S |j|dkr:ttj
ddtj
dW S |j| |j  ttjdddW S  ty } z.t| ttjt|dtjdW  Y d }~S d }~0 0 d S )Nr)   r   r   r   r   old_passwordzOld password is requiredr'   zPassword is requiredconfirm_passwordzConfirm password is requiredFz$Does not match with the old passwordr   Tz0You cannot set new password same as old passwordzSuccessfully Password Changedr5   r   )r   rl   r   rj   r   r   r\   rW   rX   rY   rS   rQ   rR   r1   check_passwordset_passwordra   r   r_   rm   )rn   ro   r   rz   r   r   r'   r   r{   r{   r|   r}      s6    .
zAgencyChangePassword.postNr~   r{   r{   r{   r|   r     s   r   c                   @   s   e Zd Zdd ZdS )AgencyCustomerLeadsc              !   C   s|  z8zt |}W n< tyL } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|dd	 }t
jj|dd}g }|D ]}||j|jj|jj|jj|jj|jj|jj|jj|jj|jj|jj|jj|jj|jj|jj|jj|jj|jj|jj|jj|jj|jj |jj!|jj"|j#|j$|j%|j&dd qztd	|d
W S  tyv } z"tdt|itj'dW  Y d }~S d }~0 0 d S )Nr)   r   r   r   r   Zproperty_project__agency_idr   PropertyProjectri   r   property_name
image_datar   videosproperty_documenthouse_numberpostal_codeproperty_dimensionregionr   rent_to_landlordcashflow_pcmcashflow_pareturn_on_investmentproperty_descriptionrental_descriptionvision_descriptioncashflow_forecastcosts_to_considerlocationr   r   	telephoner   contract_statuscontract_idtyper   rP   )(r   rl   r   rj   r   r   r\   rW   rX   rY   inquirePropertyProjectr   ri   property_projectr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rm   )rn   ro   r   rz   rr   inquirePropertyProjectObjry   	ppinquirer{   r{   r|   rR     sV    .
 zAgencyCustomerLeads.getNr   r{   r{   r{   r|   r     s   r   c                   @   s   e Zd Zdd ZdS )AgencyDashboardc              !   C   sH  zt |}W n: tyF } z"tdt|itjdW  Y d }~S d }~0 0 tjj|dd	 }t
j  }t
jj|jdd }tjj|dd }t
j d	d d
 }t
jd  }	tjj|dd }
g }|D ],}|j|j|j|j|j|jd}|| qtjj|ddd	d d
 }g }|D ]}||j|jj|jj|jj|jj|jj|jj|jj|jj|jj|jj |jj!|jj"|jj#|jj$|jj%|jj&|jj'|jj(|jj)|jj*|jj+|jj,|jj-|j.|j|j/|j0dd q0t
jj1t2dddj1t3ddd}dd |D dd |D d}td||||||	|
|d	S )Nr)   r   r   r   r   r   r   r   rE      rC   )ri   r   r   r&   r   r   r   r   r   )monthr   ri   Zrequest_countc                 S   s   g | ]}|d   dqS )r   z%Y-%m)strftime.0entryr{   r{   r|   
<listcomp>      z'AgencyDashboard.get.<locals>.<listcomp>c                 S   s   g | ]}|d  qS r   r{   r   r{   r{   r|   r     r   )labelsrQ   r   )	r)   total_requestsapproved_requests
total_leadr   allLeadsDataunique_customers_countlead_customer
graph_data)4r   rl   r   rj   r   r   r\   rW   rX   rY   r   allcountri   r   rf   valuesdistinctr   r   r&   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   annotate
TruncMonthCount)rn   ro   r   rz   rr   r  r  r  Zlatest_requestr  r  r   r   rQ   Zlatest_leadsr  r   Zmonthly_requestsr  r{   r{   r|   rR   P  s    ,#	zAgencyDashboard.getNr   r{   r{   r{   r|   r   O  s   r   c                   @   s   e Zd Zdd ZdS )AgencyCheckSubscriptionExiprec              
   C   s   zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjjdd	 }|D ]N}t
jj|jddd	 }t }|j|krbd|_|  d|_|  qbtdd	iW S  t y } z"tdt|itjdW  Y d }~S d }~0 0 d S )
Nr)   r   r   )r(   TrB   rE   Fr   )r   rl   r   rj   r   r   r\   rW   rX   rY   re   ri   rf   r   todaysub_expiry_daterJ   ra   rD   rm   )rn   ro   r   rz   rr   rC   
agency_objr  r{   r{   r|   rR     s"    .

z!AgencyCheckSubscriptionExipre.getNr   r{   r{   r{   r|   r    s   r  c                   @   s   e Zd Zdd ZdS )AgencyAddRatingReviewc           	   
   C   s@   zzt |}W n< tyL } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|d	 }|j
d}|stdditjdW S |j
d}|stdditjdW S |j
d}|stdd	itjdW S tjj|d
	 }tjj||||d tddiW S  ty: } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   r   rating_datarating_data is requiredreview_datareview_data is requiredreceive_customer_idzreceive_customer_id is requiredr   )user_send_customeragency_receive_customerratingreviewrO   )r   rl   r   rj   r   r   r\   rW   rX   rY   rQ   rR   rS   AgencyRatingAndReviewr]   rm   )	rn   ro   r   rz   rr   r  r  r  receieve_customer_objr{   r{   r|   r}     s(    .zAgencyAddRatingReview.postNr~   r{   r{   r{   r|   r    s   r  c                   @   s   e Zd Zdd Zdd ZdS )AgencyEditRatingReviewc              
   C   s
  zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|d	 }|j
d}|stdditjdW S tjj|d	 }|stdditjdW S |j|jd}td	|d
W S  ty } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   r   	rating_idrating_id is requiredr   z No rating data found is requiredr  r  rO   rP   )r   rl   r   rj   r   r   r\   rW   rX   rY   query_paramsrR   rS   r  r  r  rm   )rn   ro   r   rz   rr   r   
rating_objall_datar{   r{   r|   rR     s$    .zAgencyEditRatingReview.getc              
   C   s0  zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|d	 }|j
d}|stdditjdW S |j
d}|stdditjdW S |j
d}|stdd	itjdW S tjj|d
j||d tddiW S  ty* } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   r   r   r!  r  r  r  r  r   r"  rO   )r   rl   r   rj   r   r   r\   rW   rX   rY   rQ   rR   rS   r  r   rm   )rn   ro   r   rz   rr   r   r  r  r{   r{   r|   r}     s&    .zAgencyEditRatingReview.postNr   r{   r{   r{   r|   r    s   r  c                   @   s   e Zd Zdd ZdS )AgencyDeleteRatingReviewc              
   C   s   zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|d	 }|j
d}|stdditjdW S tjj|d	 }|  tddiW S  ty } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   r   r   r!  r   rO   )r   rl   r   rj   r   r   r\   rW   rX   rY   rQ   rR   rS   r  deleterm   )rn   ro   r   rz   rr   r   r$  r{   r{   r|   r}     s    .zAgencyDeleteRatingReview.postNr~   r{   r{   r{   r|   r&    s   r&  c                   @   s   e Zd Zdd ZdS )AgencyGetMyRatingsc           	   
   C   s   zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|d	 }t
jj|d}g }|D ]8}|jjd |jj |jj|j|j|jd}|| qttd|dW S  t y } z"tdt|itjdW  Y d }~S d }~0 0 d S )	Nr)   r   r   r   )customer_namecustomer_imager  reviewsrating_daterO   rP   )r   rl   r   rj   r   r   r\   rW   rX   rY   r  r  r2   r3   r   r  r  r   r   rm   )	rn   ro   r   rz   rr   r$  all_Datar  r%  r{   r{   r|   rR     s&    .zAgencyGetMyRatings.getNr   r{   r{   r{   r|   r(    s   r(  c                   @   s   e Zd Zdd ZdS )AgencyGetMyReviewsc           	   
   C   s   zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|d	 }t
jj|d}g }|D ]4}|jjd |jj |jj|j|jd}|| qttd|dW S  t y } z"tdt|itjdW  Y d }~S d }~0 0 d S )	Nr)   r   r   r)  r*  )r+  r,  r-  review_daterO   rP   )r   rl   r   rj   r   r   r\   rW   rX   rY   r  r  r2   r3   r   r  r   r   rm   )	rn   ro   r   rz   rr   
review_objr/  r  r%  r{   r{   r|   rR   $  s$    .zAgencyGetMyReviews.getNr   r{   r{   r{   r|   r0  #  s   r0  c                   @   s   e Zd Zdd ZdS )AgencyViewRatingReviewsc              
   C   sx  z4zt |}W n< tyL } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|d	 }|j
d}|stdditjdW S tjj|d}|r tjj|dtdd }tjj|d }g }	|D ]8}
|
jjd	 |
jj |
jj|
j|
j|
jd
}|	| qtd|	t||dW S tdd	d	d	dW S W n< tyr } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   r   rI   customer_id is requiredr)  r  rating__avgr*  )r+  r,  r  r  rating_review_daterO   )r)   rating_review_dataaverage_ratingtotal_reviews)r   rl   r   rj   r   r   r\   rW   rX   rY   r#  rR   rS   r  	aggregateAvgr	  r  r2   r   r  r  r   r   roundrm   )rn   ro   r   rz   rr   rI   r$  r8  r9  r7  r  r%  r{   r{   r|   rR   <  s4    .zAgencyViewRatingReviews.getNr   r{   r{   r{   r|   r3  ;  s   r3  c                   @   s   e Zd Zdd ZdS )agentCustomerListingc           	   
   C   s   zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|d	 }|j
d}|stdditjdW S tjj|jdd}g }|D ]2}||jj|jj|jj|jj|jj|jd qtd	|d
W S  ty } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   r   rI   r4  r   r   )rI   Zcustomer_firstnameZcustomer_lastnamecustomer_emailr,  r   rO   rP   )r   rl   r   rj   r   r   r\   rW   rX   rY   r#  rR   rS   r   ri   r   rC   r2   r3   r&   r   r   rm   )	rn   ro   r   rz   rr   rI   requestsrQ   Zrequest_objr{   r{   r|   rR   _  s.    .
zagentCustomerListing.getNr   r{   r{   r{   r|   r=  ^  s   r=  c                   @   s   e Zd Zdd ZdS )createChatTokenAgencyc              
   C   s   zt |}|r8t|tj}|d }tjj|d }ntd}|j	}t
ttt|d}trjttd}|| tjjtj jdddd	}|| d
}	ttjd|	dW S W n: ty }
 z"tdt|
itjdW  Y d }
~
S d }
~
0 0 d S )Nr   r   r   )identity)service_sidTdefaultz5You have a new message in ${CONVERSATION}: ${MESSAGE})new_message_enablednew_message_soundnew_message_template)rA  r   zToken Created Successfullyr   r)   r   )r   r   r   r   r   r\   rW   rX   rY   ri   r    account_sidapi_sid
api_secretchat_service_sidr   	add_grantclientconversationsv1servicesconfigurationnotificationsr   to_jwtr   r   r   rl   rj   rm   )rn   ro   r   r   r   rr   rA  
chat_grantnotificationr   rz   r{   r{   r|   rR   |  s2    


zcreateChatTokenAgency.getNr   r{   r{   r{   r|   r@  {  s   r@  c                   @   s   e Zd Zdd ZdS )agencyCustomerChatListingc                 C   s  znzt |}W n< tyL } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|d	 }|j
d}|stdditjdW S tjj|jdd}g }|D ]}tjj|jjjdd	d
}	d}
d}d}|	D ]F}|j r|j}
t|jd }|j}q|jrd}
t|jd }|j}q|j|j|jj|jj|jj|jj|jj|jj|
||d}|| qttjd|dW S  ty } z"tdt|itj dW  Y d }~S d }~0 0 d S )Nr)   r   r   rI   r4  )r   rE   descr   )orderlimitr5   z agofile)ri   twilio_channel_sidrI   r+  r,  r   agency_nameagency_imagelast_messagetimemessage_dater   r   )!r   rl   r   rj   r   r   r\   rW   rX   rY   r#  rR   rS   ConversationAgencyri   rf   rL  rM  rN  rZ  messageslistr?   r!   date_createdmediarC   r   imager   r   r   rm   )rn   ro   r   rz   rr   rI   rM  conversation_listconversationra  r]  r^  r_  recordconversation_datar{   r{   r|   rR     sZ    .zagencyCustomerChatListing.getNr   r{   r{   r{   r|   rU    s   rU  c                   @   s   e Zd Zdd ZdS )deleteCustomerAgencyChatc              
   C   s  zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|d	 }|j
d}|stdditjdW S |jd}|sttjddW S tjj|d		 }|  ttjd
dW S  ty
 } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   r   rI   r4  converstation_idzNo customer foundr   r   r   )r   rl   r   rj   r   r   r\   rW   rX   rY   r#  rR   rS   rQ   r`  r'  r   rm   )rn   ro   r   rz   rr   rI   rk  rM  r{   r{   r|   rR     s"    .zdeleteCustomerAgencyChat.getNr   r{   r{   r{   r|   rj    s   rj  c                   @   s   e Zd Zdd ZdS )getPrivacyPolicyc              
   C   s  zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|d	 }|j
d}|stdditjdW S tjjdd	 }|j|j|j|j|jd}ttjd	|d
W S  t y } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   r   rI   r4  Zagency_privacypage_type_nameri   rn  
page_titlepage_contentr   r   r   r   r%  )r   rl   r   rj   r   r   r\   rW   rX   rY   r#  rR   rS   ContentPagesAdminobjectri   rn  rp  rq  r   rm   )rn   ro   r   rz   rr   rI   Zprivacy_objr%  r{   r{   r|   rR     s&    .zgetPrivacyPolicy.getNr   r{   r{   r{   r|   rl    s   rl  c                   @   s   e Zd Zdd ZdS )getTermsConditionc              
   C   s   zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|d	 }t
jjdd	 }|j|j|j|j|jd}ttjd|dW S  ty } z"tdt|itjdW  Y d }~S d }~0 0 d S )	Nr)   r   r   Zagency_termsrm  ro  r   rr  )r   rl   r   rj   r   r   r\   rW   rX   rY   rs  rt  ri   rn  rp  rq  r   rm   )rn   ro   r   rz   rr   Z	terms_objr%  r{   r{   r|   rR     s     .zgetTermsCondition.getNr   r{   r{   r{   r|   ru    s   ru  c                   @   s   e Zd Zdd ZdS )getFAQagencyc           	   
   C   s   zzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 tjj|d	 }|j
d}tjjdd}g }|D ]}||j|j|jd qttjd|d	W S  t y } z"tdt|itjdW  Y d }~S d }~0 0 d S )
Nr)   r   r   rI   T)r   )ri   titlecontentr   )r   r   r/  )r   rl   r   rj   r   r   r\   rW   rX   rY   r#  rR   	AgencyFaqr   ri   rw  rx  r   rm   )	rn   ro   r   rz   rr   rI   faq_objr/  faqr{   r{   r|   rR     s$    .
zgetFAQagency.getNr   r{   r{   r{   r|   rv    s   rv  c                   @   s   e Zd Zdd ZdS )addAgencyPropertyc              
   C   s   zNzt |}W n< tyJ } z$tdt|itjdW  Y d }~W S d }~0 0 W n: ty } z"tdt|itjdW  Y d }~S d }~0 0 d S )Nr)   r   )r   rl   r   rj   r   r   rm   )rn   ro   r   rz   r{   r{   r|   rR   5  s    2zaddAgencyProperty.getNr   r{   r{   r{   r|   r|  4  s   r|  )ddjango.shortcutsr   rest_framework.viewsr   rest_framework.responser   rest_frameworkr   r   django.confr   Zrest_framework_simplejwt.tokensr	   !rest_framework_simplejwt.backendsr
   Zrest_framework.authenticationr   r   r   modelsdjango.core.paginatorr   r   r   django.db.modelsr   r   django.template.loaderr   django.http.responser   r?  django.httpr   r   Zcustomer_api.authenticationr   Zcustomer_api.modelsrT   Zadmin_api.modelsZcustomer_api.functionsdjango.corer   django.core.mailr   django.contrib.authr   twilio.jwt.access_token.grantsr   r   r   twilio.restr   Ztwilio.jwt.access_tokenr    django.utils.timesincer!   TWILIO_ACCOUNT_SIDrG  TWILIO_AUTH_TOKEN
auth_tokenTWILIO_CHAT_SIDrJ  TWILIO_SYNC_SIDsync_service_sidTWILIO_API_SIDrH  TWILIO_API_SECRETrI  rL  r"   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r&  r(  r0  r3  r=  r@  rU  rj  rl  ru  rv  r|  r{   r{   r{   r|   <module>   s   
J-;W!2P-#'7