
    h              
          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
 d dlmZ d dlmZmZ d dl mZmZmZ d d	lmZ d d
lZd d
lZd d
lZd d
lZ eddg      Zej1                  d       ee      fdedefd       Zej1                  d       eej4                         ee      fdedefd       Zej9                  d       ee      fdedefd       Zej=                  d       ee      fdedefd       Zej=                  d       ee      fdedefd       Z ejC                  d       eej4                         ed
       ee      fdedededefd       Z"y
)    )	APIRouterDepends)get_db)Session)	FileClass)DemarcationClass)Patent)DemarcationListDemarcation)
UploadFileFileHTTPException)datetimeNz/demarcationsDemarcations)prefixtags/demarcationdbc                 R    t        |      j                  | j                        }d|iS Nmessage)r   get_allpage)r   r   datas      UC:\Users\jesus\OneDrive\Desktop\escritorio\newerp\app\backend\routers\demarcations.pyindexr      s(    B''(8(89Dt    z/store	form_datac           	         d}t        |       D ]  }|j                  d      st        | |      }|s%t        j                         j                  d      }t        j                         j                  d d }t        j                  j                  |j                        d   }d}|r	| d| | n| d| }	|dk(  r#| d|	 }
t        |      j                  ||
      }n|dk(  r#| d|	 }t        |      j                  ||      }n|dk(  r#| d|	 }t        |      j                  ||      }nw|d	k(  r#| d|	 }t        |      j                  ||      }nO|d
k(  r#| d|	 }t        |      j                  ||      }n'|dk(  r"| d|	 }t        |      j                  ||      }|dz  } t        |      j!                  | 
       y )N   file_%Y_%m_%d_%H_%M_%S   r   _               )dir
startswithgetattrr   nowstrftimeuuiduuid4hexospathsplitextfilenamer   uploadr   store)r   r   i	attr_namefile	timestamp	unique_idfile_extensionfile_category_nameunique_filenameremote_path1r   remote_path2remote_path3remote_path4remote_path5remote_path6s                    r   r8   r8      s   
 	
A^ 	(9i0D$LLN334GH	 JJL,,Ra0	!#!1!1$--!@!C%2"P^YKqN;K"Lgpfqqrs|r}d~6&8%9?:K#LL'm224FG!V&8%9?:K#LL'm224FG!V&8%9?:K#LL'm224FG!V&8%9?:K#LL'm224FG!V&8%9?:K#LL'm224FG!V&8%9?:K#LL'm224FGQ;@ Ry,lT`bnp|}r   z/delete/{id}idc                 >    t        |      j                  |       }d|iS r   )r   delete)rG   r   r   s      r   rI   rI   C   s"    r"))"-Gwr   z/download/{id}c                     t        |      j                  |       }t        j                  |      }|d   d   }| }t	        |      j                  |      }t        j                  |      j                  d      }||dS )Npatent_datasupportzutf-8)	file_name	file_data)	PatentClassgetjsonloadsr   downloadbase64	b64encodedecode)rG   r   rK   rM   remote_pathfile_contentsencoded_files          r   rS   rS   I   s     b/%%b)K**[)KM*95I KK bM**;7M ##M299'BL ! r   z
/edit/{id}c                     	 t        |      j                  |       }|st        dd      d|iS # t        $ r}t        ddt	        |             d }~ww xY w)N  u   Demarcación no encontradastatus_codedetailr     zError al obtener el iva: )r   rP   r   	Exceptionstr)rG   r   demarcation_dataes       r   editrd   a   sm    Z+B/33B7  C8TUU +,, Z6OPSTUPVx4XYYZs   ,/ 	AAAz/update/{id}rL   c                    	 t        |      j                  |       }|st        dd      t        j                  |      }|d   d   }|r| nd }|}|rt        j                         j                  d      }t        j                         j                  d d }	d|j                  v r|j                  j                  d      d	   nd
}
|
r
| d|	 d|
 n| d|	 }| }t        |      j                  ||       |rt        |      j                  |       t        |      j!                  | ||       ddiS # t"        $ r}t        ddt%        |             d }~ww xY w)Nr[   zIva no encontrador\   rK   rL   r#   r$   . r%   r   z Patente actualizada exitosamenter_   zError al actualizar el iva: )rO   rP   r   rQ   rR   r   r.   r/   r0   r1   r2   r6   splitr   r7   rI   updater`   ra   )rG   r   rL   r   rK   previous_file_nameremote_path_previousnew_file_namer<   r=   r>   remote_path_newrc   s                r   rj   rj   q   sq   "]!"o))"-C8KLLjj- )7	B:L"4!5RV* //0CDI

((!,I@CwGWGW@WW--33C8<]_NKYyk9+Q~6FGbkallmnwmx_yM!.O bM  /: $"$$%9: 	Br9m<=>> ]6RSVWXSYRZ4[\\]s   D/D2 2	E;EE)#fastapir   r   app.backend.db.databaser   sqlalchemy.ormr   app.backend.classes.file_classr   %app.backend.classes.demarcation_classr   app.backend.schemasr	   r
   r   r   r   r   r   r0   rQ   r3   rT   demarcationspostr   as_formr8   rI   intrP   rS   rd   putrj    r   r   <module>r{      s   & * " 4 B & < 3 3    	 
	
 36=fo  G  
 8$[%8%89&/&~&~&~ &~P ^$!(  c  w   % 
 "#$+FO  '  $. , ' ZS Zg Z  Z .!  /t*&/	(](](] (] 		(] "(]r   