
    
Ei                        d dl mZmZ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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mZ  ee       Z! eddg      Z"e"jG                  deejH                  dd       edd       edd       edd       edd       edd       ee       e eejJ                  ejL                              fde'de'dede(de(de
defd        Z)e"jU                  d!ee   ejV                  d"d#       ed$d$%       ed&d$d'(       ed)       ed*       ed+       ee       e eejJ                  ejL                              fd,e(d-e(d.e'd)z  d/e'd)z  d0e'd)z  de
defd1       Z,e"jU                  d2eejV                  d3d4       ee       e eejJ                  ejL                              fd5e(de
defd6       Z-e"j]                  d7eejV                  d8d9       ed)d:       ed)d;       edd       edd       ee       e eejJ                  ejL                              fd5e(de'd)z  d<ed)z  de(de(de
defd=       Z/y))>    )	APIRouterFile
UploadFileDependsHTTPExceptionstatusFormQuery)Session)get_session)require_roles)UserRole)ProductTemplateReadProductTemplateListResponse)PaginatedResponse)ProductTemplateService)setup_logger)ERR_PSD_PROCESSING_FAILEDERR_RETRIEVE_PSDERR_UPDATE_TEMPLATEERR_DELETE_TEMPLATEz/api/product-templateszProduct Templates)prefixtagsz/uploadz#Upload a new product template (PSD)zKUpload a PSD file, create a DB record and generate an optional PNG preview.)response_modelstatus_codesummarydescription.z$Human-friendly name of the template.)r   z(Unique name for internal identification.zPSD file to upload.z,Generated image width for final story-board.z-Generated image height for final story-board.nameunique_namepsd_filewidthheightdbcurrent_userc           	        K   	 t        j                  | |||||j                  |       d{   }|S 7 # t        $ r  t        $ r5 t
        j                  d       t        t        j                  t              w xY ww)z=
    Upload a PSD product template and persist metadata.
    )r   r    r!   r"   r#   user_idsessionNz"Unexpected error during PSD uploadr   detail)
r   upload_templateidr   	Exceptionlogger	exceptionr   HTTP_500_INTERNAL_SERVER_ERRORr   )r   r    r!   r"   r#   r$   r%   tpls           R/var/www/html/story-book/Story-Book-python-api/app/api/routes/product_templates.pyupload_product_templater3      s     &q*::# OO
 
 

   q=>(M(MVoppqs&   B )6 46 B 6 AA==B z/listzList product templatesz'Retrieve non-deleted product templates.   )ge
      )r5   leN
created_atdescpagelimitsearchsort_byorderc                 
   	 |dvrt        dd      |dvrt        dd      t        j                  || ||||      }|S # t         $ r  t        $ r1 t        j                  d       t        t        j                  d	      w xY w)
z Return active product templates.)r,   r   r9   
updated_ati  zInvalid sort_by field)ascr:   zInvalid order)r(   r;   r<   r=   r>   r?   z&Database error while listing templateszFailed to retrieve templatesr)   )r   r   list_templatesr-   r.   r/   r   r0   )r;   r<   r=   r>   r?   r$   r%   results           r2   list_product_templatesrE   9   s    "
DD%<=='_55'66
   
AB==1
 	

s
   <? ABz/detail/{template_id}zGet product template detailz)Retrieve a single product template by ID.template_idc                     	 t        j                  | |      }|S # t        $ r  t        $ r5 t        j                  d       t        t        j                  t              w xY w)z@
    Retrieve a template by ID, ensuring it is not deleted.
    z1Unexpected error while retrieving template detailr)   )	r   get_template_by_idr   r-   r.   r/   r   r0   r   )rF   r$   r%   r1   s       r2   get_product_templaterI   a   s`    

$77RH
  
LM==#
 	

s
    AA!z/update/{template_id}zUpdate a product templatez:Update name and/or replace PSD file (regenerates preview).zNew name for template.zOptional replacement PSD file.new_psdc                    K   	 t        j                  | |||||       d{   }|S 7 # t        $ r  t        $ r5 t        j                  d       t        t        j                  t              w xY ww)z3Update the product template's metadata and/or file.)rF   r   rJ   r"   r#   r(   Nz(Unexpected error while updating templater)   )	r   update_templater   r-   r.   r/   r   r0   r   )rF   r   rJ   r"   r#   r$   r%   r1   s           r2   update_product_templaterM   |   s     "k*::#
 
 

   kCD(M(MVijjks&   A5+ )+ A5+ AA22A5)0fastapir   r   r   r   r   r   r	   r
   sqlmodelr   app.db.sessionr   app.api.depsr   app.models.userr   r   app.schemas.product_templater   r   app.schemas.commonr   %app.services.product_template_servicer   app.core.loggingr   app.core.constantsr   r   r   r   __name__r.   routerpostHTTP_201_CREATED
SUPERADMINADMINstrintr3   getHTTP_200_OKrE   rI   patchrM        r2   <module>re      sG   \ \ \  & & & Y 0 H )  
h		2:M9N	O&''1]   S&LMC-WX1FGc'UVs(WX+& t

!KLq
qq q 	q
 q 	q qq: $%89""$9   aAraC(t-f+& t

!KL



 $J
 4Z	

 :
 	
 

B &"");   +& t

!KL


 

( &""'L   D.FG!%d8X!Yc'UVs(WX+& t

!KLkk
*k $k 	k
 k 	k kkrd   