-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
135 lines (102 loc) · 4.02 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
from flask import Flask, render_template, request, url_for, redirect, send_file, make_response, jsonify
from mysql.connector import MySQLConnection, Error
import mysql.connector
from python_mysql_dbconfig import read_db_config
from graph import survey_data, survey_analysis_01, survey_analysis_02, close_connection
import logging
from flask_caching import Cache
import time
app = Flask(__name__)
application = app # our hosting requires application in passenger_wsgi
cache = Cache(app, config={'CACHE_TYPE': 'null'})
def mycache():
cache.init_app(app, config={'CACHE_TYPE': 'null'})
with app.app_context():
cache.clear()
app.config["CACHE_TYPE"] = "null"
cache.init_app(app)
app.debug = True
db_config = read_db_config()
conn = None
conn = MySQLConnection(**db_config)
@app.after_request
def add_header(response):
response.headers['Cache-Control'] = 'public, max-age=0'
return response
@app.route('/')
def home():
return render_template('survey.html')
@app.route('/addrec',methods = ['POST', 'GET'])
def addrec():
if request.method == 'POST':
try:
urname = request.form['urname']
email = request.form['email']
age = request.form['age']
profession = request.form['profession']
question1 = request.form['question1']
question2 = request.form['question2']
question3 = request.form['question3']
question4 = request.form['question4']
question5 = request.form['question5']
question6 = request.form['question6']
question7 = request.form['question7']
question8 = request.form['question8']
question9 = request.form['question9']
question10 = request.form['question10']
cursor = conn.cursor()
cursor.execute("SET AUTOCOMMIT = 1")
cursor.execute("INSERT INTO covid19_survey (Name,Email,Age,Profession,Question1,Question2,Question3,Question4,Question5,Question6,Question7,Question8,Question9,Question10) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(urname, email, age, profession, question1, question2, question3, question4, question5, question6, question7, question8, question9, question10))
conn.commit()
msg = " Thanks for your time!! Your answers have been successfully received by us."
survey_data()
survey_analysis_01()
survey_analysis_02()
print ("Success")
# time.sleep(1)
# except mysql.connector.IntegrityError as err:
except Exception as err:
print("Error: {}".format(err))
print ("Exception - Something went wrong.")
conn.rollback()
msg = "Something went wrong. Please try again."
finally:
urname = request.form['urname']
cursor.execute("select * from covid19_survey where Name= %s", (urname,))
data = cursor.fetchall()
return render_template("result.html", msg = msg ,data = data)
# conn.close()
# cursor.close()
return render_template("survey.html")
# Report Listing - For Admin
@app.route('/findme')
def list():
cursor = conn.cursor()
cursor.execute("COMMIT")
cursor.execute("select * from covid19_survey")
data = cursor.fetchall()
return render_template("list.html", data = data)
conn.close()
cursor.close()
close_connection()
@app.route('/graph', methods=["GET"])
def graph():
return render_template("survey_graph.html")
# Route for handling the login page logic
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != 'admin' or request.form['password'] != 'admin':
error = 'Invalid Credentials. Please try again.'
else:
return redirect(url_for('home'))
return render_template('login.html', error=error)
@app.errorhandler(404)
def page_not_found(e):
# note that we set the 404 status explicitly
return render_template('404.html'), 404
if __name__ == '__main__':
mycache()
app.run(debug=False)
close_connection()