-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
176 lines (144 loc) · 5.35 KB
/
main.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
import mysql.connector
from flask import Flask, render_template, request, url_for, session,redirect
import random as rd
import pyqrcode
from PIL import Image
from pyzbar.pyzbar import decode
import smtplib
import config
from pymongo import MongoClient
app = Flask(__name__)
app.secret_key = ''
# db = mysql.connector.connect(host="localhost",
# user="root",
# password="",
# database="")
client = MongoClient(
"")
db = client.ticketGenerator
collection1 = db['']
def sendmail(receiver, message):
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(config.EMAIL_ADDRESS, config.PASSWORD)
server.sendmail(config.EMAIL_ADDRESS, receiver, message)
server.quit()
@app.route('/')
def welcome():
return render_template('index.html')
@app.route('/registrationform', methods=['GET', 'POST'])
def addparticipant():
return render_template('addParticipant.html')
@app.route('/addparticipant', methods=['POST'])
def participantdatasubmit():
participantDetails = request.form
session['name'] = participantDetails['name']
session['email'] = participantDetails['email']
session['grade'] = participantDetails['grade']
session['contact'] = participantDetails['contact']
session['school'] = participantDetails['school']
session['TFA'] = rd.randint(100000, 999999)
subject = 'TICKET GENERATOR VERIFICATION TEST'
msg = f"""
Greetings,
Test Mail
Your verification code is {session['TFA']}
Regards,
Team CodeTech
Birla Vidya Niketan
"""
message = 'Subject: {}\n\n{}'.format(subject, msg)
print(f"Sending verification mail to {session['name']} ")
sendmail(participantDetails['email'], message)
return render_template('verification.html')
@app.route('/addparticipant/confirmation', methods=['POST'])
def verifycommit():
verifdata = request.form
if int(session['TFA']) == int(verifdata['verification_code']):
num = rd.randint(100000, 999999)
qrembed = f"CODEx2021_{num}"
code = pyqrcode.create(qrembed)
code.png(f"QRCodes/CODEx2021_{session['name']}.png", scale=8)
# with open(f"/Users/pancham/PycharmProjects/flasktestapp/QRCodes/CODEx2021_{session['name']}.png", 'rb') as file:
# binaryData = file.read()
# cur = db.cursor()
# cur.execute(
# "INSERT INTO participantData(name,email,class,contact,school,CODExID,qrcode) VALUES(%s,%s,%s,%s,"
# "%s,%s,%s)",
# (session['name'], session['email'], session['grade'], session['contact'], session['school'], qrembed,
# binaryData))
# db.commit()
# cur.close()
collection1.insert(
{
'_id': qrembed,
'name': session['name'].titlecase(),
'email': session['email'],
'contact': session['contact'],
'school': session['school'].titlecase(),
}
)
return 'You have successfully registered!'
else:
return "Invalid verification code, please register again."
@app.route('/scanparticipant', methods=['GET', 'POST'])
def scanparticipant():
if request.method == 'POST':
scanData = request.form
filename = scanData['QRCODE']
decoder = decode(Image.open(f'QRCodes/{filename}'))
targetParticipant = decoder[0].data.decode('ascii ')
# cur = db.cursor()
# cur.execute('SELECT * FROM participantData WHERE CODExID like' + f"'%{targetParticipant}%';")
# data = cur.fetchall()
results = collection1.find({
'_id':targetParticipant
})
if results:
for result in results:
id = result['_id']
name = result['name']
email = result['email']
contact = result['contact']
school = result['school']
return render_template('scan-results.html', name=name, id=id, email=email, contact=contact, school=school)
return render_template('scanParticipant.html')
@app.route('/participants')
def participants():
# cur = db.cursor()
# cur.execute('SELECT * FROM participantData;')
#
# userdetails = cur.fetchall()
results = collection1.find()
userdetails = []
if results:
for result in results:
userdetails.append(result)
if len(userdetails) > 0:
return render_template('users.html', userdetails=userdetails)
@app.route('/search', methods=['GET', 'POST'])
def searchbyname():
if request.method == 'POST':
search = request.form
namefilter = search['search']
# cur = db.cursor()
# cur.execute('SELECT * FROM participantData WHERE name like' + f"'%{namefilter}%';")
# searchdata = cur.fetchall()
results = collection1.find({
'name':namefilter
})
if results:
searchdata = []
for result in results:
searchdata.append(result)
return render_template('search results.html', searchdata=searchdata)
return render_template('searchbyname.html')
@app.errorhandler(404)
def error404(error):
return "<h1>ERROR 404</h1>", 404
@app.errorhandler(500)
def error404(error):
return "<h1>ERROR 500</h1>", 500
if __name__ == "__main__":
app.run(debug=True)