-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
357 lines (300 loc) · 13.6 KB
/
__init__.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from flask import Flask, render_template, request, redirect, flash, url_for, session, jsonify
from mysql_dbconnect import *
from pymysql import escape_string as thwart
from bokeh.embed import components
from graphs import *
from secret_hash import *
from functools import wraps
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import SelectField
import customfunc
import execute_schedule
import _email
import gc
import os
import pickle
import json
import data_calculation
import wordcloud_generate
#Creating the Flask app.
app = Flask(__name__)
class Multi_Select_Form(FlaskForm):
semester_type = SelectField('semester_type', choices=[('default', 'Select any entry'), ('even', 'Even'), ('odd', 'Odd')])
semester_list = SelectField('semester_list', choices=[])
class_list = SelectField('class_list', choices=[])
@app.errorhandler(404)
def page_not_found(error):
return render_template("404.html"), 404
#Created a login_required wrapper function that wraps other functions in-order to force to do login first before accessing any pages.
def login_required(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
return redirect(url_for('admin_login'))
return wrap
@app.route('/admin/triggeranalysis/success/', methods = ['GET', 'POST'])
def admin_trigger_analysis_success():
if request.method == "POST":
return render_template("triggeranalysis_success.html")
@app.route('/admin/triggeranalysis/')
def admin_trigger_analysis():
return render_template("triggeranalysis.html")
#Route and function for about functionality.
@app.route('/admin/about/', methods = ['GET', 'POST'])
def admin_about():
return render_template("about.html")
#Route and function for logs functionality.
@app.route('/admin/logs/', methods = ['GET', 'POST'])
def admin_logs():
c, conn = connect_to("fpsu")
c.execute("SELECT * FROM email_logs")
x = c.fetchall()
c.close()
conn.close()
gc.collect()
logs = []
for row in x:
log = dict(timestamp=str(row[0]), email=str(row[1]), decoded_code=str(row[2]))
logs.append(log)
return render_template("logs.html", logs=logs)
#Route and function for schedule success functionality.
@app.route('/admin/schedule/done/', methods = ['GET', 'POST'])
def admin_schedule_success():
if request.method == "POST":
try:
semester_type = request.form['semester_type']
semester_list = request.form['semester_list']
class_list = request.form['class_list']
class_list_json = []
c, conn = connect_to("pumis")
if semester_list == "all":
if semester_type == "even":
semester_list = ["2", "4", "6", "8"]
for sem in semester_list:
c.execute("SELECT class FROM semester_info WHERE semester IN (%s)", (sem))
x = c.fetchall()
for i in x:
class_list_json_Obj = {}
class_list_json_Obj['semester'] = sem
class_list_json_Obj['class'] = i[0]
class_list_json.append(class_list_json_Obj)
else:
semester_list = ["1", "3", "5", "7"]
for sem in semester_list:
c.execute("SELECT class FROM semester_info WHERE semester IN (%s)", (sem))
x = c.fetchall()
for i in x:
class_list_json_Obj = {}
class_list_json_Obj['semester'] = sem
class_list_json_Obj['class'] = i[0]
class_list_json.append(class_list_json_Obj)
c.close()
conn.close()
gc.collect()
else:
if class_list == "all":
c.execute("SELECT class FROM semester_info WHERE semester IN (%s)", (semester_list))
x = c.fetchall()
for i in x:
class_list_json_Obj = {}
class_list_json_Obj['semester'] = semester_list
class_list_json_Obj['class'] = i[0]
class_list_json.append(class_list_json_Obj)
c.close()
conn.close()
gc.collect()
else:
class_list_json_Obj = {}
class_list_json_Obj['semester'] = semester_list
class_list_json_Obj['class'] = class_list
class_list_json.append(class_list_json_Obj)
semclass_combo = jsonify({"semclass_combo" : class_list_json}).get_data(as_text=True)
emailing_data = execute_schedule.generate_list(semclass_combo)
EMAIL_COUNT = 0
for row in emailing_data:
_email.send_email(row[1], row[2], row[3])
EMAIL_COUNT += 1
err = "Email has been sent successfully."
return render_template("schedule_success.html", err=err, EMAIL_COUNT = EMAIL_COUNT)
except Exception as e:
return render_template("schedule_success.html", err=str(e))
#Route and function for schedule functionality.
@app.route('/admin/schedule/', methods = ['GET', 'POST'])
def admin_schedule():
multi_select_form = Multi_Select_Form()
multi_select_form.semester_list.choices = [("default", "Select any entry")]
multi_select_form.class_list.choices = [("default", "Select any entry")]
return render_template("schedule.html", form=multi_select_form)
#Route for AJAX. Function to handle list of semesters.
@app.route('/admin/schedule/semester/<semester_type>')
def semester_list(semester_type):
semesterArray = []
if semester_type == "even":
semesters = [8, 6, 4, 2]
for sem in semesters:
semesterObj = {}
semesterObj['id'] = int(sem)
semesterObj['name'] = str(sem)
semesterArray.append(semesterObj)
else:
semesters = [7, 5, 3, 1]
for sem in semesters:
semesterObj = {}
semesterObj['id'] = int(sem)
semesterObj['name'] = str(sem)
semesterArray.append(semesterObj)
semesterObj = {}
semesterObj['id'] = "all"
semesterObj['name'] = "All"
semesterArray.append(semesterObj)
return jsonify({"semesters" : semesterArray})
#Route for AJAX. Function to handle list of classes.
@app.route('/admin/schedule/class/<semester>')
def class_list(semester):
classArray = []
classes = customfunc.get_class_list(semester)
for _class in classes:
classObj = {}
classObj['id'] = _class
classObj['name'] = _class
classArray.append(classObj)
classObj = {}
classObj['id'] = "all"
classObj['name'] = "All"
classArray.append(classObj)
return jsonify({"classes" : classArray})
#Route and function for analysis show report functionality. A next page for analysis.html
@app.route('/admin/analysis/showreport/', methods = ['GET', 'POST'])
def admin_analysis_showreport():
if request.method == "POST":
try:
err = ""
faculty_name = request.form['faculty_name']
semester = request.form.getlist('semester')
c, conn = connect_to("pumis")
c.execute("SELECT post, dept, institute, email, id, avatar FROM faculty_new WHERE full_name=(%s)", (faculty_name))
x = c.fetchone()
c.close()
conn.close()
gc.collect()
post = x[0]
dept = x[1]
institute = x[2]
email = x[3]
id = x[4]
avatar = x[5]
dept = dept + " Department"
img_file_link = "/static/images/faculties/" + avatar
faculty_mailto_email = "mailto:" + str(email)
#positiveList
positiveList,negativeList=data_calculation.calculate_values(faculty_name,tuple(semester))
RAW_MESSAGES = data_calculation.save_data()
wordcloud_generate.generate_graph()
return render_template("showreport.html", err=err, faculty_name=faculty_name, img_file_link=img_file_link, faculty_mailto_email=faculty_mailto_email, post=post, dept=dept, institute=institute, id=id, positiveList=positiveList, negativeList=negativeList, RAW_MESSAGES=RAW_MESSAGES)
except Exception as e:
return render_template("showreport.html", err=str(e), faculty_name=faculty_name, img_file_link=img_file_link, faculty_mailto_email=faculty_mailto_email, post=post, dept=dept, institute=institute, id=id, RAW_MESSAGES=RAW_MESSAGES)
#Route and function for analysis functionality.
@app.route('/admin/analysis/', methods = ['GET', 'POST'])
def admin_analysis():
c, conn = connect_to("pumis")
c.execute("SELECT first_name, last_name FROM faculty GROUP BY first_name, last_name")
data = c.fetchall()
c.close()
conn.close()
gc.collect()
full_name_list = list()
for i in data:
full_name_list.append(i[0] + " " + i[1])
return render_template('analysis.html', full_name_list=full_name_list)
#Route and function for create form success functionality.
@app.route('/admin/createform/success/', methods=["GET", "POST"])
def admin_createform_success():
try:
err = ""
if request.method == "POST":
FORMS_LIST_count = customfunc.count_forms()
form_name = request.form['form_name']
new_quest_list = list()
for i in range(1, 11):
new_quest_list.append(str(request.form["new_quest_" + str(i)]))
resp = customfunc.jsonifyforms(form_name, new_quest_list)
if resp == True:
err = "Your form has been saved. FeedBot has started learning from it!"
return render_template("createform_success.html", err=err, FORMS_LIST_count=FORMS_LIST_count)
else:
err = "Something went wrong. Please try agani or contact admin."
return render_template("createform_success.html", err=err, FORMS_LIST_count=FORMS_LIST_count)
return render_template("createform_success.html", err=err, FORMS_LIST_count=FORMS_LIST_count)
except Exception as e:
return render_template("createform_success.html", err=str(e), FORMS_LIST_count=FORMS_LIST_count)
#Route and function for create form functionality.
@app.route('/admin/createform/')
def admin_createform():
FORMS_LIST = customfunc.list_out_forms()
FORMS_LIST_count = customfunc.count_forms()
return render_template("createform.html", FORMS_LIST=FORMS_LIST, FORMS_LIST_count=FORMS_LIST_count)
#Route and function for log out.
@app.route('/admin/logout/')
@login_required
def admin_logout():
err = "You have been successfully logged out."
session.clear()
gc.collect()
return render_template("login.html", err=err)
#Route and function for admin dashboard.
@app.route("/admin/dashboard/", methods=["GET", "POST"])
@login_required #login_required wrapper created above is used here. It should be always placed under the main Flask app wrapper.
def admin_dashboard():
return render_template("dashboard.html")
#Route and function for admin login.
@app.route("/admin/login/", methods=["GET", "POST"])
def admin_login():
err="" #Defining err a login page jinja variable.
try:
#Authenticating username and password with database.
c, conn = connect_to("fpsu")
if request.method=="POST":
username = request.form['username']
c.execute("SELECT password FROM user WHERE email = (%s)", (username))
password_stored = c.fetchone()[0]
password = request.form['password']
if password == password_stored:
session['logged_in'] = True
session['username'] = username
return redirect(url_for('admin_dashboard')) #Redirect to admin dashboard page if login is successful.
else:
err = "Invalid Credentials. Please try again!" #Error message defined.
return render_template("login.html", err=err) #Returning login page again in case of above authentication fails, alongwith err in jinja variable names err.
except Exception as e:
return render_template("login.html", err=str(e)) #DEBUG PURPOSE : Exception Handling with passing exception message into jinja varialbe err.
#Route and function for chatbot with unique_route_string.
@app.route('/chatbot/<unique_route_string>', methods=['GET'])
def chatbot_page(unique_route_string):
try:
c, conn = connect_to()
c.execute("SELECT enrollment_number FROM fpsu.route_string WHERE string=%s", (unique_route_string))
enrollment_number = (c.fetchall()[0])[0]
decrypted_string = decrypt_string(unique_route_string.encode())
sem = decrypted_string[5]
student_class = decrypted_string[6:8]
subject_fpsu_code = decrypted_string[8:]
c.execute("""SELECT faculty.first_name, faculty.last_name, subject.subject_name
FROM pumis.faculty, pumis.subject
WHERE faculty.subject_fpsu_code=%s
AND faculty.class_alloted=%s
AND faculty.semester_alloted=%s""",(subject_fpsu_code, student_class, sem))
resp = c.fetchall()
faculty_firstname = resp[0][0]
faculty_lastname = resp[0][1]
subject_name = resp[0][2]
c.close()
conn.close()
gc.collect()
return render_template("chatbot.html")
except Exception as e:
return str(e)
if __name__ == "__main__":
app.run()