
    	]j8                        d Z ddl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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 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(  ejR                  e*      Z+ddddddddddddddddddddddddddddd 	d!Z, ed" ed#gd$d% e
d&ejZ                  d'd(      ge&d)d*gi+      ,       ed- ed#gd.d/d0d-d1gd23      ,       ed4 ed#gd5d6d7e,id8e&id0d4d1gd29      ,       ed: ed#gd;d< e
d&ejZ                  d'd(      gdd=e&id0d>d1gd2?      ,       ed@ ed#gdAdB e
d&ejZ                  d'd(      gd7e,id=e&id0d"d1gd2?      ,       edC ed#gdDdE e
d&ejZ                  d'd(      gdd0dFd1gd2+      ,       G dG dHej\                                                                  Z/ edI ed#gdJdKdd8 edLddMdNdiid!O      id0dPd1gd29      ,       G dQ dRe             Z0 ed> ed#gdSdTdd= edLddUdNdiid!O      id0dVd1gd29      ,       G dW dXe             Z1 ed> ed#gdYdZdd=e(id0d[d1gd29      ,       G d\ d]ejd                               Z3 ed^ ed#gd_d`e%d=e%id0dad1gd29      ,       ed> ed#gdbdcdd=e%id0ddd1gd29      ,       G de dfe                    Z4y)gzThis 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)ViewClassPermissionall_permissions)method_decorator)OpenApiTypes)OpenApiParameterOpenApiResponseextend_schema)genericsviewsets)Token)action)MethodNotAllowed)
FormParser
JSONParserMultiPartParser)IsAuthenticated)Response)APIView)check_avatar)User)HotkeysSerializerUserSerializerUserSerializerUpdateWhoAmIUserSerializerobjectintegerzUser ID)typedescriptionstringzFirst name of the userzLast name of the userzUsername of the userzEmail of the userzAvatar URL of the userzInitials of the userzPhone number of the userbooleanz#Whether the user allows newsletters)	id
first_name	last_nameusernameemailavatarinitialsphoneallow_newsletters)r   
propertiesupdateUserszSave user detailszg
    Save details for a specific user, such as their name or contact information, in Label Studio.
    r!   path)namer   locationr   x-fern-audiencesinternal)tagssummaryr   
parametersrequest
extensions)r.   	decoratorlistz
List usersz5List the users that exist on the Label Studio server.userspublic)zx-fern-sdk-group-namezx-fern-sdk-method-namer0   )r2   r3   r   r6   createzCreate new userzCreate a user in Label Studio.zapplication/json   )r2   r3   r   r5   	responsesr6   retrievezGet user infozBGet info about a specific Label Studio user, based on the user ID.   get)r2   r3   r   r4   r5   r=   r6   partial_updatezUpdate user detailszq
        Update details for a specific user, such as their name or contact information, in Label Studio.
        destroyzDelete userz$Delete a specific Label Studio user.deletec                   L    e Zd ZeZ eej                  ej                  ej                  ej                  ej                        Z	g dZ
d Z ed       edddgej                        d	               Z fd
Z fdZ fdZ fdZ fdZd Z fdZ fdZ fdZ xZS )UserAPI)GETPUTPOSTPATCHDELETE)r@   postheadpatchrC   c                 ~    t         j                  j                  | j                  j                  j
                        S )N)organizations)r   objectsfilterr5   useractive_organizationselfs    @/root/env/lib/python3.12/site-packages/label_studio/users/api.pyget_querysetzUserAPI.get_queryset   s*    ||""1B1B1V1V"WW    T)excluderC   rK   )detailmethodspermission_requiredc                 L   |j                   dk(  rOt        |j                        }||j                  _        |j                  j                          t        ddid      S |j                   dk(  r7d |j                  _        |j                  j                          t        d      S y )NrH   rZ   zavatar savedr?   statusrJ      )methodr   FILESrR   r&   saver   )rU   r5   pkr&   s       rV   r&   zUserAPI.avatar   s     >>V#!'--0F"(GLLLLX~6sCC^^x'"&GLLLL3'' (rX   c                 \    | j                   j                  dv rt        S t        |          S )N>   rG   rI   )r5   ra   r   superget_serializer_class)rU   	__class__s    rV   rg   zUserAPI.get_serializer_class   s*    <<"22''w+--rX   c                 ^    t         t        |          }| j                  j                  |d<   |S )NrR   )rf   rE   get_serializer_contextr5   rR   )rU   contextrh   s     rV   rj   zUserAPI.get_serializer_context   s*    =?,,++rX   c                 2    t        t        | 
  |g|i |S N)rf   rE   r+   rU   r5   argskwargsrh   s       rV   r+   zUserAPI.update       Wd*7DTDVDDrX   c                 2    t        t        | 
  |g|i |S rm   )rf   rE   r8   rn   s       rV   r8   zUserAPI.list   s    Wd(B4B6BBrX   c                 2    t        t        | 
  |g|i |S rm   )rf   rE   r;   rn   s       rV   r;   zUserAPI.create   rq   rX   c                     |j                         }| j                  j                  j                  j	                  |       y rm   )rc   r5   rR   rS   add_user)rU   
serializerinstances      rV   perform_createzUserAPI.perform_create   s,    ??$--66x@rX   c                 2    t        t        | 
  |g|i |S rm   )rf   rE   r>   rn   s       rV   r>   zUserAPI.retrieve   s    Wd,WFtFvFFrX   c                    t        t        | 
  |g|i |}| j                         j                  j
                  }|D ]   }||j                  v st        dd|        d|j                  v r^t        j                  j                  |j                  j                        }|j                  |j                  ddd|j                  _        |S )	NrI   zCannot update read-only field: )rZ   r)   )r!      r   )r%   r)   zupdate-notificationsznew-user)rf   rE   rA   rg   Metaread_only_fieldsdatar   r   rP   r@   rR   r!   r%   r)   advanced_json)	rU   r5   ro   rp   resultr}   fieldrR   rh   s	           rV   rA   zUserAPI.partial_update   s    w4WNtNvN  446;;LL% 	bE$&w9XY^X_7`aa	b
 ',,.<<##w||#7D%)%;%;()	*GLL& rX   c                 2    t        t        | 
  |g|i |S rm   )rf   rE   rB   rn   s       rV   rB   zUserAPI.destroy   s    Wd+GEdEfEErX   )__name__
__module____qualname__r   serializer_classr   r   organizations_changeorganizations_viewr\   http_method_namesrW   r	   r   
avatar_anyr&   rg   rj   r+   r8   r;   rx   r>   rA   rB   __classcell__rh   s   @rV   rE   rE   B   s    R &-0000110033 CX 4 4(F!3IcIcd
( e !
(.

ECEAG(F FrX   rE   rK   zReset user tokenz*Reset the user token for the current user.zUser token responsetokenr   )r   responsereset_tokenc                   h    e Zd ZeeefZej                  j                         Z
ej                  Zd Zy)UserResetTokenAPIc                     |j                   }|j                         }t        j                  d|j                   d|j
                          t        d|j
                  id      S )NzNew token for user z is r   r<   r^   )rR   r   loggerdebugrd   keyr   rU   r5   ro   rp   rR   r   s         rV   rK   zUserResetTokenAPI.post  sQ    ||  "*477)4		{CD%)),S99rX   N)r   r   r   r   r   r   parser_classesr   rP   allquerysetr   users_token_anyr\   rK    rX   rV   r   r      s3    0 !*o>N||!H)99:rX   r   zGet user tokenz@Get a user token to authenticate to the API as the current user.rZ   	get_tokenc                   0    e Zd ZefZej                  Zd Zy)UserGetTokenAPIc                     |j                   }t        j                  j                  |      }t	        dt        |      id      S )N)rR   r   r?   r^   )rR   r   rP   r@   r   strr   s         rV   r@   zUserGetTokenAPI.get6  s8    ||!!t!,#e*-c::rX   N)	r   r   r   r   r   r   r   r\   r@   r   rX   rV   r   r     s    0 !]N)99;rX   r   zRetrieve my userzERetrieve details of the account that you are using to access the API.whoamic                   l     e Zd ZeeefZej                  j                         Z
efZeZd Z fdZ xZS )UserWhoAmIAPIc                 .    | j                   j                  S rm   )r5   rR   rT   s    rV   
get_objectzUserWhoAmIAPI.get_objectQ  s    ||   rX   c                 2    t        t        | 
  |g|i |S rm   )rf   r   r@   rn   s       rV   r@   zUserWhoAmIAPI.getT  s    ]D-gGGGGrX   )r   r   r   r   r   r   r   r   rP   r   r   r   permission_classesr   r   r   r@   r   r   s   @rV   r   r   <  sC      !*o>N||!H)++!H HrX   r   rM   zUpdate user hotkeysz=Update the custom hotkeys configuration for the current user.update_hotkeyszGet user hotkeysz?Retrieve the custom hotkeys configuration for the current user.get_hotkeysc                   (    e Zd ZegZeeefZd Z	d Z
y)UserHotkeysAPIc           	         	 |j                   }|j                  xs i }t        d|i      }|j                         rt	        |j
                  d      S t        j                  d|j                   d|j                          t	        di id      S # t        $ rQ}t        j                  d|j                   j                   dt        |              t	        dd	id
      cY d}~S d}~ww xY w)z1Retrieve the current user's hotkeys configurationcustom_hotkeysr~   r?   r^   zInvalid hotkeys data for user : z"Error retrieving hotkeys for user errorz(Failed to retrieve hotkeys configuration  N)rR   r   r   is_validr   validated_datar   warningrd   errors	Exceptionr   r   )rU   r5   ro   rp   rR   r   rv   es           rV   r@   zUserHotkeysAPI.getz  s    	_<<D!006BN*1A>0RSJ""$
 9 9#FF !?y:K\K\J]^_!12 6sCC 	_LL=glloo=NbQTUVQWPXYZW&PQZ]^^	_s%   AB =B 	C+AC& C+&C+c           	      L   	 t        |j                        }|j                         st        d|j                  dd      S |j
                  }|j                  st        ddid      S |j                  d	   |_        |j                  d	g
       t        j                  d|j                          t        |j                  d      S # t        $ rQ}t        j                  d|j
                  j                   dt        |              t        ddid      cY d}~S d}~ww xY w)z/Update the current user's hotkeys configurationr   zInvalid hotkeys configuration)r   detailsi  r^   r   zAuthentication requiredi  r   )update_fieldszUpdated hotkeys for user r?   z Error updating hotkeys for user r   z&Failed to update hotkeys configurationr   N)r   r~   r   r   r   rR   is_authenticatedr   r   rc   r   inford   r   r   r   )rU   r5   ro   rp   rv   rR   r   s          rV   rM   zUserHotkeysAPI.patch  s   	]*=J&&(*IV`VgVg hqtuu<<D ((*C DSQQ #-";";<L"MDII%5$6I7KK3DGG9=>J55cBB 	]LL;GLLOO;LBsSTvhWXW&NOX[\\	]s+   ?C	 &C	 )AC	 		D#ADD#D#N)r   r   r   r   r   r   r   r   r   r@   rM   r   rX   rV   r   r   X  s%    > ** *o>N_$]rX   r   )5__doc__loggingcore.permissionsr   r   django.utils.decoratorsr   drf_spectacular.typesr   drf_spectacular.utilsr   r   r	   rest_frameworkr
   r   rest_framework.authtoken.modelsr   rest_framework.decoratorsr   rest_framework.exceptionsr   rest_framework.parsersr   r   r   rest_framework.permissionsr   rest_framework.responser   rest_framework.viewsr   users.functionsr   users.modelsr   users.serializersr   r   r   r   	getLoggerr   r   _user_schemaINTModelViewSetrE   r   r   RetrieveAPIViewr   r   r   rX   rV   <module>r      s    A 4 . R R - 1 , 6 J J 6 , ( (  k k			8	$  $

 3

 2

 1

 .

 3

 1

 5

 @
C%(V 	Y# $\-=-=\ef
 
" 	YK%,&,!)

		 	Y!4
 '%,&.!)

" 	YX$\-=-=\ef
 '%,&+!)

$ 	Y% $\-=-=\ef
 
 '%,&.!)

, 	Y:$\-=-=\ef
 %,&.!)

"KFh## KF#-%##PKF\ 	Y"@1$#*VX,>"?
 &-&3!)

.	: 	:/.	: 	Y V1$#+fh-?"@
 &-&1!)

.;g ;/.; 	Y"[,-%,&.!)


HH,, 
H
H 	Y%S!)*%,&6!)

 	Y"U)*%,&3!)

.]W .]<.]rX   