/
app.py
66 lines (47 loc) · 2.04 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
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from sendmail import send_mail
app = Flask(__name__) #initialization
ENV = 'prod'
if ENV == 'dev':
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:123@localhost/reviews'
else:
app.debug = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://xrdqedcebdahrn:f5ab7bf6a8b8cf8162aa2c31014f749c85942ce7c76edebd6211780910fa3558@ec2-34-192-72-159.compute-1.amazonaws.com:5432/d3hfifthi1kdvr'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app) # db object
class Feedback(db.Model): # create model of db
__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())
def __init__(self, customer, dealer, rating, comments): # constractor/initializer
self.customer = customer
self.dealer = dealer
self.rating = rating
self.comments = comments
@app.route('/') # create a route
def index():
return render_template('index.html')
@app.route('/submit', methods=['POST'])
def submit(): # user submiting form
if request.method == 'POST':
customer = request.form['customer']
dealer = request.form['dealer']
rating = request.form['rating']
comments = request.form['comments']
# print(customer, dealer, rating, comments)
if customer == '' or dealer =='':
return render_template('index.html', message='PLEASE enter info')
if db.session.query(Feedback).filter(Feedback.customer == customer).count() == 0:
data = Feedback(customer, dealer, rating, comments)
db.session.add(data)
db.session.commit()
send_mail(customer, dealer, rating, comments)
return render_template('success.html')
return render_template('index.html', message='you already submitted feedback')
if __name__ == '__main__':
app.run()