a
    eagO                     @   sr  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&m'Z'm(Z(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ e
j0Z1e
j2Z3e
j4Z5e
j6Z7e
j8Z9e
j:Z;e+e1e3Z<G dd deZ=G dd deZ>G dd deZ?G d d! d!eZ@G d"d# d#eZAG d$d% d%eZBG d&d' d'eZCG d(d) d)eZDG d*d+ d+eZEG d,d- d-eZFG d.d/ d/eZGG d0d1 d1eZHG d2d3 d3eZIG d4d5 d5eZJG d6d7 d7eZKG d8d9 d9eZLd
S ):    )render)APIView)Responsestatus)datetime)settings)RefreshToken)TokenBackend)get_authorization_headerN)
exceptions   )*)	PaginatorPageNotAnInteger	EmptyPage)Q)date)render_to_string)HttpResponse)JsonResponse)redirect)authenticated)	SyncGrant	ChatGrant
VideoGrant)Client)AccessToken)	timesincec                   @   s   e Zd Zdd ZdS )LawyerChangePasswordc              
   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 )Nmessager   lawyeruserloginUserTypeno user foundold_passwordzOld password is requiredpasswordzPassword is requiredconfirm_passwordzConfirm password is requiredFz$Does not match with the old passwordstatus_codestatus_messageTz0You cannot set new password same as old passwordzSuccessfully Password Changed r*   r+   data)r   	Exceptionr   strr   HTTP_401_UNAUTHORIZEDCustomerobjectsfilterfirstHTTP_400_BAD_REQUESTr.   getr#   check_passwordset_passwordsaveHTTP_200_OKprintHTTP_500_INTERNAL_SERVER_ERROR)selfrequestuidecustomer_objr&   r'   r(    rC   1/var/www/html/Sam_Eipo/Eipo/lawyer_panel/views.pypost-   s6    .
zLawyerChangePassword.postN__name__
__module____qualname__rE   rC   rC   rC   rD   r   ,   s   r   c                   @   s   e Zd Zdd ZdS )UserContractRequestc           	      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 tjj|jd}g }|D ]d}|j|jj|jjjjd |jjjj |jjjj|jjjj|jj|jj|jj|jjd	}|| qttjd	||jd
W S  tyV } z.t| ttjt|dtjdW  Y d }~S d }~0 0 d S )Nr    r   r!   r"   r%   r!    )	id
inquire_idproperty_project_customer_nameproperty_project_customer_email&property_project_customer_phone_numbercustomer_emailcustomer_phone_numbercustomer_namecompany_namesuccess)r*   r+   r.   has_subscriptionr)   )r   r/   r   r0   r   r1   r2   r3   r4   r5   r6   LawyerContractDealsrM   inquiryproperty_project
customerId	firstNamelastNameemailphoneNumber	telephonenamerU   appendr;   rW   r<   r=   )	r>   r?   r@   rA   rB   contract_objall_Datacontractall_datarC   rC   rD   r7   M   s4    .

zUserContractRequest.getNrG   rH   rI   r7   rC   rC   rC   rD   rJ   L   s   rJ   c                   @   s   e Zd Zdd ZdS )UserContractDetailsc                 C   s  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d	 }|sztdditj
dW S |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j|jjjjd
 |jjjj |jjjj|jjjj|jj|jj|jj|jj|jjd
}ttj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%   contract_idzcontract id is requiredrM   zNo contract foundrL   )
rM   rN   rO   rP   rQ   rR   rS   rU   r    contract_statusrV   r-   r)   )r   r/   r   r0   r   r1   r2   r3   r4   r5   r6   query_paramsr7   rX   rM   rY   rZ   r[   r\   r]   r^   r_   r`   rU   r    rk   r;   r<   r=   )r>   r?   r@   rA   rB   ri   rc   rf   rC   rC   rD   r7   m   s:    .

zUserContractDetails.getNrg   rC   rC   rC   rD   rh   l   s   rh   c                   @   s   e Zd Zdd ZdS )LawyerSubscriptionExpiresc              
   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   agency)r$   T)customer	is_active-idFrV   )r   r/   r   r0   r   r1   r2   r3   r4   r5   LawyerSubscriptionsrM   order_byr   todaysub_expiry_daterW   r:   rp   r=   )r>   r?   r@   rA   customerObjro   
agency_objrt   rC   rC   rD   r7      s"    .

zLawyerSubscriptionExpires.getNrg   rC   rC   rC   rD   rm      s   rm   c                   @   s   e Zd Zdd ZdS )LawyerAddRatingReviewc           	   
   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 requiredrj   )send_user_customerlawyer_receive_customerratingreviewSuccess)r   r/   r   r0   r   r1   r2   r3   r4   r5   r.   r7   r6   lawyerRatingAndReviewcreater=   )	r>   r?   r@   rA   rv   rz   r|   r~   receieve_customer_objrC   rC   rD   rE      s(    .zLawyerAddRatingReview.postNrF   rC   rC   rC   rD   rx      s   rx   c                   @   s   e Zd Zdd Zdd ZdS )LawyerEditRatingReviewc              
   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   ry   	rating_idrating_id is requiredrj   z No rating data found is requiredr   r   r   r    r.   )r   r/   r   r0   r   r1   r2   r3   r4   r5   rl   r7   r6   r   r   r   r=   )r>   r?   r@   rA   rv   r   
rating_objrf   rC   rC   rD   r7      s$    .zLawyerEditRatingReview.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   ry   r   r   rz   r{   r|   r}   rj   r   r   )r   r/   r   r0   r   r1   r2   r3   r4   r5   r.   r7   r6   r   updater=   )r>   r?   r@   rA   rv   r   rz   r|   rC   rC   rD   rE      s&    .zLawyerEditRatingReview.postN)rG   rH   rI   r7   rE   rC   rC   rC   rD   r      s   r   c                   @   s   e Zd Zdd ZdS )LawyerDeleteRatingReviewc              
   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   ry   r   r   rj   r   )r   r/   r   r0   r   r1   r2   r3   r4   r5   r.   r7   r6   r   deleter=   )r>   r?   r@   rA   rv   r   r   rC   rC   rD   rE      s    .zLawyerDeleteRatingReview.postNrF   rC   rC   rC   rD   r      s   r   c                   @   s   e Zd Zdd ZdS )LawyerGetMyRatingsc           	   
   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   ry   r   rL   )rT   customer_imager   rating_dater   r   )r   r/   r   r0   r   r1   r2   r3   r4   r5   r   send_customerr\   r]   profileImager   
created_atrb   r=   )	r>   r?   r@   rA   rv   r   rd   r   rf   rC   rC   rD   r7      s$    .zLawyerGetMyRatings.getNrg   rC   rC   rC   rD   r      s   r   c                   @   s   e Zd Zdd ZdS )LawyerGetMyReviewsc           	   
   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   ry   r   rL   )rT   r   reviewsreview_dater   r   )r   r/   r   r0   r   r1   r2   r3   r4   r5   r   r   r\   r]   r   r   
start_daterb   r=   )	r>   r?   r@   rA   rv   
review_objrd   r   rf   rC   rC   rD   r7     s$    .zLawyerGetMyReviews.getNrg   rC   rC   rC   rD   r     s   r   c                   @   s   e Zd Zdd ZdS )LawyerViewRatingReviewsc              
   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   ry   customer_idcustomer_id is requiredr   r   rating__avgrL   )rT   r   r   r   rating_review_dater   )r    rating_review_dataaverage_ratingtotal_reviews)r   r/   r   r0   r   r1   r2   r3   r4   r5   rl   r7   r6   r   	aggregateAvgcountr   r\   r   r   r   r   rb   roundr=   )r>   r?   r@   rA   rv   r   r   r   r   r   r   rf   rC   rC   rD   r7   ,  s4    .zLawyerViewRatingReviews.getNrg   rC   rC   rC   rD   r   +  s   r   c                   @   s   e Zd Zdd ZdS )lawyerConverstationTokenc              
   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 )Nuser_idry   r   )identity)service_sidTdefaultz5You have a new message in ${CONVERSATION}: ${MESSAGE})new_message_enablednew_message_soundnew_message_template)r   tokenzToken Created Successfullyr-   r    r   )r   jwtdecoder   
SECRET_KEYr2   r3   r4   r5   rM   r   account_sidapi_sid
api_secretchat_service_sidr   	add_grantclientconversationsv1servicesconfigurationnotificationsr   to_jwtr   r   r;   r/   r0   r=   )r>   r?   r   decodedr   rv   r   
chat_grantnotificationalldatarA   rC   rC   rD   r7   P  s2    


zlawyerConverstationToken.getNrg   rC   rC   rC   rD   r   O  s   r   c                   @   s   e Zd Zdd ZdS )lawyerConverstationListingc                 C   sj  z&t |}|r:t|tj}|d }tjj|d }nd}t	jj|j
dd}g }|D ]}tjj|jjjddd}	d	}
d	}d	}|	D ]D}|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}|| q\ttjd|dW S  tyd } z"tdt|itj dW  Y d }~S d }~0 0 d S )Nr   ry   r   rK   rq   descr   )orderlimitr,   z agofile)rM   twilio_channel_sidr   rT   r   	lawyer_idlawyer_namelawyer_imagelast_messagetimemessage_daterV   r-   r    r   )!r   r   r   r   r   r2   r3   r4   r5   ConversationLawyerrM   rs   r   r   r   r   messageslistbodyr   date_createdmediaro   ra   imager!   rb   r   r   r;   r/   r0   r=   )r>   r?   r   r   r   rv   r   conversation_listconversationr   r   r   r   recordconversation_datarA   rC   rC   rD   r7   v  sV    zlawyerConverstationListing.getNrg   rC   rC   rC   rD   r   u  s   r   c                   @   s   e Zd Zdd ZdS )lawyerConverstationDeletec           	   
   C   s   zt |}|r8t|tj}|d }tjj|d }nd}|j	
d}|s^t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   ry   r   converstation_idzNo customer foundr)   rj   rV   r    r   )r   r   r   r   r   r2   r3   r4   r5   r.   r7   r   r   r6   r   r   r;   r/   r0   r=   )	r>   r?   r   r   r   rv   r   r   rA   rC   rC   rD   r7     s    zlawyerConverstationDelete.getNrg   rC   rC   rC   rD   r     s   r   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   ry   r   r   Zlawyer_privacypage_type_namerM   r   
page_titlepage_contentr   rV   r*   r+   rf   )r   r/   r   r0   r   r1   r2   r3   r4   r5   rl   r7   r6   ContentPagesAdminobjectrM   r   r   r   r;   r=   )r>   r?   r@   rA   rv   r   privacy_objrf   rC   rC   rD   r7     s&    .zgetPrivacyPolicy.getNrg   rC   rC   rC   rD   r     s   r   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   ry   Zlawyer_termsr   r   rV   r   )r   r/   r   r0   r   r1   r2   r3   r4   r5   r   r   rM   r   r   r   r;   r=   )r>   r?   r@   rA   rv   	terms_objrf   rC   rC   rD   r7     s     .zgetTermsCondition.getNrg   rC   rC   rC   rD   r     s   r   c                   @   s   e Zd Zdd ZdS )getFAQLawyerc              
   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}g }|D ]}||j|j|jd qtt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   ry   T)end_date__isnull)rM   titlecontentrV   )r*   r+   rd   )r   r/   r   r0   r   r1   r2   r3   r4   r5   	AgencyFaqrb   rM   r   r   r;   r=   )r>   r?   r@   rA   rv   faq_objrd   faqrC   rC   rD   r7     s"    .
zgetFAQLawyer.getNrg   rC   rC   rC   rD   r     s   r   )M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   requestsdjango.httpr   r   Zcustomer_api.authenticationr   Zcustomer_api.modelstwilio.jwt.access_token.grantsr   r   r   twilio.restr   Ztwilio.jwt.access_tokenr   django.utils.timesincer   TWILIO_ACCOUNT_SIDr   TWILIO_AUTH_TOKEN
auth_tokenTWILIO_CHAT_SIDr   TWILIO_SYNC_SIDsync_service_sidTWILIO_API_SIDr   TWILIO_API_SECRETr   r   r   rJ   rh   rm   rx   r   r   r   r   r   r   r   r   r   r   r   rC   rC   rC   rD   <module>   sb   
  $-$&5