o
    ] c                     @   s   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	 d dl
mZ d dlmZmZ eeZdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Ze	G dd dZdS )    N)	timedelta)timezone)	dataclass)ProcessedMedia)Subscription	Extentionc              
   C   sb   z| j j W S  tjy0 } ztd|  dd tj	 d d D W  Y d }~S d }~ww )Nz,Function valid_extensions() With Exeption : c                 S   s   g | ]}|j qS  )value).0vr   r   =/var/www/html/docker_usevoice/src/apps/subscriptions/utils.py
<listcomp>   s    z$valid_extensions.<locals>.<listcomp>   )
subscriptionplanget_valid_extentionsr   DoesNotExistloggerwarningr   objectsalluserer   r   r   valid_extensions   s   (r   c                 C   s   | t |v rdS dS )NTF)r   )file_extentionrequestr   r   r   extention_file_valid   s   r   c                 C   sZ   | j d }tj|\}}|ddt| jv r| jjrdS |ddt| jv r+dS dS )Nfilename. TF)POSTospathsplitextreplacer   r   is_staff)r   r   _file_extensionr   r   r   file_extension_valid   s
   
"r)   c              
   C   sD   z| j jjW S  ty! } ztd|  W Y d}~dS d}~ww )zG Return the file_size field of the Plan model associated with the user z4Function retrieve_elixir_of_size() With Exception : N)r   r   	file_size	Exceptionr   r   r   r   r   r   retrieve_elixir_of_size%      r,   c              
   C   sD   z| j jjW S  ty! } ztd|  W Y d }~d S d }~ww )Nz4Function retrieve_elixir_of_time() With Exception : )r   r   time_per_videor+   r   r   r   r   r   r   retrieve_elixir_of_time.   r-   r/   c                 C   s:   t |}|d kr| |ks|jrdd| ddS dddS )NFz3The file is too large for your plan [ file size <=  ]statusmessageTThe file is within the limit.)r,   r&   )r*   r   	file_planr   r   r   retrieve_limit_of_size7   s   
r6   c              
   C   s   t |}zt|j|j|jd }W n ty. } ztd|  d }W Y d }~nd }~ww | d krG|d krG| |ks>|j	rGdd| ddS dddS )	N)hoursminutessecondsz9Inside Function retrieve_limit_of_duration() Exception : Fz7The file is too large for your plan [ file duration <= r0   r1   Tr4   )
r/   r   hourminutesecondtotal_secondsr+   r   r   r&   )file_durationr   timedurationr   r   r   r   retrieve_limit_of_durationB   s   
rA   c              
   C   s   t  }| }t| |}z| jjj}W n ty1 } zt	
d|  d}W Y d}~nd}~ww |dkr@| jr@ddd|dS ||krSdd||  d	|| |dS dd||  d	|| |dS )
z= Function that check if user reached his plan's videos limit z7Inside Function has_reached_upload_limit() Exception : NTzRest MEDIA are unlimited mediaInfinity)r2   r3   Rest_MediasUploaded_dayFzRest MEDIA are z media)r   nowdater   get_count_for_userr   r   videos_per_dayr+   r   r   r&   )r   rE   current_datecount_medialimit_mediar   r   r   r   has_reached_upload_limitS   s   rL   c                   @   s:   e Zd ZU dZeed< eed< dd Zdd Zdd	 Zd
S )CancelSubscriptionz0Class for keeping track of an item in inventory.	CLIENT_IDCLIENT_SECRETc                 C   s,   d}d|i}d}t j||| j| jfd}|S )z# method that generate paypal token client_credentials
grant_typez0https://api-m.sandbox.paypal.com/v1/oauth2/token)dataauth)requestspostrN   rO   )selfrQ   body_paramsurlresponser   r   r   	gen_tokenq   s
   zCancelSubscription.gen_tokenc                 C   sP   |   jdkr&t|   j}|d }dd|i}tjd| |d}|S dS )N   access_tokenAuthorization	Bearer {}:https://api-m.sandbox.paypal.com/v1/billing/subscriptions/rX   headerszaccess denied)rZ   status_codejsonloadstextformatrT   getrV   sub_idrY   tokenra   r   r   r   get_subscription_detailsy   s   z+CancelSubscription.get_subscription_detailsc                 C   sT   |   jdkr(t|   j}|d }d|dd}tjd| d|d}|S d	S )
Nr[   r\   r^   zapplication/json)r]   zContent-typer_   z/cancel?=&=r`   z Error Token expired or not exist)rZ   rb   rc   rd   re   rf   rT   rU   rh   r   r   r   cancel_subscription   s   z&CancelSubscription.cancel_subscriptionN)	__name__
__module____qualname____doc__str__annotations__rZ   rk   rl   r   r   r   r   rM   k   s   
 rM   )r"   loggingrc   rT   datetimer   django.utilsr   dataclassesr   apps.process.modelsr   apps.subscriptions.modelsr   r   	getLogger__file__r   r   r   r)   r,   r/   r6   rA   rL   rM   r   r   r   r   <module>   s(    
		