-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1888 lines (1384 loc) · 56 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
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
import os
from flask import Flask, render_template, request, jsonify, send_file, abort, redirect
import random
from datetime import datetime
import datetime as dt
app_link = ""
app = Flask(__name__)
uid_2 = {}
prac_code = {}
class_code = {}
uid = {}
prac_date = {}
backup_cnt = 0
backup_tm = ""
with open("enc/c_dat", "r") as file:
raw_dat = file.read()
file.close()
exec(raw_dat)
print("Initial Values:\nuid_2:", uid_2, "\nprac_code:", prac_code, "\nclass_code:", class_code, "\nuid:", uid,
"\nprac_date", prac_date, "----------")
def get_user_data_by_uid(f_uid):
global uid
if uid.get(str(f_uid)):
return {
'uid': int(f_uid),
'username': uid[str(f_uid)][0],
'user-roll': int(uid[str(f_uid)][1]),
'permissions': int(uid[str(f_uid)][2]),
'password': uid[str(f_uid)][3]
}
return None
def get_class_data_by_code(f_class_code):
global class_code
if class_code.get(str(f_class_code)):
return {
'cls_code': f_class_code,
'year': class_code[str(f_class_code)][0],
'subject': class_code[str(f_class_code)][1],
'faculty': class_code[str(f_class_code)][2],
'student': class_code[str(f_class_code)][3]
}
return None
def create_class(year, subject):
global class_code, prac_code
ch = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z"]
new_class_code = ""
while 1:
for i in range(4):
new_class_code = new_class_code + ch[random.randint(0, len(ch) - 1)]
if class_code.get(str(new_class_code)) is None and prac_code.get(new_class_code) is None:
class_code[new_class_code] = [year, subject, ["0"], [], []]
return new_class_code
def create_practical(year, subject, batches: int = 1):
global prac_code, class_code, prac_date
ch = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z"]
new_class_code = ""
while 1:
for i in range(4):
new_class_code = new_class_code + ch[random.randint(0, len(ch) - 1)]
if class_code.get(str(new_class_code)) is None and prac_code.get(new_class_code) is None:
btc = {}
for n in range(int(batches)):
btc[ch[n]] = []
prac_code[new_class_code] = [year, subject, ["0"], [], [], btc]
btc_cp = {}
for n in range(int(batches)):
btc_cp[ch[n]] = []
prac_date[new_class_code] = btc_cp
return new_class_code
def create_subject(f_uid, f_class_code):
global class_code, uid_2
if class_code.get(f_class_code):
if uid.get(str(f_uid)):
if str(uid[str(f_uid)][2]) == "1" and f_uid not in class_code[f_class_code][2]:
class_code[f_class_code][2].append(str(f_uid))
return True
elif str(uid[str(f_uid)][2]) == "2" and f_uid not in class_code[f_class_code][3]:
class_code[f_class_code][3].append(str(f_uid))
if uid_2.get(str(f_uid)):
uid_2[str(f_uid)][f_class_code] = []
else:
uid_2[str(f_uid)] = {}
uid_2[str(f_uid)][f_class_code] = []
return True
else:
return False
else:
return False
return False
def create_batch(f_uid, f_class_code):
global prac_code, uid_2
f_class_code, batch = f_class_code[:-1], f_class_code[-1]
if prac_code.get(f_class_code):
if uid.get(str(f_uid)):
if str(uid[str(f_uid)][2]) == "1" and f_uid not in prac_code[f_class_code][2]:
prac_code[f_class_code][2].append(str(f_uid))
return True
elif str(uid[str(f_uid)][2]) == "2" and f_uid not in prac_code[f_class_code][3]:
prac_code[f_class_code][3].append(str(f_uid))
prac_code[f_class_code][5][batch].append(str(f_uid))
if uid_2.get(str(f_uid)):
uid_2[str(f_uid)][f_class_code] = []
else:
uid_2[str(f_uid)] = {}
uid_2[str(f_uid)][f_class_code] = []
return True
else:
return False
else:
return False
return False
def class_0():
global class_code, prac_code
cls_names = []
cls_code = []
for key in class_code.keys():
cls_names.append(class_code[key][0] + " " + class_code[key][1])
cls_code.append(str(key))
# for key in prac_code.keys():
# cls_names.append("P " + prac_code[key][0] + " " + prac_code[key][1])
# cls_code.append(str(key))
return cls_names, cls_code
def prac_0():
global prac_code
prc_name = []
prc_code = []
for key in prac_code.keys():
prc_name.append(prac_code[key][0] + " " + prac_code[key][1])
prc_code.append(str(key))
return prc_name, prc_code
def class_1(t_uid):
global class_code
cls_code = []
cls_names = []
for c in class_code:
if t_uid in class_code[c][2]:
cls_names.append(class_code[c][0] + " " + class_code[c][1])
cls_code.append(c)
return cls_names, cls_code
def prac_1(t_uid):
global prac_code
cls_code = []
cls_names = []
for c in prac_code:
if t_uid in prac_code[c][2]:
cls_names.append(prac_code[c][0] + " " + prac_code[c][1])
cls_code.append(c)
return cls_names, cls_code
def class_2():
return
def cls_std(cc):
global class_code, uid
std_id = class_code[cc][3]
std_name = []
std_roll = []
for i in std_id:
std_name.append(uid[i][0])
std_roll.append(int(uid[i][1]))
combined_list = list(zip(std_roll, std_name, std_id))
sorted_list = sorted(combined_list, key=lambda x: x[0])
if len(sorted_list) == 0:
return [], [], []
std_roll, std_name, std_id = zip(*sorted_list)
return std_name, std_roll, std_id
def prac_std(cc):
global prac_code, uid
std_name = []
std_roll = []
if len(cc) == 5:
std_id = prac_code[cc[:-1]][5][cc[-1]]
elif len(cc) == 4:
std_id = prac_code[cc][3]
else:
return [], [], []
for i in std_id:
std_name.append(uid[i][0])
std_roll.append(int(uid[i][1]))
combined_list = list(zip(std_roll, std_name, std_id))
sorted_list = sorted(combined_list, key=lambda x: x[0])
if len(sorted_list) == 0:
return [], [], []
std_roll, std_name, std_id = zip(*sorted_list)
return std_name, std_roll, std_id
@app.route("/")
def index():
global class_code
user_agent = request.headers.get('User-Agent').lower()
print(user_agent)
if user_agent != "co.median.android.awpbew":
cookie_value = request.cookies.get('user_id')
if cookie_value:
print("Cookies: ", cookie_value)
per = get_user_data_by_uid(int(cookie_value))
print(per)
if per is not None:
per = per['permissions']
if per == 0:
cc, cn = class_0()
prn, prc = prac_0()
cls_year = ["All"]
prac_year = ["All"]
for key in class_code.keys():
if class_code[key][0] not in cls_year:
cls_year.append(class_code[key][0])
for key in prac_code.keys():
if prac_code[key][0] not in prac_year:
prac_year.append(prac_code[key][0])
return render_template("hod_dash.html", PRACYEAR=prac_year, CLSYEAR=cls_year,
CLASSCODE=cc, NAME=cn, PRACODE=prc, PRANAME=prn)
elif per == 1:
cc, cn = class_1(str(cookie_value))
prc, prn = prac_1(str(cookie_value))
return render_template("faculty_dash.html", CLASSCODE=cc, NAME=cn, PRACODE=prc, PRANAME=prn)
elif per == 2:
return render_template("student_dash.html")
else:
return render_template("login.html")
else:
return render_template("login.html")
return redirect('/docs')
@app.route("/profile")
def profile():
global uid
cookie_value = request.cookies.get('user_id')
if uid[str(cookie_value)][2] == "0":
return render_template("profile.html", DASH="/", PROGRESS="/hd_cls", PRINT='/print',
NAME=uid[str(cookie_value)][0].split(" ")[0],
ID=str(cookie_value), DES="HOD",
SERVERCODE="navigator.clipboard.writeText(btoa(window.location.host, 'calisthenics'));")
elif uid[str(cookie_value)][2] == "2":
return render_template("profile.html", DASH="/", PROGRESS="/progress", PRINT='/profile',
NAME=uid[str(cookie_value)][0].split(" ")[0],
ID=str(cookie_value), DES="Student",
SERVERCODE="alert('Your do not have permission to access Server Code.');")
elif uid[str(cookie_value)][2] == "1":
return render_template("profile.html", DASH="/", PROGRESS="/faculty-class", PRINT='/print',
NAME=uid[str(cookie_value)][0].split(" ")[0],
ID=str(cookie_value), DES="HOD", SERVERCODE="")
@app.route("/hd_cls")
def hd_cls():
cc, cn = class_0()
k, m = prac_0()
for i, j in zip(k, m):
cc.append(i + " (Lab)")
cn.append(j)
return render_template("hod_class.html", CLASSCODE=cc, NAME=cn)
@app.route("/faculty-class")
def faculty_class():
cookie_value = request.cookies.get('user_id')
cc, cn = class_1(str(cookie_value))
k, m = prac_1(str(cookie_value))
for i, j in zip(k, m):
cc.append(i + " (Lab)")
cn.append(j)
return render_template("faculty_class.html", CLASSCODE=cc, NAME=cn)
@app.route("/progress")
def progress():
global uid_2, class_code, prac_code
cookie_value = request.cookies.get('user_id')
if uid_2.get(str(cookie_value)):
cc = []
cn = []
pc = []
pn = []
for i in uid_2[str(cookie_value)]:
if class_code.get(i):
cc.append(i)
print(i)
cn.append(class_code[i][0] + " " + class_code[i][1])
elif prac_code.get(i):
pc.append(i)
print(i)
pn.append(prac_code[i][0] + " " + prac_code[i][1])
return render_template("student_class.html", CLASSCODE=cc, NAME=cn, PRACODE=pc, PRANAME=pn)
else:
return render_template("student_class.html", CLASSCODE=[], NAME=[], PRACODE=[], PRANAME=[])
@app.route("/print")
def prnt():
cookie_value = request.cookies.get("user_id")
if uid.get(cookie_value):
if uid[cookie_value][2] == "0":
cc, cn = [], []
cls_year = ["All"]
for key in class_code.keys():
if class_code[key][0] not in cls_year:
cls_year.append(class_code[key][0])
cc.append(key)
cn.append(class_code[key][0] + " " + class_code[key][1])
for key in prac_code.keys():
if prac_code[key][0] not in cls_year:
cls_year.append(prac_code[key][0])
cc.append(key)
cn.append(prac_code[key][0] + " " + prac_code[key][1] + " (Lab)")
return render_template("print.html", CLASSNAME=cn, CLASSCODE=cc, CLSYEAR=cls_year, PROGRESS='/hd_cls')
elif uid[cookie_value][2] == "1":
cc, cn = [], []
cls_year = ["All"]
a, b = class_1(str(cookie_value))
cc = cc + a
cn = cn + b
a, b = prac_1(str(cookie_value))
for _, __ in zip(a, b):
cc.append(_ + " (Lab)")
cn.append(__)
# cc = cc + a
# cn = cn + b
for c in class_code:
if cookie_value in class_code[c][2] and class_code[c][0] not in cls_year:
cls_year.append(class_code[c][0])
for c in prac_code:
if cookie_value in prac_code[c][2] and prac_code[c][0] not in cls_year:
cls_year.append(prac_code[c][0])
return render_template("print.html", CLASSNAME=cc, CLASSCODE=cn, CLSYEAR=cls_year,
PROGRESS='/faculty-class')
elif uid[cookie_value][2] == "2":
return render_template("print.html")
@app.route("/print-report", methods=["POST"])
def print_request():
def get_att(cookie_value, ccc, f_d, t_d):
def date_cnt(date_list, f, t):
d_cnt = 0
for date in date_list:
eff_d = datetime.strptime(date.split("_")[0], '%d-%m-%Y')
if f <= eff_d <= t:
d_cnt = d_cnt + 1
return d_cnt
if uid_2.get(str(cookie_value)):
cnt = 0
ttl_cnt = 0
if class_code.get(ccc):
# ttl_cnt = len(class_code[ccc][4])
ttl_cnt = date_cnt(class_code[ccc][4], f_d, t_d)
# cnt = len(uid_2[str(cookie_value)][ccc])
cnt = date_cnt(uid_2[str(cookie_value)][ccc], f_d, t_d)
elif prac_code.get(ccc) and cookie_value in prac_code[ccc][3]:
for e in prac_code[ccc][5].keys():
if cookie_value in prac_code[ccc][5][e]:
# ttl_cnt = len(prac_date[ccc][e])
ttl_cnt = date_cnt(prac_date[ccc][e], f_d, t_d)
# print("e: {}, ttl_cnt: {}".format(e, ttl_cnt))
# cnt = len(uid_2[str(cookie_value)][ccc])
cnt = date_cnt(uid_2[str(cookie_value)][ccc], f_d, t_d)
if ttl_cnt == 0:
return "-"
perk = int((cnt / ttl_cnt) * 100)
return perk
return 0
print_data_request = request.get_json()
print(print_data_request)
type_batch = print_data_request["type"]
batch = print_data_request["batch"]
f_date = print_data_request["f_date"]
t_date = print_data_request["t_date"]
percent_a_b = print_data_request["percent_a_b"]
percent = print_data_request["percent"]
out_arr = [["Sr. No.", "Roll Number", "Name"], ]
f_date = datetime.strptime(f_date, '%Y-%m-%d')
t_date = datetime.strptime(t_date, '%Y-%m-%d')
print(f_date)
if f_date > t_date or f_date == t_date:
print("date miss")
return jsonify({'message': "Date mismatched."}), 401
if type_batch == "Year wise":
cc = []
pc = []
batch_loc = []
for b in batch:
for c in class_code:
if class_code[c][0] == b:
cc.append(c)
batch_loc.append(c)
out_arr[0].append(class_code[c][1])
for b in batch:
for c in prac_code:
if prac_code[c][0] == b:
pc.append(c)
batch_loc.append(c)
out_arr[0].append(prac_code[c][1] + "(Lab)")
# print(cc, pc, out_arr, )
uid_loc = []
for cl in cc:
for s in class_code[cl][3]:
if s not in uid_loc:
uid_loc.append(s)
temp_arr = [str(len(out_arr)), uid[s][1], uid[s][0]]
# for _ in range(len(batch_loc)):
# temp_arr.append("0")
out_arr.append(temp_arr)
for pl in pc:
for s in prac_code[pl][3]:
if s not in uid_loc:
uid_loc.append(s)
temp_arr = [str(len(out_arr)), uid[s][1], uid[s][0]]
# for _ in range(len(batch_loc)):
# temp_arr.append("0")
out_arr.append(temp_arr)
print("no mention\n", out_arr)
for cl in cc:
for s in class_code[cl][3]:
per = get_att(s, cl, f_date, t_date)
ind = batch_loc.index(cl) + 3
# print("ind: {}".format(ind))
len_diff = ind - len(out_arr[uid_loc.index(s) + 1])
# print("len: {}".format(len(out_arr[uid_loc.index(s) + 1])))
print("len_diff: {} for Att_Id: {}".format(len_diff, s))
for _ in range(len_diff):
out_arr[uid_loc.index(s) + 1].append("-")
out_arr[uid_loc.index(s) + 1].append(str(per))
# out_arr[uid_loc.index(s) + 1].insert(batch_loc.index(cl) + 3, str(per))
for pl in pc:
for s in prac_code[pl][3]:
per = get_att(s, pl, f_date, t_date)
ind = batch_loc.index(pl) + 3
# print("ind: {}".format(ind))
len_diff = ind - len(out_arr[uid_loc.index(s) + 1])
# print("len: {}".format(len(out_arr[uid_loc.index(s) + 1])))
print("len_diff: {} for Att_Id: {}".format(len_diff, s))
for _ in range(len_diff):
out_arr[uid_loc.index(s) + 1].append("-")
out_arr[uid_loc.index(s) + 1].append(str(per))
# out_arr[uid_loc.index(s) + 1].insert(batch_loc.index(pl) + 3, str(per))
out_arr[0].append("Overall Attendance")
final_arr = [out_arr[0], ]
for id_, rem in enumerate(out_arr[1:]):
cnt = 0
t_cnt = 0
for rec in rem[3:]:
if rec != "-":
cnt = cnt + int(rec)
t_cnt = t_cnt + 100
if t_cnt == 0:
ka = 0
# out_arr[id_+1].append("0")
else:
ka = (cnt / t_cnt) * 100
ind = len(out_arr[0]) - 1
len_diff = ind - len(rem)
for _ in range(len_diff):
rem.append("-")
rem.append(str(round(ka, 2)))
if percent_a_b == "Below":
if ka <= int(percent):
final_arr.append(rem)
elif percent_a_b == "Above":
if ka > int(percent):
final_arr.append(rem)
else:
final_arr.append(rem)
# out_arr[id_+1].append(str(ka))
f_name = f"dat/{datetime.now().strftime('%H:%M')}_Overall_Attendance_Report_{f_date.date().strftime('%d-%m')}_to_{t_date.date().strftime('%d-%m')}.csv"
with open(f_name, "a") as exp:
for row in final_arr:
for cell in row:
exp.write(str(cell) + ",")
exp.write("\n")
return jsonify({'message': 'File ready', 'file_path': app_link + f_name[4:]})
elif type_batch == "Batch wise":
cc = []
pc = []
batch_loc = []
for b in batch:
if class_code.get(b):
cc.append(b)
batch_loc.append(b)
out_arr[0].append(class_code[b][1])
elif prac_code.get(b):
pc.append(b)
batch_loc.append(b)
out_arr[0].append(prac_code[b][1] + "(Lab)")
print(cc, pc, out_arr, )
uid_loc = []
for cl in cc:
for s in class_code[cl][3]:
if s not in uid_loc:
uid_loc.append(s)
temp_arr = [str(len(out_arr)), uid[s][1], uid[s][0]]
# for _ in range(len(batch_loc)):
# temp_arr.append("0")
out_arr.append(temp_arr)
for pl in pc:
for s in prac_code[pl][3]:
if s not in uid_loc:
uid_loc.append(s)
temp_arr = [str(len(out_arr)), uid[s][1], uid[s][0]]
# for _ in range(len(batch_loc)):
# temp_arr.append("0")
out_arr.append(temp_arr)
print("no mention\n", out_arr)
for cl in cc:
for s in class_code[cl][3]:
per = get_att(s, cl, f_date, t_date)
ind = batch_loc.index(cl) + 3
# print("ind: {}".format(ind))
len_diff = ind - len(out_arr[uid_loc.index(s) + 1])
# print("len: {}".format(len(out_arr[uid_loc.index(s) + 1])))
print("len_diff: {} for Att_Id: {}".format(len_diff, s))
for _ in range(len_diff):
out_arr[uid_loc.index(s) + 1].append("0")
out_arr[uid_loc.index(s) + 1].append(str(per))
# out_arr[uid_loc.index(s) + 1].insert(batch_loc.index(cl) + 3, str(per))
for pl in pc:
for s in prac_code[pl][3]:
per = get_att(s, pl, f_date, t_date)
ind = batch_loc.index(pl) + 3
# print("ind: {}".format(ind))
len_diff = ind - len(out_arr[uid_loc.index(s) + 1])
# print("len: {}".format(len(out_arr[uid_loc.index(s) + 1])))
print("len_diff: {} for Att_Id: {}".format(len_diff, s))
for _ in range(len_diff):
out_arr[uid_loc.index(s) + 1].append("-")
out_arr[uid_loc.index(s) + 1].append(str(per))
# out_arr[uid_loc.index(s) + 1].insert(batch_loc.index(pl) + 3, str(per))
out_arr[0].append("Overall Attendance")
print("outarray: {}".format(out_arr))
final_arr = [out_arr[0], ]
for id_, rem in enumerate(out_arr[1:]):
cnt = 0
t_cnt = 0
for rec in rem[3:]:
if rec != "-":
cnt = cnt + int(rec)
t_cnt = t_cnt + 100
if t_cnt == 0:
ka = 0
# out_arr[id_+1].append("0")
else:
ka = (cnt / t_cnt) * 100
ind = len(out_arr[0]) - 1
len_diff = ind - len(rem)
for _ in range(len_diff):
rem.append("-")
rem.append(str(round(ka, 2)))
if percent_a_b == "Below":
if ka <= int(percent):
final_arr.append(rem)
elif percent_a_b == "Above":
if ka > int(percent):
final_arr.append(rem)
else:
final_arr.append(rem)
# out_arr[id_+1].append(str(ka))
f_name = f"dat/{datetime.now().strftime('%H:%M')}_Overall_Attendance_Report_{f_date.date().strftime('%d-%m')}_to_{t_date.date().strftime('%d-%m')}.csv"
with open(f_name, "a") as exp:
for row in final_arr:
for cell in row:
exp.write(str(cell) + ",")
exp.write("\n")
return jsonify({'message': 'File ready', 'file_path': app_link + f_name[4:]})
return jsonify({'message': 'Invalid credentials'}), 401
@app.route("/docs")
def docs():
files = os.listdir("./dat")
print(files)
files = [f for f in files if os.path.isfile(os.path.join("./dat", f)) and not f.startswith('.')]
files_with_creation_time = [(file, os.path.getctime(os.path.join("./dat", file))) for file in files]
# Sort the list of tuples by creation time
files_with_creation_time.sort(key=lambda x: x[1])
# Extract only the filenames from the sorted list
sorted_files = [file[0] for file in files_with_creation_time]
return render_template("uptd_docs.html", FILE=sorted_files)
@app.route("/docs/get/<file_name>")
def s_docs(file_name):
print(f"file_name: {file_name}")
if os.path.exists(f"./dat/{file_name}"):
return send_file(f"./dat/{file_name}", as_attachment=True)
else:
abort(500)
@app.route("/att/<cc>")
def att(cc):
global class_code
n, r, i = cls_std(cc)
sb_name, yr = class_code[cc][0], class_code[cc][1]
return render_template("faculty_att.html", SUBJECT=sb_name, BATCH=yr, ROLL=r, NAME=n, ID=i, CLASSCODE=cc)
@app.route("/att-p/<cc>")
def att_p(cc):
global prac_code
n, r, i = prac_std(cc)
sb_name, yr = prac_code[cc[:-1]][0], prac_code[cc[:-1]][1]
return render_template("faculty_att.html", SUBJECT=sb_name, BATCH=yr, ROLL=r, NAME=n, ID=i, CLASSCODE=cc)
# if len(cc) == 4:
# n, r, i = prac_std(cc)
# sb_name, yr = prac_code[cc][0], prac_code[cc][1]
# return render_template("faculty_att.html", SUBJECT=sb_name, BATCH=yr, ROLL=r, NAME=n, ID=i, CLASSCODE=cc)
# elif len(cc) == 5:
# cc, batch = cc[:-1], cc[-1]
# n, r, i = prac_std(cc)
# sb_name, yr = prac_code[cc][0], prac_code[cc][1]
# return render_template("faculty_att.html", SUBJECT=sb_name, BATCH=yr, ROLL=r, NAME=n, ID=i, CLASSCODE=cc)
@app.route("/att-s/<cc>")
def att_s(cc):
global prac_code
cds, nms = [], []
nm = prac_code[cc][0] + " " + prac_code[cc][1]
for k in prac_code[cc][5]:
cds.append(cc + k)
nms.append(nm + " batch " + k)
sb_name, yr = prac_code[cc][0], prac_code[cc][1]
return render_template("prac_batch.html", POSTL="/att-p/", SUBJECT=sb_name, BATCH=yr, NAME=nms, CLASSCODE=cds)
@app.route("/info/<cc>")
def class_info_rend(cc):
global class_code, prac_code, uid
std_name = []
faculty_name = []
if class_code.get(cc):
year, name, faculty, student = class_code[cc][0], class_code[cc][1], class_code[cc][2], class_code[cc][3]
n, r, i = cls_std(cc)
else:
year, name, faculty, student = prac_code[cc][0], prac_code[cc][1] + " (Lab)", prac_code[cc][2], prac_code[cc][3]
n, r, i = prac_std(cc)
for _, __ in zip(n, r):
std_name.append(str(__) + " " + str(_))
for f in faculty:
faculty_name.append(uid[f][0])
# for s in student:
# std_name.append(str(uid[s][1]) + " " + uid[s][0])
return render_template("class_info.html", BATCH=year, NAME=name,
FACULTY=faculty_name, STUDENT=std_name, CC=cc)
@app.route('/login', methods=['POST'])
def login():
user_credentials = request.get_json()
print("\n", user_credentials)
user_id = user_credentials.get("user_id")
password = user_credentials.get("password")
print(user_id, password)
ud = get_user_data_by_uid(str(user_id))
print(ud)
if ud:
if password == ud['password']:
print("login success\n")
response_data = {'message': 'Login successful', 'auth_token': 'example_auth_token'}
response = jsonify(response_data)
response.set_cookie('user_id', user_id)
return response
else:
return jsonify({'message': 'Invalid credentials'}), 401
else:
# Return an error response if authentication fails
return jsonify({'message': 'Invalid credentials'}), 401
@app.route("/signup", methods=["POST"])
def signup():
global uid
user_credentials = request.get_json()
print(user_credentials)
name = user_credentials["name"]
roll = user_credentials['roll']
password = user_credentials['password']
i = 5
while 1:
if uid.get(str(i)):
i = i + 1
else:
uid[str(i)] = [name, roll, '2', password]
return jsonify({"n_id": i})
@app.route('/send-array/<cc>', methods=['POST'])
def receive_array(cc):
global uid_2, class_code, prac_code, prac_date
temp_uid_2 = uid_2
try:
data = request.get_json()
cur_time = datetime.now().strftime("%d-%m-%Y_%H:%M")
if len(cc) == 5:
cc, batch = cc[:-1], cc[-1]
if class_code.get(cc):
if cur_time not in class_code[cc][4]:
received_array = data.get('array')
for i in received_array:
uid_2[i][cc].append(cur_time)
class_code[cc][4].append(cur_time)
print(f"received attendance for {cc}", received_array, type(received_array))
return jsonify({'success': True, 'message': 'Array received successfully'})
else:
return jsonify({'success': True, 'message': 'Array received successfully'})
elif prac_code.get(cc):
if cur_time not in prac_code[cc][4]:
received_array = data.get('array')
for i in received_array:
uid_2[i][cc].append(cur_time)
prac_code[cc][4].append(cur_time)
prac_date[cc][batch].append(cur_time)
print(f"received attendance for {cc}", received_array, type(received_array))
return jsonify({'success': True, 'message': 'Array received successfully'})
else:
return jsonify({'success': True, 'message': 'Array received successfully'})
else:
return jsonify({'success': False, 'message': f'Class code mismatched'})
except Exception as e:
uid_2 = temp_uid_2
return jsonify({'success': False, 'message': f'Error: {str(e)}'})
@app.route("/test")
def d_est():
return render_template("test.html")
@app.route("/add_class", methods=['POST'])
def add_class():
cookie_value = request.cookies.get('user_id')
data = request.get_json()
year = data.get('year')
subject = data.get('subject')
print("hello")
print("Cookies: ", cookie_value)
if cookie_value:
per = get_user_data_by_uid(int(cookie_value))
print(per)
if per is not None:
per = per['permissions']
print(per, type(per))
if per == 0:
cc = create_class(year, subject)
response_data = {'message': 'Data received successfully', 'class_code': cc}
return jsonify(response_data)
else:
return jsonify({'error': "class cannot be added"}), 401
else:
return jsonify({'error': "class cannot be added"}), 401
else:
return jsonify({'error': "class cannot be added"}), 401
@app.route("/add_practical", methods=['POST'])
def add_practical():
cookie_value = request.cookies.get('user_id')
data = request.get_json()
year = data.get('year')
subject = data.get('subject')
batch = data.get('batch')
print("Cookies: ", cookie_value)