
    h                         d dl Z d dlZd dlmZ d dl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 dZh d	Zd
Z e j(                  ed        G d d      Zy)    N)FileResponse)
UploadFileHTTPException)Session)JWTPayloadSchema)TblGroup)
TblStudentuploads>   .jpg.png.svg.jpeg2   T)exist_okc                   L    e Zd ZdedefdZdefdZdefdZd Z	defdZ
d	 Zy
)CorporateStoreServicedbtokenc                      || _         || _        y )N)r   r   )selfr   r   s      :/var/www/html/RetailAPI/app/api/corporate_store/service.py__init__zCorporateStoreService.__init__   s    
    filec                     t         j                  j                  | j                        d   j	                         }|t
        vrt        dd| d      y )N     u   ❌ Unsupported file format: z'. Allowed formats: PNG, JPG, JPEG, SVG.status_codedetail)ospathsplitextfilenamelowerALLOWED_EXTENSIONSr   )r   exts     r   validate_filez#CorporateStoreService.validate_file   s]    ggt}}-a0668((C9VWZV[  \F  8G  H  H )r   c                 `  K   t         j                  |       |j                          d {   }t        |      dz  }|t        kD  rt        dd|ddt         d      |j                  d       d {    t        | j                  d	d       }|t        dd
      | j                  j                  t              j                  t        j                  |k(        j                         }|st        dd      |j                  st        dd      |j                  }|j                   }|r4t"        j$                  j'                  |      rt#        j(                  |       t"        j$                  j+                  t,        |j.                        }t1        |d      5 }	t3        j4                  |j6                  |	       d d d        ||_        | j                  j9                          | j                  j;                  |       d|j<                  ||j                   dS 7 7 # 1 sw Y   fxY wwNi   r   u   ❌ File size z.2fz MB exceeds maximum z MB.r   r   
student_idzstudent_id missing in tokeni  zStudent not foundzGroup not assignedwbu5   ✅ File uploaded and old file replaced successfully!)messagegroup_idr$   db_path)r   r(   readlenMAX_FILE_SIZE_MBr   seekgetattrr   r   queryr	   filterr+   firstgroup0corporate_and_concept_level_organisational_chartr!   r"   existsremovejoin
UPLOAD_DIRr$   openshutilcopyfileobjr   commitrefreshr.   
r   r   contentssize_mbr+   studentr8   old_file_path	save_pathbuffers
             r   1upload_corporate_and_concept_level_organizationalzGCorporateStoreService.upload_corporate_and_concept_level_organizational   s    ++D1$h-;/%%'}4HIYHZZ^_  iil TZZt<
C8UVV''--
+22:3H3HJ3VW]]_C8KLL}}C8LMM NNRWW^^M:IIm$ GGLLT]];	)T"ftyy&1 # BK> O!MM	
 	
M % 	. #"<   )H.HAH.4H5D'H.!H"=A H.H."H+'H.c                 @  K   | j                   j                  t        j                        j	                         }|sddiS |D cg c]  }t        |t              r|d   n| }}|d   }t        j                  j                  |      sddiS t        |d      S c c}w wNerrorzNo image foundr   zFile does not exist on serverz
image/jpeg)
media_type)r   r5   r   r9   r7   
isinstancetupler!   r"   r:   r   r   	get_imagerpaths	file_paths        r   get_image_corporatez)CorporateStoreService.get_image_corporateM   s     GGMM("["[\bbd	-.. ?HHiAu-114iH!H	ww~~i(<==I,??	 I   ABB"<Bc                 `  K   t         j                  |       |j                          d {   }t        |      dz  }|t        kD  rt        dd|ddt         d      |j                  d       d {    t        | j                  d	d       }|t        dd
      | j                  j                  t              j                  t        j                  |k(        j                         }|st        dd      |j                  st        dd      |j                  }|j                   }|r4t"        j$                  j'                  |      rt#        j(                  |       t"        j$                  j+                  t,        |j.                        }t1        |d      5 }	t3        j4                  |j6                  |	       d d d        ||_        | j                  j9                          | j                  j;                  |       d|j<                  ||j                   dS 7 7 # 1 sw Y   fxY wwr*   )r   r(   r0   r1   r2   r   r3   r4   r   r   r5   r	   r6   r+   r7   r8    store_level_organisational_chartr!   r"   r:   r;   r<   r=   r$   r>   r?   r@   r   rA   rB   r.   rC   s
             r   %upload_store_level_organization_chartz;CorporateStoreService.upload_store_level_organization_chart[   s    ++D1$h-;/%%'}4HIYHZZ^_  iil TZZt<
C8UVV''--
+22:3H3HJ3VW]]_C8KLL}}C8LMM >>RWW^^M:IIm$ GGLLT]];	)T"ftyy&1 # 2;. O!==	
 	
M % 	. #"rK   c                 @  K   | j                   j                  t        j                        j	                         }|sddiS |D cg c]  }t        |t              r|d   n| }}|d   }t        j                  j                  |      sddiS t        |d      S c c}w wrM   )r   r5   r   rZ   r7   rP   rQ   r!   r"   r:   r   rR   s        r   get_image_store_levelz+CorporateStoreService.get_image_store_level   s     GGMM("K"KLRRT	.//>GHiAu-114iH!H	ww~~i(<==I,??	 IrX   N)__name__
__module____qualname__r   r   r   r   r(   rJ   rW   r[   r]    r   r   r   r      sJ    ' )9 HJ H
.
J .
b
@.

 .
b@r   r   )r!   r?   fastapi.responsesr   pandaspdfastapir   r   sqlalchemy.ormr   app.dependency.authanticationr   app.models.main.groupr   app.models.main.studentr	   r=   r&   r2   makedirsr   ra   r   r   <module>rk      sQ    	  *  - " : * . 
6   J &C@ C@r   