U
    sc                     @   sd  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 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mZmZmZ 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( ee(Z)eeZ*eeZ+ee*ddddee)ej,ddG dd deZ-ee*ddddee)ej.ddee+ej/geddddG dd deZ0ee*ddddee)ej1ddee+ej2geddddG dd deZ3ee*ddddee)ej4ddee+ej5geddddG dd deZ6ee*ddddee)ej7ddG d d! d!eZ8ee*ddddee)ej9ddee+ej:geddddG d"d# d#eZ;ee*ddddee)ej<ddee+ej=geddddG d$d% d%eZ>ee*ddddee)ej?ddee+ej:gedd&ddG d'd( d(eZ@ee*dd)ddee)ejAddG d*d+ d+eZBee*dd)ddee)eddG d,d- d-eZCee*dd)ddee)eddG d.d/ d/eZDee*dd)ddee)eddG d0d1 d1eZEee*ddddee)ejFddee+ejGgeddddG d2d3 d3eZHd4S )5    )#decorator_from_middleware_with_args)method_decoratorstatus)APIView)AuthorizatorMiddleware)defined_roles)serializers)CollaborationAccessMiddleware)Access)conf)	InstituteAPIKeyInstituteJWTInstituteStudent)GetInstituteStudentSerializer!SetInstituteStudentInputValidator"ListInstituteStudentInputValidator!GetInstituteStudentInputValidator)InstituteBalanceHistoryInitialBalance)utils)check_user_limitations)
MyResponse)SerializerValidationMiddlewareT)accept_user_tokenpost)namec                   @   s   e Zd ZdZdd ZdS )CreateInstitutea%  
    This API class is responsible for handling incoming requests for creating a institute.
    it takes name & description from user and checks if a institute with same unique name doesn't exists.
    if it passes the check, then it creates a institute with incoming name and description
    c           	      C   s.  |j d}|j d}|j d}|j d}|j d}tt|js^t|dddtjd	S t }||_||_	|j|_
||_|rtj|tjd
|_tj|tjd
|_|rtj|tjd
|_tj|tjd
|_|  tjjdtdi |d tjj|j|tjg||jd t !|j"}t|dd|dtj#d	S )Nr   descriptionlogoposterinfoerrorzPayment Requiredr   messager   max_side_sizer   )amountreasonremaining_balancereason_meta_data	institute)userr,   rolesinstitute_nameinstitute_created_atok
Successfulr   r%   r,   )$middleware_serializer_datagetr   r   middleware_userr   r   HTTP_402_PAYMENT_REQUIREDr   r   r-   r"   r   compress_imager   -maximum_side_size_compress_for_institute_logor    7maximum_side_size_compress_for_institute_logo_thumbnaillogo_thumbnail/maximum_side_size_compress_for_institute_posterr!   9maximum_side_size_compress_for_institute_poster_thumbnailposter_thumbnailsaver   objectscreater   r   dr__ROLE_OWNER__
created_atinstitute_serializers5InstituteSerializerForExamAndInstituteAndQuestionBankdataHTTP_200_OK)	selfrequestr   r   r    r!   r"   r,   institute_data rL   +/var/www/wikiazma_server/institute/views.pyr   (   sV      zCreateInstitute.postN__name__
__module____qualname____doc__r   rL   rL   rL   rM   r      s   r   idinstitute_idc                   @   s   e Zd ZdZdd ZdS )EditInstitutea  
    This API class is responsible for handling incoming requests for editing a institute. it takes name, description from user.
    first it checks if a institute with same unique name doesn't exists and then checks if a institute with incoming institute_id exists.
    if it passes both checks, then it edits desired institute with incoming name and description
    and if any of above checks fails, user faces with an error.
    c                 C   s2  |j d}|j d}|j d}|j d}|j d}|j d}|j d}|j}	||	_|sl|dkrr||	_|s~|dkr||	_|r|	j  |	j  |r|	j	  |	j
  |rtj|tjd	|	_tj|tjd	|	_|rtj|tjd	|	_	tj|tjd	|	_
|	  t|	j}
t|d
d|
dtjdS )Nr   r   r"   r    r!   delete_logodelete_poster r&   r1   r2   r3   r   )r4   r5   middleware_instituter   r   r"   r    deleter;   r!   r>   r   r8   r   r9   r:   r<   r=   r?   rE   rF   rG   r   r   rH   )rI   rJ   r   r   r"   r    r!   rV   rW   r,   rK   rL   rL   rM   r   e   sP    



  zEditInstitute.postNrN   rL   rL   rL   rM   rU   Y   s   rU   c                   @   s   e Zd ZdZdd ZdS )DeleteInstitutez`
    This API class is responsible for handling incoming requests for deleting a institute.
    c                 C   s    |j   t|dddtjdS Nr1   r2   r$   r   )rY   rZ   r   r   rH   rI   rJ   rL   rL   rM   r      s    
zDeleteInstitute.postNrN   rL   rL   rL   rM   r[      s   r[   c                   @   s   e Zd Zdd ZdS )GetInstitutec                 C   s&   t |jj}t|dd|dtjdS )Nr1   r2   r3   r   )rE   rF   rY   rG   r   r   rH   )rI   rJ   rK   rL   rL   rM   r      s    zGetInstitute.postNrO   rP   rQ   r   rL   rL   rL   rM   r^      s   r^   c                   @   s   e Zd ZdZdd ZdS )AllInstitutesz
    This API class is responsible for handling incoming requests for getting all institute which the authenticated user has access in them.
    c                 C   s   |j d}|j d}|j d}|j d}t|}tjj|jd}|rZ|j|d}| }|	||||  }	t
j|	ddj}
t|d	d
||
ddtjdS )Nsearch_wordsskiptakesort_by)r-   )Zinstitute__name__icontainsTmanyr1   r2   )total_count
institutesr   r%   rG   r   )r4   r5   r   access_sort_by_converterr   r@   filterr6   countorder_bycollaborators_serializersAccessibleInstituteSerializerrG   r   r   rH   )rI   rJ   ra   rb   rc   rd   sort_by_attraccess_listrg   Zordered_access_listZaccess_datarL   rL   rM   r      s4    
   zAllInstitutes.postNrN   rL   rL   rL   rM   r`      s   r`   c                   @   s   e Zd Zdd ZdS )GenerateAPIKeyc                 C   sb   |j d}|j d}|jjr6t|dddtjdS t|j||}t|ddd	|id
tj	dS )N	expire_atr.   r#   z4You can not generate APIKeys for a sandbox instituter$   r   r1   r2   api_keyri   )
r4   r5   rY   sandboxr   r   HTTP_403_FORBIDDENr   create_recordrH   )rI   rJ   rs   r.   rt   rL   rL   rM   r   N  s      zGenerateAPIKey.postNr_   rL   rL   rL   rM   rr   H  s   rr   c                   @   s   e Zd Zdd ZdS )APIKeysListc                 C   sT   |j jrt|dddtjdS tjj|j dddd}t|d	d
d|idtj	dS )Nr#   z4You can not get APIKeys list for a sandbox instituter$   r   r,   rS   rt   rs   r1   r2   api_keysri   )
rY   ru   r   r   rv   r   r@   rk   valuesrH   )rI   rJ   rz   rL   rL   rM   r   c  s      zAPIKeysList.postNr_   rL   rL   rL   rM   rx   ]  s   rx   
api_key_idc                   @   s   e Zd Zdd ZdS )DeleteAPIKeyc                 C   s    |j   t|dddtjdS r\   )middleware_model_recordrZ   r   r   rH   r]   rL   rL   rM   r   r  s    
zDeleteAPIKey.postNr_   rL   rL   rL   rM   r}   l  s   r}   )accept_server_api_keyc                   @   s   e Zd Zdd ZdS )GetTokenc                 C   s(   t |j}t|ddd|idtjdS )Nr1   r2   tokenri   r   )r   rw   middleware_api_keyr   r   rH   )rI   rJ   r   rL   rL   rM   r   |  s    zGetToken.postNr_   rL   rL   rL   rM   r   w  s   r   c                   @   s   e Zd ZdZdd ZdS )SetInstituteStudentz
    This API class is responsible for handling incoming requests for adding or editing an InstituteStudent in the database
    c              	   C   s   |j d}|j d}|j d}|j d}|j d}|j d}tjj|j||||||dd\}}	t|j}
t|d	d
d|
idt	j
dS )N"referer_institute_student_identity
first_name	last_nameemailphoner"   )r   r   r   r   r"   )r,   referer_identitydefaultsr1   r2   institute_studentri   r   )r4   r5   r   r@   update_or_createrY   r   rG   r   r   rH   )rI   rJ   r   r   r   r   r   r"   r   createdinstitute_student_datarL   rL   rM   r     s0    
zSetInstituteStudent.postNrN   rL   rL   rL   rM   r     s   r   c                   @   s   e Zd ZdZdd ZdS )GetInstituteStudentzf
    This API class is responsible for handling incoming requests for getting an InstituteStudent
    c                 C   s   |j d}|j d}|rXztjj|j|d}W q   t|dddtjd Y S X nV|rztjj|j|d}W q   t|dddtjd Y S X nt|dd	dtjdS t	|j
}t|d
dd|idtjdS )Nwikiazma_institute_student_idr   )r,   rS   r#   zInstitute Student not foundr$   r   )r,   r   z]either wikiazma_institute_student_id or referer_institute_student_identity should be providedr1   r2   r   ri   )r4   r5   r   r@   rY   r   r   HTTP_404_NOT_FOUNDHTTP_400_BAD_REQUESTr   rG   rH   )rI   rJ   r   r   r   r   rL   rL   rM   r     sJ     


zGetInstituteStudent.postNrN   rL   rL   rL   rM   r     s   r   c                   @   s   e Zd ZdZdd ZdS )ListInstituteStudentszd
    This API class is responsible for handling incoming requests for listing InstituteStudents
    c                 C   s   |j d}|j d}|j d}t|}tjj|jd}|rL||}||||  }t	|ddj
}t|ddd	|id
tjdS )Nrb   rc   rd   ry   Tre   r1   r2   Zinstitute_studentsri   r   )r4   r5   r   #institute_student_sort_by_converterr   r@   rk   rY   rm   r   rG   r   r   rH   )rI   rJ   rb   rc   rd   rp   r   r   rL   rL   rM   r     s"    

 zListInstituteStudents.postNrN   rL   rL   rL   rM   r     s   r   c                   @   s   e Zd Zdd ZdS )InstituteBalancec                 C   s(   |j }|j}t|ddd|idtjdS )Nr1   r2   balanceri   r   )r~   r   r   r   rH   )rI   rJ   r,   r   rL   rL   rM   r     s
    zInstituteBalance.postNr_   rL   rL   rL   rM   r     s   r   N)Idjango.utils.decoratorsr   r   rest_frameworkr   rest_framework.viewsr   authenticate.middlewarer   collaboratorsr   rB   r	   rn   collaborators.middlewarer
   Zcollaborators.modelsr   r,   r   rE   institute.modelsr   r   r   r   institute.serializersr   r   r   r   payment.modelsr   r   r   utils.limitationsr   utils.myresponser   wikiazma.middlewarer    serializer_validation_middlewareIuser_token_or_server_token_or_server_API_key_validation_and_authorization"check_access_permission_middlewareCreateInstituteInputSerializerr   EditInstituteInputSerializer__PERMISSION_EDIT_INSTITUTE__rU   DeleteInstituteInputSerializer__PERMISSION_DELETE_INSTITUTE__r[   GetInstituteInputSerializer__PERMISSION_READ_INSTITUTE__r^   AllInstitutesInputSerializerr`   GenerateAPIKeyInputSerializer__PERMISSION_WRITE_APIKEY__rr   APIKeysListInputSerializer__PERMISSION_READ_APIKEY__rx   DeleteAPIKeyInputSerializerr}   GetTokenInputSerializerr   r   r   r   InstituteBalanceInputSerialize%__PERMISSION_READ_INSTITUTE_BILLING__r   rL   rL   rL   rM   <module>   s   
8
4

,
  v



#'
