
    U+i!                     <   d dl mZmZmZ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 d dlmZ d d	lmZ d d
lmZ d dlZd dlZ eddg      Zej9                  d       ed       edddd       ee      fdedededee   def
d       Zej?                  d       ee      fdedefd       Z ej?                  d       ed       ed       ee      fdee   dee   defd       Z!ej?                  d        ee      fdededefd!       Z"y)"    )	APIRouterstatus
UploadFileFileDependsQuery)JSONResponse)FolderClass)StudentClass)get_db)get_current_active_user)	UserLogin)Optional)Session)PathNz/student-document-fileszStudent Document Files)prefixtagsz"/upload/{student_id}/{document_id}.i  i4  u5   Año del período escolar (query; interceptor/cookie))geledescription
student_iddocument_idfileperiod_yeardbc                   K   	 t        |      }|j                  |       }t        |t              rg|j                  d      s|j                  d      dk(  rBt	        t
        j                  d|j                  d      xs |j                  dd      dd      S |j                  r-t        |j                        j                  j                         nd	}t        j                         j                  dd
 }d|  d| d| | }	t        d      }
|
j                  dd       |
|	z  }|j                          d{   }t!        |d      5 }|j#                  |       ddd       t%        |      }|j'                  | ||	|t)        |      nd      }t        |t              ri|j                  d      dk(  rU|j+                         r	 t-        j.                  |       t	        t
        j0                  d|j                  dd      dd      S t	        t
        j2                  dd|j                  d      |j                  d      | ||	t)        |      |j                  dd      S 7 )# 1 sw Y   xY w#  Y xY w# t4        $ rE}|j7                          t	        t
        j0                  ddt)        |       dd      cY d}~S d}~ww xY ww)u   
    Sube un archivo de documento para un estudiante y documento específico.
    Maneja el control de versiones automáticamente.
    errorr     messagezEstudiante no encontradoNr   r   datastatus_codecontent    student__doc__z#files/system/student_document_filesT)parentsexist_okwb)r   r   	file_pathr     z1Error guardando registro del archivo de documento   z(Archivo de documento subido exitosamenteid
version_id)r0   r1   r   r   filenamer-   original_filenamezError subiendo archivo: )r   get
isinstancedictr	   r   HTTP_404_NOT_FOUNDr2   r   suffixloweruuiduuid4hexmkdirreadopenwriter
   storestrexistsosunlinkHTTP_500_INTERNAL_SERVER_ERRORHTTP_201_CREATED	Exceptionrollback)r   r   r   r   r   student_servicestudent_resultfile_extension	unique_idunique_filename
upload_dirr-   r$   fdocument_file_servicestore_resultes                    R/var/www/pie360backend.cl/public_html/app/backend/routes/student_document_files.pyupload_document_filerU      s    "T
&r*(,,Z8nd+1C1CG1LP^PbPbckPlpwPw"55!-11':wn>P>PQZ\v>w   @D}}dmm,3399;RT JJL$$Ra(	$ZLk]!I;~N^_ ?@
50	 		#)T" 	aGGG	 !,B,22!#%,7,CK(	 3 
 lD)l.>.>x.HG.S!IIi(  "AA!+//	;no   //E&**40"."2"2<"@",#. /!$Y)-
 	
; $	 	"6  	

==5c!fX>
 	
	
s   KBI6 KBI6 6I7I6 I"A)I6 I/ .I6 KAI6 KI6 "I,'I6 /I31I6 6	K?:J?9K:K?KKz/{id}r0   c           	        K   	 t        |      }|j                  |       }t        |t              rC|j                  d      dk(  r/t	        t
        j                  d|j                  dd      dd      S t	        t
        j                  d	d
|d      S # t        $ r5}t	        t
        j                  ddt        |       dd      cY d}~S d}~ww xY ww)z4
    Obtiene un archivo de documento por su ID.
    r   r   r   r   z"Archivo de documento no encontradoNr    r"      zArchivo de documento encontrador.   z'Error obteniendo archivo de documento: )r
   r4   r5   r6   r	   r   r7   HTTP_200_OKrH   rF   rB   )r0   r   rQ   resultrS   s        rT   get_document_filerZ   z   s     
 +B&**2.fd#

8(<(G"55!%zz)5YZ   **<
 	
  
==DSVHM
 	

sA   CA.B 2C3B C	C*CCCCCz/listc           	        K   	 t        |      }|j                  | |      }t        |t              rC|j	                  d      dk(  r/t        t        j                  d|j	                  dd      dd      S t        t        j                  d	|rd
nd|d      S # t        $ r5}t        t        j                  ddt        |       dd      cY d}~S d}~ww xY ww)zk
    Lista los archivos de documentos almacenados.
    Puede filtrarse por student_id y/o document_id.
    r   r   r.   r   z%Error listando archivos de documentosNr    r"   rW   "Archivos de documentos encontrados)No hay archivos de documentos registradosz'Error listando archivos de documentos: )r
   get_allr5   r6   r4   r	   r   rF   rX   rH   rB   r   r   r   rQ   r!   rS   s         rT   list_document_filesr`      s     
 +B$,,ZEdD!dhhx&8G&C"AA!#xx	3Z[   **CG?Mx
 	
  
==DSVHM
 	

A   CA/B 3C4"B C	C *C
CCCCz,/student/{student_id}/document/{document_id}c           	        K   	 t        |      }|j                  | |      }t        |t              rC|j	                  d      dk(  r/t        t        j                  d|j	                  dd      dd      S t        t        j                  d	|rd
nd|d      S # t        $ r5}t        t        j                  ddt        |       dd      cY d}~S d}~ww xY ww)u   
    Obtiene todos los archivos de documento para un estudiante y documento específico.
    Útil para ver el historial de versiones.
    r   r   r.   r   z'Error obteniendo archivos de documentosNr    r"   rW   r\   r]   z)Error obteniendo archivos de documentos: )r
   get_by_student_and_documentr5   r6   r4   r	   r   rF   rX   rH   rB   r_   s         rT   *get_document_files_by_student_and_documentrd      s     
 +B$@@[YdD!dhhx&8G&C"AA!#xx	3\]   **CG?Mx
 	
  
==Fs1vhO
 	

ra   )#fastapir   r   r   r   r   r   fastapi.responsesr	   /app.backend.classes.student_document_file_classr
   !app.backend.classes.student_classr   app.backend.db.databaser   app.backend.auth.auth_userr   app.backend.schemasr   typingr   sqlalchemy.ormr   pathlibr   rD   r:   student_document_filespostintrU   r4   rZ   r`   rd        rT   <module>rt      s   G G * G : * > )  "  	 "$
"	# 
 AB Cy!&K	" &/d
d
d
 d
 #	d
 	d
 Cd
L G$ &/&
&
&
 %&
P G$ %d!&t&/(
(
#(
 	(
 %(
T JK &/(
(
(
 	(
 L(
rs   