-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
96 lines (69 loc) · 2.83 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
from email_validator import validate_email, EmailNotValidError
from flask import Flask, render_template, request, flash, url_for, redirect
from flask_debugtoolbar import DebugToolbarExtension
from flask_mail import Mail, Message
def env(x):
return os.environ.get(x)
app = Flask(__name__)
# noinspection SpellCheckingInspection
app.config["SECRET_KEY"] = "1NDckZzNTIyNzM1kcHRgfvMpx7" # os.urandom(24)
app.config["DEBUG_TB_INTERCEPT_REDIRECTS"] = False # Disable redirect interception
app.config["MAIL_SERVER"] = env("MAIL_SERVER")
app.config["MAIL_PORT"] = env("MAIL_PORT")
app.config["MAIL_USE_TLS"] = env("MAIL_USE_TLS")
app.config["MAIL_USE_SSL"] = env("MAIL_USE_SSL")
app.config["MAIL_USERNAME"] = env("MAIL_USERNAME")
app.config["MAIL_PASSWORD"] = env("MAIL_PASSWORD")
app.config["MAIL_DEFAULT_SENDER"] = env("MAIL_DEFAULT_SENDER")
mail = Mail(app)
toolbar = DebugToolbarExtension(app)
# noinspection PyUnresolvedReferences
def send_email(to, subject, template, **kwargs):
msg = Message(subject, recipients=[to])
# template file is exists?
the_file = f"templates/contact/emails/{template}"
if not os.path.exists(f"{the_file}.txt") or not os.path.exists(f"{the_file}.html"):
raise FileNotFoundError(f"Template file not found: {template}")
msg.body = render_template(f"contact/emails/{template}.txt", **kwargs)
msg.html = render_template(f"contact/emails/{template}.html", **kwargs)
mail.send(msg)
@app.route("/", methods=["GET"], endpoint="hello-default-ep")
def root():
return render_template("hello.html", name="Anon")
@app.route("/<string:name>", methods=["GET"], endpoint="hello-ep")
def user(name: str):
return render_template("hello.html", name=name)
@app.route("/contact")
def contact():
return render_template("contact/contact.html")
@app.route("/contact/complete", methods=["GET", "POST"])
def contact_complete():
if request.method != "POST":
return redirect(url_for("contact_complete"))
name = request.form.get("name")
email = request.form.get("email")
message = request.form.get("message")
is_valid = True
field_of_error = None
if not name or not message:
is_valid = False
field_of_error = "name" if not name else "email" if not email else "message"
flash(f"Please fill the {field_of_error} field", "error")
try:
validate_email(email)
except EmailNotValidError:
is_valid = False
field_of_error = "email"
if not is_valid:
flash(f"Please enter a valid {field_of_error}", "error")
return redirect(url_for("contact"))
send_email(
email,
"Thank you for contacting us!",
"contact_email",
name=name,
message=message
)
flash("You are already sent message once over!", "success")
return render_template("contact/contact_complete.html")