-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseFunctions.py
More file actions
335 lines (265 loc) · 9.26 KB
/
DatabaseFunctions.py
File metadata and controls
335 lines (265 loc) · 9.26 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from datetime import datetime
import sqlite3
def create_user(name, email, card_number, sid):
conn = sqlite3.connect('swipe_system.db', timeout=5)
c = conn.cursor()
# Ensure the users table exists
c.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
sid TEXT UNIQUE NOT NULL,
card_number TEXT UNIQUE NOT NULL
)
''')
try:
# Insert new user
c.execute('''
INSERT INTO users (name, email, card_number, sid)
VALUES (?, ?, ?, ?)
''', (name, email, card_number, sid))
conn.commit()
print(f"User '{name}' created successfully.")
return "success"
except sqlite3.IntegrityError as e:
if 'email' in str(e):
print("Error: This email is already registered.")
return "email error"
elif 'card_number' in str(e):
print("Error: This card number is already in use.")
return "card error"
else:
print(f"Database error: {e}")
return "error"
finally:
conn.close()
def log_check_in(card_number, box):
conn = sqlite3.connect('swipe_system.db', timeout=5)
c = conn.cursor()
try:
card_number = card_number.strip()
# Fetch user
c.execute('SELECT id, name, sid FROM users WHERE card_number = ?', (card_number,))
user = c.fetchone()
if not user:
print("Card not recognized.")
return "error"
user_id, name, sid = user
# Check active session
c.execute('SELECT id FROM logs WHERE user_id = ? AND log_out_time IS NULL', (user_id,))
active_log = c.fetchone()
if active_log:
return "already_checked_in"
# Insert new log (includes SID)
now = datetime.now().isoformat(timespec='seconds')
c.execute('''
INSERT INTO logs (user_id, sid, box, log_in_time, log_out_time)
VALUES (?, ?, ?, ?, NULL)
''', (user_id, sid, box, now))
# Write row to Google Sheet
sheet_row = write_row_to_sheet([
name, # col 1
box, # col 2
email, # col 3
sid, # col 4
now, # col 5 (checkout)
"" # col 6 (return empty)
])
# Save row number to logs table
c.execute("""
INSERT INTO logs (user_id, sid, box, log_in_time, log_out_time, sheet_row)
VALUES (?, ?, ?, ?, NULL, ?)
""", (user_id, sid, box, now, sheet_row))
conn.commit()
print(f"{name} checked in at {now}")
return "success"
finally:
conn.close()
def log_check_out(card_number):
conn = sqlite3.connect('swipe_system.db', timeout=5)
c = conn.cursor()
try:
card_number = card_number.strip()
# Find user by card number
c.execute('SELECT id, name FROM users WHERE card_number = ?', (card_number,))
user = c.fetchone()
if not user:
print("Card not recognized.")
return "error"
user_id, name = user
now = datetime.now().isoformat(timespec='seconds')
# Find active log session
c.execute('''
SELECT id FROM logs
WHERE user_id = ? AND log_out_time IS NULL
ORDER BY log_in_time DESC
LIMIT 1
''', (user_id,))
log_entry = c.fetchone()
if not log_entry:
print("No active check-in found for this user.")
return "no sesh"
log_id = log_entry[0]
# Update log-out time
c.execute('''
UPDATE logs
SET log_out_time = ?
WHERE id = ?
''', (now, log_id))
log_id = log_entry[0]
# Get sheet row number
c.execute("SELECT sheet_row FROM logs WHERE id = ?", (log_id,))
sheet_row = c.fetchone()[0]
# Update Google Sheet
update_return_date(sheet_row, now)
conn.commit()
print(f"{name} checked out at {now}")
return "success"
finally:
conn.close()
def get_email(card_number):
conn = sqlite3.connect('swipe_system.db', timeout=5)
c = conn.cursor()
try:
c.execute('SELECT email FROM users WHERE card_number = ?', (card_number.strip(),))
row = c.fetchone()
return row[0] if row else None
finally:
conn.close()
def get_admin_email(card_number):
conn = sqlite3.connect('swipe_system.db', timeout=5)
c = conn.cursor()
try:
c.execute('SELECT email FROM admin WHERE card_number = ?', (card_number.strip(),))
row = c.fetchone()
return row[0] if row else None
finally:
conn.close()
def delete_user(identifier):
conn = sqlite3.connect('swipe_system.db', timeout=5)
c = conn.cursor()
try:
identifier = identifier.strip()
c.execute("""
DELETE FROM users
WHERE email = ? OR card_number = ? OR name = ?
""", (identifier, identifier, identifier))
conn.commit()
return "success" if c.rowcount > 0 else "not found"
finally:
conn.close()
def export_logs_to_text_file(output_file='user_logs.txt'):
conn = sqlite3.connect('swipe_system.db', timeout=5)
c = conn.cursor()
try:
c.execute("""
SELECT u.name, u.email, l.log_in_time, l.log_out_time
FROM logs l
JOIN users u ON l.user_id = u.id
ORDER BY l.log_in_time ASC
""")
rows = c.fetchall()
finally:
conn.close()
# write file outside DB block
with open(output_file, 'w') as f:
f.write("User Check-In/Check-Out Report\n")
f.write("=" * 40 + "\n\n")
for name, email, log_in, log_out in rows:
f.write(f"Name: {name}\n")
f.write(f"Email: {email}\n")
f.write(f"Check-in Time: {log_in}\n")
f.write(f"Check-out Time: {log_out or 'N/A'}\n")
f.write("-" * 40 + "\n")
def create_admin(name, email, card_number):
conn = sqlite3.connect('swipe_system.db', timeout = 5)
c = conn.cursor()
# Ensure the users table exists
c.execute('''
CREATE TABLE IF NOT EXISTS admin (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
card_number TEXT UNIQUE NOT NULL
)
''')
try:
# Insert new user
c.execute('''
INSERT INTO admin (name, email, card_number)
VALUES (?, ?, ?)
''', (name, email, card_number))
conn.commit()
print(f"Admin '{name}' created successfully.")
return "success"
except sqlite3.IntegrityError as e:
if 'email' in str(e):
print("Error: This email is already registered.")
return "email error"
elif 'card_number' in str(e):
print("Error: This card number is already in use.")
return "card error"
else:
print(f"Database error: {e}")
return "error"
finally:
conn.close()
def admin_exists():
conn = sqlite3.connect('swipe_system.db')
c = conn.cursor()
try:
c.execute('SELECT COUNT(*) FROM admin')
count = c.fetchone()[0]
if count > 0:
return True # Admins exist
else:
return False # No admin in table yet
except sqlite3.Error as e:
print(f"Database error: {e}")
return False # Assume none exist if there's an error
finally:
conn.close()
def export_tables_to_text_files(db_path='swipe_system.db'):
conn = sqlite3.connect(db_path)
c = conn.cursor()
tables = ['users', 'logs', 'admin']
try:
for table in tables:
# Fetch all data
c.execute(f"SELECT * FROM {table}")
rows = c.fetchall()
# Fetch column names
col_names = [description[0] for description in c.description]
# Write to text file
with open(f"{table}_export.txt", 'w', encoding='utf-8') as f:
f.write('\t'.join(col_names) + '\n') # Header
for row in rows:
f.write('\t'.join(str(item) if item is not None else '' for item in row) + '\n')
print(f"Exported table '{table}' to {table}_export.txt")
return "export success"
except sqlite3.Error as e:
print(f"Error exporting tables: {e}")
return "export failed"
finally:
conn.close()
def admin_user_exists(card_number, db_path='swipe_system.db'):
conn = sqlite3.connect(db_path)
c = conn.cursor()
try:
c.execute('SELECT 1 FROM admin WHERE card_number = ?', (card_number,))
return c.fetchone() is not None
except sqlite3.Error as e:
print(f"Database error: {e}")
return False
finally:
conn.close()
def get_user_info(card_number):
conn = sqlite3.connect('swipe_system.db', timeout=5)
c = conn.cursor()
try:
c.execute("SELECT name, email, sid FROM users WHERE card_number = ?", (card_number.strip(),))
row = c.fetchone()
return row if row else ("Unknown", "Unknown", "Unknown")
finally:
conn.close()