/
app.py
75 lines (56 loc) · 2.05 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
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from send_mail import send_mail
app = Flask(__name__)
ENV = "dev"
if ENV == "dev":
# allow the server to keep reloading
app.debug = True
# conntect to the database
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///site.db"
else:
app.debug = False
# prevent the warming message
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
# define the database model
class Feedback(db.Model):
__tablename__ = "feedback"
id = db.Column(db.Integer, primary_key = True)
customer = db.Column(db.String(200), unique = True)
dealer = db.Column(db.String(200))
rating = db.Column(db.Integer)
comments = db.Column(db.Text())
# constructor
def __init__(self, customer, dealer, rating, comments):
self.customer = customer
self.dealer = dealer
self.rating = rating
self.comments = comments
# show the home page
@app.route("/")
def index():
return render_template("index.html")
@app.route("/submit", methods=["POST"])
def submit():
if request.method == "POST":
customer = request.form["customer"]
dealer = request.form["dealer"]
rating = request.form["rating"]
comments = request.form["comments"]
# validation
if customer == "" or dealer == "":
return render_template("index.html", message="Make sure you filled out all the fields")
# prevent user from submitting twice
if db.session.query(Feedback).filter(Feedback.customer == customer).count() == 0:
data = Feedback(customer, dealer, rating, comments)
# add the data to the database
db.session.add(data)
# save the changes
db.session.commit()
send_mail(customer, dealer, rating, comments)
return render_template("success.html")
return render_template("index.html", message="You have already submitted")
if __name__ == "__main__":
# run the server
app.run()