
    	]j                        d Z ddlZddlZddlZddlZddlmZ ddlZddl	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 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)  ejT                  e+      Z,da-d Z.d Z/d Z0d Z1 G d de)      Z2d Z3d Z4d Z5d Z6d Z7d Z8e# e%ddg      d               Z9y) 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)FileWrapper)utils)	all_flagsflag_setget_feature_file_path)generate_time_series_json)collect_versions)	find_file)settings)logout)HttpResponseHttpResponseForbiddenJsonResponse)redirectrenderreverse)csrf_exempt)require_http_methods)extend_schema)APIViewc                 $   | j                   }|j                  re|j                  -d| j                  vrt	        |        t        t        d            S t        d|      rt        | d      S t        t        d            S t        t        d            S )Norganization_pkz
user-login)fflag_all_feat_dia_1777_ls_homepage_shortzhome/home.htmlzprojects:project-index)	useris_authenticatedactive_organizationsessionr   r   r   r   r   )requestr   s     A/root/env/lib/python3.12/site-packages/label_studio/core/views.pymainr       s    <<D##+0A0X7OGL122 ?F'#344G$<=>> GL)**    c           
         | j                   dk(  }t        |      }| j                   dk(  rt        t        dd      sy| j                  j
                  rct        t              D ci c]G  }|j                  d      s4t        t        t        |      d      s|t        t        t        |            I c}|d<   t        j                  |dd	      }t        d
|z   dz         S t        |      S c c}w )zGet platform versionz	/version/)forceCLOUD_INSTANCEF___call__r      )indentensure_ascii<pre></pre>)pathr	   getattrr   r   is_superuserdir
startswithhasattrstrjsondumpsr   r   )r   	http_pageresultkeys       r   version_pager8   2   s    
 +II.F ||{"x!159gll>W>W x="~~c*778S;QS]3^ S3/00"F: F15AGf,x788F##"s   #AC)c                 l    t         j                  d       t        t        j                  ddi            S )zSystem health infozGot /health request.statusUP)loggerdebugr   r3   r4   r   s    r   healthr?   J   s(    
LL'(

Hd#3455r!   c                     t        d      S )z!Empty page for metrics evaluation r   r>   s    r   metricsrC   P   s    r!   c                   6    e Zd ZdZdZdZ ed      d        Zy)TriggerAPIErrorz500 response for testing T)excludec                     t        d      )Ntest)	Exception)selfr   s     r   getzTriggerAPIError.get[   s    r!   N)__name__
__module____qualname____doc__authentication_classespermission_classesr   rL   rF   r!   r   rE   rE   U   s(    "4   ! r!   rE   c                 ~    t         j                  j                         }t        t	        j
                  |      d      S )zGet last editor files   r:   )r   commonfind_editor_filesr   r3   r4   )r   responses     r   editor_filesrY   `   s*    ||--/H

8,S99r!   c                    | j                   j                  dd      }| j                   j                  dd      j                  d      }| j                   j                  d      }| j                   j                  dd      }|j                  dd      }d	ddd
d}||v r||   }d}t	        d |g|z   D              rd}|s5t        |D cg c]  }t        |       c}dgz         }t        d|dz         }t        |||      }	t        j                  j                  |	      j                  d||      j                  d      }
d}t        |
d      }d| d|d<   ||d<   |S c c}w )z(Generate time series example for previewtimerA   values,tfsepz\t	. )dotcommatabspaceTc              3   <   K   | ]  }|j                           y w)N)isdigit).0ns     r   	<genexpr>z&samples_time_series.<locals>.<genexpr>u   s     
>1199;
>s   Fr      )indexheaderr_   utf-8ztime-series.csvzapplication/csvcontent_typezattachment; filename=""zContent-Dispositionfilename)GETrL   splitreplaceallmaxintranger   pd	DataFrame	from_dictto_csvencoder   )r   time_columnvalue_columnstime_format	separatoraliasesrn   vmax_column_ntscsv_datars   rX   s                r   samples_time_seriesr   f   sc   ++//&"-KKKOOHb177<M++//$'K s+I!!%.ICsCGGI&	 F

> =
>> M:qCF:aS@Aa!12	";{	KB||%%b)00uVQZ0[bbcjkH !HH3DEH(>xj&JH"##HZO ;s   Ec                    t         4t        t        d      d      5 }t        j                  |      a ddd       | j
                  j                  dd      }| j
                  j                  dd      }g }t         D ]  }|j                  ||d   ||d   i        t        t        j                  |      d	
      S # 1 sw Y   xY w)z'Generate paragraphs example for previewNzparagraphs.jsonro   )encodingnameKeyauthortextKeytextapplication/jsonrp   )
_PARAGRAPH_SAMPLEopenr
   r3   loadrt   rL   appendr   r4   )r   fname_keytext_keyr6   lines         r   samples_paragraphsr      s      )-.A 	-Q $		!	-{{y(3H{{y&1HF! Jxh4<HIJ 

6*9KLL	- 	-s   B;;Cc                 4   d}d}	 t        j                  |ddddd      }|j                          t        |j                  d      S # t        $ rE i }d}||j                  |d	<   |j
                  }t        t        j                  |      d|
      cY S w xY w)z]Fetch live tips from github raw liveContent.json to avoid caching and client side CORS issueszhttps://raw.githubusercontent.com/HumanSignal/label-studio/refs/heads/develop/web/apps/labelstudio/src/components/HeidiTips/liveContent.jsonNzno-cacher   )zCache-ControlzContent-TypeAccept   )headerstimeouti  detail)rq   r:   rp   )
requestsrL   raise_for_statusrJ   reasonstatus_coder   r3   r4   content)r   urlrX   r   r   s        r   
heidi_tipsr      s     YCHf<<&0BT`rs
 	!!# ((7IJJ  f (GH"..KDJJw/>PYdeefs   ,A	 	ABBc                 r     t         j                  j                  t        j                           fd}|S )zRLoad any file, replace {{HOSTNAME}} => settings.HOSTNAME, send it as http responsec                 d   t        d      5 }|j                         }|j                  dt        j                        }t        j                         }|j                  |       |j                  d       t        |      }t        |      }t        |      |d<   |cd d d        S # 1 sw Y   y xY w)Nrz{{HOSTNAME}}r   rp   zContent-Length)r   readrv   r   HOSTNAMEioStringIOwriteseekr   r   len)r   r   bodyoutwrapperrX   rq   path_on_disks         r   
serve_filez2static_file_with_host_resolver.<locals>.serve_file   s    ,$ 	668D<<0A0ABD++-CIIdOHHQK!#&G#G,GH),TH%&	 	 	s   BB&&B/)osr,   joinr   STATIC_ROOT)r   rq   r   s   `` r   static_file_with_host_resolverr      s*    77<< 4 4lCL r!   c                 z   | j                   }|j                  s
t               S t        | j                         }t        j
                  t        j                  t               t        j                  t        t        d      rt        j                  nd d|d<   t        dt        j                  |d      z   dz   d	      S )
Nr$   )FEATURE_FLAGS_DEFAULT_VALUEFEATURE_FLAGS_FROM_FILEFEATURE_FLAGS_FILEVERSION_EDITIONr$   z$systemr*      )r(   r+   rT   rU   )r   r   r   r   r   r   r   r   r   r1   r$   r   r3   r4   )r   r   flagss      r   feature_flagsr      s    <<D  $&&gll#E'/'K'K#+#C#C35#335<XGW5X(11^bE) $**U1"==HQTUUr!   POSTrt   c                     t        d      S )zfLightweight endpoint to collect usage metrics from the frontend only when COLLECT_ANALYTICS is enabled   rU   rB   r>   s    r   collect_metricsr      s     s##r!   ):rP   r   r3   loggingr   wsgiref.utilr   pandasr{   r   corer   core.feature_flagsr   r   r   core.label_configr   core.utils.commonr	   core.utils.ior
   django.confr   django.contrib.authr   django.httpr   r   r   django.shortcutsr   r   r   django.views.decorators.csrfr   django.views.decorators.httpr   drf_spectacular.utilsr   rest_framework.viewsr   	getLoggerrM   r<   r   r    r8   r?   rC   rE   rY   r   r   r   r   r   r   rF   r!   r   <module>r      s    	   	 $    I I 7 . #   & I I 6 6 4 = / (			8	$  +&$06
 g  :DM"K6*V" vuo&$ ' $r!   