
    	]j$                        d Z ddlZddlZddl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 ddlmZmZ ddlmZ dd	lmZmZ dd
lmZmZ ddlmZmZmZ ddlmZ  ej>                  e       Z! G d dejD                        Z# G d dejH                        Z% G d dejD                        Z& G d dejH                        Z' G d dejP                        Z) G d dejP                        Z* G d dejV                        Z, ed       G d dejV                               Z- ed       G d d ej\                               Z/ G d! d"e,      Z0 G d# d$e,      Z1 G d% d&e/      Z2 G d' d(e/      Z3y))zThis 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)	read_yaml)settings)extend_schema)ExportStorageSerializerImportStorageSerializer)Project)genericsstatus)NotFoundValidationError)
FormParser
JSONParserMultiPartParser)Responsec                   \    e Zd Z eej
                  ej                        Zee	e
fZeZd Zy)ImportStorageListAPIGETPOSTc                    | j                   j                  j                  d      }|st        d      t	        j
                  t        |      }| j                  | j                   |       | j                  j                  j                  }|j                  j                  |j                        }|j                  |       |S Nprojectz%query parameter "project" is required)pk)
project_idrequestquery_paramsgetr   r   get_object_or_404r
   check_object_permissionsserializer_classMetamodelobjectsfilteridensure_storage_statusesself
project_pkr   StorageClassstoragess        F/root/env/lib/python3.12/site-packages/label_studio/io_storages/api.pyget_querysetz!ImportStorageListAPI.get_queryset       \\..229=
!"IJJ,,WD%%dllG<,,1177''..'**.E 	,,X6    N)__name__
__module____qualname__r   r   storages_viewstorages_changepermission_requiredr   r   r   parser_classesr	   r#   r0    r2   r/   r   r      s8    -)),, !*o>N.r2   r   c                        e Zd ZdZ eej                  ej                  ej                  ej                        Ze	e
efZeZ ed       fd       Z xZS )ImportStorageDetailAPI"RUD storage by pk specified in URLr   PATCHPUTDELETETexcludec                 2    t        t        | 
  |g|i |S N)superr<   putr+   r   argskwargs	__class__s       r/   rG   zImportStorageDetailAPI.put:        +T6wPPPPr2   )r3   r4   r5   __doc__r   r   r6   r7   r8   r   r   r   r9   r	   r#   r   rG   __classcell__rK   s   @r/   r<   r<   .   b    ,-))--++..	 !*o>N.4 Q !Qr2   r<   c                   b    e Zd Z eej
                  ej                        Zee	e
fZeZd Zd Zy)ExportStorageListAPIr   c                    | j                   j                  j                  d      }|st        d      t	        j
                  t        |      }| j                  | j                   |       | j                  j                  j                  }|j                  j                  |j                        }|j                  |       |S r   r   r*   s        r/   r0   z!ExportStorageListAPI.get_querysetH   r1   r2   c                     |j                   j                  di |j                  }	 |j                          |j                         }t        j                  r|j                          y y # t        $ r}t        |      d }~ww xY w)Nr:   )
r$   r%   validated_datavalidate_connection	Exceptionr   saver   SYNC_ON_TARGET_STORAGE_CREATIONsync)r+   
serializerinstanceexcstorages        r/   perform_createz#ExportStorageListAPI.perform_createV   sv     ):??((E:+D+DE	'((* //#33LLN 4	  	'!#&&	's   A* *	B3A>>BN)r3   r4   r5   r   r   r6   r7   r8   r   r   r   r9   r   r#   r0   r_   r:   r2   r/   rR   rR   ?   s=    -)),, !*o>N.r2   rR   c                        e Zd ZdZ eej                  ej                  ej                  ej                        Ze	e
efZeZ ed       fd       Z xZS )ExportStorageDetailAPIr=   r>   TrB   c                 2    t        t        | 
  |g|i |S rE   )rF   ra   rG   rH   s       r/   rG   zExportStorageDetailAPI.putp   rL   r2   )r3   r4   r5   rM   r   r   r6   r7   r8   r   r   r   r9   r   r#   r   rG   rN   rO   s   @r/   ra   ra   d   rP   r2   ra   c                   L    e Zd Z eej
                        Zeee	fZ
eZd Zd Zy)ImportStorageSyncAPIr   c                 v    | j                   j                  j                  }|j                  j	                         S rE   r#   r$   r%   r&   all)r+   ImportStorageClasss     r/   r0   z!ImportStorageSyncAPI.get_queryset}   /    !2277==!))--//r2   c                 N   | j                         }|j                  s6ddt        |j                         di}t	        t
        j                  |      S |j                          |j                          |j                          t	        | j                  |      j                        S NmessagezStorage z is not synchronizable)r   data
get_objectsynchronizablestrr(   r   r   HTTP_400_BAD_REQUESTrV   rZ   refresh_from_dbr#   rn   r+   r   rI   rJ   r^   response_datas         r/   postzImportStorageSyncAPI.post       //#%%&(3wzz?2CCY(Z[M6#>#>]SS##%!--g6;;<<r2   N)r3   r4   r5   r   r   storages_syncr8   r   r   r   r9   r	   r#   r0   rw   r:   r2   r/   rd   rd   u   4    -** !*o>N.0	=r2   rd   c                   L    e Zd Z eej
                        Zeee	fZ
eZd Zd Zy)ExportStorageSyncAPIre   c                 v    | j                   j                  j                  }|j                  j	                         S rE   rg   )r+   ExportStorageClasss     r/   r0   z!ExportStorageSyncAPI.get_queryset   rj   r2   c                 N   | j                         }|j                  s6ddt        |j                         di}t	        t
        j                  |      S |j                          |j                          |j                          t	        | j                  |      j                        S rl   ro   ru   s         r/   rw   zExportStorageSyncAPI.post   rx   r2   N)r3   r4   r5   r   r   ry   r8   r   r   r   r9   r   r#   r0   rw   r:   r2   r/   r|   r|      rz   r2   r|   c                   4    e Zd Zej                  ZeeefZ	d Z
y)StorageValidateAPIc                 H    ddl m}  ||| j                         t               S )N   validate_storage_instance)	functionsr   r#   r   )r+   r   rI   rJ   r   s        r/   createzStorageValidateAPI.create   s    8!'4+@+@Azr2   N)r3   r4   r5   r   r7   r8   r   r   r   r9   r   r:   r2   r/   r   r      s    )99 *o>Nr2   r   TrB   c                   d     e Zd Zej                  ZeeefZ	dZ
d fd	Z ed      d        Z xZS )ImportStorageListFilesAPINc                 2    || _         t        |   |i | y rE   )r#   rF   __init__)r+   r#   rI   rJ   rK   s       r/   r   z"ImportStorageListFilesAPI.__init__   s     0$)&)r2   TrB   c                 `   ddl m}  ||| j                        }t        |j                  j                  dt        j                              }	 g }t        j                         }d}	|j                         D ]x  }
|j                  |j                  |
             t        |      |k\  r|j                  d d d d        n3t        j                         |z
  |	kD  sc|j                  dd d d        n t        d|i      S # t        $ r&}t        j!                  d|       t#        d	      d }~ww xY w)
Nr   r   limit   )keylast_modifiedsizez$... storage scan timeout reached ...fileszError listing storage files: %szFailed to list storage files)r   r   r#   intrn   r    r   DEFAULT_STORAGE_LIST_LIMITtimeiter_objectsappendget_unified_metadatalenr   rW   logger	exceptionr   )r+   r   rI   rJ   r   r\   r   r   
start_timetimeout_secondsobjectr]   s               r/   r   z ImportStorageListFilesAPI.create   s   8,Wd6K6KLGLL$$Wh.Q.QRS	BEJ O"//1 X::6BC u:&LLd!ST 99;+o=LL)Obfpt!uv We,-- 	B>D!"@AA	Bs   B
C> $C> >	D-!D((D-rE   )r3   r4   r5   r   r7   r8   r   r   r   r9   r#   r   r   r   rN   rO   s   @r/   r   r      sC     *99 *o>N* 4 B !Br2   r   c                   V    e Zd Zej                  ZeeefZ	dZ
 ed      d        Zd Zy)StorageFormLayoutAPINTrB   c                    t         j                  j                  t         j                  j                  t	        j
                  | j                              d      }t         j                  j                  |      s"t        d| j                  j                         t        |      }| j                  |      }t        || j                           S )Nzform_layout.ymlz#"form_layout.yml" is not found for )ospathjoindirnameinspectgetfilerK   existsr   r3   r   post_process_formr   storage_type)r+   r   rI   rJ   form_layout_fileform_layouts         r/   r    zStorageFormLayoutAPI.get   s    77<<8W(XZklww~~./@AXAX@YZ[[ 01,,[9D$5$5677r2   c                     |S rE   r:   )r+   r   s     r/   r   z&StorageFormLayoutAPI.post_process_form   s    r2   )r3   r4   r5   r   r7   r8   r   r   r   r9   r   r   r    r   r:   r2   r/   r   r      s=     *99 *o>NL4 8 !8r2   r   c                       e Zd ZeZy)ImportStorageValidateAPIN)r3   r4   r5   r	   r#   r:   r2   r/   r   r          .r2   r   c                       e Zd ZeZy)ExportStorageValidateAPIN)r3   r4   r5   r   r#   r:   r2   r/   r   r      r   r2   r   c                       e Zd ZdZy)ImportStorageFormLayoutAPIImportStorageNr3   r4   r5   r   r:   r2   r/   r   r          "Lr2   r   c                       e Zd ZdZy)ExportStorageFormLayoutAPIExportStorageNr   r:   r2   r/   r   r      r   r2   r   )4rM   r   loggingr   r   core.permissionsr   r   core.utils.ior   django.confr   drf_spectacular.utilsr   io_storages.serializersr   r	   projects.modelsr
   rest_frameworkr   r   rest_framework.exceptionsr   r   rest_framework.parsersr   r   r   rest_framework.responser   	getLoggerr3   r   ListCreateAPIViewr   RetrieveUpdateDestroyAPIViewr<   rR   ra   GenericAPIViewrd   r|   CreateAPIViewr   r   RetrieveAPIViewr   r   r   r   r   r:   r2   r/   <module>r      sP     	  A #   / T # + ? J J ,			8	$855 0QXBB Q""855 "JQXBB Q"=822 =0=822 =0	// 	 t&B 6 6 &B &BR t833  (/1 //1 /#!5 ##!5 #r2   