-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
195 lines (144 loc) · 5.38 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from flask import Flask, render_template, request, redirect, g, flash
from dynaconf import Dynaconf
import pymysql
import pymysql.cursors
import flask_login
from flask_login import UserMixin, login_user, logout_user, login_required, current_user
settings = Dynaconf(settings_file=['settings.toml'])
app = Flask(__name__)
app.secret_key = 'your_secret_key'
def get_db():
'''Opens a new database connection per request.'''
if not hasattr(g, 'db'):
g.db = connect_db()
return g.db
def connect_db():
return pymysql.connect(
host="10.100.33.60",
user=settings.db_user,
password=str(settings.db_password),
database=settings.db_name,
cursorclass=pymysql.cursors.DictCursor,
autocommit=True
)
class User(UserMixin):
def __init__(self, user_id, username, email):
self.id = user_id
self.username = username
self.email = email
@staticmethod
def get(user_id):
cursor = get_db().cursor()
cursor.execute(f"SELECT * FROM `Users` WHERE id = {user_id}")
user_data = cursor.fetchone()
cursor.close()
if user_data:
return User(user_data['id'], user_data['username'], user_data['email'])
else:
return None
# Initialize Flask-Login
login_manager = flask_login.LoginManager(app)
@login_manager.user_loader
def load_user(user_id):
return User.get(user_id)
@app.route('/')
def landing_page():
return render_template('landing.html.jinja')
@app.route('/maps')
def maps_page():
cursor = get_db().cursor()
cursor.execute("SELECT * FROM `Locations`")
result = cursor.fetchall()
return render_template('maps.html.jinja', locations = result)
@app.route('/locations')
def locations_page():
connection = connect_db()
with connection.cursor() as cursor:
sql = "SELECT name, address, description FROM Locations"
cursor.execute(sql)
locations_data = cursor.fetchall()
connection.close()
return render_template('locations.html.jinja', locations=locations_data)
@app.route('/contact')
def contact_page():
return render_template('contact.html.jinja')
@app.route('/feedback')
def feedback_page():
connection = connect_db()
try:
with connection.cursor() as cursor:
sql = "SELECT r.user_id, r.review_text, r.rating, u.username FROM `Reviews` r JOIN `Users` u ON r.user_id = u.id"
cursor.execute(sql)
reviews_data = cursor.fetchall()
finally:
connection.close()
return render_template('feedback.html.jinja', reviews=reviews_data)
@app.route('/aboutus')
def aboutus_page():
return render_template('aboutus.html.jinja')
@app.route('/questionnnaire')
def questionnnaire_page():
return render_template('questionnnaire.html.jinja')
@app.route('/signup', methods=['GET', 'POST'])
def signup_page():
if request.method == 'POST':
email = request.form['email']
username = request.form['username']
password = request.form['password']
cursor = get_db().cursor()
cursor.execute(
f"INSERT INTO `Users` (email, username, password) VALUES ('{email}', '{username}', '{password}')")
cursor.close()
get_db().commit()
return redirect('/signin')
return render_template('signup.html.jinja')
@app.route('/signin', methods=['GET', 'POST'])
def signin_page():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
cursor = get_db().cursor()
cursor.execute(f"SELECT * FROM `Users` WHERE username='{username}'")
result = cursor.fetchone()
cursor.close()
if result and result['password'] == password:
user = User(result['id'], result['username'], result['email'])
login_user(user)
return redirect('/')
else:
error = "Invalid username or password. Please try again."
return render_template('signin.html.jinja', error=error)
return render_template('signin.html.jinja')
@app.route('/submit_review', methods=['POST'])
@login_required
def submit_review():
if request.method == 'POST':
review_text = request.form['review']
rating = request.form.get('rate')
if not review_text or not rating:
flash('Please provide both a review and a rating.', 'error')
return redirect('/feedback')
if current_user.is_authenticated:
user_id = current_user.id
connection = connect_db()
try:
with connection.cursor() as cursor:
sql = "INSERT INTO `Reviews` (user_id, review_text, rating) VALUES (%s, %s, %s)"
cursor.execute(sql, (user_id, review_text, rating))
print(sql)
connection.commit()
flash('Review submitted successfully.', 'success')
return redirect('/feedback')
except Exception as e:
flash(f'An error occurred: {str(e)}', 'error')
return redirect('/feedback')
finally:
connection.close()
else:
flash('You need to log in to submit a review.', 'error')
return redirect('/signin')
@app.route('/thankyou')
def thankyou():
return render_template('Thankyou.html.jinja')
if __name__ == '__main__':
app.run(debug=True)