main.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. choozmo_db = os.environ.get("CHOOZMO_DATABASE_URL")
  17. hhh_db = os.environ.get("HHH_DATABASE_URL")
  18. def render_template(template, kwargs):
  19. templateLoader = jinja2.FileSystemLoader(searchpath="./templates")
  20. templateEnv = jinja2.Environment(loader=templateLoader)
  21. templ = templateEnv.get_template(template)
  22. return templ.render(kwargs)
  23. app = FastAPI()
  24. app.add_middleware(
  25. CORSMiddleware,
  26. allow_origins=["*"],
  27. allow_credentials=True,
  28. allow_methods=["*"],
  29. allow_headers=["*"],
  30. )
  31. class User(BaseModel):
  32. name: str
  33. email: EmailStr
  34. phone: str
  35. sex: Optional[str] = None
  36. class Html(User):
  37. area: Optional[str] = None
  38. type: Optional[str] = None
  39. mode: Optional[str] = None
  40. budget: Optional[str] = None
  41. pin: Optional[str] = None
  42. room: Optional[str] = None
  43. hall: Optional[str] = None
  44. restroom: Optional[str] = None
  45. style: Optional[str] = None
  46. time: Optional[str] = None
  47. class User2(BaseModel):
  48. name: str
  49. email: EmailStr
  50. phone: str
  51. class Html2(User2):
  52. loc: str
  53. h_class: str
  54. size: str
  55. version: str
  56. class User3(BaseModel):
  57. name: str
  58. email: EmailStr
  59. phone: str
  60. class Html3(User2):
  61. house_type: str
  62. location: str
  63. house_year: str
  64. pin: str
  65. change_area: str
  66. floor: str
  67. compartment: str
  68. ceiling: str
  69. room: str
  70. liveroom: str
  71. bathroom: str
  72. @app.post('/hhh/mail/deco/v1')
  73. async def hhh_send_mail(html: Html):
  74. ##TBD
  75. ##xoops.renovation_request
  76. db = dataset.connect(hhh_db)
  77. query = "UPDATE renovation_reuqest SET loan=1 Where phone = \'" + str(html.phone) + "\' AND email = \'" + html.email +"\' AND name = \'" + html.name +"\'"
  78. result = db.query(query)
  79. SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
  80. RECIPIENT = html.email
  81. AWS_REGION = "us-east-1"
  82. CHARSET = "UTF-8"
  83. client = boto3.client('ses',region_name=AWS_REGION)
  84. context = {}
  85. context["name"] = html.name
  86. context["phone"] = html.phone
  87. context["mail"] = html.email
  88. context["sex"] = html.sex
  89. context["area"] = html.area
  90. context["type"] = html.type
  91. context["mode"] = html.mode
  92. context["budget"] = html.budget
  93. context["pin"] = html.pin
  94. context["room"] = html.room
  95. context["hall"] = html.hall
  96. context["restroom"] = html.restroom
  97. context["style"] = html.style
  98. context["time"] = str(html.time)
  99. msg = MIMEMultipart()
  100. msg["Subject"] = "富邦裝修需求貸款通知信"
  101. msg["From"] = "noreply@hhh.com.tw"
  102. msg["To"] = RECIPIENT
  103. body = MIMEText(render_template("email.html",{"context":context}), "html")
  104. msg.attach(body)
  105. response = client.send_raw_email(
  106. Source=msg["From"],
  107. Destinations=[msg["To"]],
  108. RawMessage={"Data": msg.as_string()}
  109. )
  110. @app.post('/hhh/mail/deco/v2')
  111. async def hhh_send_mail(html: Html2, content_type: Optional[str] =Header(None)):
  112. ##TBD
  113. ## db.ptt.cx/hhh.deco_request
  114. db = dataset.connect(choozmo_db)
  115. query = "UPDATE deco_request SET loan=1 Where phone = \'" + str(html.phone) + "\' AND email = \'" + html.email +"\' AND name = \'" + html.name +"\'"
  116. result = db.query(query)
  117. SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
  118. RECIPIENT = html.email
  119. AWS_REGION = "us-east-1"
  120. CHARSET = "UTF-8"
  121. client = boto3.client('ses',region_name=AWS_REGION)
  122. context = {}
  123. context["name"] = html.name
  124. context["phone"] = html.phone
  125. context["mail"] = html.email
  126. context["loc"] = html.loc
  127. context["h_class"] = html.h_class
  128. context["size"] = html.size
  129. msg = MIMEMultipart()
  130. msg["Subject"] = "富邦裝修需求貸款通知信"
  131. msg["From"] = "noreply@hhh.com.tw"
  132. msg["To"] = RECIPIENT
  133. body = MIMEText(render_template("email_v2.html",{"context":context}), "html")
  134. msg.attach(body)
  135. response = client.send_raw_email(
  136. Source=msg["From"],
  137. Destinations=[msg["To"]],
  138. RawMessage={"Data": msg.as_string()}
  139. )
  140. # print(response)
  141. @app.post('/hhh/mail/deco/v3')
  142. async def hhh_send_mail(html: Html3, content_type: Optional[str] =Header(None)):
  143. ##TBD
  144. ## alter xoops.calulator loon set 1
  145. db = dataset.connect(hhh_db)
  146. query = "UPDATE calculator SET loan=1 Where phone = \'" + str(html.phone) + "\' AND email = \'" + html.email +"\' AND name = \'" + html.name +"\'"
  147. result = db.query(query)
  148. SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
  149. RECIPIENT = html.email
  150. AWS_REGION = "us-east-1"
  151. CHARSET = "UTF-8"
  152. client = boto3.client('ses',region_name=AWS_REGION)
  153. context = {}
  154. context["name"] = html.name
  155. context["phone"] = html.phone
  156. context["mail"] = html.email
  157. context["house_type"] = html.house_type
  158. context["location"] = html.location
  159. context["house_year"] = html.house_year
  160. context["pin"] = html.pin
  161. context["change_area"] = html.change_area
  162. context["floor"] = html.floor
  163. context["compartment"] = html.compartment
  164. context["ceiling"] = html.ceiling
  165. context["room"] = html.room
  166. context["liveroom"] = html.liveroom
  167. context["bathroom"] = html.bathroom
  168. msg = MIMEMultipart()
  169. msg["Subject"] = "富邦裝修需求貸款通知信"
  170. msg["From"] = "noreply@hhh.com.tw"
  171. msg["To"] = RECIPIENT
  172. body = MIMEText(render_template("email_v3.html",{"context":context}), "html")
  173. msg.attach(body)
  174. response = client.send_raw_email(
  175. Source=msg["From"],
  176. Destinations=[msg["To"]],
  177. RawMessage={"Data": msg.as_string()}
  178. )