forked from terriblesaint/uonewapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
1004 lines (757 loc) · 37.1 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, render_template, request, redirect, url_for, flash, session, g, send_file
from werkzeug.security import generate_password_hash
from helpers import connect_to_database,get_skey
from users import select_all_users, get_user_data, login_check_existing_user, register_check_existing_user
from selections import check_selection_toggle, select_selections, select_delivery, select_history, get_names
from mclinks import select_links
from datetime import datetime
import sqlite3
import re
import pandas as pd
import random
import os
import io
app = Flask(__name__, static_url_path='/static', static_folder='static')
app.config['DEBUG'] = True
app.secret_key = 'iamasecret'
# Before all requests, create the following variables
@app.before_request
def authentication():
if 'id' in session:
user_data = get_user_data()
selection_toggle = check_selection_toggle()
# Get variables for User - Level, Selection Toggle, and Display Name
g.is_admin = user_data['is_admin']
g.sel_lock = user_data['sel_lock']
g.display_name = user_data['display_name']
# Get variable for Global - Selection Toggle.
g.selection_toggle = selection_toggle['selections_toggle']
# User Registration
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
display_name = request.form['display_name']
password = request.form['password']
confirm_password = request.form['confirm_password']
# Check for empty fields
if not username or not display_name or not password or not confirm_password:
flash('All fields are required.', 'danger')
# Check for matching password input
elif password != confirm_password:
flash('Passwords do not match.', 'danger')
# Password complexity validation
elif not (
len(password) >= 8 and
re.search(r'[A-Z]', password) and # At least one uppercase letter
re.search(r'[a-z]', password) and # At least one lowercase letter
re.search(r'\d', password) and # At least one digit
re.search(r'[!@#$%^&*()_+=\-[\]{};:\'",.<>?]', password) # At least one symbol
):
flash('Password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one digit, and one symbol.','danger')
else:
# Check if the username already exists
if register_check_existing_user(username):
flash('Username already exists. Please choose a different one.', 'danger')
else:
# Register user
conn, cursor = connect_to_database('uonew.db')
hashed_password = generate_password_hash(password)
cursor.execute('INSERT INTO users (username, password, display_name) VALUES (?, ?, ?)', (username, hashed_password, display_name))
conn.commit()
conn.close()
flash('Registration successful! Please log in.', 'success')
return redirect(url_for('login'))
return redirect(url_for('register'))
return render_template('register.html', page_identifier='register')
# Login Page Functionality
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if not username or not password:
flash('Please fill in both fields', 'danger')
return render_template('login.html')
user_data = login_check_existing_user(username, password)
if user_data:
session['id'] = user_data['id']
flash('Login successful!', 'success')
return redirect('ma')
if user_data:
session['id'] = user_data['id']
flash('Login successful!', 'success')
return redirect('ma')
else:
flash('Incorrect username or password.', 'danger')
return render_template('login.html', page_identifier='login')
# Logout Functionality
@app.route('/logout')
def logout():
session.pop('id', None)
flash('You have been logged out.', 'success')
return redirect('login')
# Toggles Mastery Link Selections on/off
@app.route('/global_selection_toggle', methods=['POST'])
def global_selection_toggle():
selection_toggle = check_selection_toggle()
conn, cursor = connect_to_database('uonew.db')
if selection_toggle[0] == 0:
cursor.execute("UPDATE global SET selections_toggle = 1 WHERE id = 1")
else:
cursor.execute("UPDATE global SET selections_toggle = 0 WHERE id = 1")
conn.commit()
conn.close()
return redirect(url_for('a_selections'))
# Reset link selections for ALL users
@app.route('/reset_all_selections', methods=['POST'])
def reset_all_selections():
if request.method == 'POST':
conn, cursor = connect_to_database('uonew.db')
cursor.execute("DELETE FROM selections")
conn.commit()
conn.close()
flash('All User selections have been reset', 'success')
return redirect(url_for('a_selections'))
# Function for the user to update an individual link selection
@app.route('/update_user_selection', methods=['POST'])
def update_user_selection():
if request.method == 'POST':
sel_id = request.form['selection_id']
quality = request.form['quality']
quantity = request.form['quantity']
mclink_id = request.form['mclink_id']
sel_name = request.form['link_name']
if quantity == '0':
flash('JUST DELETE IT!', 'danger')
return redirect(url_for('selections'))
if not (quality and quantity):
flash('All fields are required', 'danger')
return redirect(url_for('selections'))
conn, cursor = connect_to_database('uonew.db')
# Check if quality has been changed
cursor.execute("SELECT quality FROM mclinks WHERE id = ?", (mclink_id,))
old_quality = cursor.fetchone()[0]
if old_quality != quality:
# Quality has been changed, update mclink_id and quanity
cursor.execute("SELECT id, quantity FROM mclinks WHERE name = ? AND quality = ?", (sel_name, quality,))
mclinks = cursor.fetchone()
if int(quantity) > int(mclinks['quantity']):
flash('Not enough of that link available', 'danger')
return redirect(url_for('selections'))
cursor.execute("UPDATE selections SET s_quantity = ?, mclink_id = ? WHERE id = ?", (quantity, mclinks['id'], sel_id,))
conn.commit()
conn.close()
flash('Your selection has been updated', 'success')
return redirect(url_for('selections'))
else:
# Quality has not been changed, only update s_quantity
cursor.execute("SELECT id, quantity FROM mclinks WHERE name = ? AND quality = ?", (sel_name, quality,))
mclinks = cursor.fetchone()
if int(quantity) > int(mclinks['quantity']):
flash('Not enough of that link available', 'danger')
return redirect(url_for('selections'))
cursor.execute("UPDATE selections SET s_quantity = ? WHERE id = ?", (quantity, sel_id,))
conn.commit()
conn.close()
flash('Your selection has been updated', 'success')
return redirect(url_for('selections'))
@app.route('/admin_update_user_selection', methods=['POST'])
def admin_update_user_selection():
if request.method == 'POST':
sel_id = request.form['selection_id']
quality = request.form['quality']
quantity = request.form['quantity']
mclink_id = request.form['mclink_id']
sel_name = request.form['link_name']
if quantity == '0':
flash('Just Delete it!!', 'danger')
return redirect(url_for('a_selections'))
if not (quality and quantity):
flash('All fields are required', 'danger')
return redirect(url_for('a_selections'))
conn, cursor = connect_to_database('uonew.db')
# Get quality of selection before being updated
cursor.execute("SELECT quality FROM mclinks WHERE id = ?", (mclink_id,))
old_quality = cursor.fetchone()[0]
if old_quality != quality:
# Quality has been changed, update mclink_id
cursor.execute("SELECT id, quantity FROM mclinks WHERE name = ? AND quality = ?", (sel_name, quality,))
mclinks = cursor.fetchone()
if int(quantity) > int(mclinks['quantity']):
flash('Not enough of that link available', 'danger')
return redirect(url_for('a_selections'))
cursor.execute("UPDATE selections SET s_quantity = ?, mclink_id = ? WHERE id = ?", (quantity, mclinks['id'], sel_id,))
conn.commit()
conn.close()
flash('Your selection has been updated', 'success')
return redirect(url_for('a_selections'))
else:
# Quality has not been changed, only update s_quantity
cursor.execute("SELECT id, quantity FROM mclinks WHERE name = ? AND quality = ?", (sel_name, quality,))
mclinks = cursor.fetchone()
if int(quantity) > int(mclinks['quantity']):
flash('Not enough of that link available', 'danger')
return redirect(url_for('a_selections'))
cursor.execute("UPDATE selections SET s_quantity = ? WHERE id = ?", (quantity, sel_id,))
conn.commit()
conn.close()
flash('Your selection has been updated', 'success')
return redirect(url_for('a_selections'))
# Function for the user to delete an individual link selection
@app.route('/delete_user_selection', methods=['POST'])
def delete_user_selection():
if request.method == 'POST':
conn, cursor = connect_to_database('uonew.db')
user_data = get_user_data()
user_id = user_data['id']
selection_id = request.form['selection_id']
cursor.execute("DELETE FROM selections WHERE user_id =? AND id =?", (user_id, selection_id))
conn.commit()
conn.close()
flash('Your selection has been removed', 'success')
return redirect(url_for('selections'))
# Function for the admin to delete a users individual link selection
@app.route('/admin_delete_user_selection', methods=['POST'])
def admin_delete_user_selection():
if request.method == 'POST':
selection_id = request.form['selection_id']
conn, cursor = connect_to_database('uonew.db')
cursor.execute("DELETE FROM selections WHERE id =?", (selection_id,))
conn.commit()
conn.close()
flash('Their selection has been removed', 'success')
return redirect(url_for('a_selections'))
# Function for the user to delete an individual link selection
@app.route('/reset_user_selections', methods=['POST'])
def reset_user_selections():
if request.method == 'POST':
user_data = get_user_data()
user_id = user_data['id']
conn, cursor = connect_to_database('uonew.db')
cursor.execute("DELETE FROM selections WHERE user_id =?", (user_id,))
conn.commit()
conn.close()
flash('All your selections have been reset', 'success')
status = user_data['sel_lock']
print(status)
return redirect(url_for('selections'))
# Function for the user to lock in or to keep adding/editing link selections
@app.route('/user_selection_toggle', methods=['POST'])
def user_selection_toggle():
user_data = get_user_data()
user_id = user_data['id']
conn, cursor = connect_to_database('uonew.db')
selection_status = user_data['sel_lock'] # Corrected to use user_data instead of user_id
if selection_status == 0:
cursor.execute("UPDATE users SET sel_lock = 1 WHERE id = ?", (user_id,))
g.sel_lock = 1
flash('Your selections are set!', 'success')
else:
cursor.execute("UPDATE users SET sel_lock = 0 WHERE id = ?", (user_id,))
g.sel_lock = 0
flash('Your selections are open!', 'success')
conn.commit()
conn.close()
# Redirect or render_template as required
return redirect(url_for('selections')) # Example redirect back to selections page
# Function for admin to update any user column from user table in db
@app.route('/admin_update_user', methods=['POST'])
def admin_update_user():
if request.method == 'POST':
# Extract data from the form submission
user_id = request.form['id']
username = request.form['username']
display_name = request.form['display_name']
password = request.form['password']
user_level = request.form['user_level']
conn, cursor = connect_to_database('uonew.db')
if password: # Check if password is not empty
hashed_password = generate_password_hash(password)
else:
# Fetch the existing hashed password from the database
cursor.execute("SELECT password FROM users WHERE id = ?", (user_id,))
hashed_password = cursor.fetchone()[0] # Get the first column of the first row
cursor.execute("UPDATE users SET username = ?, display_name = ?, password = ?, is_admin = ? WHERE id = ?", (username, display_name, hashed_password, user_level, user_id))
# Commit the transaction
conn.commit()
# Close the connection
conn.close()
flash('User updated successfully', 'success')
return redirect(url_for('a_users'))
# Function for admin to a new user to the user table in db
@app.route('/admin_add_user', methods=['POST'])
def admin_add_user():
if request.method == 'POST':
# Extract data from the form submission
username = request.form['username']
displayname = request.form['displayname']
password = request.form['password']
user_level = request.form['user_level']
if not (username and displayname and password and user_level):
flash('All fields are required', 'danger')
return redirect(url_for('a_users'))
conn, cursor = connect_to_database('uonew.db')
hashed_password = generate_password_hash(password)
cursor.execute("INSERT INTO users (username, display_name, password, is_admin) VALUES (?, ?, ?, ?);", (username, displayname, hashed_password, user_level))
# Commit the transaction
conn.commit()
# Close the connection
conn.close()
flash('User added successfully', 'success')
return redirect(url_for('a_users'))
# Function for admin remove a user from the user table in db
@app.route('/delete_a_user', methods=['POST'])
def delete_a_user():
if request.method == 'POST':
# Extract data from the form submission
user_id = request.form['id']
conn, cursor = connect_to_database('uonew.db')
cursor.execute("DELETE FROM users WHERE id = ?;", (user_id,))
# Commit the transaction
conn.commit()
# Close the connection
conn.close()
flash('User deleted successfully', 'success')
return redirect(url_for('a_users'))
# Route for updating Links
@app.route('/update_a_mclinks', methods=['POST'])
def update_a_mclinks():
if request.method == 'POST':
# Extract data from the form submission
mclink_id = request.form['id']
name = request.form['name']
quality = request.form['quality']
quantity = request.form['quantity']
market_price = request.form['market_price']
guild_price = request.form['guild_price']
if not (name and quality and quantity and market_price and guild_price):
flash('All fields are required', 'danger')
return redirect(url_for('a_mclinks'))
conn, cursor = connect_to_database('uonew.db')
# Perform the update operation
cursor.execute("UPDATE mclinks SET name = ?, quality = ?, quantity = ?, market_price = ?, guild_price = ? WHERE id = ?", (name, quality, quantity, market_price, guild_price, mclink_id))
# Commit the transaction
conn.commit()
# Close the connection
conn.close()
flash('Database updated successfully', 'success')
return redirect(url_for('a_mclinks'))
@app.route('/delete_a_mclink', methods=['POST'])
def delete_a_mclink():
if request.method == 'POST':
# Extract data from the form submission
mclink_id = request.form['id']
conn, cursor = connect_to_database('uonew.db')
cursor.execute("DELETE FROM mclinks WHERE id = ?;", (mclink_id,))
# Commit the transaction
conn.commit()
# Close the connection
conn.close()
flash('Mastery Chain Link deleted successfully', 'success')
return redirect(url_for('a_mclinks'))
@app.route('/add_a_mclink', methods=['POST'])
def add_a_mclink():
if request.method == 'POST':
# Extract data from the form submission
name = request.form['name']
quality = request.form['quality']
quantity = request.form['quantity']
market_price = request.form['market_price']
guild_price = request.form['guild_price']
if not (name and quality and quantity and market_price and guild_price):
flash('All fields are required', 'danger')
return redirect(url_for('a_mclinks'))
conn, cursor = connect_to_database('uonew.db')
cursor.execute("INSERT INTO mclinks (name, quality, quantity, market_price, guild_price) VALUES (?, ?, ?, ?, ?);", (name, quality, quantity, market_price, guild_price))
# Commit the transaction
conn.commit()
# Close the connection
conn.close()
flash('Mastery Chain Link added successfully', 'success')
return redirect(url_for('a_mclinks'))
# Route for '/' (Login Check)
@app.route('/')
def login_check():
# Check if user is logged in
if 'id' in session:
# If logged in, redirect to '/ma'
return redirect(url_for('ma'))
else:
# If not logged in, redirect to '/login'
return redirect(url_for('login'))
# Route for Index / Main Template
@app.route('/index')
def index():
return render_template('index.html')
# Route for Members' Area
@app.route('/ma')
def ma():
return render_template('ma.html', page_identifier='ma')
# Route for User - Mastery Chain Link List
@app.route('/mclinks', methods=['GET'])
def mclinks():
mclinks_data = select_links()
return render_template('mclinks.html', mclinks_data=mclinks_data, page_identifier='mclinks')
# Route for Admin - Mastery Chain Link List
@app.route('/a_mclinks', methods=['GET'])
def a_mclinks():
mclinks_data = select_links()
return render_template('a_mclinks.html', mclinks_data=mclinks_data, page_identifier='a_mclinks')
# Route for User - Mastery Chain Link Selections
@app.route('/selections', methods=['GET', 'POST'])
def selections():
# Get User ID
user_data = get_user_data()
user_id = user_data['id']
# Initiate DB Connection
conn, cursor = connect_to_database('uonew.db')
# Get unique names in database for dropdown menu
cursor.execute("SELECT DISTINCT name FROM mclinks;")
unique_names = cursor.fetchall()
# Get total cost of all links chosen by user
cursor.execute("SELECT SUM(links_total) FROM selections WHERE user_id=?", (user_id,))
total_links_total = cursor.fetchone()[0]
if total_links_total is not None:
total_links_total = "{:,}".format(total_links_total)
conn.close()
# Get all selected links of user (including mclinks data)
selections_data = select_selections(user_id)
return render_template('selections.html', unique_names=unique_names, selections_data=selections_data, total_links_total=total_links_total, page_identifier='selections')
@app.route('/add_selection', methods=['POST'])
def add_selection():
if request.method == 'POST':
name = request.form['name']
quality = request.form['quality']
quantity = int(request.form['quantity'])
if quantity == '0':
flash('Just Delete it!!', 'danger')
return redirect(url_for('selections'))
# Get User ID
user_data = get_user_data()
user_id = user_data['id']
# Ensure all fields are used
if not (name and quality and quantity):
flash('All fields are required', 'danger')
return redirect(url_for('selections'))
# Initiate DB Connection
conn, cursor = connect_to_database('uonew.db')
cursor.execute("SELECT * FROM mclinks WHERE name=? AND quality=?", (name, quality))
mclink_data = cursor.fetchone()
if not mclink_data:
flash('No matching link found', 'danger')
return redirect(url_for('selections'))
if quantity > mclink_data['quantity']:
flash('Not enough quantity available', 'danger')
return redirect(url_for('selections'))
# Check if selection exists
cursor.execute("SELECT * FROM selections WHERE mclink_id=? AND user_id=?", (mclink_data['id'], user_id))
existing_selection = cursor.fetchone()
if existing_selection:
# If selection exists, update selection
new_quantity = existing_selection['s_quantity'] + quantity
if new_quantity > mclink_data['quantity']:
flash('Not enough quantity available', 'danger')
return redirect(url_for('selections'))
else:
cursor.execute("UPDATE selections SET s_quantity=? WHERE id=?", (new_quantity, existing_selection['id']))
else:
# If selection doesn't exist, add new selection
links_total = quantity * mclink_data['guild_price']
cursor.execute("INSERT INTO selections (mclink_id, s_quantity, user_id, links_total) VALUES (?, ?, ?, ?)", (mclink_data['id'], quantity, user_id, links_total))
conn.commit()
conn.close()
flash('Selection added successfully', 'success')
return redirect(url_for('selections'))
# Route for Admin - Mastery Chain Link Selections
@app.route('/a_selections', methods=['POST', 'GET'])
def a_selections():
if g.selection_toggle == 1:
# If selection feature is enabled
s_toggle = 'Selections are ENABLED!'
else:
# If selection feature is not enabled
s_toggle = 'Selections are DISABLED!'
selection_data = select_selections()
conn, cursor = connect_to_database('uonew.db')
# Get unique names in database
cursor.execute(
"SELECT DISTINCT selections.user_id, users.id, users.display_name, users.sel_lock FROM selections INNER JOIN users ON selections.user_id = users.id;")
unique_names = cursor.fetchall()
# Fetch total sum of links for each user
totals = {}
for names in unique_names:
user_id = names['id']
cursor.execute("SELECT SUM(links_total) FROM selections WHERE user_id=?", (user_id,))
total_price = cursor.fetchone()[0]
totals[user_id] = "{:,}".format(total_price)
# Get unique links in database
cursor.execute("SELECT DISTINCT name FROM mclinks;")
unique_links = cursor.fetchall()
conn.commit()
conn.close()
return render_template('a_selections.html', s_toggle=s_toggle, selection_data=selection_data,
unique_names=unique_names, totals=totals, unique_links=unique_links, page_identifier='a_selections')
@app.route('/mclinks_delivery', methods=['GET', 'POST'])
def mclinks_delivery():
delivery_data = select_delivery()
conn, cursor = connect_to_database('uonew.db')
# Get unique names in database
cursor.execute("SELECT DISTINCT link_delivery.user_id, users.id, users.display_name FROM link_delivery INNER JOIN users ON link_delivery.user_id = users.id;")
unique_names = cursor.fetchall()
# Fetch total sum of links for each user
totals = {}
for names in unique_names:
user_id = names['id']
cursor.execute("SELECT SUM(links_total) FROM link_delivery WHERE user_id=?", (user_id,))
total_price = cursor.fetchone()[0]
totals[user_id] = "{:,}".format(total_price)
conn.commit()
conn.close()
return render_template('mclinks_delivery.html', delivery_data=delivery_data,
unique_names=unique_names, totals=totals, page_identifier='mclinks_delivery')
@app.route('/a_history')
def a_history():
history_data = select_history()
conn, cursor = connect_to_database('uonew.db')
# Get unique names in database
cursor.execute("SELECT DISTINCT history.user_id, users.id, users.display_name FROM history INNER JOIN users ON history.user_id = users.id;")
unique_names = cursor.fetchall()
# Fetch total sum of links for each user
totals = {}
for names in unique_names:
user_id = names['id']
cursor.execute("SELECT SUM(links_total) FROM history WHERE user_id=?", (user_id,))
total_price = cursor.fetchone()[0]
totals[user_id] = "{:,}".format(total_price)
conn.commit()
conn.close()
return render_template('a_history.html', history_data=history_data,
unique_names=unique_names, totals=totals, page_identifier='a_history')
# Route for Admin - User Management
@app.route('/a_users')
def a_users():
users_data = select_all_users()
return render_template('a_users.html', users_data=users_data, page_identifier='a_users')
# Route for User - User Management
@app.route('/user', methods=['GET', 'POST'])
def user():
user_data = get_user_data()
return render_template('user.html', user_data=user_data, page_identifier='user')
@app.route('/user_update_user', methods=['POST'])
def user_update_user():
if request.method == 'POST':
# Extract data from the form submission
user_id = request.form['id']
username = request.form['username']
display_name = request.form['display_name']
conn, cursor = connect_to_database('uonew.db')
cursor.execute("UPDATE users SET username = ?, display_name = ? WHERE id = ?", (username, display_name, user_id))
conn.commit()
conn.close()
g.display_name = display_name
flash('User details updated successfully', 'success')
return redirect(url_for('user'))
@app.route('/user_update_pass', methods=['POST'])
def user_update_pass():
if request.method == 'POST':
# Extract data from the form submission
user_id = request.form['id']
old_password = request.form['old_password']
new_password = request.form['new_password']
confirm_password = request.form['confirm_password']
# Check for empty fields
if not old_password or not new_password or not confirm_password:
flash('All fields are required.', 'danger')
# Check for matching password input
elif new_password != confirm_password:
flash('Passwords do not match.', 'danger')
# Password complexity validation
elif not (
len(new_password) >= 8 and
re.search(r'[A-Z]', new_password) and # At least one uppercase letter
re.search(r'[a-z]', new_password) and # At least one lowercase letter
re.search(r'\d', new_password) and # At least one digit
re.search(r'[!@#$%^&*()_+=\-[\]{};:\'",.<>?]', new_password) # At least one symbol
):
flash('Password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one digit, and one symbol.','danger')
else:
conn, cursor = connect_to_database('uonew.db')
hashed_password = generate_password_hash(new_password)
cursor.execute('UPDATE users SET password = ? WHERE id = ?', (hashed_password, user_id))
conn.commit()
conn.close()
flash('Password Updated Successful', 'success')
return redirect(url_for('user'))
# Function to assign links to users
@app.route('/test_assign_links')
def test_assign_links():
conn, cursor = connect_to_database('uonew.db')
# Get unique user IDs and randomize
unique_names = get_names()
user_ids = [name['id'] for name in unique_names]
random.shuffle(user_ids)
# Prepare to track item quantities dynamically
item_quantities = {}
for user_id in user_ids:
# Get user's selections
selections = select_selections(user_id)
for selection in selections:
mclink_id = selection['mclink_id']
required_quantity = selection['s_quantity']
# Check and update item quantity from dynamic tracking or database
if mclink_id not in item_quantities:
item_quantities[mclink_id] = selection['quantity'] # Initial quantity from the selections join query
print("ID QUAN:", item_quantities[mclink_id], "QUANVAIL:", selection['quantity'])
# Check if enough items are available
if item_quantities[mclink_id] >= required_quantity:
# Assign item and update tables
print("mclink", mclink_id, "quan", required_quantity, "id", user_id, "total", selection['links_total'])
cursor.execute("INSERT INTO link_delivery (mclink_id, s_quantity, user_id, links_total) VALUES (?, ?, ?, ?)",(mclink_id, required_quantity, user_id, selection['links_total']))
conn.commit()
# Update the tracked quantity
item_quantities[mclink_id] -= required_quantity
else:
# Handle unavailable items
print(f"Not enough of item {mclink_id} for user {user_id}")
conn.close()
return redirect(url_for('mclinks_delivery'))
@app.route('/assign_links')
def assign_links():
conn, cursor = connect_to_database('uonew.db')
# Get unique user IDs and randomize
unique_names = get_names()
user_ids = [name['id'] for name in unique_names]
random.shuffle(user_ids)
# Prepare to track item quantities dynamically
item_quantities = {}
for user_id in user_ids:
# Get user's selections
selections = select_selections(user_id)
for selection in selections:
mclink_id = selection['mclink_id']
required_quantity = selection['s_quantity']
# Check and update item quantity from dynamic tracking or database
if mclink_id not in item_quantities:
item_quantities[mclink_id] = selection['quantity'] # Initial quantity from the selections join query
print("ID QUAN:",item_quantities[mclink_id], "QUANVAIL:", selection['quantity'])
# Check if enough items are available
if item_quantities[mclink_id] >= required_quantity:
# Assign item and update tables
print("mclink",mclink_id, "quan",required_quantity, "id",user_id, "total",selection['links_total'])
cursor.execute("INSERT INTO link_delivery (mclink_id, s_quantity, user_id, links_total) VALUES (?, ?, ?, ?)", (mclink_id, required_quantity, user_id, selection['links_total']))
conn.commit()
# Update the tracked quantity
item_quantities[mclink_id] -= required_quantity
else:
# Handle unavailable items
print(f"Not enough of item {mclink_id} for user {user_id}")
# Delete user's selections
cursor.execute("DELETE FROM selections")
conn.commit()
cursor.execute("UPDATE global SET selections_toggle = 0 WHERE id = 1")
conn.commit()
conn.close()
return redirect(url_for('mclinks_delivery'))
# Reset link selections for ALL users
@app.route('/test_reset_delivery', methods=['POST'])
def test_reset_delivery():
if request.method == 'POST':
conn, cursor = connect_to_database('uonew.db')
cursor.execute("DELETE FROM link_delivery")
conn.commit()
conn.close()
flash('All Assigned Links have been reset', 'success')
return redirect(url_for('a_selections'))
@app.route('/test_reset_history', methods=['POST'])
def test_reset_history():
if request.method == 'POST':
conn, cursor = connect_to_database('uonew.db')
cursor.execute("DELETE FROM history")
conn.commit()
conn.close()
flash('History has been wiped', 'success')
return redirect(url_for('a_history'))
@app.route('/test_delivery', methods=['POST'])
def test_delivery():
if request.method == 'POST':
delivery_user = request.form['delivery_user']
delivery_data = select_delivery(delivery_user)
admin_id = session['id']
conn, cursor = connect_to_database('uonew.db')
current_timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
for record in delivery_data:
cursor.execute("INSERT INTO history (mclink_id, s_quantity, user_id, links_total, admin_id, datetime) VALUES (?, ?, ?, ?, ?, ?)",(record['mclink_id'], record['s_quantity'], record['user_id'], record['links_total'], admin_id, current_timestamp))
conn.commit()
conn.close()
flash("User's Links Delivered", 'success')
return redirect(url_for('a_history'))
@app.route('/delivery', methods=['POST'])
def delivery():
if request.method == 'POST':
delivery_user = request.form['delivery_user']
delivery_data = select_delivery(delivery_user)
admin_id = session['id']
conn, cursor = connect_to_database('uonew.db')
current_timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
for record in delivery_data:
print('qaunt1',record['s_quantity'], record['mclink_id'])
cursor.execute(
"INSERT INTO history (mclink_id, s_quantity, user_id, links_total, admin_id, datetime) VALUES (?, ?, ?, ?, ?, ?)",
(record['mclink_id'], record['s_quantity'], record['user_id'], record['links_total'], admin_id,
current_timestamp))
conn.commit()
print('subtract quant',record['s_quantity'], record['mclink_id'])
cursor.execute(
"UPDATE mclinks SET quantity = quantity - ? WHERE id = ?",
(record['s_quantity'], record['mclink_id']))
conn.commit()
cursor.execute("DELETE FROM link_delivery WHERE user_id =?", (delivery_user,))
conn.commit()
conn.close()
flash("User's Links Delivered", "success")
return redirect(url_for('a_history'))
@app.route('/import_csv', methods=['GET','POST'])
def import_csv():
# Initialize table
if request.method == 'POST':
file = request.files['file']
if file:
# Rename and save file
filename = 'links.csv'
file.save(filename)
conn, cursor = connect_to_database('uonew.db')
# Read uploaded file using pandas
data = pd.read_csv(filename)
for index, row in data.iterrows():
# Extract specific columns from the DataFrame
name = row['Name']
quality = row['Quality']
quantity = row['Quantity']
market_price = row['Market Price']
guild_price = row['Guild Price']
cursor.execute("UPDATE mclinks SET quantity = ?, market_price = ?, guild_price = ? WHERE name = ? AND quality = ?", (quantity, market_price, guild_price, name, quality))
conn.commit()
conn.close()
flash('List Updated.', 'success')
return redirect(url_for('a_mclinks'))
@app.route('/export_csv', methods=['GET', 'POST'])
def export_csv():
if request.method == 'POST':
# Select links data
links_data = select_links()
# Convert links to DataFrame with specified column names
df = pd.DataFrame(links_data, columns=['ID', 'Name', 'Quality', 'Quantity', 'Market Price', 'Guild Price'])
# Write DataFrame to buffer
buffer = io.BytesIO()
df.to_csv(buffer, index=False)
buffer.seek(0)
# Return CSV file as a downloadable attachment
return send_file(buffer,
download_name='links.csv',
as_attachment=True,
mimetype='text/csv')
flash('File Provided.', 'success')
return redirect(url_for('a_mclinks'))