main.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. app = FastAPI()
  11. origins = [
  12. "https://hhh.com.tw"
  13. "http://172.105.205.52",
  14. "http://172.105.205.52:8001",
  15. "http://172.104.93.163",
  16. ]
  17. app.add_middleware(
  18. CORSMiddleware,
  19. # allow_origins=origins,
  20. allow_origins=["*"],
  21. allow_credentials=True,
  22. allow_methods=["*"],
  23. allow_headers=["*"],
  24. )
  25. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  26. class client_info(BaseModel):
  27. name:str
  28. organization:str
  29. position:str
  30. phone:str
  31. email:str
  32. cert_last5:str
  33. where_learn:str
  34. why_sign:str
  35. @app.post("/add_client_info")
  36. async def add_client_info(ci:client_info):
  37. statement = 'SELECT * FROM client_info WHERE email="'+ci.email+'"'
  38. email_cnt = 0
  39. for row in db.query(statement):
  40. email_cnt = email_cnt + 1
  41. statement = 'SELECT * FROM client_info WHERE phone="'+ci.phone+'"'
  42. phone_cnt = 0
  43. for row in db.query(statement):
  44. phone_cnt = phone_cnt + 1
  45. return_code = 0
  46. #return code 0=good, 1=email duplication, 2=phone duplication, 3= both duplicate
  47. if email_cnt>0 and phone_cnt==0:
  48. return_code = 1
  49. if email_cnt==0 and phone_cnt>0:
  50. return_code = 2
  51. if email_cnt>0 and phone_cnt>0:
  52. return_code = 3
  53. if return_code ==0 :
  54. request_table = db['client_info']
  55. time_stamp = datetime.fromtimestamp(time.time())
  56. time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
  57. pk = request_table.insert({'name':ci.name,'organization':ci.organization,'position':ci.position,'phone':ci.phone,'email':ci.email
  58. ,'cert_last5':ci.cert_last5,'where_learn':ci.where_learn,'why_sign':ci.why_sign,'time_stamp':time_stamp})
  59. return return_code
  60. else :
  61. return return_code
  62. make_ci_df()
  63. return return_code
  64. def make_ci_df():
  65. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  66. statement = 'SELECT * FROM client_info ORDER BY time_stamp DESC'
  67. #2021-05-23 15:57:43
  68. df = pd.DataFrame()
  69. for row in db.query(statement):
  70. date_format = "%Y-%M-%d %H:%M:%S"
  71. #fdate = datetime.strptime(row['ts_date'],date_format)
  72. fdate = row['time_stamp'].strftime('%Y-%m-%d')
  73. df = df.append({'serial id':row['id'],'姓名':row['name'],'公司':row['organization']
  74. ,'職稱':row['position'],'電話':row['phone'],"e-mail":row['email'],'已匯款,後五碼':row['cert_last5']
  75. ,'如何知道這個課程':row['where_learn'],'爲什麼想報名':row['why_sign']}, ignore_index=True)
  76. def save_sheet(df,filename,tabname,startpos='A1'):
  77. scope = ['https://spreadsheets.google.com/feeds',
  78. 'https://www.googleapis.com/auth/drive']
  79. # credentials = ServiceAccountCredentials.from_json_keyfile_name('c:\\keys\\spread2.json', scope)
  80. credentials = ServiceAccountCredentials.from_json_keyfile_name('spread2.json', scope)
  81. gc = gspread.authorize(credentials)
  82. spread = Spread(filename,creds=credentials)
  83. spread.df_to_sheet(df, index=False, sheet=tabname, start=startpos, replace=False)