-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.py
94 lines (76 loc) · 3.08 KB
/
index.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
import os, json
from sys import argv
from flask import Flask, request, redirect, url_for, send_from_directory, render_template
from werkzeug import secure_filename
import time
from random import random
from flask_mysqldb import MySQL
import MySQLdb
BASE_FOLDER = 'src/registration/'
UPLOAD_FOLDER = BASE_FOLDER + 'Source'
ALLOWED_EXTENSIONS = set(['jpg', 'jpeg'])
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'Attendences'
mysql = MySQL(app)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return filename[-3:].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
return redirect(url_for('upload_file'))
@app.route('/uploadIdCard', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# If post request has file in it
if 'file' in request.files:
file = request.files['file']
if file and allowed_file(file.filename):
print ('**found file', file.filename)
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
time.sleep(random() * .1 * 60)
Flask_dir = os.getcwd()
### Change Directory to ID card Folder of run.sh
os.chdir(BASE_FOLDER)
os.system('./run.sh')
#print(os.getcwd())
f = open("Output/json/" + "res_" + filename.split('.')[0] + ".json", "r")
a = f.read()
y = json.loads(a)
roll_number = y['roll_no']
## Back to normal directory
os.chdir(Flask_dir)
#print(os.getcwd())
return redirect(url_for('show_details', filename=roll_number))
return render_template('index.html')
@app.route('/showDetails/<filename>')
def show_details(filename):
return render_template('show.html', path=BASE_FOLDER, id=filename)
@app.route('/update/<id>')
def updatedatabase(id):
f = open("static/json/" + id + ".json", "r")
a = f.read()
y = json.loads(a)
cur = mysql.connection.cursor()
try:
try:
cur.execute("INSERT INTO Students(collegeName, name, roll_no, email, branch, mobile, validity) VALUES (%s, %s, %s, %s, %s, %s, %s)", (y['college'], y['name'], y['roll_no'], y['emails'], y['branch'], y['mobile'], y['valid']))
mysql.connection.commit()
# NB : you won't get an IntegrityError when reading
except :
print('Please Try Again!!')
return render_template('fail.html')
finally:
cur.close()
return render_template('success.html')
if __name__ == '__main__':
if len(argv) == 2:
port = int(argv[1])
print (f"Application is ready and listening to port {port}")
app.run(host='localhost', port=port, debug=True)
else:
app.run(host='localhost', port=8080, debug=True)
print (f"Application is ready and listening to port {port}")