#pip install fastapi
#pip install uvicorn
from fastapi.responses import HTMLResponse
from fastapi import FastAPI,Form, Request,UploadFile
from fastapi.templating import Jinja2Templates
import uvicorn
import zipfile

from chz_gmail_temp import mail2
from personal_settings import *

app = FastAPI()
templates=Jinja2Templates(directory="html_directory")

@app.get("/email_test")
def read_root(request: Request):
    return templates.TemplateResponse('home.html',{"request": request})




@app.post("/submitform")
def create_upload_file(file: UploadFile,username: str = Form(...)):
    file_location = "./templates/use_zip.zip"
    with open(file_location, 'wb+') as f:
        f.write(file.file.read())
    zf = zipfile.ZipFile(file_location, 'r')
    print(zf.namelist())
    zf.extractall("./templates/")

    mailobj={}
    mailobj['toaddr']=username
    mailobj['gmail_user']=gmail_user
    mailobj['gmail_password']=gmail_password
    mailobj['title'] = gmail_title
    mailobj['totext'] = gmail_totext
    mail2(mailobj)
    return "down"
    # return {"username": username,"filename": file.filename}




if __name__ == "__main__":
    uvicorn.run("main:app", host="127.0.0.1", port=5000)




























# @app.get("/items/{item_id}")
# def read_item(item_id: int, q: Optional[str] = None):
#     return {"item_id": item_id, "q": q}