Ver código fonte

Merge branch 'master' of http://git.choozmo.com:3000/choozmo/HHH_Home_Decoration_2 into master

jeter20131220 3 anos atrás
pai
commit
8869ef6289
2 arquivos alterados com 11 adições e 8 exclusões
  1. BIN
      api/__pycache__/main.cpython-39.pyc
  2. 11 8
      api/main.py

BIN
api/__pycache__/main.cpython-39.pyc


+ 11 - 8
api/main.py

@@ -11,7 +11,10 @@ import pandas as pd
 from gspread_pandas import Spread, Client
 from oauth2client.service_account import ServiceAccountCredentials
 import gspread
+import asyncio
 app = FastAPI()
+import nest_asyncio
+nest_asyncio.apply()
 
 origins = [
     "https://hhh.com.tw"
@@ -28,7 +31,7 @@ app.add_middleware(
     allow_methods=["*"],
     allow_headers=["*"],
 )
-db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
+
 
 class deco_request(BaseModel):
     id: int
@@ -67,6 +70,7 @@ class client_info(BaseModel):
     
 @app.get("/deco_request")
 async def get_req():
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
     request_table = db['deco_request']
     statement = 'SELECT * FROM deco_request'
     reqs = []
@@ -78,6 +82,7 @@ async def get_req():
 
 @app.post("/deco_request")
 async def create_req(req:deco_request):
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
     request_table = db['deco_request']
     time_stamp = datetime.fromtimestamp(time.time())
     time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
@@ -88,6 +93,7 @@ async def create_req(req:deco_request):
 
 @app.post("/add_client_info")
 async def add_client_info(ci:client_info):
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
     request_table = db['client_info']
     time_stamp = datetime.fromtimestamp(time.time())
     time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
@@ -98,7 +104,7 @@ async def add_client_info(ci:client_info):
     return pk
 @app.post("/deco_request_detail")
 async def create_req_detail(req:deco_request_detail):
-    
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
     statement = 'SELECT * FROM deco_request WHERE email="'+req.email+'"'
     email_cnt = 0
     for row in db.query(statement):
@@ -129,15 +135,14 @@ async def create_req_detail(req:deco_request_detail):
         ,'h_class':req.h_class,'type':req.type,'budget':req.budget,'size':req.size,'bed_num':req.bed_num
         ,'liv_num':req.liv_num,'bath_num':req.bath_num,'style':req.style,'prefer_date':req.prefer_date,'time_stamp':req.time_stamp,'gender':req.gender})
         req.id = pk
-        make_req_df()
+        asyncio.run(make_req_df())
         return return_code
     else :
         return return_code
     
 
-def make_req_df():
+async def make_req_df():
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
-
     statement = 'SELECT * FROM deco_request ORDER BY time_stamp DESC'
     #2021-05-23 15:57:43
     df = pd.DataFrame()
@@ -151,10 +156,9 @@ def make_req_df():
         ,'地區':row['loc'],'房屋類型':row['h_class'],"房屋型態":row['type'],'預算':row['budget']
         ,'坪數':row['size'],'房數':row['bed_num'],'廳數':row['liv_num'],'衛浴數':row['bath_num']
         ,'風格類型':row['style'],'預選裝修日期':row['prefer_date']}, ignore_index=True)
-        save_sheet(df,'deco_request','sheet1')
+    save_sheet(df,'deco_request','sheet1')
     
 def save_sheet(df,filename,tabname,startpos='A1'):
-
     scope = ['https://spreadsheets.google.com/feeds',
             'https://www.googleapis.com/auth/drive']
 
@@ -163,7 +167,6 @@ def save_sheet(df,filename,tabname,startpos='A1'):
 
     gc = gspread.authorize(credentials)
     spread = Spread(filename,creds=credentials)
-
     spread.df_to_sheet(df, index=False, sheet=tabname, start=startpos, replace=False)