-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
147 lines (114 loc) · 4.13 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import webbrowser
import talisman as talisman
import werkzeug
from flask import Flask, render_template, request, json, session, send_file, g, url_for
from flaskext.mysql import MySQL
app = Flask(__name__)
mysql = MySQL()
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = ''
app.config['MYSQL_DATABASE_DB'] = 'Feedback'
app.config['MYSQL_DATABASE_HOST'] = 'db_server'
app.config['MYSQL_PORT'] = '3306'
mysql.init_app(app)
@app.route("/")
def main():
if not session.get('logged_in'):
return render_template('login.html')
return render_template('login.html')
@app.errorhandler(500)
def handle_internal_server_error(error):
return str(error)
@app.route('/home', methods=['POST'])
def login():
_username = request.form['username']
_password = request.form['inputPassword']
if _username and _password:
conn = mysql.connect()
cursor = conn.cursor()
all_data = None
query = "select user_name from users where user_username='{0}' and user_password='{1}'".format(_username,
_password)
try:
cursor.execute(query)
data = cursor.fetchone()
except Exception as ex:
raise Exception(query, ex)
# Populate all hotel reviews
all_data = fetch_reviews(conn, cursor)
conn.close()
if data is not None:
session['logged_in'] = True
return render_template('index.html', items=all_data, section="hero")
else:
return "No user found"
else:
return json.dumps({'html': '<span>Enter the required fields</span>'})
def fetch_reviews(conn, cursor):
query = "select review_hotel, review_city, review_body, review_rating from reviews ORDER BY review_id DESC limit 10"
try:
cursor.execute(query)
all_data = cursor.fetchall()
except:
conn.rollback()
return all_data
@app.route('/home/submitted_review', methods=['POST'])
def writeblog():
_review_hotel = request.form['hotel']
_review_city = request.form['city']
_review_body = request.form['review']
_review_rating = request.form['rating']
conn = mysql.connect()
cursor = conn.cursor()
query = "insert into reviews (review_hotel, review_city, review_body, review_rating) values ('{0}', '{1}', '{2}', '{3}');".format(
_review_hotel, _review_city, _review_body, _review_rating)
try:
cursor.execute(query)
conn.commit()
except:
conn.rollback()
all_data = fetch_reviews(conn, cursor)
conn.close()
return render_template('index.html', items=all_data)
@app.route('/home', methods=['GET'])
def search():
data = None
if not session.get('logged_in'):
return render_template('login.html')
try:
_search_term = request.args['search_input']
except werkzeug.exceptions.BadRequest:
conn = mysql.connect()
cursor = conn.cursor()
all_data = fetch_reviews(conn, cursor)
conn.close()
return render_template('index.html', items=all_data)
conn = mysql.connect()
cursor = conn.cursor()
query = "select review_hotel, review_city, review_body, review_rating from reviews where review_hotel like '%{0}%' or review_city like '%{0}%' or review_body like '%{0}%' ORDER BY review_id DESC limit 10".format(
_search_term)
try:
cursor.execute(query)
data = cursor.fetchall()
except:
conn.rollback()
conn.close()
g.search_term = _search_term
return render_template('index.html', items=data, section="features")
@app.route("/logout")
def logout():
session['logged_in'] = False
app.secret_key = os.urandom(12)
return render_template('login.html')
@app.route('/home/getfiles', methods=['GET'])
def get_file():
if not session.get('logged_in'):
return render_template('login.html')
else:
file = request.args['file']
return send_file(file)
if __name__ == "__main__":
app.secret_key = os.urandom(12)
app.run(host='0.0.0.0', debug=True)