
    	,h                        d dl Z d dlZd dlmZmZ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mZ d dlZd dlmZ  e       Zej-                  d      d	        Zej-                  d
      d        ZdZ e j4                  ed       ej7                  d       ed      fdefd       Zej7                  d       ed       ee       ee      fdededefd       Zy)    N)	APIRouterDependsFile
UploadFile)FileResponseJSONResponse)Session)get_db)JWTPayloadSchemaget_current_user)
TblStudentz/download-excel-sample1c                  *   K   d} t        | dd      S w)Nzfiles/Excel Sample 1.xlsxAapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetzExcel Sample 1.xlsxpath
media_typefilenamer   	file_paths    8/var/www/html/RetailAPI/app/api/excel_download/router.pydownloadExcel_Sample1r      !     +IV&    z/download-excel-sample2c                  *   K   d} t        | dd      S w)Nzfiles/Excel Sample 2.xlsxr   zExcel Sample 2.xlsxr   r   r   s    r   downloadExcel_Sample2r      r   r   uploaded_filesT)exist_okz/upload-excel-sample2.filec                   K   | j                   xs d}t        j                  j                  t        |      }t        |d      5 }t        j                  | j                  |       d d d        t        d|d      S # 1 sw Y   xY ww)Nuploaded_file.xlsxwbzFile uploaded successfully!)messager   content)
r   osr   join
UPLOAD_DIRopenshutilcopyfileobjr   r   )r   r   file_locationbuffers       r   upload_excel2r.   5   sj     MM9%9Hj(;M	mT	"f499f- 
# ,IW_ `aa 
#	"s   AB!A;$B;B Bz/upload-excel-sample1dbtokenc                   K   t        |dd       }|t        dddi      S |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)                          t        d|j*                  |d      S # 1 sw Y   OxY ww)N
student_idi  errorzstudent_id missing in token.)status_coder%   i  zStudent not found.z#Group not assigned to this student.r!   r"   z-File uploaded and group updated successfully!)r#   group_idr   r$   )getattrr   queryr   filterr2   firstgroupr   r&   r   r'   r(   r)   r*   r+   r   pd
read_excelsample_excel_onecommitr5   )
r   r/   r0   r2   studentr:   r   r,   r-   dfs
             r   upload_excel1rA   @   s-     d3Jg?]5^__ hhz"))**?*?:*MNTTVGg?S5TUU ==g?d5effMME }}4 4HGGLLX6M	mT	"f499f- 
# 
}	%B &EIIKBNN!   
#	"s   CE!E=AEEE)r&   r*   fastapir   r   r   r   fastapi.responsesr   r   sqlalchemy.ormr	   app.database.main.mysqlr
   app.dependency.authanticationr   r   pandasr;   app.models.main.studentr   excel_routergetr   r   r(   makedirspostr.   rA        r   <module>rO      s   	  8 8 8 " * L  .{+, - +, - 
 J &* *++/9 bj b ,b *+Cy&/%&67&
&& & ,&rN   