o
    Ci+                     @   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	 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 d dlmZ d dlmZmZ d dlmZ d dlmZ G dd deZG dd deZ G dd deZ!dS )    )APIView)Responsestatus)swagger_auto_schema)transaction)Q)csrf_exempt)models)role_serializers)datetimetimezone)token_function)require_permissionCustomIsAuthenticated)method_decorator)OAuth2Authenticationc                   @   sH   e Zd ZegZegZejZ	e
edeejdeejdd ZdS )
CreateRolezrole.create)request_bodyc           
      C   s  |j j}tjjj|d}|js!|jjdd	 s!t
dditjdS | j|jd}|jdd	r|jd
 }|jdd}|jdg }tjjj||d	 rYt
dd| ditjdS tjjj|d}tjjj|||d}	|	j| t
d|	j|	jdd |D dtjdS t
dditjdS )Nidadminnamemessagez&You are not authorized to create rolesr   )dataT)raise_exceptionr   description permissions)name__iexact
created_byzRole "z" already existsid__in)r   r   r!   zRole created successfullyc                 S   s   g | ]}|j qS  r   ).0pr$   r$   +/var/www/Datamplify/authentication/roles.py
<listcomp>A   s    z#CreateRole.post.<locals>.<listcomp>)r   role_id	role_namer   zSerializer Error)userr   auth_modelsUserProfileobjectsgetis_superuserrolesfilterexistsr   r   HTTP_403_FORBIDDENserializer_classr   is_validvalidated_dataRoleHTTP_406_NOT_ACCEPTABLE
Permissioncreater   setr   HTTP_201_CREATEDHTTP_400_BAD_REQUEST)
selfrequestuser_idr+   
serializerr   r   permission_idsr   roler$   r$   r'   post   sB   

zCreateRole.postN)__name__
__module____qualname__r   authentication_classesr   permission_classesauth_serializersCreateRoleSerializerr5   r   r   r   r	   r   atomicrE   r$   r$   r$   r'   r      s    

r   c                   @   s0   e Zd ZegZegZeede	dd Z
dS )RoleList	role.viewc                 C   s   |j j}tjjj|d}|js!|jjdd	 s!t
dditjdS tjjtddt|j d	B d
}tj|dd}t
dt|j|jdtjdS )zC
        Get all roles created by the logged-in Admin user
        r   r   r   r   z$You are not authorized to view rolesr   T)
is_default)r!   z-created_at)manyzRoles fetched successfully)r   countr1   )r+   r   r,   r-   r.   r/   r0   r1   r2   r3   r   r   r4   r8   r   order_byrK   RoleListSerializerlenr   HTTP_200_OK)r?   r@   rA   r+   r1   rB   r$   r$   r'   r/   P   s    &zRoleList.getN)rF   rG   rH   r   rI   r   rJ   r   r   r	   r/   r$   r$   r$   r'   rN   L   s    
rN   c                   @   sl   e Zd ZegZegZeede	dd Z
eede	ejdd Zeede	ejdd	 Zd
S )
RoleDetailrO   c                 C   s   |j j}tjjj|d}|js!|jjdd	 s!t
dditjdS ztjjj||d}W n tjjy@   t
dditjd Y S w t|}t
d	|jd
tjdS )zQ
        Get detailed info of a single Role (created by logged-in Admin)
        r   r   r   r   z+You are not authorized to view role detailsr   )r   r!   z$Role not found or not created by youz!Role details fetched successfullyr   rD   )r+   r   r,   r-   r.   r/   r0   r1   r2   r3   r   r   r4   r8   DoesNotExistHTTP_404_NOT_FOUNDrK   RoleDetailSerializerr   rV   )r?   r@   r)   rA   r+   rD   rB   r$   r$   r'   r/   u   s,   

zRoleDetail.getz	role.editc           
      C   s   |j j}z
tjjj|d}W n tjjy"   tdditj	d Y S w t
|jjt
|kr6tdditjdS |jd|j}|jd|j}|jdg }||_||_|  |rgtjjj|d	}|j| t|}	td
|	jdtjdS )z8
        Update a custom role (only by creator)
        r   r   Role not foundr   z*You are not authorized to update this roler   r   r   r"   zRole updated successfullyrX   )r+   r   r,   r8   r.   r/   rY   r   r   rZ   strr!   r4   r   r   r   saver:   r2   r   r<   rK   r[   rV   )
r?   r@   r)   rA   rD   r   r   permissions_idsr   rB   r$   r$   r'   put   s&   
zRoleDetail.putzrole.deletec                 C   s   |j j}z
tjjj|d}W n tjjy"   tdditj	d Y S w t
|jjt
|kr6tdditjdS |  tdditjdS )z8
        Delete a custom role (only by creator)
        r   r   r\   r   z*You are not authorized to delete this rolezRole deleted successfully)r+   r   r,   r8   r.   r/   rY   r   r   rZ   r]   r!   r4   deleterV   )r?   r@   r)   rA   rD   r$   r$   r'   ra      s   zRoleDetail.deleteN)rF   rG   rH   r   rI   r   rJ   r   r   r	   r/   r   rM   r`   ra   r$   r$   r$   r'   rW   q   s    

 
%rW   N)"rest_framework.viewsr   rest_framework.responser   rest_frameworkr   drf_yasg.utilsr   	django.dbr   django.db.modelsr   django.views.decorators.csrfr	   authenticationr
   r,   r   rK   r   r   tzauthentication.utilsr   authentication.permissionsr   r   django.utils.decoratorsr   &oauth2_provider.contrib.rest_frameworkr   r   rN   rW   r$   r$   r$   r'   <module>   s"   :%