
    8j$                     @   d dl 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mZ d dlmZmZ d dlmZ ddlmZmZmZ  G d de      Z G d de      Z edg      d        Z  G d de      Z! G d de      Z" G d de      Z#y)    N)settingsstatus)JsonResponse)Response   )Users)generate_access_token)APIView)api_view)QAvg)verify_tokensend_otp_to_phone)get_object_or_404)ProfessionalSerializerProfessionalsSerializerUserProfileSerializerc                       e Zd Zd Zy)SendOtpc                    t        j                  |j                        }|d   }t        t	        j
                  dd            }t        |       t        ||      }|dk(  rK||j                  d<   ||j                  d<   t        dt        j                  ddt        j                  	      S t        d
t        j                  ddt        j                  	      S )Nmobilei  i'  Totpz'OTP send your mobile phone successfullymessager   Fsafer   z7OTP not send your mobile phone. Please try again later.)jsonloadsbodystrrandomrandintprintr   sessionr   r   HTTP_200_OKHTTP_400_BAD_REQUEST)selfrequestrequest_datar   r   results         ,/var/www/html/HOMYLIO_ADMIN/accounts/apis.pypostzSendOtp.post   s    zz',,/h'&..t,-c
"63/T>(.GOOH%%(GOOE"-Vagasas t  |A  JP  J\  J\  ]  ]-fqw  rM  rM  !N  UZ  ci  c~  c~        N__name__
__module____qualname__r-    r.   r,   r   r      s    r.   r   c                       e Zd Zd Zy)	VerifyOtpc                 J   	 t        j                  |j                        }|d   }||j                  d   k(  r	 t        j
                  j                  |j                  d   d      }|j                  dk(  ri|d   |_        |j                          t        |      }t        d|j                  ||j                  t        j                  ddt        j                  	      S t        d
t        j                   ddt        j                   	      S t1        dt        j                   d      S # t        j"                  $ r t	               }dj%                  d t'        d      D              }d|z   |_        d|_        d|_        d|_        d|_        |j                  d   |_        |d   |_        |j                          t        |      }t1        d|t        j                  d      cY S w xY w# t2        $ r1}t5        |       t1        dt        j                   d      cY d }~S d }~ww xY w)Nr   r      )r   	user_type	fcm_tokenzLogin successfully)r   user_idtokenr   r   Fr   zODesigner Account with this mobile no. already exists. Please use different one.r    c              3   F   K   | ]  }t        j                  d         yw)$1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZN)r"   choice).0is     r,   	<genexpr>z!VerifyOtp.post.<locals>.<genexpr>3   s     'pbc6\(]'ps   !   zuser-r   r   )r   r;   r   zInvalid OTP.z-Something went wrong. Please try again later.)r   r   r    r%   r	   objectsgetr8   r9   saver
   r   idr   r   r&   r'   DoesNotExistjoinrangeusername	is_activeis_superuseris_staffr   	Exceptionr$   )r(   r)   r*   r   useraccess_tokenrK   es           r,   r-   zVerifyOtp.post#   s=   	A::gll3Lu%Cgooe,,} ==,,gooh6O]^,_D~~*)5k)B		'<T'B+9MY]Y`Y`lx  FJ  FQ  FQ  \b  \n  \n  -o  v{  DJ  DV  DV   W  W+  :K  V\  Vq  Vq  -r  y~  GM  Gb  Gb   c  c  ^fFaFa bcc )) } 7D!ww'pglmngo'pqH$+h$6DM%&DN()D%%&DN$%DM")//(";DK%1+%>DNIIK#8#>L#1EQ]hnhzhz${||}  	A!H)Xcic~c~  A  A	AsH   6G( B%D( ,D( G( (B:G%"G( $G%%G( (	H"1&HH"H"Nr/   r3   r.   r,   r5   r5   "   s    Ar.   r5   rE   c           
         t        |       }|d k(  rt        dt        j                  d      S 	 t        j
                  j                  |      }t        d|j                  |j                  t        |j                        |j                  |j                  t        j                  d      S # t        $ r1}t        |       t        dt        j                  d      cY d }~S d }~ww xY w)NUser not logged inr   rG   zUser logged in)r   namerG   profile_picemailr   r   )r   r   r   HTTP_401_UNAUTHORIZEDr	   rD   rE   rV   rG   r!   rW   rX   r   r&   rO   r$   )r)   rP   rR   s      r,   
verifyUserrZ   F   s     Dt|%9FD`D`abb	f==$$$$/D)9DIIVZV]V]orsw  tD  tD  pE  QU  Q[  Q[  hl  hs  hs  ~D  ~P  ~P  Q  R  R 	f8(<vGcGcdee	fs   A;B* *	C$3&CC$C$c                       e Zd Zd Zd Zy)UserProfilec                 Z   t        |      }|s,t        dt        j                  dt        j                        S 	 t        j
                  j                  |      }t        |d|i      }t        |j                  t        j                  dt        j                        S # t        j                  $ r/ t        dt        j                  dt        j                        cY S t        $ rA}t        |       t        d	t        j                  dt        j                        cY d }~S d }~ww xY w)
NrT   r   r   rU   r)   )context)profiler   User not foundSomething went wrong)r   r   r   rY   r	   rD   rE   r   datar&   rH   HTTP_404_NOT_FOUNDrO   r$   HTTP_500_INTERNAL_SERVER_ERRORr(   r)   r:   rP   
serializerrR   s         r,   rE   zUserProfile.getX   s   w'3$:: 33 "	==$$$0D."G,J
 )$00 ))  !! 	/$77 00   		!H5$CC << 		s$   A$B   ?D*!D*)6D%D*%D*c                 @   t        |      }t        |j                         |s,t        dt        j
                  dt        j
                        S 	 t        j                  j                  |      }|j                  j                  d|j                        |_	        |j                  j                  d|j                        |_
        |j                  j                  d|j                        |_        |j                  j                  d|j                        |_        |j                  j                  d	      r |j                  j                  d	      |_        |j                          t        d
t        j                   dt        j                         S # t        j"                  $ r/ t        dt        j$                  dt        j$                        cY S t&        $ rA}t        |       t        dt        j(                  dt        j(                        cY d }~S d }~ww xY w)NrT   r   r   rU   rV   rX   r   addressrW   zProfile updated successfullyr`   ra   )r   r$   rb   r   r   rY   r	   rD   rE   rV   rX   r   rh   FILESrW   rF   r&   rH   rc   rO   rd   )r(   r)   r:   rP   rR   s        r,   r-   zUserProfile.post   s   w'gll3$:: 33 '	==$$$0D((;DI ))'4::>DJ!,,**8T[[ADK"<<++It||DDL}}  /#*==#4#4]#C IIK=$00 ))  !! 	/$77 00   		!H5$CC << 		s%   EF ?HH6HHHN)r0   r1   r2   rE   r-   r3   r.   r,   r\   r\   V   s    /d4r.   r\   c                       e Zd Zd Zy)AccountsProfessionalsViewc                 L   	 |j                   j                  dd      }|j                   j                  dd       }t        j                  j	                  dd      j                  d      j                  d      j                  t        d	t        d
                  j                         }|r|j	                  |      }|r|j	                  |      }t        |d      }t        d|j                  d      S # t        $ r!}t        dt        |      d      cY d }~S d }~ww xY w)Nsearchr<   sub_category_idFr   )contractor_profile__isnullcontractor_profile__is_verifiedcontractor_profile"contractor_profile__sub_categories$work_sub_category_contractor__ratingr   )(work_sub_category_contractor__rating__gt)filter)
avg_rating)&contractor_profile__sub_categories__id)name__icontainsT)many   )r   professionals  r   r   )GETrE   r	   rD   ru   select_relatedprefetch_relatedannotater   r   distinctr   r   rb   rO   r!   )r(   r)   rm   rn   querysetrf   rR   s          r,   rE   zAccountsProfessionalsView.get   s(   .	[[__Xr2F%kkoo.?FO}}+++023 ,  n$4h:aH  
 hj " #??;J +  #??$* +  1EJ!+  
  	q6  	s   C6C9 9	D#DD#D#Nr0   r1   r2   rE   r3   r.   r,   rk   rk      s    0r.   rk   c                       e Zd Zd Zy)AccountsProfessionalViewc                 N   	 |j                   j                  d      }|st        ddd      S t        t        j
                  j                  d      |      }t        |      }t        d|j                  d      S # t        $ r!}t        d	t        |      d      cY d }~S d }~ww xY w)
Nr:   i  zuser_id is requiredr}   rq   rU   rz   )r   r_   r|   )r~   rE   r   r   r	   rD   r   r   rb   rO   r!   re   s         r,   rE   zAccountsProfessionalView.get   s    	kkooi0G!4!  
 %,,-ABD
 05J%??  
  	q6  	s#   *A: AA: :	B$BB$B$Nr   r3   r.   r,   r   r      s    r.   r   )$r   r"   django.confr   rest_frameworkr   django.httpr   rest_framework.responser   modelsr	   authr
   rest_framework.viewsr   rest_framework.decoratorsr   django.db.modelsr   r   utils.helpersr   r   django.shortcutsr   serializersr   r   r   r   r5   rZ   r\   rk   r   r3   r.   r,   <module>r      s        ! $ ,  ' ( . # 9 . _ _g " A  AH 
5'
f 
fh' hX2 2lw r.   