U
    ]Od*                     @   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 d d	lmZ d d
l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!dS )    N)settings)
ArrayField)models)Q)timezone)FieldTracker)User)conf))find_best_max_collaborators_per_institute*find_best_max_question_banks_per_institute)find_best_max_questions_per_question_bank)
logs_adder)public_storagec                   @   st  e Zd ZejdddejddZejdddZ	ej
ddddZejeej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Zee edZejdddZejdddZ e!dgdZ"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$d% Z-d&d' Z.d(d) Z/d*d+ Z0dS ),	InstituteTFprimary_keyuniquedb_indexdefaulteditabled   
max_lengthr     r   nullblank	on_deleter   zinstitute/logo)	upload_tor   r   storagezinstitute/logo_thumbnailszinstitute/posterszinstitute/posters_thumbnailsr   r   )r   r   r   )r   auto_now_addr   auto_nowr   name)fieldsc                 C   s   | j S N)r&   self r+   ,/var/www/wikiazma_server/institute/models.py__str__.   s    zInstitute.__str__Nc                 C   s,   | j s&ddlm} |jjdd\| _ }| j S )Nr   )EnvironmentT)
active_env)_active_environmentplan.modelsr.   objectsget_or_create)r*   r.   _r+   r+   r,   active_environment4   s    zInstitute.active_environmentc                 C   sD   | j s>ddlm} |jjtt dtddB | t d| _ | j S )Nr   )InstitutePlan)end_date__gteT)end_date__isnull)	institutestart_date__lte)_plansr1   r6   r2   filterr   r   now)r*   r6   r+   r+   r,   plans<   s    zInstitute.plansc                 C   s"   | j rt| jS t| j| jS d S r(   )sandboxr
   r>   r5   r)   r+   r+   r,   $best_max_collaborators_per_instituteD   s    
z.Institute.best_max_collaborators_per_institutec                 C   s   t | j| jS r(   )r   r>   r5   r)   r+   r+   r,   %best_max_question_banks_per_instituteK   s    z/Institute.best_max_question_banks_per_institutec                 C   s   t | j| jS r(   )r   r>   r5   r)   r+   r+   r,   $best_max_questions_per_question_bankR   s    z.Institute.best_max_questions_per_question_bankc                 C   s   ddl m} || jS )Nr   )InstituteBalanceHistory)payment.modelsrC   Zget_last_institute_balanceremaining_balance)r*   rC   r+   r+   r,   balanceY   s    zInstitute.balancec                 C   s   || j krdS dS d S )NTF)_subscribed_question_banksr*   qb_pkr+   r+   r,   question_bank_is_picked^   s    
z!Institute.question_bank_is_pickedc                 C   s2   |j | ks&| |js&|jr*|jdkr*dS dS d S )Nr   TF)r9   rJ   pkpublicprice)r*   Zqbr+   r+   r,   can_read_question_bankd   s    &z Institute.can_read_question_bankc                 C   s"   || j kr| j | |   d S r(   )rG   appendsaverH   r+   r+   r,   subscribe_qbj   s    
zInstitute.subscribe_qbc                 C   s,   t |}|| jkr(| j| |   d S r(   )uuidUUIDrG   removerP   )r*   rI   Z
qb_pk_uuidr+   r+   r,   unsubscribe_qbo   s    

zInstitute.unsubscribe_qb)1__name__
__module____qualname__r   	UUIDFieldrR   uuid4id	CharFieldr&   	TextFielddescription
ForeignKeyr   CASCADEuser
ImageFieldr   ZlogoZlogo_thumbnailposterposter_thumbnail	JSONFieldinfoZon_exam_finished_redirect_urlBooleanFieldr?   r   listrG   DateTimeField
created_atmodified_atr   Ztrackerr-   r;   r0   propertyr5   r>   r@   rA   rB   rF   rJ   rN   rQ   rU   r+   r+   r+   r,   r      st               





r   c                   @   s   e Zd ZejdddejddZeje	ej
ddZejeej
dddZejeej
dd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ZejddddZejdddZejdddZedd ZdS )TransferOwnershipHistoryTFr   r   	old_owner)r   related_namer   	new_ownerr   r   @   )r   r   r   r   r   r   r   r"   r$   c                  C   sb   t jtt tjtj	d dtt tjtj	d dB } | 
 }|   td|  d S )Ndays)Znew_owner_accept_at__lt)Zold_owner_accept_at__ltz/Event: SCHEDULE, cleanup, transfer_ownerships, )rm   r2   r<   r   r   r=   datetime	timedeltainstitute_confZ&transfer_ownership_cleanup_offset_dayscountdeleter   )Ztransfer_ownershipsZtransfer_ownerships_countr+   r+   r,   "cleanup_expired_transfer_ownership   s     

z;TransferOwnershipHistory.cleanup_expired_transfer_ownershipN)rV   rW   rX   r   rY   rR   rZ   r[   r_   r   r`   r9   r   rn   rp   r]   r^   r\   Znew_owner_accept_keyZold_owner_accept_keyri   Znew_owner_accept_atZold_owner_accept_atrj   rk   staticmethodrz   r+   r+   r+   r,   rm   v   sb                       rm   c                   @   s   e Zd ZejdddejddZeje	ej
ddZejdddZeejddedd	Zejddd
ZejdddZejddddZdd Zedd Zedd ZdS )APIKeyTFr   r     r   r   )r   )r   r   r"   r$   rr   c                 C   s   | j jS r(   )r9   r&   r)   r+   r+   r,   r-      s    zAPIKey.__str__c                 C   s,   t jtjtjt| jt j	dtj
ddS )N)issaudZinstitute_id
random_keyHS256	algorithm)jwtencoder   
JWT_ISSUERJWT_AUDIENCEstrr[   rR   rZ   hex
SECRET_KEY)r9   r+   r+   r,   create_api_key   s      zAPIKey.create_api_keyc                 C   s"   t | }t jj| |||d |S )N)r9   api_keyroles	expire_at)r|   r   r2   create)r9   r   r   r   r+   r+   r,   create_record   s    
zAPIKey.create_recordN)rV   rW   rX   r   rY   rR   rZ   r[   r_   r   r`   r9   r\   r   r   rh   r   ri   rj   rk   r   r-   r{   r   r   r+   r+   r+   r,   r|      s.        
r|   c                   @   s   e 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 )InstituteJWTT)r   r   r   r   r}   )r   r   r   r"   r$   c                 C   s   | j dd  S )Ni)tokenr)   r+   r+   r,   r-      s    zInstituteJWT.__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 )Nrs   )expr~   r   Z
api_key_idr   r   r   )r   r   ru   r=   rv   rw   !institute_jwt_expire_time_in_daysr   r   r   r   r[   rR   rZ   r   r   )r   r+   r+   r,   
create_jwt   s       zInstituteJWT.create_jwtc                 C   s2   t | }t jj| |t tjtj	d d |S )Nrs   )r   r   r   )
r   r   r2   r   r   r=   ru   rv   rw   r   )r   r   r+   r+   r,   r      s    
zInstituteJWT.create_recordc                  C   s4   t jjt d} |  }|   td|  d S )N)expire_at__ltz)Event: SCHEDULE, cleanup, InstituteJWTs, )r   r2   r<   r   r=   rx   ry   r   )jwts
jwts_countr+   r+   r,   cleanup_expired_institutejwts   s    z*InstituteJWT.cleanup_expired_institutejwtsN)rV   rW   rX   r   BigAutoFieldr[   r_   r|   r`   r   r\   r   ri   rj   rk   r   r-   r{   r   r   r   r+   r+   r+   r,   r      s"     

r   c                   @   s  e Zd ZejdddejddZejdddZ	ej
eej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edddZejdedddZejdddZej
eejddddZejdddZejdddZG dd dZedd Z dd Z!dS )InstituteStudentTFr   r   r   r   .   )r   r   r   r         zinstitute_student/image)r   r    r   r   z!institute_student/image_thumbnailr!   )r   r   r   r   r"   r$   c                   @   s   e Zd ZddgZdS )zInstituteStudent.Metar9   referer_identityN)rV   rW   rX   unique_togetherr+   r+   r+   r,   Meta   s   r   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,   	full_name   s    zInstituteStudent.full_namec                 C   s   | j S r(   )r   r)   r+   r+   r,   r-     s    zInstituteStudent.__str__N)"rV   rW   rX   r   rY   rR   rZ   r[   r\   r   r_   r   PROTECTr9   r   r   emailphonerb   r   imageimage_thumbnailre   rf   r   SET_NULLZwikiazma_accountri   rj   rk   r   rl   r   r-   r+   r+   r+   r,   r      sx                            

r   )"ru   rR   r   django.confr   Zdjango.contrib.postgres.fieldsr   	django.dbr   django.db.modelsr   django.utilsr   Zmodel_utilsr   authenticate.modelsr   r9   r	   rw   
plan.utilsr
   r   r   utils.utilsr   wikiazma.storage_helperr   Modelr   rm   r|   r   r   r+   r+   r+   r,   <module>   s$   b""'