-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
1257 lines (780 loc) · 39.3 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
#python -m http.server 8000 --bind 127.0.0.1
from bottle import route, run, debug, template, request, static_file, error ,get,post,response,redirect
# only needed when you run Bottle on mod_wsgi
from bottle import default_app
import datetime
import mysql.connector
import collections
from mysql.connector import errorcode
from more_itertools import unique_everseen
#current_students
config = {
'user': 'root',
'password': 'root',
'host': '127.0.0.1',
'database': 'hms',
'raise_on_warnings': True,
}
cnx = mysql.connector.connect(**config)
from bottle import static_file
@route('/static/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='static') # use static or ./static, / implies absolute path
@route('/ajax_student_added')
def ajax_student_added():
return 'The new student was inserted into the database'
@get('/')
def just_get():
redirect('/index')
@get('/index')
def index_get():
if(request.get_cookie("user") is None):
redirect('/login')
return template('tpl/index',lolcat=request.get_cookie("user"),str="Successfully logged in as {}".format(request.get_cookie("user")))
@get('/logout')
def logout_get():
response.set_cookie("user","",expires=0)
return "Logged out."
@get('/login')
def login_get():
return template('tpl/login')
@post('/login')
def login_post():
c = cnx.cursor()
c.execute("SELECT 1 FROM student where roll_no={} and year!='0' ".format(request.POST.get('1')))
result = c.fetchone()
c.close()
if((result is None) and request.POST.get('1')!='0'):
return {'text':"Roll no. doesn't exist "}
if(request.POST.get('1')!=request.POST.get('2')):
return {'text':"Incorrect Password"}
else:
response.set_cookie("user",request.POST.get('1'))
#return "Successfully logged in as {}. ".format(request.POST.get('1'))
return {'redirect':"/index"}
@get('/next_year')
def next_year_get():
if(request.get_cookie("user") is None):
redirect('/login')
if(request.get_cookie("user")!='0'):
return "Access denied."
return template('tpl/next_year')
@post('/next_year')
def next_year_post():
"""Check to see if an uploaded file contains
a target string 'Bobalooba'"""
upload = request.files.get('newfile')
# only allow upload of text files
if upload.content_type != 'text/plain':
return "Only text files allowed"
output="Done ."
c=cnx.cursor()
for line in upload.file.readlines():
query="""call fail({});""".format(line.decode())
try:
c.execute(query)
except mysql.connector.Error as err:
output= "Failed failing student {} in database: {} <br/>".format(line.decode(),err) + output
cnx.commit()
c.close()
c=cnx.cursor()
query="""call forward()"""
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed forward student in database: {}".format(err))
cnx.commit()
c.close()
return output
@get('/show_students')
def show_students():
if(request.get_cookie("user") is None):
redirect('/login')
if(request.get_cookie("user")!='0'):
return "Access denied."
c = cnx.cursor()
c.execute("SELECT * FROM student ")
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='student' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/make_table', rows=result,columns=column_names,lolcat=request.get_cookie("user"))
return output
# note that %s evaluates to 'string' not string
@get('/show_hostel')
def show_hostel():
c = cnx.cursor()
try:
c.execute("SELECT * FROM hostel ")
except mysql.connector.Error as err:
return ("Failed fetching from table hostel: {}".format(err))
result = c.fetchall()
cnx.commit()
c.close()
c = cnx.cursor()
try:
c.execute("SELECT column_name from information_schema.columns where table_name='hostel' and table_schema='hms'")
except mysql.connector.Error as err:
return ("Failed fetching column names of table hms.hostel, please make sure that it exists: {}".format(err))
column_names = c.fetchall()
cnx.commit()
c.close()
output = template('tpl/make_table', rows=result,columns=column_names,lolcat=request.get_cookie("user"))
return output
@get('/update_gate_record')
def update_gate_record_get():
if(request.get_cookie("user") is None):
redirect('/login')
if(request.get_cookie("user")!='0'):
return "Access denied."
return template('tpl/update_get_record',lolcat=request.get_cookie("user"))
@post('/update_gate_record')
def update_gate_record_post():
if(request.POST.get('10')=='1'):
c=cnx.cursor()
query="""call gate_record_in({});""".format(request.POST.get('1'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed making sure not 2 null entry at same time gate_record in database: {}".format(err))
cnx.commit()
c.close()
c=cnx.cursor()
query=""" INSERT into gate_record(roll_no,purpose) values ({},'{}' ) """.format(request.POST.get('1'),request.POST.get('2'))
try:
c.execute(query)
except mysql.connector.Error as err:
if err.errno == 1452:
return ("Student doesn't exist")
return ("Failed adding gate_record in database: {}".format(err))
cnx.commit()
c.close()
c=cnx.cursor()
query="""SELECT * from gate_record where roll_no={} order by exit_time desc limit 1""".format(request.POST.get('1'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed fetching gate_record from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='gate_record' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
elif (request.POST.get('10')=='2'):
c=cnx.cursor()
query=""" call gate_record_in({}) """.format(request.POST.get('1'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed updating entry time for gate_record in database: {}".format(err))
cnx.commit()
c.close()
c=cnx.cursor()
query="""SELECT * from gate_record where roll_no={} order by entry_time desc,exit_time desc limit 1""".format(request.POST.get('1'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed fetching gate_record from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='gate_record' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
elif(request.POST.get('10')=='3'):
c = cnx.cursor()
if(request.POST.get('5') =='0' and request.POST.get('9') =='0'):
query=""" SELECT * from current_students natural join gate_record order by roll_no asc,isnull(entry_time) desc,entry_time desc """
elif(request.POST.get('5') =='0' and request.POST.get('9') =='1'):
query=""" SELECT * from current_students natural join gate_record where isnull(entry_time) order by roll_no asc """
elif(request.POST.get('5') =='1' and request.POST.get('9') =='0'):
query=""" SELECT * from student natural join gate_record where roll_no={} order by isnull(entry_time) desc,entry_time desc """.format(request.POST.get('7'))
elif(request.POST.get('5') =='1' and request.POST.get('9') =='1'):
query=""" SELECT * from student natural join gate_record where roll_no={} and isnull(entry_time) order by entry_time desc """.format(request.POST.get('7'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed fetching from gate record case 3 from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='student' and table_schema='hms'")
column_names=c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='gate_record' and table_schema='hms'")
column_names+=c.fetchall()
column_names=list(unique_everseen(column_names))
column_names[0],column_names[1]=column_names[1],column_names[0]
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
@get('/event')
def event_get():
if(request.get_cookie("user") is None):
redirect('/login')
if(request.get_cookie("user")!='0'):
c=cnx.cursor()
query="""SELECT hostel_id from student where roll_no={}""".format(request.get_cookie("user"))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed getting hostel id from user: {}".format(err))
result=c.fetchone();
c.close()
c=cnx.cursor()
query="""SELECT * from event where start_time>now() order by field(hostel_id,{},{} ,{} ) asc ,start_time desc""".format(result[0] if result[0] else 3 ,(result[0]+1)%3 if (result[0]+1)%3 else 3,(result[0]+2)%3 if (result[0]+2)%3 else 3)
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed getting custom (user) event from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='event' and table_schema='hms' ")
column_names=c.fetchall()
c.close()
output = template('tpl/make_table', rows=result,columns=column_names,lolcat=request.get_cookie("user"))
return output
return template('tpl/event',lolcat=request.get_cookie("user"))
@post('/event')
def event_post():
if(request.POST.get('10') == '1'):
c=cnx.cursor()
date_in = request.POST.get('2') #u'2015-01-02T00:00'
date_out = datetime.datetime(*[int(v) for v in date_in.replace('T', '-').replace(':', '-').split('-')])
query=""" INSERT into event(description,start_time,expenditure,hostel_id) values ('{}','{}',{},{} ) """.format(request.POST.get('1'),str(date_out),request.POST.get('3'),request.POST.get('4'))
print(query)
try:
c.execute(query)
except mysql.connector.Error as err:
if err.errno == 1452:
return ("Hostel doesn't exist")
return ("Failed adding to event in database: {}".format(err))
cnx.commit()
c.close()
c=cnx.cursor()
query="""SELECT * from event order by event_id desc limit 1"""
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed fetching from event 1 from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='event' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
elif(request.POST.get('10')=='2'):
c=cnx.cursor()
if(request.POST.get('5')=='0' and request.POST.get('9')=='0' ):
query="""SELECT * from event order by event_id desc"""
elif(request.POST.get('5')=='0' and request.POST.get('9')=='1'):
query="""SELECT * from event where start_time>now() order by start_time desc"""
elif(request.POST.get('5')=='0' and request.POST.get('9')=='2'):
query="""SELECT * from event where start_time<now() order by start_time desc"""
elif(request.POST.get('5')=='1' and request.POST.get('9')=='0'):
query="""SELECT * from event where description like '%{}%' order by event_id desc""".format(request.POST.get('1'))
elif(request.POST.get('5')=='1' and request.POST.get('9')=='1'):
query="""SELECT * from event where description like '%{}%' and start_time>now() order by start_time desc""".format(request.POST.get('1'))
elif(request.POST.get('5')=='1' and request.POST.get('9')=='2'):
query="""SELECT * from event where description like '%{}%' and start_time<now() order by start_time desc""".format(request.POST.get('1'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed show_it from event from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='event' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
@get('/courier')
def courier_get():
if(request.get_cookie("user") is None):
redirect('/login')
if(request.get_cookie("user")!='0'):
c=cnx.cursor()
query="""SELECT * from courier where roll_no={} order by isnull(collected_date) desc,received_date desc""".format(request.get_cookie("user"))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed getting custom (user) courier from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='courier' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/make_table', rows=result,columns=column_names,lolcat=request.get_cookie("user"))
return output
return template('tpl/courier',lolcat=request.get_cookie("user"))
@post('/courier')
def courier_post():
if(request.POST.get('10') == '1'):
c=cnx.cursor()
query=""" INSERT into courier(roll_no,description) values ({},'{}' ) """.format(request.POST.get('1'),request.POST.get('2'))
try:
c.execute(query)
except mysql.connector.Error as err:
#print(query)
if err.errno == 1452:
return ("Student doesn't exist")
return ("Failed adding to courier in database: {}".format(err))
cnx.commit()
c.close()
c=cnx.cursor()
query="""SELECT * from courier where roll_no={} order by courier_id desc limit 1""".format(request.POST.get('1'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed fetching from courier from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='courier' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
elif(request.POST.get('10')=='2') :
c=cnx.cursor()
query=""" call courier_col({},{}) """.format(request.POST.get('1'),request.POST.get('2'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed updating collected date for courier in database: {}".format(err))
cnx.commit()
c.close()
c=cnx.cursor()
query="""SELECT * from courier where roll_no={} and courier_id={} """.format(request.POST.get('1'),request.POST.get('2'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed fetching courier from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='courier' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
elif(request.POST.get('10')=='3'):
c=cnx.cursor()
if(request.POST.get('5')=='0' and request.POST.get('9')=='0' ):
query="""SELECT * from courier order by roll_no asc,courier_id asc"""
elif(request.POST.get('5')=='0' and request.POST.get('9')=='1'):
query="""SELECT * from courier where isnull(collected_date) order by roll_no asc,courier_id asc"""
elif(request.POST.get('5')=='0' and request.POST.get('9')=='2'):
query="""SELECT * from courier where not isnull(collected_date) order by roll_no asc,courier_id asc"""
elif(request.POST.get('5')=='1' and request.POST.get('9')=='0'):
query="""SELECT * from courier where roll_no={} order by isnull(collected_date) desc,courier_id desc""".format(request.POST.get('7'))
elif(request.POST.get('5')=='1' and request.POST.get('9')=='1'):
query="""SELECT * from courier where roll_no={} and isnull(collected_date) order by courier_id desc""".format(request.POST.get('7'))
elif(request.POST.get('5')=='1' and request.POST.get('9')=='2'):
query="""SELECT * from courier where roll_no={} and not isnull(collected_date) order by courier_id desc""".format(request.POST.get('7'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed show_it from courier from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='courier' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
@get('/complaint')
def complaint_get():
if(request.get_cookie("user") is None):
redirect('/login')
if(request.get_cookie("user")!='0'):
output = template('tpl/complaint_s', rolling=request.get_cookie("user"))
return output
return template('tpl/complaint',lolcat=request.get_cookie("user"))
@post('/complaint')
def complaint_post():
if(request.POST.get('10') == '1'):
c=cnx.cursor()
query=""" INSERT into complaint(roll_no,description) values ({},'{}' ) """.format(request.POST.get('1'),request.POST.get('2'))
try:
c.execute(query)
except mysql.connector.Error as err:
if err.errno == 1452:
return ("Student doesn't exist")
return ("Failed adding to complaint in database: {}".format(err))
cnx.commit()
c.close()
c=cnx.cursor()
query="""SELECT * from complaint where roll_no={} order by complaint_id desc limit 1""".format(request.POST.get('1'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed fetching from complaint from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='complaint' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
elif(request.POST.get('10')=='2') :
c=cnx.cursor()
query=""" call complaint_res({},{},'{}') """.format(request.POST.get('1'),request.POST.get('2'),request.POST.get('3'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed updating resolved date for complaint in database: {}".format(err))
cnx.commit()
c.close()
c=cnx.cursor()
query="""SELECT * from complaint where roll_no={} and complaint_id={} """.format(request.POST.get('1'),request.POST.get('2'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed fetching complaint from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='complaint' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
elif(request.POST.get('10')=='3'):
c=cnx.cursor()
if(request.POST.get('5')=='0' and request.POST.get('9')=='0' ):
query="""SELECT * from complaint order by roll_no asc,complaint_id asc"""
elif(request.POST.get('5')=='0' and request.POST.get('9')=='1'):
query="""SELECT * from complaint where isnull(resolved_date) order by roll_no asc,complaint_id asc"""
elif(request.POST.get('5')=='0' and request.POST.get('9')=='2'):
query="""SELECT * from complaint where not isnull(resolved_date) order by roll_no asc,complaint_id asc"""
elif(request.POST.get('5')=='1' and request.POST.get('9')=='0'):
query="""SELECT * from complaint where roll_no={} order by isnull(resolved_date) desc,complaint_id desc""".format(request.POST.get('7'))
elif(request.POST.get('5')=='1' and request.POST.get('9')=='1'):
query="""SELECT * from complaint where roll_no={} and isnull(resolved_date) order by complaint_id desc""".format(request.POST.get('7'))
elif(request.POST.get('5')=='1' and request.POST.get('9')=='2'):
query="""SELECT * from complaint where roll_no={} and not isnull(resolved_date) order by complaint_id desc""".format(request.POST.get('7'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed show_it from complaint from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='complaint' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
@get('/update_visitor')
def update_visitor_get():
if(request.get_cookie("user") is None):
redirect('/login')
if(request.get_cookie("user")!='0'):
return "Access denied."
return template('tpl/update_visitor',lolcat=request.get_cookie("user"))
@post('/update_visitor')
def update_visitor_post():
if(request.POST.get('10') == '1'):
c=cnx.cursor()
query=""" INSERT into visitor(name,roll_no,contact_no,purpose) values ('{}',{},{},'{}' ) """.format(request.POST.get('1'),request.POST.get('2'),request.POST.get('3'),request.POST.get('4'))
try:
c.execute(query)
except mysql.connector.Error as err:
if err.errno == 1452:
return ("Student doesn't exist")
return ("Failed adding to visitor in database: {}".format(err))
cnx.commit()
c.close()
c=cnx.cursor()
query="""SELECT * from visitor where roll_no={} order by entry_time desc limit 1""".format(request.POST.get('2'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed fetching from visitor from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='visitor' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
elif(request.POST.get('10')=='2') :
c=cnx.cursor()
query=""" call visitor_out({},{}) """.format(request.POST.get('1'),request.POST.get('2'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed updating exit time for visitor in database: {}".format(err))
cnx.commit()
c.close()
c=cnx.cursor()
query="""SELECT * from visitor where roll_no={} and visitor_id={} """.format(request.POST.get('1'),request.POST.get('2'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed fetching visitor from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='visitor' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
elif(request.POST.get('10')=='3'):
c=cnx.cursor()
if(request.POST.get('5')=='0' and request.POST.get('6')=='0' and request.POST.get('9')=='0' ):
query="""SELECT * from visitor order by roll_no asc,visitor_id asc"""
elif(request.POST.get('5')=='0' and request.POST.get('6')=='0' and request.POST.get('9')=='1'):
query="""SELECT * from visitor where isnull(exit_time) order by roll_no asc,visitor_id asc"""
elif(request.POST.get('5')=='0' and request.POST.get('6')=='1' and request.POST.get('9')=='0'):
query="""SELECT * from visitor where name like '%{}%' order by roll_no asc,isnull(exit_time) desc, entry_time desc""".format(request.POST.get('8'))
elif(request.POST.get('5')=='0' and request.POST.get('6')=='1' and request.POST.get('9')=='1'):
query="""SELECT * from visitor where name like '%{}%' and isnull(exit_time) order by roll_no asc,isnull(exit_time) desc, entry_time desc""".format(request.POST.get('8'))
elif(request.POST.get('5')=='1' and request.POST.get('6')=='0' and request.POST.get('9')=='0'):
query="""SELECT * from visitor where roll_no={} order by isnull(exit_time) desc,entry_time desc""".format(request.POST.get('7'))
elif(request.POST.get('5')=='1' and request.POST.get('6')=='0' and request.POST.get('9')=='1'):
query="""SELECT * from visitor where roll_no={} and isnull(exit_time) order by entry_time desc""".format(request.POST.get('7'))
elif(request.POST.get('5')=='1' and request.POST.get('6')=='1' and request.POST.get('9')=='0'):
query="""SELECT * from visitor where roll_no={} and name like '%{}%' order by isnull(exit_time) desc, entry_time desc""".format(request.POST.get('7'),request.POST.get('8'))
elif(request.POST.get('5')=='1' and request.POST.get('6')=='1' and request.POST.get('9')=='1'):
query="""SELECT * from visitor where roll_no={} and name like '%{}%' and isnull(exit_time) order by entry_time desc""".format(request.POST.get('7'),request.POST.get('8'))
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed fetching from visitor from database: {}".format(err))
result = c.fetchall()
c.execute("SELECT column_name from information_schema.columns where table_name='visitor' and table_schema='hms'")
column_names=c.fetchall()
c.close()
output = template('tpl/only_table', rows=result,columns=column_names)
return output
@get('/new_emp')
def new_emp_get():
if(request.get_cookie("user") is None):
redirect('/login')
if(request.get_cookie("user")!='0'):
return "Access denied."
return template('tpl/new_emp.tpl',lolcat=request.get_cookie("user"))
@post('/new_emp')
def new_emp_post():
slist=[]
for i in range(1,9):
slist.append(request.POST.get('{}'.format(i)))
c = cnx.cursor()
query="""INSERT INTO employee (`name`, `employee_id`,`contact_no`, `dob`, `gender`, `address`, `designation`, `hostel_id`)
VALUES (""" + "%s,"*7 +"%s);"
try:
c.execute(query,slist)
except mysql.connector.Error as err:
return ("Failed adding employee to database: {}".format(err))
cnx.commit()
c.close()
c=cnx.cursor()
query="""SELECT date_of_joining,salary from employee where employee_id={} """.format(slist[1])
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed querying to database: {}".format(err))
result=c.fetchone()
cnx.commit()
c.close()
return '<p>The new employee was inserted into the database, the joining date is {} and salary is {} </p>'.format(result[0],result[1])
@get('/new_student')
def new_get():
if(request.get_cookie("user") is None):
redirect('/login')
if(request.get_cookie("user")!='0'):
return "Access denied."
return template('tpl/new_student.tpl')
@post('/new_student')
def new_post():
slist=[]
for i in range(1,9):
slist.append(request.POST.get('{}'.format(i)))
# s1 = request.POST.get('1')
# s2 = request.POST.get('2')
# s3 = request.POST.get('3')
# s4 = request.POST.get('4')
# s5 = request.POST.get('5')
# s6 = request.POST.get('6')
# s7 = request.POST.get('7')
# s8 = request.POST.get('8')
# s9 = request.POST.get('9')
# s10 = request.POST.get('10')
# s11 = request.POST.get('11')
c = cnx.cursor()
query="""INSERT INTO `hms`.`student` (`name`, `roll_no`, `dob`, `gender`, `address`, `contact_no`, `year`, `branch`)
VALUES (""" + "%s,"*7 +"%s);"
try:
c.execute(query,slist)
except mysql.connector.Error as err:
if err.errno==1062:
return ("Student roll no. already exists")
return ("Failed adding student to database: {}".format(err))
cnx.commit()
c.close()
c=cnx.cursor()
query="""SELECT hostel_id,flat,room from student where roll_no={} """.format(slist[1])
try:
c.execute(query)
except mysql.connector.Error as err:
return ("Failed querying to database: {}".format(err))
result=c.fetchone()
cnx.commit()
c.close()
return '<p>The new student was inserted into the database, the alloted room is {} {} {}</p>'.format(result[0],result[1],result[2])
@get('/update_student')
def update_get():
if(request.get_cookie("user") is None):
redirect('/login')
if(request.get_cookie("user")!='0'):
return "Access denied."
return template('tpl/update_student.tpl')
@post('/update_student')
def update_post():
c=cnx.cursor()
query=""" UPDATE `student` SET name='{}',contact_no={},address='{}',branch='{}' WHERE `roll_no`={};""".format(request.POST.get('11'),request.POST.get('2'),request.POST.get('3'),request.POST.get('4'),request.POST.get('1'))
try:
c.execute(query)
except mysql.connector.Error as err: