
    ~R:i                        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
 d dl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 d dlmZ d dlmZ d dlmZ d dl m!Z!m"Z"  ee#      Z$ e       Z% G d d      Z&y)    )Path)Optional)
UploadFileHTTPException)Session)uuid4N)ProductTemplate)ProductTemplateRead)PaginatedResponse)ProductTemplateRepository)validate_base_filesave_upload_file_streamedsafe_delete)generate_psd_preview)extract_psd_ratio)setup_logger)get_settings)ERR_NOT_FOUNDERR_FILE_INVALIDc                       e Zd Zedededededef
d       Z	edede
e   de
e   dedef
d	       Zeded
edede
e   de
e   de
e   fd       Zedededefd       Zededefd       Zy)ProductTemplateServicenamepsd_fileuser_idsessionreturnc                   K   t        |       t        t        j                        }|j	                  dd       	 t        ||       d {   }|t               j                   dz  }t        ||      }|rt        |      nd }	t        |      }
t!        | |t        |      |	|
      }t#        j$                  ||      }|S 7 u# t        $ r+}t        j                  d       t        dt              d }~ww xY ww)NTparentsexist_okzFailed to save PSD file  status_codedetail_preview.png)r   uploaded_by	file_pathpreview_png_pathratio)r   r   settingsPSD_FILES_DIRECTORYmkdirr   	Exceptionlogger	exceptionr   r   r   hexr   strr   r	   r   create)r   r   r   r   	store_dirr'   excpreview_pathpreview_resultpreview_relr)   tpls               W/var/www/html/story-book/Story-Book-python-api/app/services/product_template_service.pyupload_templatez&ProductTemplateService.upload_template   s      	8$556	t4	J7)LLI !egkk],#??-iF-;c.) "), )n(
 (..w<
/ M 	J67C8HII	Js;   8C9C 
C C A1C9 C 	C6&C11C66C9template_idnew_psdc                   K   t        j                  ||       }|r|j                  rt        dt              |r||_        |rt        |       t        t        j                        }|j                  dd       	 t        ||       d {   }|t               j                   dz  }t!        ||      }|rt#        |      nd }	t%        |      }
t'        t        |j(                               |j*                  rt'        t        |j*                               t#        |      |_        |	|_        |
|_        t        j.                  ||      }|S 7 # t        $ r t        dt              w xY ww)N  r"   Tr   r!   r%   )r   	get_by_id
is_deletedr   r   r   r   r   r*   r+   r,   r   r-   r   r   r0   r   r1   r   r   r'   r(   r)   update)r;   r   r<   r   r8   r3   new_pathr5   r6   r7   r)   s              r9   update_templatez&ProductTemplateService.update_templateA   s=     (11';GcnnCFFCHw'X99:IOOD4O8N!:7I!NN
 %%'++l'CCL1(LIN1?#n-TK &h/E S]]+,##D!5!567MCM#.C CI'..w<
- O N#<LMMNs1   A8E-;E 
EE B>E-E E**E-pagelimitsearchsort_byorderc                 j    t        j                  | |||||      \  }}t        t           ||||      S )z*Return all active (not deleted) templates.)r   rD   rE   rF   rG   rH   )totalrD   rE   items)r   list_advancedr   r
   )r   rD   rE   rF   rG   rH   rK   rJ   s           r9   list_templatesz%ProductTemplateService.list_templateso   sK     1>>
u !!45	
 	
    c                 p    t        j                  ||       }|r|j                  rt        dt              |S )zL
        Retrieve a template ensuring it exists and is not deleted.
        r>   r"   )r   r?   r@   r   r   r;   r   r8   s      r9   get_template_by_idz)ProductTemplateService.get_template_by_id   s1    
 (11';GcnnCFF
rN   c                    t        j                  ||       }|st        dt              t        j                  ||       t        t        |j                               |j                  rt        t        |j                               yy)z5Soft-delete template and remove files (non-critical).r>   r"   N)	r   r?   r   r   soft_delete_templater   r   r'   r(   rP   s      r9   delete_templatez&ProductTemplateService.delete_template   sj     (11';GCFF!66wD 	D'(S1123  rN   N)__name__
__module____qualname__staticmethodr1   r   intr   r	   r:   r   rC   rM   rQ   rT    rN   r9   r   r      s2   %%% % 	%
 
% %N ++sm+ *%+ 	+
 
+ +Z 


 
 	

 #
 }
 
2  g /   4S 47 4 4rN   r   )'pathlibr   typingr   fastapir   r   sqlmodelr   uuidr   jsonapp.models.product_templater	   app.schemas.product_templater
   app.schemas.commonr   &app.repositories.product_template_repor   app.utils.imager   r   r   app.utils.psd_sharedr   app.utils.psd_ratior   app.core.loggingr   app.core.configr   app.core.constantsr   r   rU   r.   r*   r   rZ   rN   r9   <module>rk      sU      -    7 < 0 L V V 5 1 ) ( >	h	>I4 I4rN   