conradlan преди 3 години
родител
ревизия
5c48ae7f8e
променени са 6 файла, в които са добавени 393 реда и са изтрити 7 реда
  1. 65 7
      main.py
  2. 269 0
      main_2.py
  3. 1 0
      templates/email.html
  4. 1 0
      templates/email_v2.html
  5. 1 0
      templates/email_v3.html
  6. 56 0
      templates/email_v4.html

+ 65 - 7
main.py

@@ -16,7 +16,8 @@ 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):
@@ -24,7 +25,8 @@ def render_template(template, kwargs):
     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(
@@ -89,16 +91,21 @@ async def hhh_send_mail(html: Html):
     ##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 = html.email
+    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
@@ -137,13 +144,18 @@ async def hhh_send_mail(html: Html2, content_type: Optional[str] =Header(None)):
     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 = html.email
+    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
@@ -174,13 +186,19 @@ async def hhh_send_mail(html: Html3, content_type: Optional[str] =Header(None)):
     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 = html.email
+    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
@@ -208,4 +226,44 @@ async def hhh_send_mail(html: Html3, content_type: Optional[str] =Header(None)):
         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()}
+#     )

+ 269 - 0
main_2.py

@@ -0,0 +1,269 @@
+
+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()}
+#     )

+ 1 - 0
templates/email.html

@@ -9,6 +9,7 @@
         <h2>Value: {{value}}</h2>
       {% endfor %} -->
       <h1>客戶資料</h1>
+      <div>編號:{{context.time_stamp}}</div>
       <div>姓名:{{context.name}}</div>
       <div>電話:{{context.phone}}</div>
       <div>郵件:{{context.mail}}</div>

+ 1 - 0
templates/email_v2.html

@@ -5,6 +5,7 @@
     </head>
     <body>
       <h1>客戶資料</h1>
+      <div>編號:{{context.time_stamp}}</div>
       <div>姓名:{{context.name}}</div>
       <div>電話:{{context.phone}}</div>
       <div>郵件:{{context.mail}}</div>

+ 1 - 0
templates/email_v3.html

@@ -9,6 +9,7 @@
         <h2>Value: {{value}}</h2>
       {% endfor %} -->
       <h1>客戶資料</h1>
+      <div>編號:{{context.time_stamp}}</div>
       <div>姓名:{{context.name}}</div>
       <div>電話:{{context.phone}}</div>
       <div>郵件:{{context.mail}}</div>

+ 56 - 0
templates/email_v4.html

@@ -0,0 +1,56 @@
+<!DOCTYPE html>
+<html>
+    <head>
+		<title>index</title>
+    </head>
+    <body>
+    {% for Key, Value in context.items() %}
+      <h1>Key: {{key}}</h1>
+      <h2>Value: {{value}}</h2>
+    {% endfor %}
+      <!-- <h1>客戶資料</h1>
+      <div>姓名:{{context.name}}</div>
+      <div>電話:{{context.phone}}</div>
+      <div>郵件:{{context.mail}}</div> -->
+      <!-- <div>性別:{{context.sex}}</div> -->
+      <!-- <h1>裝修資料</h1>
+      <table>
+        <tr>
+          <th>地區</th>
+          <th>{{context.location}}</th>
+        </tr>
+        <tr>
+          <th>房屋類型</th>
+          <th>{{context.house_type}}</th>
+        </tr>
+        <tr>
+          <th>屋齡</th>
+          <th>{{context.house_year}}</th>
+        </tr>
+        <tr>
+          <th>變動裝修</th>
+          <th>{{context.change_area}}</th>
+        </tr>
+        <tr>
+          <th>坪數</th>
+          <th>{{context.pin}}</th>
+        </tr>
+        <tr>
+          <th>房、廳、衛</th>
+          <th>{{context.room}}房{{context.liveroom}}廳{{context.bathroom}}衛</th>
+        </tr>
+        <tr>
+          <th>天花板</th>
+          <th>{{context.ceiling}}</th>
+        </tr>
+        <tr>
+          <th>地板</th>
+          <th>{{context.floor}}</th>
+        </tr>
+        <tr>
+          <th>隔間</th>
+          <th>{{context.compartment}}</th>
+        </tr>
+      </table> -->
+    </body>
+</html>