
    Nh}G                        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 ddlmZ d d	l d d	l 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mZ d d	l d d	l d dlmZ  G d de      Z  G d de      Z! G d de      Z" G d de      Z# G d de      Z$ G d de      Z% G d de      Z& G d d e      Z' G d! d"e      Z( G d# d$e      Z) G d% d&e      Z* G d' d(e      Z+y))    )APIView)Responsestatus)make_passwordcheck_password)RefreshToken   )authenticated)User)*)django_pbkdf2_sha256N)settings)render_to_string)	send_mail)mail)datetimec                       e Zd Zd Zy)userRegisterc                    |j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|st        ddit        j                        S |st        ddit        j                        S |st        ddit        j                        S t
        j                  j                  |	      j                         rt        dd
it        j                        S t
        j                  j                  ||t        |            }t        ddit        j                        S )Nnameemailpasswordmessagezname  is requiredr   email is requiredpassword is requiredr   z#User with this email already exists)r   r   r   zUser registered successfully)datagetr   r   HTTP_400_BAD_REQUESTEndUserobjectsfilterexistscreater   HTTP_201_CREATED)selfrequestr   r   r   users         -/var/www/html/aiagenthome/user_panel/views.pypostzuserRegister.post   s   			&	!$
,,

7
#%\\j)(	i!45f>Y>YZZ	
I23F<W<W
XX	
I56v?Z?Z
[[__%(//1
IDEfNiNi
jj			
	(# 
  
$
 
9<=fF]F]	^^    N__name__
__module____qualname__r+    r,   r*   r   r      s    _r,   r   c                       e Zd Zd Zy)	loginUserc                    |j                   j                  d      }|j                   j                  d      }|st        ddit        j                        S |st        ddit        j                        S 	 t
        j                  j                  |      }t        ||j                        st        dd	it        j                        S t        j                  |      }t        d
|j                  t        |j                        t        |      dt        j                         S # t
        j                  $ r  t        ddit        j                        cY S w xY w)Nr   r   r   r   r   r   r   zUser Not credentialszInvalid credentialszLogin successful)r   usernameaccessrefresh)r   r   r   r   r    r!   r"   DoesNotExistHTTP_401_UNAUTHORIZEDr   r   r	   for_userr   straccess_tokenHTTP_200_OK)r'   r(   r   r   r)   r7   s         r*   r+   zloginUser.post5   s&   
,,

7
#%\\j)(	
I12&:U:U
VV	
I45V=X=X
YY]
//

E

*4 
$--	0
I45f>Z>Z
[[!!$''	 ii%%&'l	
 
    
		 ]
I56v?[?[
\\]s   6 D$ $0EENr-   r1   r,   r*   r3   r3   4   s     r,   r3   c                       e Zd Zd Zy)userChangePasswordc                 R   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                         j                         }|j                  dk(  r(t        d|j                  dt        j                        S |j                  j!                  d      }|st        dd	it        j"                        S |j                  j!                  d
      }|st        ddit        j"                        S |j$                  }t'        j(                  ||      }	|	dk(  rt        ddit        j*                        S |}
t'        j,                  |
      }||_        |j/                          t        ddi      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j0                        cY d }~S d }~ww xY w)Nr   r   iduser not foundF8Your account has been Inactive, Please contact to admin.r   r   old_passwordzold password is requirednew_passwordnew password is requiredzOld password is incorrectSuccess)r   	Exceptionr   r;   r   r9   r!   r"   r#   firstHTTP_404_NOT_FOUND
SuperAdminallr   HTTP_406_NOT_ACCEPTABLEr   r   r    r   handlerverifyHTTP_403_FORBIDDENhashsaveHTTP_500_INTERNAL_SERVER_ERROR)r'   r(   uideuser_obj	admin_objrF   rG   r   checkr   encrypt_passwords               r*   r+   zuserChangePassword.postS   s   TN

 C oo$$$,2248
Y 01&:S:STT##'')//19oo%Ybkbqbqr  {A  {Y  {Y  Z  Z,,"">2<
Y9:&B]B]^^,,"">2<
Y9:&B]B]^^%%>>>,~65unY:;6C\C\]]8ll8,'8==?
Ii(
))1  NYA'0L0LMMN2 
 T
Ic!f%V-R-R
SSTsg   F1 AG- A"G- >9G- 89G- 2AG- 6:G- 1	G*:%G%G* G- %G**G- -	H&6%H!H&!H&Nr-   r1   r,   r*   r?   r?   R   s    Tr,   r?   c                       e Zd Zd Zy)userForgetPasswordc                 `   	 |j                   }|j                  d      j                         j                         }|st	        ddi      S t
        j                  j                  |      j                         }|dk(  rt	        ddit        j                        S t
        j                  j                  |      j                         }|rt        j                  dd	d
      }||_        |j                          t!        d|d       |j"                  }d}t%        dd|i      }	|	}
t&        j(                  }t+        j,                  ||
||g|	       t	        d|	d      S t	        ddi      S # t.        $ r;}t!        |       t	        dt1        |      it        j2                        cY d }~S d }~ww xY w)Nr   r   Email is requiredr   r   zPThis email does not exist in our database, please enter the valid email address.r   i  i'     z------------zthe otpzForget Password OTPzforget_password_otp.htmlotp)html_messagezRendered HTML)r   htmlzEmail not exists)r   r   striplowerr   r!   r"   r#   countr   rL   rK   random	randrangeforgot_password_otprT   printr   r   r   EMAIL_HOST_USERr   r   rJ   r;   rU   )r'   r(   r   r   r)   rX   theotpto_emailsubjectrb   plain_message
from_emailrW   s                r*   r+   zuserForgetPassword.postu   s   U<<DHHW##%++-Ei 3455OO"""/557Dqyi rs  |B  |U  |U  V  V&&U&399;HT4+V"(X!]]_
>(9-X$W$%?%Q\!]**Z	^^G]J
Q]^    i 2344
 U	!HYs1v&f.S.STTUs2   AE) 
AE) CE) E) )	F-20F("F-(F-Nr-   r1   r,   r*   r]   r]   t   s    Ur,   r]   c                       e Zd Zd Zy)verifyForgetPasswordOTPc                    	 |j                   j                  d      j                         j                         }|s,t	        t
        j                  ddt
        j                        S |j                   j                  d      }|s,t	        t
        j                  ddt
        j                        S t        j                  j                  |      j                         }|s,t	        t
        j                  ddt
        j                        S t        j                  j                  ||	      j                         }|rt	        t
        j                  d
d      S t	        t
        j                  dd      S # t        $ r?}t	        t
        j                  t        |      dt
        j                        cY d }~S d }~ww xY w)Nr   r_   status_codestatus_messager   ra   zotp is Requiredr   zEmail not exist)r   ri   zSuccessfully VerifiedzInvalid otp)r   r   rd   re   r   r   r    r!   r"   r#   r$   rK   r=   rJ   rU   r;   )r'   r(   r   ra   check_emailrX   rW   s          r*   r+   zverifyForgetPasswordOTP.post   s   PLLW%++-335EV%@%@Refnt  oJ  oJ  K  K
,,

5
!CV%@%@Rcdms  nI  nI  J  J//(((7>>@KV%@%@Rcdlr  mH  mH  I  I%%U%MSSUHF$6$6H_`aaF$?$?Q^_``
 P6#H#HZ]^_Z`aio  jO  jO  P  PPs9   A$E5 'AE5 0AE5 AE5 E5 5	F=>4F82F=8F=Nr-   r1   r,   r*   rr   rr      s    Pr,   rr   c                       e Zd Zd Zy)changeForgetPasswordc                 &   	 |j                   j                  d      j                         j                         }|st	        ddit
        j                        S t        j                  j                  |      j                         }|st	        ddit
        j                        S t        j                  j                         j                         }|j
                  dk(  r(t	        d|j                  d	t
        j                        S |j                   j                  d
      }|st	        ddit
        j                        S |j                   j                  d      }|st	        ddit
        j                        S ||k7  rt	        ddit
        j                         S t#        j$                  |      }||_        |j)                          t	        ddi      S # t*        $ r0}t	        dt-        |      it
        j.                        cY d }~S d }~ww xY w)Nr   r   r   r   r   rC   FrD   rE   rG   rH   confirm_passwordzconfirm password is requiredz+Password and confirm password are not same.rI   )r   r   rd   re   r   r   rL   r!   r"   r#   rK   rM   rN   r   rO   r    rR   rP   rS   r   rT   rJ   r;   rU   )	r'   r(   r   rX   rY   rG   r{   r[   rW   s	            r*   r+   zchangeForgetPassword.post   s   T<<G$**,2245
Y 34V=V=VWWoo$$5$17798
Y 01&:S:STT##'')//19oo%Ybkbqbqr  {A  {Y  {Y  Z  Z,,"">2<
Y9:&B]B]^^ll&&'9:
Y=>fFaFabb&&YLMU[UnUnooll#34'8==?
Ii(
))	 T
Ic!f%V-R-R
SSTsD   AG AG %A"G 9G 9G <!G 8G 	H %HHHNr-   r1   r,   r*   ry   ry      s    Tr,   ry   c                       e Zd Zd Zd Zy)updateProfilec                 T   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  |j                  |j                  |j                  |j                  d}t        t        j                   d|d      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j"                        cY d }~S d }~ww xY w)Nr   r   rA   rC   )r   genderphone_numberimager   zFetched Successfully)ru   rv   all_data)r   rJ   r   r;   r   r9   r!   r"   r#   rK   rL   r   r   r   r   r   r=   rU   )r'   r(   rV   rW   rX   r   s         r*   r   zupdateProfile.get   s    TL

 C oo$$$,2248
Y 01&:S:STT==__((NNNN8 &"4"4F\hpq
rr  LYs1v&f.J.JKKL 
 T
Ic!f%V-R-R
SSTsN   B2 AC. AC. 2	C+;%C& C+!C. &C++C. .	D'7%D"D'"D'c                 j   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  j                  d	      }	||_        ||_        ||_        ||_        |	|_        |j%                          t'        d
|       t        t        j(                  dd      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j*                        cY d }~S d }~ww xY w)Nr   r   rA   rC   r   r   r   r   r   rX   zProfile updated successfullyrt   )r   rJ   r   r;   r   r9   r!   r"   r#   rK   rL   r   r   r   r   r   r   r   rT   rj   r=   rU   )
r'   r(   rV   rW   rX   r   r   r   r   r   s
             r*   r+   zupdateProfile.post   si   TL

 C oo$$$,2248
Y 01&:S:STT
,,

6
"4LLX&6!!.1<<<G$5<<G$5 8=8?'88>8>==?H
&"4"4Fde
ff1  LYs1v&f.J.JKKL2 
 T
Ic!f%V-R-R
SSTsN   D= AE9 C!E9 =	E6%E1+E6,E9 1E66E9 9	F2%F-'F2-F2Nr.   r/   r0   r   r+   r1   r,   r*   r}   r}      s    T0Tr,   r}   c                       e Zd Zd Zy)UploadImagesc                 :   	 |j                   }|j                  d      }g }|D ]  }t        |      }|j                  |         t	        t
        j                  d|d      S # t        $ r0}t	        dt        |      it
        j                        cY d }~S d }~ww xY w)Nr   rI   )ru   rv   r   r   r   )
r   getlistuploadTheDocumentappendr   r   r=   rJ   r;   rU   )r'   r(   r   images
image_urlsr   
image_pathrW   s           r*   r+   zUploadImages.post  s    	V
,,4LL!6:u"5)Jj!  &"4"4iWab
cc	 V
Is1v&v/T/T
UUVs   AA! !	B*%BBBNr-   r1   r,   r*   r   r     s    
Vr,   r   c                       e Zd Zd Zd Zy)chatBotc                 H   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                  t        |            }g }|D ]  }|j                  |j                  |j                  |j                  |j                   |j"                  |j$                  |j&                  |j(                  |j*                  |j,                  d
        t        d|dt        j.                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r1}t        d	t        |      d
t        j0                        cY d }~S d }~ww xY w)Nr   r   rA   rC   )user_id)
rB   r   chatbot_namebusiness_typesbusiness_logobusiness_namebusiness_pdfchatbot_colorchatbot_style
created_atrI   r   r   Internal server errorr   details)r   rJ   r   r;   r   r9   r!   r"   r#   rK   rL   ChatBotr   rB   r   r   r   r   r   r   r   r   r   r=   rU   )r'   r(   rV   rW   rX   chatBotschatbot_listingbots           r*   r   zchatBot.get  sb   zL

 C oo$$#$.4468
Y 01&:S:STToo$$s3x$88?s
&&{{$$((&&&&$$&&&&..   y/B6K]K]
^^1  LYs1v&f.J.JKKL4 
 z
63q6JSYSxSx
yyzsN   D+ AE' CE' +	E$4%EE$E' E$$E' '	F!0&FF!F!c                 `   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  }|j                  j                  d      }|j                  d      }t        ||g      st        ddit        j                        S g d}t         j"                  j%                  |j&                        d	   j)                         }	|	|vrt        dd
it        j                        S t         j"                  j+                  t,        j.                  dt        |      |      }
t         j1                  |
d       t         j"                  j+                  |
|j&                        }t3        |d      5 }|j5                         D ]  }|j7                  |        	 d d d        t         j"                  j9                  |t,        j.                        }t:        j                  j=                  |||j                  d      |j                  d      |j                  d      ||j                  d      |j                  d      |j                  d      	      }t        d|j>                  dt        j@                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# 1 sw Y   xY w# t        $ r1}t        dt        |      dt        jB                        cY d }~S d }~ww xY w)Nr   r   rA   rC   r   r   z+business_pdf and chatbot_name are required.)z.pdfz.csvz.xlsxr
   z0Invalid file type. Allowed types: PDF, CSV, XLSXr   T)exist_okwbr   r   r   r   r   r   )	r)   r   r   r   r   r   r   r   r   zChatbot setting created)r   
chatbot_idr   r   )"r   rJ   r   r;   r   r9   r!   r"   r#   rK   rL   r   FILESr   rN   r    ospathsplitextr   re   joinr   
MEDIA_ROOTmakedirsopenchunkswriterelpathr   r%   rB   r&   rU   )r'   r(   rV   rW   rX   r   filer   allowed_extextfolder_path	save_pathfchunkrelative_file_pathchatbots                   r*   r+   zchatBot.post5  s~   /zN

 C oo$$$,2248
Y 01&:S:STT
,,4
--

N
+4((>*<
t\"
#Y MNW]WrWrss*;			$))	$Q	'	-	-	/3	Y RS\b\w\wxxh1163s8\R;;;{T;*ww||K39YWWU^  	 	83F3FG__##	88,-((?+((?+#((?+((?+xx% $ 
7 8

S\b\s\s
ttS  NYA'0L0LMMN. 	( 
 z
63q6JSYSxSx
yyzsu   J* AK3 A!K3 =AK3 BK3 "'K&
CK3 *	K#3%KK#K3 K##K3 &K0+K3 3	L-<&L("L-(L-Nr   r1   r,   r*   r   r     s    zB0zr,   r   c                       e Zd Zd Zy)editchatBotc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                  ||      j                         }|st        ddit        j                        S |j                  }|j                  d      }|j                  d      }	|j                  d	      }
|j                  d
      }|j                  d      }|r|st        ddit        j                        S ||_        |	r|	n|j                   |_        |
r|
|_        ||_        |r||_        |j)                          t        ddit        j*                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r1}t        dt        |      dt        j,                        cY d }~S d }~ww xY w)Nr   r   rA   User not found)rB   r)   zChatbot not foundr   r   r   r   r   zRequired fields missingzChatbot updated successfullyr   r   )r   rJ   r   r;   r   r9   r!   r"   r#   rK   rL   r   r   r   r    r   r   r   r   r   rT   r=   rU   )r'   r(   bot_idrV   rW   rX   r   r   r   r   r   r   r   s                r*   r+   zeditchatBot.post  s   (zN

 C oo$$$,2248
Y 01&:S:STT			6		9	?	?	A3
Y 34V=V=VWW
,,4((>*<HH-.>88O,=88O,=((>*<
mY 9:6C^C^__"3*8c>P>P3%C$3#C88:
-! !A  NYA'0L0LMMNH 
 z
63q6JSYSxSx
yyzs\   F AG	 AG	 )BG	 +A!G	 	G%G;G<G	 GG	 		H&G>8H>HNr-   r1   r,   r*   r   r     s    )zr,   r   c                       e Zd Zd Zy)userLeadc                 6   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  }t        j                  j                  |j                  d      |j                  d      |j                  d      |j                  d      |j                  d	      |j                  d
      |j                  d      |j                  d      |j                  d      	      }d|j                   d|j                    d|j"                   }t$        j                  j'                         }|D ]#  }	t(        j                  j                  |	d       % t        ddit        j*                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r1}t        dt        |      dt        j,                        cY d }~S d }~ww xY w)Nr   r   rA   rC   r   r   leadnoteaddresstyper   r   r   )	r   r   r   r   r   r   r   r   r   zNew lead submitted by z (z
) - Lead: zNew lead submitted)adminr   zLead created and admin notifiedr   r   )r   rJ   r   r;   r   r9   r!   r"   r#   rK   rL   r   	UserLeadsr%   r   r   r   r   r   rN   AdminNotificationr&   rU   )
r'   r(   rV   rW   rX   r   r   r   adminsr   s
             r*   r+   zuserLead.post  s   #{L

 C oo$$$,2248
Y 01&:S:STT
,,4



"
"	&	
((7
	&		&	hhy!
((6
XXh.),' # 
4 &dii[4::,jT7LL6u$$! %   I@A&JaJa
bb=  LYs1v&f.J.JKKL> 
 { 7CFKTZTyTyzz{sN   F" AG EG "	G+%GGG GG 	H'&HHHNr-   r1   r,   r*   r   r     s    ${r,   r   c                       e Zd Zd Zy)subscriptionc                 P   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                  d      }g }|D ]  }|j                  |j                  |j                  |j                  |j                   |j"                  |j$                  |j&                  |j(                  |j*                  |j,                  |j.                  d       t        d|d	t        j0                        c S  y # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r1}t        d
t        |      dt        j2                        cY d }~S d }~ww xY w)Nr   r   rA   r   T)	is_active)rB   
plane_namedescriptionpriceend_date
start_dater   
updated_atr   	durationsr   rI   r   r   r   )r   rJ   r   r;   r   r9   r!   r"   r#   rK   rL   Subscriptionr   rB   r   r   r   r   r   r   r   r   r   r   r=   rU   )r'   r(   rV   rW   rX   subscriptionssubscriptios_listsubs           r*   r   zsubscription.get  s\   !5N

 C oo$$$,2248
Y 01&:S:STT''...>=s
&&..??YY......HH  	3DEfN`N`aa   NYA'0L0LMMN8 
 5
63q6J335 55sT   D/ AE+ CE+ -E+ /	E(8%E#E(E+ #E((E+ +	F%4&F F% F%N)r.   r/   r0   r   r1   r,   r*   r   r     s    "5r,   r   ),rest_framework.viewsr   rest_framework.responser   rest_frameworkr   django.contrib.auth.hashersr   r   rest_framework_simplejwt.tokensr	   authenticationr   modelsr   user_panel.modelsadmin_panel.modelspasslib.hashr   rP   rg   django.confr   django.template.loaderr   django.core.mailr   django.corer   admin_panel.functionsadmin_panel.viewsr   r   r3   r?   r]   rr   ry   r}   r   r   r   r   r   r1   r,   r*   <module>r      s    ( , ! E 8 )     8    3 &  #  
_7 _6   <T TD U  UFPw P,T7 T:7TG 7TtV7 VSzg Szf*z' *zX&{w &{R#57 #5r,   