-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (41 loc) · 1.9 KB
/
main.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
from days.day_060.files.helpers import *
def day_060():
title("FLASK: FORMS AND REQUESTS")
template_folder_path = os.path.join(os.path.dirname(__file__), "files", "templates")
static_folder_path = os.path.join(os.path.dirname(__file__), "files", "static")
app = Flask(
__name__, template_folder=template_folder_path, static_folder=static_folder_path
)
load_dotenv()
SENDER_EMAIL = os.getenv("SENDER_EMAIL")
GOOGLE_APP_PASSWORD = os.getenv("GOOGLE_APP_PASSWORD")
RECEIVER_EMAIL = os.getenv("RECEIVER_EMAIL")
posts = requests.get("https://api.npoint.io/c790b4d5cab58020d391").json()
def send_email(name, email, phone, message):
email_message = f"Subject:New Message\n\nName: {name}\nEmail: {email}\nPhone: {phone}\nMessage:{message}"
with smtplib.SMTP("smtp.gmail.com") as connection:
connection.starttls()
connection.login(SENDER_EMAIL, GOOGLE_APP_PASSWORD)
connection.sendmail(SENDER_EMAIL, RECEIVER_EMAIL, email_message)
@app.route("/")
def get_all_posts():
return render_template("index.html", all_posts=posts)
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/contact", methods=["GET", "POST"])
def contact():
if request.method == "POST":
data = request.form
send_email(data["name"], data["email"], data["phone"], data["message"])
return render_template("contact.html", msg_sent=True)
return render_template("contact.html", msg_sent=False)
@app.route("/post/<int:index>")
def show_post(index):
requested_post = None
for blog_post in posts:
if blog_post["id"] == index:
requested_post = blog_post
return render_template("post.html", post=requested_post)
if __name__ == "days.day_060.main":
app.run(debug=True, use_reloader=False)