-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.py
169 lines (153 loc) · 7.24 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
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
import json
import random
from flask import Flask, request, jsonify
import pymysql
import yaml
app = Flask(__name__)
app.secret_key = 'kushop007'
# Configure db
db = yaml.safe_load(open('db.yaml'))
mysql_host = db['mysql_host']
mysql_user = db['mysql_user']
mysql_password = db['mysql_password']
mysql_db = db['mysql_db']
def get_connection():
return pymysql.connect(host=mysql_host, user=mysql_user, password=mysql_password, db=mysql_db)
@app.route('/loginasfaculty', methods=['POST'])
def login_as_faculty():
msg = ''
connection = get_connection()
cursor = connection.cursor()
if request.method == 'POST':
try:
data = request.data # Access the raw text data from the request body
data_dict = json.loads(data.decode('utf-8')) # Parse the text as YAML if it's in YAML format
if data_dict is not None and 'user' in data_dict and 'pass' in data_dict:
ID = data_dict['user']
Pass = data_dict['pass']
print(ID, Pass)
cursor.execute('SELECT * FROM facultyauthentication WHERE FacultyID=%s AND Password=%s', (ID, Pass))
record = cursor.fetchone()
if record:
return jsonify({'status': 'success', 'message': 'Login successful'})
else:
return jsonify({'status': 'fail', 'message': 'Incorrect username or password'})
else:
return jsonify({'status': 'fail', 'message': 'Invalid data format'})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)})
finally:
cursor.close()
connection.close()
@app.route('/loginasstudent', methods=['POST'])
def login_as_student():
msg = ''
connection = get_connection()
cursor = connection.cursor()
if request.method == 'POST':
try:
data = request.data # Access the raw text data from the request body
data_dict = json.loads(data.decode('utf-8')) # Parse the text as YAML if it's in YAML format
if data_dict is not None and 'user' in data_dict and 'pass' in data_dict:
ID = data_dict['user']
Pass = data_dict['pass']
print(ID, Pass)
cursor.execute('SELECT * FROM studentauthentication WHERE Roll_number=%s AND Stu_Password=%s', (ID, Pass))
record = cursor.fetchone()
if record:
return jsonify({'status': 'success', 'message': 'Login successful'})
else:
return jsonify({'status': 'fail', 'message': 'Incorrect username or password'})
else:
return jsonify({'status': 'fail', 'message': 'Invalid data format'})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)})
finally:
cursor.close()
connection.close()
@app.route('/start_session', methods=['POST'])
def start_session():
connection = get_connection()
cursor = connection.cursor()
if request.method == 'POST':
data = request.data # Access the raw text data from the request body
data_dict = json.loads(data.decode('utf-8'))
IP_address = data_dict['ip_address']
Faculty_ID = data_dict['faculty_id']
Date_time = data_dict['date_time']
print(IP_address,Date_time,Faculty_ID)
Session_ID = random.randint(100000, 999999)
Session_ID_Str='S'+str(Session_ID)
cursor.execute('INSERT INTO sessionauthentication (IP_address, Session_ID) VALUES (%s, %s)',(IP_address, Session_ID_Str))
cursor.execute('INSERT INTO sessionDetails (Session_ID, Faculty_ID, Date_time) VALUES (%s, %s, %s)',(Session_ID_Str, Faculty_ID, Date_time))
# creating a new roll number table
try:
cursor.execute("CREATE TABLE `{}` (Roll_number varchar(12) primary key)".format(Session_ID_Str))
return jsonify({'status': 'table created successfully'})
except Exception as e:
return jsonify({"error": f"Error: {e}"})
return jsonify({'message': 'Session has started.'})
@app.route('/mark_attendance',methods = ['POST'])
def mark_attendance():
connection = get_connection()
cursor = connection.cursor()
if request.method == 'POST':
try:
data = request.data # Access the raw text data from the request body
data_dict = json.loads(data.decode('utf-8'))
#app sending data
IP_address = data_dict['ip_address']
Session_ID = data_dict['session_id']
Session_ID='S'+str(Session_ID)
Roll_number = data_dict['roll_number']
print(IP_address,Session_ID,Roll_number)
cursor.execute('SELECT * FROM sessionauthentication where Session_ID = %s AND IP_address = %s',(Session_ID,IP_address))
record = cursor.fetchone()
if record:
# Use placeholders to insert data into the table and avoid SQL injection
insert_query = f'INSERT INTO {Session_ID} (Roll_number) VALUES (%s)'
cursor.execute(insert_query, (Roll_number,))
connection.commit()
return jsonify({'status': 'attendance marked'})
else:
return jsonify({'message': 'database does not exist'})
except Exception as e:
return jsonify({'error':f'{e}'})
@app.route('/add_attendance',methods = ['POST'])
def edit_attendance():
connection = get_connection()
cursor = connection.cursor()
if request.method == 'POST':
data = request.data # Access the raw text data from the request body
data_dict = json.loads(data.decode('utf-8'))
Roll_number = data_dict['roll_number']
Session_ID = data_dict['session_id']
Session_ID='S'+str(Session_ID)
try:
# Use placeholders to insert data into the table and avoid SQL injection
insert_query = f'INSERT INTO {Session_ID} (Roll_number) VALUES (%s)'
cursor.execute(insert_query, (Roll_number,))
connection.commit()
return jsonify({'status': 'attendance marked'})
except Exception as e:
return jsonify({'message':f'{e}'})
@app.route('/remove_attendance',methods = ['POST'])
def remove_attendance():
connection = get_connection()
cursor = connection.cursor()
if request.method == 'POST':
data = request.data # Access the raw text data from the request body
data_dict = json.loads(data.decode('utf-8'))
Roll_number = data_dict['roll_number']
Session_ID = data_dict['session_id']
Session_ID = 'S' + str(Session_ID)
try:
# Use a placeholder for the condition in the DELETE statement
delete_query = f'DELETE FROM {Session_ID} WHERE Roll_number = %s'
cursor.execute(delete_query, (Roll_number,))
connection.commit()
return jsonify({'status': 'attendance removed'})
except Exception as e:
return jsonify({'message': f'{e}'})
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')