
    Ehc                         d dl Z d dlZd dl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  G d d	      Zy)
    N)File
UploadFile)FileResponseJSONResponse)Session)
UPLOAD_DIR)JWTPayloadSchema)
TblStudentc                   j    e Zd ZdedefdZd ZdZ ej                  ed        e
d      fd	efd
Zy)OnllinePoliciseServicedbtokenc                      || _         || _        y )N)r   r   )selfr   r   s      F/var/www/html/retail-simulation-api/app/api/online_policies/service.py__init__zOnllinePoliciseService.__init__   s    
    c                 *   K   d}t        |dd      S w)Nz"files/6Sample_Online_Policies.xlsxzAapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetz6Sample_Online_Policies.xlsx)path
media_typefilename)r   )r   	file_paths     r   download_online_excelz,OnllinePoliciseService.download_online_excel   s!     8	\5
 	
s   uploaded_filesT)exist_ok.filec                   K   t        | j                  dd       }|t        dddi      S | j                  j	                  t
              j                  t
        j                  |k(        j                         }|st        dddi      S |j                  st        dddi      S |j                  }|j                  xs d}t        j                  j                  t        |      }t        |d	      5 }t!        j"                  |j$                  |       d d d        t'        j(                  |      }||_        | j                  j-                          t        d
|j.                  |d      S # 1 sw Y   YxY ww)N
student_idi  errorzstudent_id missing in token.)status_codecontentzStudent not found.z#Group not assigned to this student.zuploaded_file.xlsxwbz-File uploaded and group updated successfully!)messagegroup_idr   )r!   )getattrr   r   r   queryr
   filterr   firstgroupr   osr   joinr   openshutilcopyfileobjr   pd
read_excelonline_policies_excelcommitr$   )	r   r   r   studentr)   r   file_locationbufferdfs	            r   upload_online_policies_excelz3OnllinePoliciseService.upload_online_policies_excel   s0    TZZt<
C'Ca9bcc''--
+22:3H3HJ3VW]]_C'CW9XYY}}C'Ch9ijj==8$8Z:-&&tyy&1 ' ]]=)&3#F%%
  	 '&s   C.E50!E)AE5)E2.E5N)__name__
__module____qualname__r   r	   r   r   r   r*   makedirsr   r   r7    r   r   r   r      sE    ' )9 
 "JBKK
T*CG9 j r   r   )r*   r-   pandasr/   fastapir   r   fastapi.responsesr   r   sqlalchemy.ormr   app.api.corporate_store.servicer   app.dependency.authanticationr	   app.models.main.studentr
   r   r<   r   r   <module>rD      s*    	   $ 8 " 6 : ., ,r   