-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
86 lines (79 loc) · 2.95 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
from flask import Flask, render_template, request, redirect
from flask_mail import Mail,Message
from pymongo import MongoClient
app = Flask(__name__)
app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.comfig["MAIL_PORT"] = 587
app.config["MAIL_USERNAME"] = ""
app.config["MAIL_PASSWORD"] = ""
mail = Mail(app)
my_client = MongoClient("localhost", 27017)
my_db = my_client["calci"] # database
results = my_db["results"] # collection
isLoggedIn = False
credentials = {
"[email protected]":"Steeve#123",
"[email protected]":"Tony#123"
}
@app.route("/", methods=["GET", "POST"])
def homepage():
global isLoggedIn
if request.method == "POST":
log_email = request.form["email"]
log_password = request.form["password"]
if (log_email in credentials) and (credentials[log_email]==log_password):
isLoggedIn = True
return redirect("/calci")
else:
return redirect("/")
else:
return render_template("login.html")
@app.route("/calci", methods=["GET", "POST"])
def calculator():
if isLoggedIn == True:
if request.method == "POST":
n1 = int(request.form["num1"])
opr = request.form["opr"]
n2 = int(request.form["num2"])
msg = Message(subject="Calculation",sender="",recipients=[""])
if opr == "add":
res = f"{n1} + {n2} is {n1+n2}"
results.insert_one({
"number1":n1, "number2":n2, "operator":opr, "output":res
})
msg.body = res
mail.send(msg)
return render_template("index.html", output=res)
elif opr == "sub":
res = f"{n1} - {n2} is {n1-n2}"
results.insert_one({
"number1":n1, "number2":n2, "operator":opr, "output":res
})
msg.body = res
mail.send(msg)
return render_template("index.html", output=res)
elif opr == "mul":
res = f"{n1} x {n2} is {n1*n2}"
results.insert_one({
"number1":n1, "number2":n2, "operator":opr, "output":res
})
msg.body = res
mail.send(msg)
return render_template("index.html", output=res)
elif opr == "div":
try:
res = f"{n1} / {n2} is {n1/n2}"
results.insert_one({
"number1":n1, "number2":n2, "operator":opr, "output":res
})
msg.body = res
mail.send(msg)
return render_template("index.html", output=res)
except Exception as e:
error = "Please change num2 as non-zero"
return render_template("index.html", output=error)
else:
return render_template("index.html")
else:
return redirect("/")
app.run(debug=True)