o
    Mf)                     @   s  d 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 ddlmZmZmZmZmZ ddl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, ddl-m.Z. ddl/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5 e6e7Z8ee	j9Z:ededgddddG dd dej;Z<G d d! d!e$Z=ededgd"d#ej>d$ej?ej@d%d&gd'dG d(d) d)ejAZBed*edgd+d,ej>d-ej?ej@d%d&ej>d.ej?ej@d/d&gd0d1d2d3d4dG d5d6 d6eejCZDededgd7d8dded9edgd:d;ddG d<d= d=ejEZFeded>gd?d@dAe idBdG dCdD dDejGZHedEed>gdFdGdAe idBdG dHdI dIe.ZIdS )JzThis file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.
    N)flag_set)GetParentObjectMixin)	load_func)settings)reverse)method_decorator)openapi)swagger_auto_schema)OrganizationOrganizationMember)OrganizationIdSerializerOrganizationInviteSerializer OrganizationMemberUserSerializerOrganizationSerializerOrganizationsParamsSerializer)genericsstatus)NotFoundPermissionDenied)get_object_or_404)PageNumberPagination)
FormParser
JSONParserMultiPartParser)IsAuthenticated)Response)APIView)User)ViewClassPermissionall_permissions)bool_from_requestgetOrganizationszList your organizationsz_
        Return a list of the organizations you've created or that you have access to.
        )tagsoperation_summaryoperation_description)name	decoratorc                       sn   e Zd Zej Zeee	fZ
eejejejejejdZeZdd Z fddZedd fdd	Z  ZS )
OrganizationListAPI)GETPUTPOSTPATCHDELETEc                 C   s   |j | jjjj ddd S )NT)deleted_at__isnull)organizationmember__in)filterrequestuser
om_throughdistinct)selfqueryset r7   `/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/label_studio/organizations/api.pyfilter_queryset=   s
   z#OrganizationListAPI.filter_querysetc                       t t| j|g|R i |S N)superr(   r!   r5   r1   argskwargs	__class__r7   r8   r!   B      zOrganizationListAPI.getNauto_schemac                    r:   r;   )r<   r(   postr=   r@   r7   r8   rE   E      zOrganizationListAPI.post)__name__
__module____qualname__r
   objectsallr6   r   r   r   parser_classesr   r   organizations_vieworganizations_changeorganizations_createpermission_requiredr   serializer_classr9   r!   r	   rE   __classcell__r7   r7   r@   r8   r(   '   s    

r(   c                       s$   e Zd ZdZdZ fddZ  ZS )OrganizationMemberPagination   	page_sizec                    s,   | j |jv r|j| j  dkrdS t |S )Nz-1i@B )page_size_query_paramquery_paramsr<   get_page_size)r5   r1   r@   r7   r8   rX   N   s   z*OrganizationMemberPagination.get_page_size)rG   rH   rI   rU   rV   rX   rR   r7   r7   r@   r8   rS   J   s    rS   zGet organization members listz:Retrieve a list of the organization members and their IDs.idz5A unique integer value identifying this organization.)r&   typein_description)r#   r$   r%   manual_parametersc                   @   sF   e Zd ZeeefZeej	ej
ej
ej
dZeZeZdd Zdd ZdS )OrganizationMemberListAPI)r)   r*   r,   r-   c                 C   s   t | jjdd| jdS )Ncontributed_to_projectsF)r_   r1   )r    r1   r)   )r5   r7   r7   r8   get_serializer_contexts   s   z0OrganizationMemberListAPI.get_serializer_contextc                 C   sx   t j| jjj| j| j d}td| jjr6t| jj	d}|j
dd |jd}|r0|jdS |jdS |jdS )Npk.fix_backend_dev_3134_exclude_deactivated_usersdataT)raise_exceptionactiveuser__username)r   r   r1   r2   organizationsr?   lookup_fieldr   r   r)   is_validvalidated_datar!   active_membersorder_bymembers)r5   org
serializerrg   r7   r7   r8   get_querysety   s   z&OrganizationMemberListAPI.get_querysetN)rG   rH   rI   r   r   r   rL   r   r   rM   rN   rP   r   rQ   rS   pagination_classr`   rr   r7   r7   r7   r8   r^   X   s    
r^   deletez"Soft delete an organization memberz+Soft delete a member from the organization.rb   user_pkzPA unique integer value identifying the user to be deleted from the organization.zMember deleted successfully.zUser cannot soft delete self.Member not found)   i  i  )r#   r$   r%   r]   	responsesc                   @   sH   e Zd ZeejdZej	 Z
eeefZeefZeZdgZdddZdS )OrganizationMemberDetailAPI)r-   rt   Nc                 C   sz   |   }||jjkrtdtt|d}tt||d}|jd ur$td|j	|jj
kr4tdditjdS |  tddS )	Nz@You can delete members only for your current active organizationra   )r2   organizationrv   detailzUser cannot soft delete selfr   rw   )get_parent_objectr2   active_organizationr   r   r   r   
deleted_atr   user_idrY   r   r   HTTP_405_METHOD_NOT_ALLOWEDsoft_delete)r5   r1   rb   ru   rp   r2   memberr7   r7   r8   rt      s   

z"OrganizationMemberDetailAPI.delete)NN)rG   rH   rI   r   r   rN   rP   r
   rJ   rK   parent_querysetr   r   r   rL   r   HasObjectPermissionpermission_classesr   rQ   http_method_namesrt   r7   r7   r7   r8   ry      s    

ry   z Get organization settingsz8Retrieve the settings for a specific organization by ID.patchzUpdate organization settingsz6Update the settings for a specific organization by ID.c                       sd   e Zd ZeeefZej	 Z
ejZeZdZdZ fddZ fddZedd fd	d
Z  ZS )OrganizationAPIzorganizations-dashboardrb   c                    r:   r;   )r<   r   r!   r=   r@   r7   r8   r!      rB   zOrganizationAPI.getc                    r:   r;   )r<   r   r   r=   r@   r7   r8   r      rB   zOrganizationAPI.patchNrC   c                    r:   r;   )r<   r   putr=   r@   r7   r8   r      rF   zOrganizationAPI.put)rG   rH   rI   r   r   r   rL   r
   rJ   rK   r6   r   rN   rP   r   rQ   redirect_routeredirect_kwargr!   r   r	   r   rR   r7   r7   r@   r8   r      s    

r   InviteszGet organization invite linkzWGet a link to use to invite a new member to an organization in Label Studio Enterprise.   )r#   r$   r%   rx   c                   @   s*   e Zd ZefZej Ze	j
Zdd ZdS )OrganizationInviteAPIc                 O   sb   |j j}dtd|j}ttdrtjr|tjdd}t	||jdd}|
  t|jdd	S )
N{}?token={}user-signupFORCE_SCRIPT_NAME    
invite_urltokenrd   r   r|   )r2   r~   formatr   r   hasattrr   r   replacer   rk   r   re   r5   r1   r>   r?   rp   r   rq   r7   r7   r8   r!      s   zOrganizationInviteAPI.getN)rG   rH   rI   r   rL   r
   rJ   rK   r6   r   rN   rP   r!   r7   r7   r7   r8   r      s
    

r   rE   zReset organization tokenzQReset the token used in the invitation link to invite someone to an organization.c                   @   s    e Zd ZejZefZdd ZdS )OrganizationResetTokenAPIc                 O   sd   |j j}|  td|j d|j  dtd|j}t	||jdd}|
  t|jddS )	NzNew token for organization z is r   r   r   rd      r|   )r2   r~   reset_tokenloggerdebugrb   r   r   r   r   rk   r   re   r   r7   r7   r8   rE   
  s   zOrganizationResetTokenAPI.postN)	rG   rH   rI   r   organizations_inviterP   r   rL   rE   r7   r7   r7   r8   r      s    
r   )J__doc__loggingcore.feature_flagsr   core.mixinsr   core.utils.commonr   django.confr   django.urlsr   django.utils.decoratorsr   drf_yasgr   drf_yasg.utilsr	   organizations.modelsr
   r   organizations.serializersr   r   r   r   r   rest_frameworkr   r   rest_framework.exceptionsr   r   rest_framework.genericsr   rest_framework.paginationr   rest_framework.parsersr   r   r   rest_framework.permissionsr   rest_framework.responser   rest_framework.viewsr   users.modelsr   label_studio.core.permissionsr   r   label_studio.core.utils.paramsr    	getLoggerrG   r   MEMBER_PERMr   ListCreateAPIViewr(   rS   	ParameterTYPE_INTEGERIN_PATHListAPIViewr^   RetrieveDestroyAPIViewry   RetrieveUpdateAPIViewr   RetrieveAPIViewr   r   r7   r7   r7   r8   <module>   s    


"		