Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
117 lines (100 loc) · 3.79 KB
/
Copy pathmain.py
File metadata and controls
117 lines (100 loc) · 3.79 KB
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
# importing flask and mysql
from flask import Flask, render_template, request , redirect
from flask_mysqldb import MySQL
#importing libs for automated emails
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
app = Flask(__name__)
# configureing my sql initials to start with flask
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'Zero@123'
app.config['MYSQL_DB'] = 'Issues'
mysql = MySQL(app)
# the landing page url
@app.route("/")
def index():
return render_template('index.html')
# function to submit query to database
@app.route("/query", methods=['POST'])
def query():
try:
form_data = request.form # requesting the form data using request module
FirstName = form_data.get('fname')
LastName = form_data.get('lname')
Email = form_data.get('email')
Rnumber = form_data.get('rnum')
DescIssue = form_data.get('describe')
cur = mysql.connection.cursor()
sql_query = "INSERT INTO userissues (Query_Date ,Query_Time ,FirstName, LastName, Email, Rnumber, DescIssue) VALUES (curdate(), curtime() ,%s, %s, %s,%s,%s);" # mysql command
cur.execute(sql_query, (FirstName, LastName, Email, Rnumber, DescIssue))
mysql.connection.commit() # query added to the database opertaion performed
cur.close()
return render_template('index.html', success=True)
except Exception as e: # for an error
print(f"Error: {e}")
return render_template('index.html', success=False, error=str(e))
# admin-login lading page
@app.route('/login')
def login():
return render_template('login.html')
# user page which shows the query-data
@app.route('/admin')
def admin():
cur = mysql.connection.cursor()
sql_query = 'Select * from userissues'
cur.execute(sql_query)
mysql.connection.commit()
data = cur.fetchall() # fetching the data from the database.
cur.close()
return render_template('admin.html', query_data = data)
# delete the records
@app.route('/delete' , methods=['POST'])
def delete():
try:
id = request.form.get('value')
cur = mysql.connection.cursor()
sql_query = 'delete from userissues where id = %s ;'
cur.execute(sql_query , (int(id),))
mysql.connection.commit()
cur.close()
return redirect('admin')
except Exception as e:
print(f'error{e}')
return redirect('admin')
# send the resolve message
@app.route('/resolveMessage' , methods = ['POST'])
def resolveMessage():
try:
id = request.form.get('value')
resolvemessage = request.form.get('message')
email = request.form.get('email')
cur = mysql.connection.cursor()
sql_query = 'UPDATE userissues SET Remarks = %s WHERE id = %s;'
cur.execute(sql_query , (resolvemessage , id))
mysql.connection.commit()
cur.close()
# making direct reply to the user
smtp = smtplib.SMTP('smtp.gmail.com' ,587)
smtp.ehlo()
smtp.starttls()
# setting up your google account
smtp.login('thegreatking911@gmail.com' ,'dvdvdqyshrveuzjb')
def message(subject = 'Query Resolved' , text=""):
msg = MIMEMultipart()
msg['Subject'] = subject
msg.attach(MIMEText(text))
return msg
msg = message("Query Resolved" , resolvemessage)
#sending the email
to = [email]
smtp.sendmail(from_addr="IITGN@gmail.com" , to_addrs=to , msg=msg.as_string())
smtp.quit()
return redirect('admin')
except Exception as e:
print(f'error {e}')
return redirect('admin')
# run the app
if __name__ == '__main__':
app.run(debug=True)