o
    xb:                     @   sb  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	m
Z
 d dlZd dlZd dlmZ d dlZd dlZd dlZd dlZd dlZd dlmZ d d	lmZ d d
l mZmZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm Z  d dlm!Z! dej"d< i a#dZ$dd Z%dd Z&dd Z'dd Z(da)i a*dd Z+da,da-dd Z.dS )    )renderredirect)datetime)sync_to_async)messages)UserN)JsonResponse)HttpResponseRedirect)HttpResponse)get_object_or_404r   )Payment)RegistrationtrueDJANGO_ALLOW_ASYNC_UNSAFEzhello-helloc                 C   s   t dtiS )Nstatus_code)r   r   request r   3C:\Users\DAVID\Desktop\project\apps\helios\views.py	getthread   s   r   c                 C   s   t | ddtiS )Nzdologin.htmlusername)r   r   r   r   r   r   dologin"   s   r   c                     sr   zt j } | D ]}|j}|j}||| f  W S W d S  ty)   td Y d S  tjj	y8   td Y d S w )Nzsomething happen.urlopen error [Errno 11001] getaddrinfo failed)
r   objectsallnotesamount	ExceptionprinturlliberrorURLError)datacustomer_datamsisdntransamountr   r   r   Getdata$   s   
r&   c              	      s.  zwt  I d H \}}}d}t }d}ttt| }t || k rwtdI d H  |dkr8td W d S tj	 }t
j	 }|D ]&}	|	j}
|	j}| }|
|krk|dkrkd}t }t || k rktdI d H  t dkrt|tt }|dk rd}tjj|	jd	j|d
 ntjj|	jd	j|d
 tj d fddtdD }tjdd|ddddd}d}tj }d}tj|d}|| }|d}|rtjdd|dd|dd}|rtjd||ddd}|jdkr|ja|tf  W S n`t dkrQt|tt }|dk r&d}tjj|	jd	j|d
 n=tjj|	jd	j|d
 tj d fddtdD }tjd |d!d"d#d$ d%}nt d&krt|tt }|dk rsd}tjj|	jd	j|d
 ntjj|	jd	j|d
 tj d fd'dtdD }d%}nt d(krt|tt }|dk rd}tjj|	jd	j|d
 ntjj|	jd	j|d
 tj d fd)dtdD }d%}nt d*krt|tt }|dk rd}tjj|	jd	j|d
 nltjj|	jd	j|d
 tj d fd+dtdD }d%}nLt d,krYt|tt }|dk r9d}tjj|	jd	j|d
 n*tjj|	jd	j|d
 tj d fd-dtdD }d%}n
| d.k rctd/ t || k siqDt || k s%W d S W d S  ty   td0 Y d S  ty   td1 Y d S w )2Ni@ 254000000000g      ?l   o; doner      5)pk)r    c                 3       | ]}t  V  qd S Nrandomchoice.0iZlettersr   r   	<genexpr>R       zloop.<locals>.<genexpr>   z"http://127.0.0.1:8000/user-create/3zCleartext-Passwordz:=Z
12345678wp)idr   	attributeopvalue)jsonZ2hrs)hoursz%d %B %Y %H:%M:%S
Expirationz#http://127.0.0.1:8000/Radusergroup/1)r   	groupnamepriority   10c                 3   r-   r.   r/   r2   r5   r   r   r6   {   r7   ZcomcastZ	1234678wpDATAz00:03:00)servernamepasswordZprofileZlimit_uptimez-Dear customer your successfuly purchased 1hr Z20c                 3   r-   r.   r/   r2   r5   r   r   r6      r7   Z30c                 3   r-   r.   r/   r2   r5   r   r   r6      r7   Z250c                 3   r-   r.   r/   r2   r5   r   r   r6      r7   900c                 3   r-   r.   r/   r2   r5   r   r   r6      r7   0z
no balance*cannot unpack non-iterable NoneType objectz(NameError: name 'MyModel' is not defined) r&   timeintphoneasynciosleepr   r   r   r   r   r   r   plansfilterr:   updatestringdigitsjoinrangerequestspostr   now	timedeltastrftimer   Zhotspotadd	TypeError
ValueError)r   r$   r%   r"   timeoutZtimeout_startnumberansr:   r#   Z	msisdn_dbgZbalanceZnew_balancer   responsegroupsZcurrent_date_and_timer?   Zhours_addedZfuture_date_and_timer@   notifyr   r5   r   loop0   s   












wrh   r,   c                    sh   zt  I d H \}}}d}trttt| a||tttdaW n ty-   td Y nw t	| dtS )Nr'   )r%   r$   rO   rR   r   rL   zprocessing.html)
r&   rO   rN   rc   rR   r   contextr_   r   r   )r   r$   r%   r"   rb   r   r   r   
processing   s    
rj   c                    s   j dkrFjdajdadd dfdd} fd	d
fddt dkr:tt	  t
 |  tdS tdS )NPOSTrR   rO   zKhttps://api.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentialsZ up863hWneHvgTzQHTAKTRUPFmJOmx9GBZ3HYLLjkSnTRBn2N5c                     s   t  I d H } t   I d H }t t I d H }tjt j fd}tjt j  fd}tjt jt fd}tjt jtfd}|  |  |  |  | rg|	  |	  |	  d S d S )N)targetargs)
rP   create_taskr&   	threadingThreadrunrh   startis_aliverW   )Ztask1Ztask2Ztask3t1t2t3Zt5)express	get_tokenr   r   r   main   s"   zhelios.<locals>.mainc               	      s   z?t  4 I d H )} | j t  dI d H }| I d H }|d atW  d   I d H  W S 1 I d H s9w   Y  W d S  tyN   td Y d S w )N)authaccess_tokenzopps! something happen)	aiohttpClientSessiongetformatZ	BasicAuthr>   r{   r   r   )sessionsZurl_responsetoken)app_key
app_secretlive_urlr   r   rx      s    4zhelios.<locals>.get_tokenc                     s     I d H } d}d}t }d}d}d}d}ttt| }|}	d}
ttj dd	 d
ddddd}dt|t||}t	
t|dd}||d|d|t|	dt|	|||
d}t 4 I d H }zqd| dd}d|d}|j|||dI d H }ttj dd	 d
ddddd}dt|t||}t	
t|dd}||d|dd}d| dd}d|d}|j|||dI d H }| I d H }W n tjjy   td Y nw W d   I d H  d S W d   I d H  d S 1 I d H s	w   Y  d S )NZ7302281Z@5359d4ee9b66f9acb2df4c31370d675de045d53c457f498b87af930df478d98cz;https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequestz*https://comcast.co.ke/pay/callback_url.phpZ
ComcastLtdr'   zPublic hotspot service.r   -r,    :z	{0}{1}{2}utf8)encodingzutf-8ZCustomerBuyGoodsOnlineZ5299403)BusinessShortCodePassword	TimestampZTransactionTypeZAmountZPartyAZPartyBZPhoneNumberZCallBackURLZAccountReferenceZTransactionDescz
Bearer {0}zapplication/json)AuthorizationzContent-Typez{0}{1}z /mpesa/stkpush/v1/processrequest)headersr>   )r   r   r   ZCheckoutRequestIDz/mpesa/stkpushquery/v1/queryr   )rR   rN   rO   strr   r[   splitreplacer   base64	b64encodebytesdecoder|   r}   rZ   r>   r   r    r!   r   )r{   Zbusiness_shortcodeZpasscoder   r   Zcallback_urlZreference_coderb   rc   phone_numberdescriptionrM   rI   encodedpayloadr   r   urlresponse_urlZsaf_urlZdata_responsere   )rx   r   r   rw      sf   000zhelios.<locals>.expressWindowsrj   z
mpesa.html)methodrk   r~   rR   rO   platformsystemrP   set_event_loop_policyWindowsSelectorEventLoopPolicynew_event_looprun_until_completer   r   )r   ry   r   )r   r   rw   rx   r   r   r   helios   s   
5
r   )/django.shortcutsr   r   r\   r   asgiref.syncr   django.contribr   django.contrib.auth.modelsr   rY   rM   django.httpr   r>   routeros_apisysr0   rU   r	   r
   r   rP   r|   osr   urllib.requestr   r   ro   Zcustomer.modelsr   r   environr   r   r   r   r&   rh   rc   ri   rj   rO   rR   r   r   r   r   r   <module>   sP    
 