
    Li<                        d dl mZmZmZmZmZmZmZ d dlm	Z	m
Z
mZmZmZ d dlmZmZmZ d dlmZmZmZ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 m!Z!m"Z"m#Z# d d
l$Z%d d
l&Z&d Z'd Z(d Z)d Z*d Z+d Z,d Z-d Z.d Z/d
S )    )whatsapp_send_initiatedemail_send_initiatedwhatsapp_send_proofemail_send_proofsms_send_initiatedsms_send_proofget_unique_tracking_id)PersonalDetailsSelectedTileTransaction
ScreenshotContactMessage)renderredirectget_object_or_404)ProjectInstitutionNotificationPreferenceProjectUpdatestransaction)r   IntegrityError)messages)reverse)FCountExpressionWrapper
FloatFieldNc           	         t          t          |d          }t          j                            |t          d          d                              t          dd          t          t          d          dz  t          d          z  t                      	          
          
                    d          d d         }t          | d||d          S )NTidtable_statusfunding_goal
created_bycurrent_amount__ltr"   r   distinctcurrent_amount      Y@output_fieldcontributor_countprogress-started_at   z
index.htmlinsprjr   r   r   objectsfilterr   annotater   r   r   order_byr   requestins_idr3   projectss       =C:\Users\anoop\OneDrive\Desktop\AspireAid\userModule\views.py	userIndexr?      s    
KF
F
F
FC%%.HYHYgk% ( ((05aeCfCfCf:KQO_M`M`chMhklm{k|k|M|YcYeYe;g ;g ;g )1 )h )hhphpq~hh  AC  BC  ACDH '<s((C(CDDD    c           	         	 t          t          |d          }n4# t          $ r' t          j        | d           t          d          cY S w xY w| j                            dd| d          }| j        dk    r	 t          j
                    5  | j                            d          }| j                            d	          }| j                            d
          }| j                            d          }| j                            d          }t          |||||g          s0t          j        | d           t          |          cd d d            S |                                rt          |          dk    r0t          j        | d           t          |          cd d d            S t          j                            ||||||           t          j        | d           d d d            n# 1 swxY w Y   nO# t&          $ r t          j        | d           Y n.t          $ r"}	t          j        | d|	            Y d }	~	nd }	~	ww xY wt          |          S )NTr    zInstitution not found./HTTP_REFERER/user/POST
first_name	last_nameemailphonemessagez#All form fields must be filled out.
   z+Please enter a valid 10-digit phone number.)rF   rG   rH   rI   rJ   r3   zFYour message has been sent successfully! We will get back to you soon.zFA database error occurred while saving your message. Please try again.zAn unexpected error occurred: )r   r   	Exceptionr   errorr   METAgetmethodr   atomicrE   allisdigitlenr   r6   createsuccessr   )
r;   r<   r3   redirect_urlrF   rG   rH   rI   rJ   es
             r>   
contact_usrY      s"   TJJJ   w 8999}} <##N4FV4F4F4FGGL~	J#%% t t$\--l;;
#L,,[99	((11((11!,**955J	5%IJJ 2N7,QRRR#L11t t t t t t t t }} 2#e***:*:N7,YZZZ#L11t t t t t t t t &--)'# .     *rsss1t t t t t t t t t t t t t t t2  	n 	n 	nN7$lmmmmm 	J 	J 	JN7$HQ$H$HIIIIIIII	JL!!!sk    .A
A:G= B:G1G= A
G1G= +:G1%G= 1G55G= 8G59G= =I		I	'II	c                 V    t          t          |d          }t          | dd|i          S )NTr    z
about.htmlr3   r   r   r   r;   r<   r3   s      r>   aboutr]   C   s,    
KF
F
F
FC',s444r@   c                 V    t          t          |d          }t          | dd|i          S )NTr    zuser-credit.htmlr3   r[   r\   s      r>   creditr_   H   s-    
KF
F
F
FC',uSk:::r@   c           	         t          t          |d          }t          j                            |t          d          d                              t          dd          t          t          d          dz  t          d          z  t                      	          
          
                    d          }t          | d||d          S )NTr    r#   r$   r   r'   r)   r*   r+   r-   r0   zuser-projects.htmlr2   r5   r:   s       r>   userAllProjectra   M   s    
KF
F
F
FC%%>IZIZim% ( ((05aeCfCfCf:K678H6I6IE6QTUVdTeTe6eBL,,;P ;P ;P )1 )Q )Q RZQYZgQhQh	 
 '/s(0K0KLLLr@   c                    t          t          |d          }t          t          |          }|j        dk    r t	          |j        |j        z  dz  d          nd|_        t          j        	                    |d          
                                |_        t          j        	                    |	                              d
          }| j        dk    r| j                            d          }|s!t#          t%          d||d                    S t%          dd|j        i          }t(          j                            |j        |d          }t#          | d|           S d}	|j        |j        k    s|j        rd}	|j        sd}	|||	|d}
|	dv rt3          | d|
          S t5          |j        |j        z            }t9          t          j        	                    |d                              dd                    }t9          d |D                       }t9          t          j        	                    |d                              dd                    }t9          d |D                       }t=          |          t=          |          z   }||z
  }t?          d|dz             }|
                     |||||d            t3          | d|
          S )!NTr    r!   r   d      Verified)projectstatus)rg   z-update_daterE   selected_tiles_inputuser_single_project)r<   prj_id)kwargsuser_checkoutr<   )
project_idselected_tiles?Active	CompletedClosed)r3   rg   project_statusupdates)rr   Expiredrs   zuser-single-project.htmltiles_bought__tiles)flatc                     g | ]=}|                     d           D ]%}|                                t          |          &>S -splitrS   int.0sts      r>   
<listcomp>z%userSingleProject.<locals>.<listcomp>~   sG    bbbaggcllbbVWV_V_VaVabc!ffbbbbr@   
Unverifiedc                     g | ]=}|                     d           D ]%}|                                t          |          &>S rz   r|   r   s      r>   r   z%userSingleProject.<locals>.<listcomp>   sM    #n#n#nqRSRYRYZ]R^R^#n#nQbcbkbkbmbm#nCFF#n#n#n#nr@      )total_tilesavailable_tiles_countt_rangesold_tiles_setprocessing_tiles_set)!r   r   r   r#   roundr)   r/   r   r6   r7   countcontributors_countr   r9   rP   rE   rO   r   r   r!   urllibparse	urlencode	closed_byr"   r   r~   
tile_valuesetvalues_listrT   rangeupdate)r;   rk   r<   r3   r4   project_updatesro   checkout_urlquery_stringrt   common_contextr   r   
sold_tilesr   processing_tilesunavailable_tiles_countr   
tile_ranges                      r>   userSingleProjectr   W   s:   
KF
F
F
FC
G
/
/
/CNQN^abNbNb5#,s/??3FJJJhiCL(077J7WW]]__C$,33C3@@II.YYO~ ))*@AA 	iG$9V_eBfBfggghhh#&7IJJJ|--SVWe.f.fgg<88,88999 "s111S]1(N 	&%N ,&	
 
 ???'#=~NNN c.#.@AAK #**3z*JJVVWl\` W b bc cN bbnbbbccJ#&#**3|*LLXXYn^b Y d d$e $e   ##n#n4H#n#n#noo&)*oo<L8M8M&M#$/2I$I!q+/22J !!+Wl  yC  V`  yI  #K  #K  L  L  L'#=~NNNr@   c                 8	   t          t          |          }| j        dk    r| j                            d          }| j                            d          }t          t
          |          }t          |                    d                    }t          | d||||d          S | j        dk    r| j	                            d          }| j	                            d          }| j	                            d	          }| j	                            d
          }	| j	                            d          }
| j	                            d          }t          t
          |          }t          |                    d                    |j
        z  }|j        |j        k    s|j        r+t          j        | d           t!          d| d| d          S |j        s+t          j        | d           t!          d| d| d          S t$          j                            ||d                                          r+t          j        | d           t!          d| d| d          S 	 t-          j                    5  t0          j                            |	||
|          }t$          j                            |||          }t4          j                            ||||ddt7                                }d d d            n# 1 swxY w Y   | d|j         }d}g }t:          j                            |          }|j        r<t?          ||          }|d         dk    r d}|                     d|d                      |j!        r<tE          ||          }|d         dk    r d}|                     d!|d                      |j#        rH|	rFtI          || %                    d| d                    \  }}|sd}|                     d"|            |rt          j&        | d#| d$           n1d%}d&'                    |          }t          j(        | | d'|            n/# tR          $ r"}t          j        | d(|            Y d }~nd }~ww xY wt!          d| d| d          S d S ))Nrc   GETrn   ro   r{   zuser-checkout.html)r3   rg   ro   r   rE   fnamerH   phnaddrz This project has been completed.rD   /single-project/rB   z3This project is no longer active for contributions.T)rg   tilesr"   z'These tiles have already been selected.)rH   	full_namerI   address)rg   senderr   INRr   )tiles_boughtr   rg   amountcurrencyrh   tracking_id/proof/institutionrh   rM   FSMS sending failed: rJ   !WhatsApp message failed to send: Email sending failed: zPayment for the tiles zR has been initiated and a confirmation has been sent via SMS, WhatsApp, and Email.zoYour payment has been successfully initiated, but we encountered issues with some of the notification services. 
 Details: zFailed to perform checkout: )*r   r   rP   r   rO   r   rT   r}   r   rE   r   r#   r)   r   r   rM   r   r"   r   r6   r7   existsdb_transactionrQ   r
   rU   r   r	   r!   r   sms_enabledr   appendwhatsapp_enabledr   email_enabledr   build_absolute_urirV   joinwarningrL   )r;   r<   r   rn   ro   rg   selected_tile_countselected_tiles_strr   rH   r   r   total_amountr   selected_tile_instancer   proof_upload_urlall_notifications_sentnotification_errors	noti_pref
sms_resultwhatsapp_resultemail_successemail_messagebase_messagedetailed_errorsrX   s                              r>   userCheckoutViewr      s   #KF;;;K~[__\22
 )9::#G
;;;!."6"6s";";<<g3[T[HV`s6u 6u v v 	v 
6	!	!\%%l33
$\--.>??  ))  ))lu%%|''#G
;;;-33C8899G<NN7#999W=N9N7$FGGGJVJJZJJJKKK# 	LN7$YZZZJVJJZJJJKKK&&w>P_c&ddkkmm 	LN7$MNNNJVJJZJJJKKK%	H&(( X X(077eeZ]fj7kk)5)=)D)DW\bi{)D)|)|&)188F\djsz  CO  Y^  fr  U  W  W8  X  XX X X X X X X X X X X X X X X
 #)AAAA%)""$.6::{:SSI$ _/=MNN
h'722-2*'../]jQZF[/]/]^^^) q"9+GW"X"X"8,77-2*'../oSbclSm/o/oppp& Y5 Y/CKQXQkQk  mI  vF  mI  mI  mI  RJ  RJ  0K  0K,}$ Y-2*'../W/W/WXXX% X   *iBT  *i  *i  *i  j  j  j  j  Q"%((+>"?"? \*V*V_*V*VWWW 	H 	H 	HN7$F1$F$FGGGGGGGG	HFFFFFFGGGw 
"	!s>    Q 3A8K7+Q 7K;;Q >K;?EQ 
RQ<<Rc                 j   t          t          |          }t          t          |          }|j        r|j        j        nd}|r"t          |                    d                    nd}| j        dk    r| j        	                    d          }|s0t          j        | d           t          d| d	|j         d
          S 	 t          j                    5  	 t           j        	                    |          }|j        j        }	||_        |                                 t*          j                            |	          rt+          j        |	           n6# t           j        $ r$ t           j                            ||          }
Y nw xY wd d d            n# 1 swxY w Y   d}g }t4          j        	                    |          }|j        r;t9          |          }|d         dk    r d}|                    d|d                     |j        r;t?          |          }|d         dk    r d}|                    d|d                     |j         r:|j!        j"        r.tG          |          \  }}|sd}|                    d|            |rt          j$        | d           n1d}d%                    |          }t          j&        | | d|            n/# tN          $ r"}t          j        | d|            Y d }~nd }~ww xY wt          d| d|j(        j         d
          S tS          | d|||d          S )Nrc    r{   r   rE   proof_of_paymentzPlease select a file to upload.rD   r   rB   r   )r   screen_shotTr   rh   rM   Fr   rJ   r   r   zeProof of payment uploaded successfully and a confirmation has been sent via email, SMS, and WhatsApp.ziProof of payment uploaded successfully, but we encountered issues with some of the notification services.r   r   z'An error occurred during proof upload: r   zuser-proof-upload.html)r3   trar   )*r   r   r   r   r   rT   r}   rP   FILESrO   r   rM   r   r!   r   rQ   r   r6   r   pathsaveosr   removeDoesNotExistrU   r   r   r   r   r   r   r   r   rH   r   rV   r   r   rL   rg   r   )r;   r<   trans_idr3   r   tiles_stringr   proofold_screenshotold_file_pathnew_screenshotr   r   r   r   r   r   r   r   r   rX   s                        r>   userProofUploadr      s   
KF
3
3
3C
KH
5
5
5C-0-=E3#))2L,8?C""3''(((aE~!!"455 	?N7$EFFF=V==CF===>>>,	S&(( c c
c%/%7%;%;%;%L%LN$2$>$CM16N."'')))w~~m44 1	-000!. c c c%/%7%>%>3\a%>%b%bNNNcc c c c c c c c c c c c c c c &*""$.6::s:KKI$ _+C00
h'722-2*'../]jQZF[/]/]^^^) q"5c":":"8,77-2*'../oSbclSm/o/oppp& Y3:+; Y/?/D/D,}$ Y-2*'../W/W/WXXX% X   *Q  R  R  R  R  K"%((+>"?"? \*V*V_*V*VWWW 	S 	S 	SN7$Qa$Q$QRRRRRRRR	SJJJJJJKKK'3SUZ5[5[\\\sb   K FA:EF0FFFFK FK FD5K 
K=K88K=c                    t           j                            |          }t          j                                        }d}| j        dk    r,| j                            dd                                          }|}|rd|v r5t          j                            ||          	                    d          }n}|
                                r5t          j                            ||          	                    d          }n4t          j                            ||	          	                    d          }|D ]B}|j        r2t          |j        j                            d
                    |_        ;d|_        Ct!          | d|||d          S )Nrc   r   rE   track@)sender__emailproject__created_byz-transaction_time)sender__phoner   )r   r   r{   r   zuser-track-status.html)r3   r   search)r   r6   rO   r   nonerP   rE   stripr7   r9   rS   r   rT   r   r}   	num_tilesr   )r;   r<   r3   transactionssearch_contenttrack_queryr   s          r>   userTrackStatusr     s   


!
!V
!
,
,C&++--LN~l&&w3399;;$ 	$k!!*299jm9nnww  yL   M   M$$&& K*299jm9nnww  yL   M   M*299khk9lluu  wJ   K   K! $ $> $"%an&:&@&@&E&E"F"FAKK"#AKK'3Sao5p5pqqqr@   )0adminModule.utilsr   r   r   r   r   r   r	   userModule.modelsr
   r   r   r   r   django.shortcutsr   r   r   adminModule.modelsr   r   r   r   	django.dbr   r   r   django.contribr   django.urlsr   django.db.modelsr   r   r   r   urllib.parser   r   r?   rY   r]   r_   ra   r   r   r   r    r@   r>   <module>r      s   o  o  o  o  o  o  o  o  o  o  o  o  o  o  o  o  o  o d d d d d d d d d d d d d d @ @ @ @ @ @ @ @ @ @ [ [ [ [ [ [ [ [ [ [ [ [ 3 3 3 3 3 3 1 1 1 1 1 1 1 1 # # # # # #       D D D D D D D D D D D D     				E E E(" (" ("V5 5 5
; ; ;
M M M4O 4O 4OnFH FH FHR<] <] <]~r r r r rr@   