U
    scq/                     @   s  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 d dlmZ d dlmZmZmZmZ d d	lmZ d d
lmZ G dd dejZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZ dS )    N)settings)models)Q)timezone)now)conf)*find_best_max_institutes_per_user_in_plansfind_best_min_wage_fee_in_plansfind_best_tax_factor_in_plansfind_best_wage_factor_in_plans)
logs_adder)public_storagec                   @   s  e Zd ZdZejdddejddZej	ddddZ
ej	ddddZej	ddddddZej	ddddddZej	d	dddddZej	d	dddddZejd
ddedZejdddedZejdddedZejdddedZejdddZej	ddddZej	ddddddZej	ddddddZejdddZejdddZdd Zedd Z edd Z!dZ"dZ#edd Z$edd Z%edd  Z&ed!d" Z'ed#d$ Z(ed%d& Z)dS )'Userz
    This class is responsible for creating User table in database
    all attributes and methods of User should defines in this class
    TF)primary_keyuniquedb_indexdefaulteditable.   
max_lengthnullblank   )r   r   r   r   r      zusers/profile/images)	upload_tor   r   storagezusers/profile/image_thumbnailszusers/posterszusers/posters_thumbnailsr   r      x   auto_now_addr   auto_nowr   c                 C   s   | j  d| jr| jn| j S Nz - )	full_namephoneemailself r*   //var/www/wikiazma_server/authenticate/models.py__str__7   s    zUser.__str__c                 C   s>   | j r| jr| j  d| j S | j r*| j S | jr6| jS dS d S )N -)
first_name	last_namer(   r*   r*   r+   r%   :   s    zUser.full_namec                 C   s   ddl m} || jS )Nr   )UserBalanceHistory)Zpayment.modelsr1   Zget_last_user_balanceZremaining_balance)r)   r1   r*   r*   r+   balanceE   s    zUser.balanceNc                 C   s,   | j s&ddlm} |jjdd\| _ }| j S )Nr   )EnvironmentT)
active_env)_active_environmentplan.modelsr3   objectsget_or_create)r)   r3   _r*   r*   r+   active_environmentM   s    zUser.active_environmentc                 C   sD   | j s>ddlm} |jjtt dtddB | t d| _ | j S )Nr   )UserPlan)Zend_date__gteT)Zend_date__isnull)userZstart_date__lte)_plansr6   r;   r7   filterr   r   r   )r)   r;   r*   r*   r+   plansT   s    z
User.plansc                 C   s   t | j| jS N)r   r?   r:   r(   r*   r*   r+   best_max_institutes_per_user\   s    z!User.best_max_institutes_per_userc                 C   s   t | j| jS r@   )r	   r?   r:   r(   r*   r*   r+   best_min_wage_fee`   s    zUser.best_min_wage_feec                 C   s   t | j| jS r@   )r   r?   r:   r(   r*   r*   r+   best_wage_factord   s    zUser.best_wage_factorc                 C   s   t | j| jS r@   )r
   r?   r:   r(   r*   r*   r+   best_tax_factorh   s    zUser.best_tax_factor)*__name__
__module____qualname____doc__r   	UUIDFielduuiduuid4id	CharFieldr/   r0   usernamer'   r&   recovery_phone
ImageFieldr   imageZimage_thumbnailZposterZposter_thumbnail	JSONFieldinfopasswordZ	g_subjectZfb_idDateTimeFieldZ	join_datemodified_atr,   propertyr%   r2   r=   r5   r:   r?   rA   rB   rC   rD   r*   r*   r*   r+   r      s                       







r   c                   @   s   e Zd ZejddddZejdddddZejdddddZ	ejdddddZ
ejdddddZejdddddZejdd	Zejedd
Zejedd
Zedd Zdd ZdS )VerificationAttemptTr   r   r   -   r   r   r   r   r   r   F   )r   r   r   c                  C   sB   t jjt tjtjd d} | 	 }| 
  td|  d S )NhoursZcreated_at__ltz/Event: SCHEDULE, cleanup, VerificationAttempt, )rX   r7   r>   r   r   datetime	timedeltaauthenticate_confZtemp_user_cleanup_offset_hourscountdeleter   )Zverification_attemptsZverification_attempts_countr*   r*   r+   %cleanup_expired_verification_attemptsy   s    z9VerificationAttempt.cleanup_expired_verification_attemptsc                 C   s   | j  d| j d| j S r$   )r'   r&   ipr(   r*   r*   r+   r,      s    zVerificationAttempt.__str__N)rE   rF   rG   r   BigAutoFieldrL   rM   rg   
EmailFieldr'   r&   rN   rO   Zverification_coderU   r   
created_atrV   staticmethodrf   r,   r*   r*   r*   r+   rX   m   s   
rX   c                   @   sn   e Zd ZejdddddZejdddddZejddddZejdddddZ	ej
eddZej
eddZd	S )
WrongVerificationAttemptrZ   Tr[   r   r   r   Fr]   N)rE   rF   rG   r   rM   rg   ri   r'   r&   rN   rU   r   rj   rV   r*   r*   r*   r+   rl      s   rl   c                   @   s   e Zd ZdZejddddZejdddZej	dddddZ
ej	dddddZejd	ddd
ZejeddZejeddZedd Zdd ZdS )WrongPasswordAttemptz
    This class is responsible for creating WrongPasswordAttempt table in database
    all attributes and methods of WrongPasswordAttempt should defines in this class
    TrY   rZ   r   r   r   Fr[   r   r   r]   c                  C   sB   t jjt tjtjd d} | 	 }| 
  td|  d S )Nr^   r`   z2Event: SCHEDULE, cleanup, wrong password attempt, )rm   r7   r>   r   r   ra   rb   rc   Z+wrong_password_attempt_cleanup_offset_hoursrd   re   r   )Zwrong_password_attemptsZwrong_password_attempts_countr*   r*   r+   &cleanup_expired_wrong_password_attempt   s    z;WrongPasswordAttempt.cleanup_expired_wrong_password_attemptc                 C   s   | j  d| j S r$   )r'   r&   r(   r*   r*   r+   r,      s    zWrongPasswordAttempt.__str__N)rE   rF   rG   rH   r   rh   rL   rM   rg   ri   rN   r'   r&   rU   r   rj   rV   rk   ro   r,   r*   r*   r*   r+   rm      s   
rm   c                   @   s   e Zd ZdZejddddZejeej	ddZ
ejddddZejdddZejdddZe Zd	d
 Zedd Zedd Zedd ZdS )UserJWTz
    This class is responsible for creating JWToken table in database
    all attributes and methods of JWToken should defines in this class
    TrY   )	on_deleter   i  )r   r   r   r    r"   c                 C   s   | j dd  S )Ni)tokenr(   r*   r*   r+   r,      s    zUserJWT.__str__c                 C   sB   t jtj tjtjd tjtj	t
| jt jdtjddS )zL
        This function used for creating a jwt token and returns it
        days)expZissZauduser_idZ
random_keyZHS256)	algorithm)jwtencodera   r   rb   rc   user_jwt_expire_time_in_daysr   
JWT_ISSUERJWT_AUDIENCEstrrL   rJ   rK   hex
SECRET_KEY)r<   r*   r*   r+   
create_jwt   s       zUserJWT.create_jwtc                 C   s2   t | }t jj| |t tjtj	d d |S )z
        This function used for creating a jwt record in database. it takes a user object
        as its argument and create a jwt record for that user in database.
        rs   )r<   rr   	expire_at)
rp   r   r7   creater   r   ra   rb   rc   rz   )r<   rr   r*   r*   r+   create_record   s    
zUserJWT.create_recordc                  C   s4   t jjt d} |  }|   td|  d S )N)Zexpire_at__ltz$Event: SCHEDULE, cleanup, UserJWTs, )rp   r7   r>   r   r   rd   re   r   )ZjwtsZ
jwts_countr*   r*   r+   cleanup_expired_userjwts   s    z UserJWT.cleanup_expired_userjwtsN)rE   rF   rG   rH   r   rh   rL   
ForeignKeyr   CASCADEr<   rM   rr   rU   rj   rV   r   r,   rk   r   r   r   r*   r*   r*   r+   rp      s   
	
rp   c                   @   s   e Zd ZejdddZe Ze ZejdddZ	ejdddZ
ejdddddZejdddZejddZejdddZejddd	Zed
d Zdd ZdS )SMSCallr   Trn   r   (   r[   r   r    r"   c                  C   sB   t jjt tjtjd d} | 	 }| 
  td|  d S )Nr^   r`   z%Event: SCHEDULE, cleanup, SMS Calls, )r   r7   r>   r   r   ra   rb   rc   Zsms_calls_cleanup_offset_hoursrd   re   r   )Z	sms_callsZsms_calls_countr*   r*   r+   cleanup_expired_sms_calls   s    z!SMSCall.cleanup_expired_sms_callsc                 C   s   | j S r@   )r&   r(   r*   r*   r+   r,      s    zSMSCall.__str__N)rE   rF   rG   r   rM   r&   	TextFieldmessageurlheader	post_dataZpattern_coderesponseBooleanFieldsuccessrU   rj   rV   rk   r   r,   r*   r*   r*   r+   r      s$      
r   c                   @   s   e Zd ZejddddZejdddZe Z	ejdddZ
ejdddZejdddZejdddZejddZejdddZejdddZed	d
 Zdd ZdS )	EmailCallTrY   r   rn   r   r   r    r"   c                  C   sB   t jjt tjtjd d} | 	 }| 
  td|  d S )Nr^   r`   z'Event: SCHEDULE, cleanup, Email Calls, )r   r7   r>   r   r   ra   rb   rc   Z email_calls_cleanup_offset_hoursrd   re   r   )Zemail_callsZemail_calls_countr*   r*   r+   cleanup_expired_email_calls  s    z%EmailCall.cleanup_expired_email_callsc                 C   s   | j S r@   )r'   r(   r*   r*   r+   r,     s    zEmailCall.__str__N)rE   rF   rG   r   rh   rL   rM   r'   r   r   r   r   r   r   r   r   rU   rj   rV   rk   r   r,   r*   r*   r*   r+   r      s   
r   )!ra   rJ   rx   django.confr   	django.dbr   django.db.modelsr   django.utilsr   django.utils.timezoner   authenticater   rc   Z
plan.utilsr   r	   r
   r   Zutils.utilsr   Zwikiazma.storage_helperr   Modelr   rX   rl   rm   rp   r   r   r*   r*   r*   r+   <module>   s$   [1