main.py 3.6 KB

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