from enum import Enum import time from datetime import datetime from typing import Optional from pydantic import BaseModel from fastapi import FastAPI, Query, UploadFile, File from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware import json import urllib.request import dataset,json import dataset from datetime import datetime, timedelta from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.application import MIMEApplication import os import boto3 from botocore.exceptions import ClientError app = FastAPI() origins = [ "http://139.162.121.30", "http://139.162.121.30:8002", ] app.add_middleware( CORSMiddleware, # allow_origins=origins, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # 取得資料庫連線 def get_db_connection(): # Choozmo DB # db = dataset.connect("mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4") # 測試機 DB db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57-cluster.cluster-cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/stage?charset=utf8mb4') # 正式機 DB # db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57-cluster.cluster-cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4') return db # 寄送執行項目預定日修改Email通知 def hhh_send_mail(email, subject, html): SENDER = "Gorgeous Space " RECIPIENT = email AWS_REGION = "us-east-1" CHARSET = "UTF-8" client = boto3.client('ses',region_name=AWS_REGION) try: msg = MIMEMultipart() msg["Subject"] = subject msg["From"] = "noreply@hhh.com.tw" msg["To"] = email # Set message body body = MIMEText(html, "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']) @app.get("/is_set_date_changed_notice") async def is_set_date_changed_notice(exd_id: str = '', exf_id: str = '', set_date: str = ''): print ('exd_id:' + exd_id + ', exf_id:' + exf_id + ', set_date:' + set_date) # test # 取得資料庫連線 db = get_db_connection() subject = "" # 取得執行項目資料 q = "SELECT * \ FROM execute_detail \ WHERE exd_id = " + exd_id q_result_count = len(list(db.query(q))) if q_result_count == 0: # 無合約資料 return for r in db.query(q): print ('set_date = ' + set_date + ', r[set_date] = ' + r['set_date']) # test # 判斷執行項目預定日是否有修改 if set_date == r['set_date']: # 執行項目預定日未修改 print ('執行項目預定日未修改,不須通知') return else: # 執行項目預定日有修改 print ('執行項目預定日有修改,須通知') subject = '[執行項目-預定日修改通知]' + datetime.now().strftime("%Y-%m-%d %H:%M") html = """ """ ''' # 發送通知給負責業務、Kevin、Sam email_list = ['sam@hhh.com.tw','kevin.h@hhh.com.tw'] # 負責業務 print('負責業務: ' + r['sales_man']) email_list.append(r['sales_man']) ''' ''' # 列出該合約底下所有執行項目(含已完成、未完成) q1 = "SELECT * \ FROM execute_detail \ WHERE exf_id=" + exf_id + \ " ORDER BY execute_man" q1_result_count = len(list(db.query(q1))) if q1_result_count > 0: # 有執行項目 html += "
\ \ \ \ \ \ \ \ \ \ \ "; idx = 1 for r1 in db.query(q1): if r1['set_date'] is None: r1['set_date'] = '無' if r1['alert_date_1'] is None: r1['alert_date_1'] = '無' # 執行單位Email if r1['execute_man'] == 'hhh_edit@hhh.com.tw': r1['execute_man'] = '編輯採訪部' elif r1['execute_man'] == 'hhh_mk@hhh.com.tw': r1['execute_man'] = '行銷企劃部' elif r1['execute_man'] == 'hhh_web@hhh.com.tw': r1['execute_man'] = '網站工程部' elif r1['execute_man'] == 'hhh_video@hhh.com.tw': r1['execute_man'] = '影音企劃部' elif r1['execute_man'] == 'agent@hhh.com.tw': r1['execute_man'] = '幸福經紀人' elif r1['execute_man'] == 'hhh_admin@hhh.com.tw': r1['execute_man'] = '行政部' elif r1['execute_man'] == 'hhh_sales@hhh.com.tw': r1['execute_man'] = '業務部' html += "" idx += 1 html += "
#大項目執行項單位是否完成完成人備註預計排程日期第一次提醒日
" + str(idx) + "" + r1['lv1'] + "" + r1['lv2'] + "" + r1['execute_man'] + "" + r1['is_complete'] + "" + r1['complete_man'] + "" + r1['note'] + "" + str(r1['set_date']) + "" + str(r1['alert_date_1']) + "
" html += "

幸福空間經營團隊敬上

※此信為系統自動寄送,請勿直接回信。謝謝!
" html += """ """ ''' ''' # Email清單 email_list = ['mike@choozmo.com'] # 寄送合約狀態修改Email通知 for email in email_list: hhh_send_mail(email, subject, html) '''