U
    sct%                     @   s   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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ZG dd dZdS )    status)defined_roles)role_permission_check)Access)ExamPageExamTemplateExam)	InstituteAPIKey)Question)QuestionBank)QuizQuizTemplate	QuizSheet)AnswerSheet)
MyResponsec                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
CollaborationAccessMiddlewarez
    This middleware class is responsible for checking user access permission.
    if it was successful it passes the request,
    otherwise it responses the user with an access denied error!
    Nc                 C   s"   || _ || _|| _|| _|| _d S N)get_responsepermissionsmodeldb_field_nameserializer_field_name)selfr   r   r   r   r    r   4/var/www/wikiazma_server/collaborators/middleware.py__init__   s
    z&CollaborationAccessMiddleware.__init__c                 C   s   |  |}|S r   r   r   requestresponser   r   r   __call__   s    
z&CollaborationAccessMiddleware.__call__c                 C   s  t |dr|j| j}| j|i}z| jjjf |}W n"   t|dddtj	d Y S X d }| jt
krp|}n| jtkr|j}n| jtkr|j}n| jtkr|jj}n| jtkr|j}nv| jtkr|j}nd| jtkr|jjj}nN| jtkr|j}n<| jtkr|j}n(| jtkr|jj}n| jtkr0|j}||_||_ztjj|j|d}	|	|_W n"   t|dddtj	d Y S X |	j}
| jD ],}t |
|st|dddtj	d  S qn\t |dr| jD ]0}t |j!j|st|dddtj	d  S qnt|dddtj"dS d S )	Nmiddleware_usererror	Forbiddenr   messager   )user	institutemiddleware_api_keyBad Request)#hasattrmiddleware_serializer_datagetr   r   r   objectsr   r   HTTP_403_FORBIDDENr
   r   r)   r   r   question_bankr   r	   r   questionr   r   r   quizr   middleware_institutemiddleware_model_recordr   r#   Zmiddleware_accessrolesr   r   r*   HTTP_400_BAD_REQUEST)r   r    	view_func	view_argsview_kwargsserializer_field_value
query_dictmodel_recordr)   access_recordr6   
permissionr   r   r   process_view'   sf    










 

 
 z*CollaborationAccessMiddleware.process_view)NNN__name__
__module____qualname____doc__r   r"   r@   r   r   r   r   r      s   
	r   c                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
SubscriptionAccessMiddlewarez
    This middleware checks if the user (representative of context_institute_id) has
    owner or subscription or ... accessibility or not
    Nc                 C   s   || _ || _|| _|| _d S r   )r   r   r   r   )r   r   r   r   r   r   r   r   r      s    z%SubscriptionAccessMiddleware.__init__c                 C   s   |  |}|S r   r   r   r   r   r   r"      s    
z%SubscriptionAccessMiddleware.__call__c           	      C   s   t |dr|j| j}| j|i}z| jjjf |}W n"   t|dddtj	d Y S X | jt
krl||_n0| jtkr|j|_||_nt|dddtjdS |j}||jrd S t|dddtj	dS nt|dddtjdS d S )Nr4   r$   r%   r&   r   r+   )r,   r-   r.   r   r   r   r/   r   r   r0   r   Zmiddleware_question_bankr   r1   Zmiddleware_questionr7   r4   can_read_question_bank)	r   r    r8   r9   r:   r;   r<   r=   Zcontext_instituter   r   r   r@      s(    



z)SubscriptionAccessMiddleware.process_view)NNNrA   r   r   r   r   rF      s   
rF   N)rest_frameworkr   collaboratorsr   drZcollaborators.defined_rolesr   collaborators.modelsr   Zexam.modelsr   r   r	   institute.modelsr
   r   question.modelsr   question_bank.modelsr   quiz.modelsr   r   r   Ztext_book.modelsr   utils.myresponser   r   rF   r   r   r   r   <module>   s   z