123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- import boto3
- import jinja2
- import os
- from typing import Optional
- from fastapi import Form, FastAPI, Header
- from fastapi.middleware.cors import CORSMiddleware
- from botocore.exceptions import ClientError
- from pydantic import BaseModel, EmailStr
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- from datetime import date
- import dataset
- from os.path import join, dirname
- from dotenv import load_dotenv
- # dotenv_path = join(dirname(__file__),'./env/.env')
- dotenv_path = join(dirname(__file__),'.env/.env_test')
- load_dotenv(dotenv_path)
- choozmo_db = os.environ.get("CHOOZMO_DATABASE_URL")
- hhh_db = os.environ.get("HHH_DATABASE_URL")
- def render_template(template, kwargs):
- templateLoader = jinja2.FileSystemLoader(searchpath="./templates")
- templateEnv = jinja2.Environment(loader=templateLoader)
- templ = templateEnv.get_template(template)
- return templ.render(kwargs)
- receiver = "andy@hhh.com.tw"
- # receiver = "andy@hhh.com.tw, agent@hhh.com.tw"
- app = FastAPI()
- app.add_middleware(
- CORSMiddleware,
- allow_origins=["*"],
- allow_credentials=True,
- allow_methods=["*"],
- allow_headers=["*"],
- )
- class User(BaseModel):
- name: str
- email: EmailStr
- phone: str
- sex: Optional[str] = None
- class Html(User):
- area: Optional[str] = None
- type: Optional[str] = None
- mode: Optional[str] = None
- budget: Optional[str] = None
- pin: Optional[str] = None
- room: Optional[str] = None
- hall: Optional[str] = None
- restroom: Optional[str] = None
- style: Optional[str] = None
- time: Optional[str] = None
- class User2(BaseModel):
- name: str
- email: EmailStr
- phone: str
- class Html2(User2):
- loc: str
- h_class: str
- size: str
- version: str
- class User3(BaseModel):
- name: str
- email: EmailStr
- phone: str
- class Html3(User2):
- house_type: str
- location: str
- house_year: str
- pin: str
- change_area: str
- floor: str
- compartment: str
- ceiling: str
- room: str
- liveroom: str
- bathroom: str
- @app.post('/hhh/mail/deco/v1')
- async def hhh_send_mail(html: Html):
- ##TBD
- ##xoops.renovation_request
- db = dataset.connect(hhh_db)
- query = "UPDATE renovation_reuqest SET loan=1 Where phone = \'" + str(html.phone) + "\' AND email = \'" + html.email +"\' AND name = \'" + html.name +"\'"
- db.query(query)
- 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;"
- result = db.query(query)
- for i in result:
- time_stamp = i['ctime'].strftime("%Y%m%d%H%M")
-
- SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
- RECIPIENT = receiver
- AWS_REGION = "us-east-1"
- CHARSET = "UTF-8"
- client = boto3.client('ses',region_name=AWS_REGION)
-
- context = {}
- context["time_stamp"] = time_stamp
- context["name"] = html.name
- context["phone"] = html.phone
- context["mail"] = html.email
- context["sex"] = html.sex
- context["area"] = html.area
- context["type"] = html.type
- context["mode"] = html.mode
- context["budget"] = html.budget
- context["pin"] = html.pin
- context["room"] = html.room
- context["hall"] = html.hall
- context["restroom"] = html.restroom
- context["style"] = html.style
- context["time"] = str(html.time)
-
- msg = MIMEMultipart()
- msg["Subject"] = "富邦裝修需求貸款通知信"
- msg["From"] = "noreply@hhh.com.tw"
- msg["To"] = RECIPIENT
- body = MIMEText(render_template("email.html",{"context":context}), "html")
- msg.attach(body)
- response = client.send_raw_email(
- Source=msg["From"],
- Destinations=[msg["To"]],
- RawMessage={"Data": msg.as_string()}
- )
- @app.post('/hhh/mail/deco/v2')
- async def hhh_send_mail(html: Html2, content_type: Optional[str] =Header(None)):
- ##TBD
- ## db.ptt.cx/hhh.deco_request
- db = dataset.connect(choozmo_db)
- query = "UPDATE deco_request SET loan=1 Where phone = \'" + str(html.phone) + "\' AND email = \'" + html.email +"\' AND name = \'" + html.name +"\'"
- result = db.query(query)
- 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;"
- result = db.query(query)
- for i in result:
- time_stamp = i['time_stamp'].strftime("%Y%m%d%H%M")
- SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
- RECIPIENT = receiver
- AWS_REGION = "us-east-1"
- CHARSET = "UTF-8"
- client = boto3.client('ses',region_name=AWS_REGION)
-
- context = {}
- context["time_stamp"] = time_stamp
- context["name"] = html.name
- context["phone"] = html.phone
- context["mail"] = html.email
- context["loc"] = html.loc
- context["h_class"] = html.h_class
- context["size"] = html.size
-
- msg = MIMEMultipart()
- msg["Subject"] = "富邦裝修需求貸款通知信"
- msg["From"] = "noreply@hhh.com.tw"
- msg["To"] = RECIPIENT
-
- body = MIMEText(render_template("email_v2.html",{"context":context}), "html")
- msg.attach(body)
- response = client.send_raw_email(
- Source=msg["From"],
- Destinations=[msg["To"]],
- RawMessage={"Data": msg.as_string()}
- )
- # print(response)
- @app.post('/hhh/mail/deco/v3')
- async def hhh_send_mail(html: Html3, content_type: Optional[str] =Header(None)):
- ##TBD
- ## alter xoops.calulator loon set 1
- db = dataset.connect(hhh_db)
- query = "UPDATE calculator SET loan=1 Where phone = \'" + str(html.phone) + "\' AND email = \'" + html.email +"\' AND name = \'" + html.name +"\'"
- result = db.query(query)
- 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;"
- result = db.query(query)
- for i in result:
- time_stamp = i['create_time'].strftime("%Y%m%d%H%M")
- SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
- RECIPIENT = receiver
- AWS_REGION = "us-east-1"
- CHARSET = "UTF-8"
- client = boto3.client('ses',region_name=AWS_REGION)
-
- context = {}
- context["time_stamp"] = time_stamp
- context["name"] = html.name
- context["phone"] = html.phone
- context["mail"] = html.email
- context["house_type"] = html.house_type
- context["location"] = html.location
- context["house_year"] = html.house_year
- context["pin"] = html.pin
- context["change_area"] = html.change_area
- context["floor"] = html.floor
- context["compartment"] = html.compartment
- context["ceiling"] = html.ceiling
- context["room"] = html.room
- context["liveroom"] = html.liveroom
- context["bathroom"] = html.bathroom
-
- msg = MIMEMultipart()
- msg["Subject"] = "富邦裝修需求貸款通知信"
- msg["From"] = "noreply@hhh.com.tw"
- msg["To"] = RECIPIENT
-
- body = MIMEText(render_template("email_v3.html",{"context":context}), "html")
- msg.attach(body)
- response = client.send_raw_email(
- Source=msg["From"],
- Destinations=[msg["To"]],
- RawMessage={"Data": msg.as_string()}
- )
- # @app.post('/hhh/mail/deco/v4')
- # async def hhh_send_mail(html: Html3):
- # SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
- # RECIPIENT = html.email
- # AWS_REGION = "us-east-1"
- # CHARSET = "UTF-8"
- # client = boto3.client('ses',region_name=AWS_REGION)
-
- # context = {}
- # context["name"] = html.name
- # context["phone"] = html.phone
- # context["mail"] = html.email
- # context["house_type"] = html.house_type
- # context["location"] = html.location
- # context["house_year"] = html.house_year
- # context["pin"] = html.pin
- # context["change_area"] = html.change_area
- # context["floor"] = html.floor
- # context["compartment"] = html.compartment
- # context["ceiling"] = html.ceiling
- # context["room"] = html.room
- # context["liveroom"] = html.liveroom
- # context["bathroom"] = html.bathroom
-
- # msg = MIMEMultipart()
- # msg["Subject"] = "富邦裝修需求貸款通知信"
- # msg["From"] = "noreply@hhh.com.tw"
- # msg["To"] = RECIPIENT
-
- # body = MIMEText(render_template("email_v4.html",{"context":context}), "html")
- # msg.attach(body)
- # response = client.send_raw_email(
- # Source=msg["From"],
- # Destinations=[msg["To"]],
- # RawMessage={"Data": msg.as_string()}
- # )
|