main.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. from enum import Enum
  2. import time
  3. from datetime import datetime
  4. from typing import Optional
  5. from pydantic import BaseModel
  6. from fastapi import FastAPI, Query, UploadFile, File
  7. import dataset,json
  8. from fastapi import FastAPI
  9. from fastapi.middleware.cors import CORSMiddleware
  10. import pandas as pd
  11. from gspread_pandas import Spread, Client
  12. from oauth2client.service_account import ServiceAccountCredentials
  13. import gspread
  14. app = FastAPI()
  15. origins = [
  16. "https://hhh.com.tw"
  17. "http://172.105.205.52",
  18. "http://172.105.205.52:8002",
  19. "http://172.104.93.163",
  20. ]
  21. app.add_middleware(
  22. CORSMiddleware,
  23. # allow_origins=origins,
  24. allow_origins=["*"],
  25. allow_credentials=True,
  26. allow_methods=["*"],
  27. allow_headers=["*"],
  28. )
  29. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  30. class deco_request(BaseModel):
  31. id: int
  32. name: str
  33. email: str
  34. phone: str
  35. class deco_request_detail(BaseModel):
  36. id: int
  37. name: str
  38. email: str
  39. phone: str
  40. #detail
  41. loc: str
  42. h_class: str
  43. type: str
  44. budget: str
  45. size: float
  46. bed_num: int
  47. liv_num: int
  48. bath_num: int
  49. style: str
  50. prefer_date: str
  51. time_stamp: str
  52. gender: str
  53. class client_info(BaseModel):
  54. name:str
  55. organization:str
  56. position:str
  57. phone:str
  58. email:str
  59. cert_last5:str
  60. where_learn:str
  61. why_sign:str
  62. @app.get("/deco_request")
  63. async def get_req():
  64. request_table = db['deco_request']
  65. statement = 'SELECT * FROM deco_request'
  66. reqs = []
  67. for req in db.query(statement):
  68. reqs.append({'name':req['name'],'email':req['email'],'phone':req['phone'],'loc':req['loc']
  69. ,'h_class':req['h_class'],'type':req['type'],'budget':req['budget'],'size':req['size'],'bed_num':req['bed_num']
  70. ,'liv_num':req['liv_num'],'bath_num':req['bath_num'],'style':req['style'],'prefer_date':req['prefer_date'],'gender':req.gender})
  71. return reqs
  72. @app.post("/deco_request")
  73. async def create_req(req:deco_request):
  74. request_table = db['deco_request']
  75. time_stamp = datetime.fromtimestamp(time.time())
  76. time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
  77. pk = request_table.insert({'name':req.name,'email':req.email,'phone':req.phone,'time_stamp':time_stamp})
  78. req.id = pk
  79. return req
  80. @app.post("/add_client_info")
  81. async def add_client_info(ci:client_info):
  82. request_table = db['client_info']
  83. time_stamp = datetime.fromtimestamp(time.time())
  84. time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
  85. pk = request_table.insert({'name':ci.name,'organization':ci.organization,'position':ci.position,'phone':ci.phone,'email':ci.email
  86. ,'cert_last5':ci.cert_last5,'where_learn':ci.where_learn,'why_sign':ci.why_sign,'time_stamp':time_stamp})
  87. return pk
  88. @app.post("/deco_request_detail")
  89. async def create_req_detail(req:deco_request_detail):
  90. statement = 'SELECT * FROM deco_request WHERE email="'+req.email+'"'
  91. email_cnt = 0
  92. for row in db.query(statement):
  93. email_cnt = email_cnt + 1
  94. statement = 'SELECT * FROM deco_request WHERE phone="'+req.phone+'"'
  95. phone_cnt = 0
  96. for row in db.query(statement):
  97. phone_cnt = phone_cnt + 1
  98. return_code = 0
  99. #return code 0=good, 1=email duplication, 2=phone duplication, 3= both duplicate
  100. '''
  101. if email_cnt>0 and phone_cnt==0:
  102. return_code = 1
  103. if email_cnt==0 and phone_cnt>0:
  104. return_code = 2
  105. if email_cnt>0 and phone_cnt>0:
  106. return_code = 3
  107. '''
  108. if return_code ==0 :
  109. request_table = db['deco_request']
  110. time_stamp = datetime.fromtimestamp(time.time())
  111. time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
  112. req.time_stamp = time_stamp
  113. pk = request_table.insert({'name':req.name,'email':req.email,'phone':req.phone,'loc':req.loc
  114. ,'h_class':req.h_class,'type':req.type,'budget':req.budget,'size':req.size,'bed_num':req.bed_num
  115. ,'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})
  116. req.id = pk
  117. make_req_df()
  118. return return_code
  119. else :
  120. return return_code
  121. def make_req_df():
  122. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  123. statement = 'SELECT * FROM deco_request ORDER BY time_stamp DESC'
  124. #2021-05-23 15:57:43
  125. df = pd.DataFrame()
  126. for row in db.query(statement):
  127. date_format = "%Y-%M-%d %H:%M:%S"
  128. #fdate = datetime.strptime(row['ts_date'],date_format)
  129. fdate = row['time_stamp'].strftime('%Y-%m-%d')
  130. df = df.append({'serial id':row['id'],'姓名':['name'],'email':row['email'],'電話':row['phone']
  131. ,'地區':row['loc'],'房屋類型':row['h_class'],"房屋型態":row['type'],'預算':row['budget']
  132. ,'坪數':row['size'],'房數':row['bed_num'],'廳數':row['liv_num'],'衛浴數':row['bath_num']
  133. ,'風格類型':row['style'],'預選裝修日期':row['prefer_date']}, ignore_index=True)
  134. save_sheet(df,'deco_request','sheet1')
  135. def save_sheet(df,filename,tabname,startpos='A1'):
  136. scope = ['https://spreadsheets.google.com/feeds',
  137. 'https://www.googleapis.com/auth/drive']
  138. # credentials = ServiceAccountCredentials.from_json_keyfile_name('c:\\keys\\spread2.json', scope)
  139. credentials = ServiceAccountCredentials.from_json_keyfile_name('spread2.json', scope)
  140. gc = gspread.authorize(credentials)
  141. spread = Spread(filename,creds=credentials)
  142. spread.df_to_sheet(df, index=False, sheet=tabname, start=startpos, replace=False)