main_2.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import boto3
  2. import jinja2
  3. import os
  4. from typing import Optional
  5. from fastapi import Form, FastAPI, Header
  6. from fastapi.middleware.cors import CORSMiddleware
  7. from botocore.exceptions import ClientError
  8. from pydantic import BaseModel, EmailStr
  9. from email.mime.multipart import MIMEMultipart
  10. from email.mime.text import MIMEText
  11. from datetime import date
  12. import dataset
  13. from os.path import join, dirname
  14. from dotenv import load_dotenv
  15. # dotenv_path = join(dirname(__file__),'./env/.env')
  16. dotenv_path = join(dirname(__file__),'.env/.env_test')
  17. load_dotenv(dotenv_path)
  18. choozmo_db = os.environ.get("CHOOZMO_DATABASE_URL")
  19. hhh_db = os.environ.get("HHH_DATABASE_URL")
  20. def render_template(template, kwargs):
  21. templateLoader = jinja2.FileSystemLoader(searchpath="./templates")
  22. templateEnv = jinja2.Environment(loader=templateLoader)
  23. templ = templateEnv.get_template(template)
  24. return templ.render(kwargs)
  25. receiver = "andy@hhh.com.tw"
  26. # receiver = "andy@hhh.com.tw, agent@hhh.com.tw"
  27. app = FastAPI()
  28. app.add_middleware(
  29. CORSMiddleware,
  30. allow_origins=["*"],
  31. allow_credentials=True,
  32. allow_methods=["*"],
  33. allow_headers=["*"],
  34. )
  35. class User(BaseModel):
  36. name: str
  37. email: EmailStr
  38. phone: str
  39. sex: Optional[str] = None
  40. class Html(User):
  41. area: Optional[str] = None
  42. type: Optional[str] = None
  43. mode: Optional[str] = None
  44. budget: Optional[str] = None
  45. pin: Optional[str] = None
  46. room: Optional[str] = None
  47. hall: Optional[str] = None
  48. restroom: Optional[str] = None
  49. style: Optional[str] = None
  50. time: Optional[str] = None
  51. class User2(BaseModel):
  52. name: str
  53. email: EmailStr
  54. phone: str
  55. class Html2(User2):
  56. loc: str
  57. h_class: str
  58. size: str
  59. version: str
  60. class User3(BaseModel):
  61. name: str
  62. email: EmailStr
  63. phone: str
  64. class Html3(User2):
  65. house_type: str
  66. location: str
  67. house_year: str
  68. pin: str
  69. change_area: str
  70. floor: str
  71. compartment: str
  72. ceiling: str
  73. room: str
  74. liveroom: str
  75. bathroom: str
  76. @app.post('/hhh/mail/deco/v1')
  77. async def hhh_send_mail(html: Html):
  78. ##TBD
  79. ##xoops.renovation_request
  80. db = dataset.connect(hhh_db)
  81. query = "UPDATE renovation_reuqest SET loan=1 Where phone = \'" + str(html.phone) + "\' AND email = \'" + html.email +"\' AND name = \'" + html.name +"\'"
  82. db.query(query)
  83. query = "SELECT ctime FROM renovation_reuqest WHERE phone = \'" + str(html.phone) + "\' AND email = \'" + html.email +"\' AND name = \'" + html.name +"\' ORDER BY id desc LIMIT 1;"
  84. result = db.query(query)
  85. for i in result:
  86. time_stamp = i['ctime'].strftime("%Y%m%d%H%M")
  87. SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
  88. RECIPIENT = receiver
  89. AWS_REGION = "us-east-1"
  90. CHARSET = "UTF-8"
  91. client = boto3.client('ses',region_name=AWS_REGION)
  92. context = {}
  93. context["time_stamp"] = time_stamp
  94. context["name"] = html.name
  95. context["phone"] = html.phone
  96. context["mail"] = html.email
  97. context["sex"] = html.sex
  98. context["area"] = html.area
  99. context["type"] = html.type
  100. context["mode"] = html.mode
  101. context["budget"] = html.budget
  102. context["pin"] = html.pin
  103. context["room"] = html.room
  104. context["hall"] = html.hall
  105. context["restroom"] = html.restroom
  106. context["style"] = html.style
  107. context["time"] = str(html.time)
  108. msg = MIMEMultipart()
  109. msg["Subject"] = "富邦裝修需求貸款通知信"
  110. msg["From"] = "noreply@hhh.com.tw"
  111. msg["To"] = RECIPIENT
  112. body = MIMEText(render_template("email.html",{"context":context}), "html")
  113. msg.attach(body)
  114. response = client.send_raw_email(
  115. Source=msg["From"],
  116. Destinations=[msg["To"]],
  117. RawMessage={"Data": msg.as_string()}
  118. )
  119. @app.post('/hhh/mail/deco/v2')
  120. async def hhh_send_mail(html: Html2, content_type: Optional[str] =Header(None)):
  121. ##TBD
  122. ## db.ptt.cx/hhh.deco_request
  123. db = dataset.connect(choozmo_db)
  124. query = "UPDATE deco_request SET loan=1 Where phone = \'" + str(html.phone) + "\' AND email = \'" + html.email +"\' AND name = \'" + html.name +"\'"
  125. result = db.query(query)
  126. query = "SELECT time_stamp FROM deco_request WHERE phone = \'" + str(html.phone) + "\' AND email = \'" + html.email +"\' AND name = \'" + html.name +"\' ORDER BY id desc LIMIT 1;"
  127. result = db.query(query)
  128. for i in result:
  129. time_stamp = i['time_stamp'].strftime("%Y%m%d%H%M")
  130. SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
  131. RECIPIENT = receiver
  132. AWS_REGION = "us-east-1"
  133. CHARSET = "UTF-8"
  134. client = boto3.client('ses',region_name=AWS_REGION)
  135. context = {}
  136. context["time_stamp"] = time_stamp
  137. context["name"] = html.name
  138. context["phone"] = html.phone
  139. context["mail"] = html.email
  140. context["loc"] = html.loc
  141. context["h_class"] = html.h_class
  142. context["size"] = html.size
  143. msg = MIMEMultipart()
  144. msg["Subject"] = "富邦裝修需求貸款通知信"
  145. msg["From"] = "noreply@hhh.com.tw"
  146. msg["To"] = RECIPIENT
  147. body = MIMEText(render_template("email_v2.html",{"context":context}), "html")
  148. msg.attach(body)
  149. response = client.send_raw_email(
  150. Source=msg["From"],
  151. Destinations=[msg["To"]],
  152. RawMessage={"Data": msg.as_string()}
  153. )
  154. # print(response)
  155. @app.post('/hhh/mail/deco/v3')
  156. async def hhh_send_mail(html: Html3, content_type: Optional[str] =Header(None)):
  157. ##TBD
  158. ## alter xoops.calulator loon set 1
  159. db = dataset.connect(hhh_db)
  160. query = "UPDATE calculator SET loan=1 Where phone = \'" + str(html.phone) + "\' AND email = \'" + html.email +"\' AND name = \'" + html.name +"\'"
  161. result = db.query(query)
  162. query = "SELECT create_time FROM calculator WHERE phone = \'" + str(html.phone) + "\' AND email = \'" + html.email +"\' AND name = \'" + html.name +"\' ORDER BY id desc LIMIT 1;"
  163. result = db.query(query)
  164. for i in result:
  165. time_stamp = i['create_time'].strftime("%Y%m%d%H%M")
  166. SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
  167. RECIPIENT = receiver
  168. AWS_REGION = "us-east-1"
  169. CHARSET = "UTF-8"
  170. client = boto3.client('ses',region_name=AWS_REGION)
  171. context = {}
  172. context["time_stamp"] = time_stamp
  173. context["name"] = html.name
  174. context["phone"] = html.phone
  175. context["mail"] = html.email
  176. context["house_type"] = html.house_type
  177. context["location"] = html.location
  178. context["house_year"] = html.house_year
  179. context["pin"] = html.pin
  180. context["change_area"] = html.change_area
  181. context["floor"] = html.floor
  182. context["compartment"] = html.compartment
  183. context["ceiling"] = html.ceiling
  184. context["room"] = html.room
  185. context["liveroom"] = html.liveroom
  186. context["bathroom"] = html.bathroom
  187. msg = MIMEMultipart()
  188. msg["Subject"] = "富邦裝修需求貸款通知信"
  189. msg["From"] = "noreply@hhh.com.tw"
  190. msg["To"] = RECIPIENT
  191. body = MIMEText(render_template("email_v3.html",{"context":context}), "html")
  192. msg.attach(body)
  193. response = client.send_raw_email(
  194. Source=msg["From"],
  195. Destinations=[msg["To"]],
  196. RawMessage={"Data": msg.as_string()}
  197. )
  198. # @app.post('/hhh/mail/deco/v4')
  199. # async def hhh_send_mail(html: Html3):
  200. # SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
  201. # RECIPIENT = html.email
  202. # AWS_REGION = "us-east-1"
  203. # CHARSET = "UTF-8"
  204. # client = boto3.client('ses',region_name=AWS_REGION)
  205. # context = {}
  206. # context["name"] = html.name
  207. # context["phone"] = html.phone
  208. # context["mail"] = html.email
  209. # context["house_type"] = html.house_type
  210. # context["location"] = html.location
  211. # context["house_year"] = html.house_year
  212. # context["pin"] = html.pin
  213. # context["change_area"] = html.change_area
  214. # context["floor"] = html.floor
  215. # context["compartment"] = html.compartment
  216. # context["ceiling"] = html.ceiling
  217. # context["room"] = html.room
  218. # context["liveroom"] = html.liveroom
  219. # context["bathroom"] = html.bathroom
  220. # msg = MIMEMultipart()
  221. # msg["Subject"] = "富邦裝修需求貸款通知信"
  222. # msg["From"] = "noreply@hhh.com.tw"
  223. # msg["To"] = RECIPIENT
  224. # body = MIMEText(render_template("email_v4.html",{"context":context}), "html")
  225. # msg.attach(body)
  226. # response = client.send_raw_email(
  227. # Source=msg["From"],
  228. # Destinations=[msg["To"]],
  229. # RawMessage={"Data": msg.as_string()}
  230. # )