2020-04-06 20:14:19 +08:00
|
|
|
from flask import current_app
|
|
|
|
from flask import json
|
|
|
|
from flask import jsonify
|
|
|
|
from flask import make_response
|
|
|
|
from flask import request
|
|
|
|
from flask_mail import Message
|
|
|
|
from app.api import bp
|
|
|
|
from app import mail
|
|
|
|
|
|
|
|
|
2020-04-15 09:19:03 +08:00
|
|
|
@bp.after_request
|
|
|
|
def after(response):
|
|
|
|
response.headers["Access-Control-Allow-Origin"] = "*"
|
|
|
|
response.headers["Access-Control-Allow-Headers"] = "*"
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2020-04-06 20:14:19 +08:00
|
|
|
@bp.route("/rfq", methods=["POST"])
|
|
|
|
def send_rfq():
|
|
|
|
payload = request.json
|
|
|
|
payload = json.loads(json.htmlsafe_dumps(payload))
|
|
|
|
|
2020-04-14 13:23:01 +08:00
|
|
|
if payload is None:
|
|
|
|
resp = jsonify(error="invalid data")
|
|
|
|
return make_response(resp, 400)
|
|
|
|
|
2020-04-06 20:14:19 +08:00
|
|
|
if "email" not in payload:
|
|
|
|
resp = jsonify(error="missing email")
|
|
|
|
return make_response(resp, 400)
|
|
|
|
|
|
|
|
if "body" not in payload:
|
|
|
|
resp = jsonify(error="missing body")
|
|
|
|
return make_response(resp, 400)
|
|
|
|
|
2020-04-15 09:41:40 +08:00
|
|
|
if "configuration" not in payload:
|
|
|
|
resp = jsonify(error="missing configuration")
|
|
|
|
return make_response(resp, 400)
|
|
|
|
|
2020-04-06 20:14:19 +08:00
|
|
|
recipient = current_app.config["MAIL_RECIPIENT"]
|
|
|
|
|
|
|
|
msg = Message(
|
2020-04-14 12:47:45 +08:00
|
|
|
"[ORDER HARDWARE - RFQ from %s]" % payload['email'],
|
2020-04-15 09:25:05 +08:00
|
|
|
reply_to=recipient,
|
2020-04-06 20:14:19 +08:00
|
|
|
sender=payload["email"],
|
|
|
|
recipients=[recipient])
|
|
|
|
msg.body = payload["body"]
|
|
|
|
msg.html = payload["body"]
|
|
|
|
|
2020-04-15 09:41:40 +08:00
|
|
|
msg_client_confirmation = Message(
|
|
|
|
"[M-Labs - Order Hardware]",
|
|
|
|
reply_to=recipient,
|
|
|
|
sender=recipient,
|
|
|
|
recipients=[payload["email"]])
|
|
|
|
msg_client_confirmation.body = "Hello! We've received your request and " \
|
|
|
|
"will be in contact soon. " \
|
|
|
|
"Here is a reminder of your configuration: {}" \
|
|
|
|
"Thank you!".format(payload["configuration"])
|
|
|
|
msg_client_confirmation.html = "Hello!<br />" \
|
|
|
|
"We've received your request and will be in contact soon.<br />" \
|
|
|
|
"Here is a reminder of your configuration: {}<br /><br />" \
|
|
|
|
"Thank you!".format(payload["configuration"])
|
|
|
|
|
|
|
|
with mail.connect() as conn:
|
|
|
|
conn.send(msg)
|
|
|
|
conn.send(msg_client_confirmation)
|
2020-04-06 20:14:19 +08:00
|
|
|
|
|
|
|
return jsonify("ok")
|