2020-04-15 12:36:30 +08:00
|
|
|
from os import getenv
|
|
|
|
|
2020-04-15 11:40:34 +08:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
from flask import Flask
|
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
|
2020-04-15 11:40:34 +08:00
|
|
|
from flask_mail import Mail
|
2020-04-06 20:14:19 +08:00
|
|
|
from flask_mail import Message
|
2020-04-15 11:40:34 +08:00
|
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
2024-01-08 16:47:42 +08:00
|
|
|
from jinja2.utils import htmlsafe_json_dumps
|
2020-04-06 20:14:19 +08:00
|
|
|
|
2020-04-15 12:36:30 +08:00
|
|
|
|
2020-04-15 11:40:34 +08:00
|
|
|
load_dotenv()
|
2020-04-06 20:14:19 +08:00
|
|
|
|
2020-04-15 11:40:34 +08:00
|
|
|
app = Flask(__name__)
|
|
|
|
app.config.update(
|
2020-04-15 12:36:30 +08:00
|
|
|
DEBUG=getenv("FLASK_DEBUG") == "True",
|
2020-04-15 11:40:34 +08:00
|
|
|
MAIL_SERVER=getenv("FLASK_MAIL_SERVER"),
|
|
|
|
MAIL_PORT=getenv("FLASK_MAIL_PORT"),
|
|
|
|
MAIL_USE_SSL=getenv("FLASK_MAIL_USE_SSL"),
|
|
|
|
MAIL_DEBUG=False,
|
|
|
|
MAIL_USERNAME=getenv("FLASK_MAIL_USERNAME"),
|
|
|
|
MAIL_PASSWORD=getenv("FLASK_MAIL_PASSWORD"),
|
2020-04-15 21:17:19 +08:00
|
|
|
MAIL_RECIPIENT=getenv("FLASK_MAIL_RECIPIENT"),
|
|
|
|
MAIL_SENDER=getenv("FLASK_MAIL_SENDER")
|
2020-04-15 11:40:34 +08:00
|
|
|
)
|
|
|
|
app.wsgi_app = ProxyFix(app.wsgi_app)
|
|
|
|
|
|
|
|
mail = Mail(app)
|
|
|
|
|
|
|
|
|
|
|
|
@app.after_request
|
2020-04-15 09:19:03 +08:00
|
|
|
def after(response):
|
|
|
|
response.headers["Access-Control-Allow-Origin"] = "*"
|
|
|
|
response.headers["Access-Control-Allow-Headers"] = "*"
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2020-04-15 11:40:34 +08:00
|
|
|
@app.route("/rfq", methods=["POST"])
|
2020-04-06 20:14:19 +08:00
|
|
|
def send_rfq():
|
|
|
|
payload = request.json
|
2024-01-08 16:47:42 +08:00
|
|
|
payload = json.loads(htmlsafe_json_dumps(payload))
|
2020-04-06 20:14:19 +08:00
|
|
|
|
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)
|
|
|
|
|
2020-04-15 21:17:19 +08:00
|
|
|
if "note" not in payload:
|
|
|
|
resp = jsonify(error="missing note")
|
2020-04-06 20:14:19 +08:00
|
|
|
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-15 21:17:19 +08:00
|
|
|
sender = current_app.config["MAIL_SENDER"]
|
2020-04-06 20:14:19 +08:00
|
|
|
recipient = current_app.config["MAIL_RECIPIENT"]
|
|
|
|
|
|
|
|
msg = Message(
|
2020-04-15 21:17:19 +08:00
|
|
|
"RFQ for Sinara hardware from {}".format(payload["email"]),
|
|
|
|
sender=sender,
|
|
|
|
recipients=[recipient, payload["email"]])
|
|
|
|
msg.body = ("From: {}\nConfiguration: {}\nNote: {}"
|
|
|
|
.format(payload["email"], payload["configuration"], payload["note"]))
|
2020-04-15 09:41:40 +08:00
|
|
|
|
|
|
|
with mail.connect() as conn:
|
|
|
|
conn.send(msg)
|
2020-04-06 20:14:19 +08:00
|
|
|
|
|
|
|
return jsonify("ok")
|