Browse Source

add loan flag

conradlan 3 years ago
parent
commit
7ea70a6b13
3 changed files with 221 additions and 96 deletions
  1. 4 0
      .gitignore
  2. 161 96
      main.py
  3. 56 0
      templates/email_v3.html

+ 4 - 0
.gitignore

@@ -1 +1,5 @@
 pa.txt
+.env/
+__pycache__/
+.vscode/
+service-account-credentials.json

+ 161 - 96
main.py

@@ -1,12 +1,29 @@
 
 import boto3
-from fastapi import Form, FastAPI
+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')
+
+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)
 
 
 app = FastAPI()
@@ -21,19 +38,19 @@ class User(BaseModel):
     name: str
     email: EmailStr
     phone: str
-    sex: str
+    sex: Optional[str] = None
 
 class Html(User):
-    area: str
-    type: str
-    mode: str
-    budget: str
-    pin: float
-    room: str
-    hall: str
-    restroom: str
-    style: str
-    time: date
+    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
@@ -44,103 +61,151 @@ 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):
-    def render_template(template, kwargs):
-        import jinja2
-        templateLoader = jinja2.FileSystemLoader(searchpath="./templates")
-        templateEnv = jinja2.Environment(loader=templateLoader)
-        templ = templateEnv.get_template(template)
-        return templ.render(kwargs)
-
+    ##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 +"\'"
+    result = db.query(query)
+    
     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)
     
-    try:
-        context = {}
-        
-        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()}
-        )
-        print(response)
-    # Display an error if something goes wrong. 
-    except ClientError as e:
-        print(e.response['Error']['Message'])
-    else:
-        print("Email sent! Message ID:"),
-        print(response['MessageId'])
+    context = {}
+    
+    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(name: str = Form(...), phone: str = Form(...), email: EmailStr = Form(...), loc: str = Form(...),h_class: str = Form(...),size: float = Form(...)):
-    def render_template(template, kwargs):
-        import jinja2
-        templateLoader = jinja2.FileSystemLoader(searchpath="./templates")
-        templateEnv = jinja2.Environment(loader=templateLoader)
-        templ = templateEnv.get_template(template)
-        return templ.render(kwargs)
+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)
+
     SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
-    RECIPIENT = email
+    RECIPIENT = html.email
     AWS_REGION = "us-east-1"
     CHARSET = "UTF-8"
     client = boto3.client('ses',region_name=AWS_REGION)
     
-    try:
-        context = {}
-        
-        context["name"] = name
-        context["phone"] = phone
-        context["mail"] = email
-        context["loc"] = loc
-        context["h_class"] = h_class
-        context["size"] = 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)
-    # Display an error if something goes wrong. 
-    except ClientError as e:
-        print(e.response['Error']['Message'])
-    else:
-        print("Email sent! Message ID:"),
-        print(response['MessageId'])
+    context = {}
+    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)
+
+    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_v3.html",{"context":context}), "html")
+    msg.attach(body)
+
+    response = client.send_raw_email(
+        Source=msg["From"],
+        Destinations=[msg["To"]],
+        RawMessage={"Data": msg.as_string()}
+    )

+ 56 - 0
templates/email_v3.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>