forked from nishith-p-shetty/flask-feedback-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
285 lines (240 loc) · 8.46 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import psycopg2
import os
from datetime import datetime as dt
from flask import Flask, render_template, request, session, redirect, url_for, flash
from dotenv import load_dotenv
load_dotenv()
NO_OF_TEAMS = 3
ADMIN_USERNAME = os.environ.get("FEEDBACK_FORM_ADMIN_USERNAME")
ADMIN_PASSWORD = os.environ.get("FEEDBACK_FORM_ADMIN_PASSWORD")
SECRET_KEY = os.environ.get("FEEDBACK_FORM_SECRET_KEY")
DB_HOST = os.environ.get("FEEDBACK_FORM_DB_HOST")
DB_NAME = os.environ.get("FEEDBACK_FORM_DB_NAME")
DB_USER = os.environ.get("FEEDBACK_FORM_DB_USER")
DB_PASSWORD = os.environ.get("FEEDBACK_FORM_DB_PASSWORD")
conn = psycopg2.connect(
host=DB_HOST, user=DB_USER, password=DB_PASSWORD, database=DB_NAME, sslmode="prefer"
)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS reviewer (
reviewer_id SERIAL PRIMARY KEY,
reviewer_name VARCHAR(30),
review_time TIMESTAMP
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS team (
team_id SERIAL PRIMARY KEY,
team_number INTEGER UNIQUE
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS feedbacks (
feedback_id SERIAL PRIMARY KEY,
reviewer_id INTEGER,
team_id INTEGER,
field1_rating SMALLINT,
field2_rating SMALLINT,
field3_rating SMALLINT,
field4_rating SMALLINT,
field5_rating SMALLINT,
average_rating REAL,
feedback VARCHAR(255),
FOREIGN KEY (reviewer_id) REFERENCES reviewer(reviewer_id),
FOREIGN KEY (team_id) REFERENCES team(team_id)
);
""")
for team_id in range(1, NO_OF_TEAMS + 1):
cursor.execute(
""" INSERT INTO team (team_number) VALUES (%s) ON CONFLICT (team_number) DO NOTHING""",
(team_id,),
)
cursor.close()
conn.commit()
conn.close()
app = Flask(__name__, template_folder="public/templates", static_folder="public/static")
app.secret_key = SECRET_KEY
app.config.update(
SESSION_COOKIE_SECURE=True,
SESSION_COOKIE_HTTPONLY=True,
SESSION_COOKIE_SAMESITE='Lax',
SESSION_PROTECTION='strong'
)
@app.route("/", methods=["GET"])
def index():
return render_template("index.html", no_t=NO_OF_TEAMS)
@app.route("/submit", methods=["POST"])
def submit():
form_data = request.form
conn = psycopg2.connect(
host=DB_HOST, user=DB_USER, password=DB_PASSWORD, database=DB_NAME
)
cursor = conn.cursor()
cursor.execute(
"INSERT INTO reviewer (reviewer_name, review_time) VALUES (%s, %s) RETURNING reviewer_id",
(form_data["reviewer_name"], dt.now()),
)
reviewer_row = cursor.fetchone()
reviewer_id = reviewer_row[0] if reviewer_row is not None else None
feedbacks = []
for team_id in range(1, NO_OF_TEAMS + 1):
field1_rating = int(form_data.get(f"team_id{team_id}_field1_rating", 1))
field2_rating = int(form_data.get(f"team_id{team_id}_field2_rating", 1))
field3_rating = int(form_data.get(f"team_id{team_id}_field3_rating", 1))
field4_rating = int(form_data.get(f"team_id{team_id}_field4_rating", 1))
field5_rating = int(form_data.get(f"team_id{team_id}_field5_rating", 1))
feedback = form_data.get(f"team_id{team_id}_feedback")
# cursor.execute('''''')
feedbacks.append(
(
reviewer_id,
team_id,
field1_rating,
field2_rating,
field3_rating,
field4_rating,
field5_rating,
field1_rating,
field2_rating,
field3_rating,
field4_rating,
field5_rating,
feedback,
)
)
cursor.executemany(
"""INSERT INTO feedbacks
(reviewer_id, team_id, field1_rating, field2_rating, field3_rating, field4_rating, field5_rating, average_rating, feedback)
VALUES (%s, %s, %s, %s, %s, %s, %s, (SELECT AVG(s) FROM UNNEST(ARRAY[%s, %s, %s, %s, %s]) s), %s)""",
feedbacks,
)
conn.commit()
cursor.close()
conn.close()
return render_template("success.html", name=form_data["reviewer_name"])
@app.route("/login", methods=["GET", "POST"])
def login():
error = None
if request.method == "POST":
if (
request.form["username"] != ADMIN_USERNAME
or request.form["password"] != ADMIN_PASSWORD
):
error = "Invalid Credentials. Please try again."
else:
session["logged_in"] = True
flash("Login Successful")
return redirect(url_for("dashboard"))
return render_template("login.html", error=error)
@app.route("/dashboard", methods=["GET", "POST"])
def dashboard():
page = request.args.get("page", 1, type=int)
search = request.args.get("search", "", type=str).strip().lower()
conn = psycopg2.connect(
database=DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST
)
cursor = conn.cursor()
# Caluculate the total number of pages
cursor.execute(
"""SELECT COUNT(*) FROM feedbacks JOIN reviewer
ON
feedbacks.reviewer_id = reviewer.reviewer_id
WHERE LOWER(reviewer_name) LIKE %s""",
("%" + search + "%",),
)
total_feedbacks = cursor.fetchone()
total_feedbacks = total_feedbacks[0] if total_feedbacks is not None else 0
total_pages = (total_feedbacks // 5) + 1
# Fetch 5 feedbacks with reviewer and team details
cursor.execute(
"""
SELECT
feedbacks.feedback_id,
reviewer.reviewer_name,
team.team_number,
feedbacks.field1_rating,
feedbacks.field2_rating,
feedbacks.field3_rating,
feedbacks.field4_rating,
feedbacks.field5_rating,
feedbacks.average_rating,
reviewer.review_time,
feedbacks.feedback
FROM feedbacks
INNER JOIN reviewer ON feedbacks.reviewer_id = reviewer.reviewer_id
INNER JOIN team ON feedbacks.team_id = team.team_id
WHERE LOWER(reviewer.reviewer_name) LIKE %s
ORDER BY reviewer.review_time DESC
LIMIT 20 OFFSET %s;
""",
("%" + search + "%", (page - 1) * 5),
)
data = cursor.fetchall()
cursor.execute("""
(SELECT 'average_rating' AS rating_type, team_id, AVG(average_rating) AS rating
FROM feedbacks
GROUP BY team_id
ORDER BY rating DESC)
UNION ALL
(SELECT 'field1_rating' AS rating_type, team_id, AVG(field1_rating) AS rating
FROM feedbacks
GROUP BY team_id
ORDER BY rating DESC)
UNION ALL
(SELECT 'field2_rating' AS rating_type, team_id, AVG(field2_rating) AS rating
FROM feedbacks
GROUP BY team_id
ORDER BY rating DESC)
UNION ALL
(SELECT 'field3_rating' AS rating_type, team_id, AVG(field3_rating) AS rating
FROM feedbacks
GROUP BY team_id
ORDER BY rating DESC)
UNION ALL
(SELECT 'field4_rating' AS rating_type, team_id, AVG(field4_rating) AS rating
FROM feedbacks
GROUP BY team_id
ORDER BY rating DESC)
UNION ALL
(SELECT 'field5_rating' AS rating_type, team_id, AVG(field5_rating) AS rating
FROM feedbacks
GROUP BY team_id
ORDER BY rating DESC);
""")
best_teams = cursor.fetchall()
cursor.close()
conn.close()
return render_template(
"dashboard.html",
best_teams=best_teams,
data=data,
total_pages=total_pages,
page=page,
search=search,
no_of_teams=NO_OF_TEAMS,
)
@app.route("/logout")
def logout():
session.pop("logged_in", None)
flash("Logout Successful")
return redirect(url_for("login"))
@app.route("/delete_feedback/<int:feedback_id>", methods=["GET"])
def delete_feedback(feedback_id):
if not session.get("logged_in"):
flash("Please login to delete feedback.")
return redirect(url_for("login"))
conn = psycopg2.connect(
database=DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST
)
cursor = conn.cursor()
cursor.execute("DELETE FROM feedbacks WHERE feedback_id = %s", (feedback_id,))
conn.commit()
cursor.close()
conn.close()
flash("Feedback deleted successfully.")
return redirect(url_for("dashboard"))
if __name__ == "__main__":
host = os.environ.get("HOST", "0.0.0.0")
port = int(os.environ.get("PORT", 3000))
app.run(host=host, port=port)