main.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from fastapi import FastAPI, Form, Request
  2. from fastapi.encoders import jsonable_encoder
  3. from fastapi.middleware.cors import CORSMiddleware
  4. import dataset
  5. import pandas as pd
  6. from starlette.responses import FileResponse, StreamingResponse
  7. from fastapi.responses import HTMLResponse
  8. from fastapi.staticfiles import StaticFiles
  9. import io
  10. from starlette.responses import RedirectResponse
  11. import codecs
  12. import random
  13. from starlette.status import HTTP_302_FOUND,HTTP_303_SEE_OTHER
  14. import smtplib
  15. import traceback
  16. import os
  17. from email.mime.text import MIMEText
  18. from email.mime.image import MIMEImage
  19. from email.mime.multipart import MIMEMultipart
  20. import requests
  21. app = FastAPI()
  22. app.add_middleware(
  23. CORSMiddleware,
  24. allow_origins=['*'],
  25. allow_credentials=True,
  26. allow_methods=["*"],
  27. allow_headers=["*"],
  28. )
  29. def get_db():
  30. # db = dataset.connect('mysql://choozmo:pAssw0rd@139.162.121.30:33306/hhh?charset=utf8mb4')
  31. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  32. step_questions_table = db['step_questions']
  33. return step_questions_table
  34. def mail_to_user(umail):
  35. r = requests.get('http://3.220.159.187:8081/mailto/'+umail)
  36. print(r.text)
  37. # os.chdir('/root/hhh_step_question/step_question/tests')
  38. os.system('python3 /root/hhh_step_question/step_question/tests/dftest.py &')
  39. # umail
  40. @app.post("/step_questions/submit", response_class=RedirectResponse)
  41. async def submit(request: Request):
  42. form_data = await request.form()
  43. # form_data = request.form()
  44. email=form_data.get('q3')
  45. result = {
  46. 'sex': form_data.get('q1'),
  47. 'phone': form_data.get('q2'),
  48. 'email': email,
  49. 'building_case_name': form_data.get('q4'),
  50. 'building_case_type': form_data.get('q5'),
  51. 'decoration_style': ','.join(form_data.getlist('q6')),
  52. 'decoration_budget': ','.join(form_data.getlist('q7')),
  53. 'decoration_size': ','.join(form_data.getlist('q8')),
  54. }
  55. print(result)
  56. get_db().insert(result)
  57. # fr=codecs.open('static/index_complete.html','r','utf-8')
  58. # lines=fr.read()
  59. # fr.close()
  60. # response = RedirectResponse(url='/a1/index_complete.html')
  61. # lines="""<html><body onload="document.location.replace( '/a1/index_complete.html?par={0}');">
  62. # </body></html>"""
  63. # return RedirectResponse(url='/a1/index_complete.html')
  64. # return HTMLResponse(content=lines.format(str(random.randint(100000,999999))), status_code=200)
  65. mail_to_user(email)
  66. return RedirectResponse(url="/a1/index_complete_msg.html", status_code=HTTP_302_FOUND)
  67. # return result
  68. app.mount("/a1", StaticFiles(directory="static"), name="static")
  69. @app.get("/a1")
  70. async def redirect():
  71. # url = app.url_path_for("/a1/index.html")
  72. response = RedirectResponse(url='/a1/index.html')
  73. return response
  74. #@app.get("/a1", response_class=HTMLResponse)
  75. #@app.get("/a1", response_class=HTMLResponse)
  76. #async def read_items():
  77. # return """
  78. # <html>
  79. # <head>
  80. # <title>Some HTML in here</title>
  81. # </head>
  82. # <body>
  83. # <h1>Look ma! HTML!</h1>
  84. # </body>
  85. # </html>
  86. # """
  87. @app.get("/step_questions")
  88. async def get_step_question(request: Request):
  89. db_data = list(get_db().find())
  90. df = pd.DataFrame(db_data, columns=db_data[0].keys())
  91. stream = io.StringIO()
  92. df.to_csv(stream, index = False)
  93. # response = StreamingResponse(data_frame.to_csv(index=False), media_type="text/csv")
  94. response = StreamingResponse(iter([stream.getvalue()]), media_type="text/csv")
  95. response.headers["Content-Disposition"] = "attachment; filename=export.csv"
  96. return response