-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdashboard.py
707 lines (513 loc) · 15.5 KB
/
dashboard.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
"""
Created on Tue Nov 28 20:59:55 2017
@author: abhiram haridas (abhiramharidas@gmail.com)
"""
"""
This is the main file for the admin console application. The menu for the main program can be found at the end of this file.
Separate functions like edit_home, edit_about etc handles editing the respective pages of the website.
This file imports from dash_utils.py which is a set of utility functions. The main program expects admin-csea.json file to
be available in the working directory.
Conventions Followed
db_ref_string : The template string which when substituted gives the actual reference string to a specific database node
db_ref : The actual reference to the database object
blob_ref_string : The template string which when substituted gives the actual reference string to a specific storage location
blob : The actual reference to the storage object
All network operations have been enclosed in try blocks. If any of them fails due to any unknown reasons, a network
error message is displayed. Actual error stack trace can be obtained by slightly modifying network_error method in this file.
A small description of each function is available at the beginning of each function.
"""
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
from firebase_admin import storage
import sys
import traceback
import json
from datetime import datetime
import dash_utils
cred = credentials.Certificate('./admin-csea.json') #authenticating with the database
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://cseanitcweb.firebaseio.com',
'storageBucket': 'cseanitcweb.appspot.com'
})
bucket = storage.bucket() # Reference to the storage bucket
# This functions displays an error message and exits. This can be edited to view error stack trace. The program exits after the message.
def network_error():
print "\nUnable to read/write data Check your internet connection and try again. If connection is alright modify source to view stacktrace.\n"
# print "\n The traceback for the error occurred\n" #*********** Uncomment this part to view stack trace of the error ***********
# traceback.print_exc()
# print "\n\n"
sys.exit()
# Function for editing the home page including slider images, right pane, cneter pane and upcoming events
def edit_home():
while(1):
print("\nEnter 1 to edit slider images")
print("Enter 2 to edit right pane")
print("Enter 3 to edit center pane ")
print("Enter 4 to add upcoming event")
print("Enter 5 to exit")
choice = raw_input()
try:
choice = int(choice)
except ValueError:
print "\nError! Invalid input.\n"
continue
if(choice == 1):
db_ref_string = 'home/slider'
blob_ref_string = 'home/slider/%s'
file1 = raw_input("Enter path to image1 : ")
file2 = raw_input("Enter path to image2 : ")
file3 = raw_input("Enter path to image3 : ")
error_flag, error_msg = dash_utils.file_exist(file1)
if (error_flag == 1):
print "\nError! Path to image1 does not exist.\n"
continue
error_flag, error_msg = dash_utils.file_exist(file2)
if (error_flag == 1):
print "\nError! Path to image2 does not exist.\n"
continue
error_flag, error_msg = dash_utils.file_exist(file3)
if (error_flag == 1):
print "\nError! Path to image3 does not exist.\n"
continue
try:
blob = bucket.blob(blob_ref_string % "1")
blob.upload_from_filename(file1)
except:
network_error()
try:
blob = bucket.blob(blob_ref_string % "2")
blob.upload_from_filename(file2)
except:
network_error()
try:
blob = bucket.blob(blob_ref_string % "3")
blob.upload_from_filename(file3)
except:
network_error()
try:
db_ref = db.reference(db_ref_string)
slider_data = {
'1': blob_ref_string % "1",
'2': blob_ref_string % "2",
'3': blob_ref_string % "3"
}
except:
network_error()
elif(choice == 2):
db_ref_string = 'home'
html = raw_input("Enter right pane html : ")
try:
db_ref = db.reference(db_ref_string)
db_ref.update({
'right_pane' : html
})
except:
network_error()
elif(choice == 3):
db_ref_string = 'home'
html = raw_input("Enter center pane html : ")
try:
db_ref = db.reference(db_ref_string)
db_ref.update({
'center_pane' : html
})
except:
network_error()
elif(choice == 4):
db_ref_string = 'home/upcoming/%s/%s' # home/upcoming/year/title
title = raw_input("\nEnter activity title : ")
short_desc = raw_input("Enter very short description (max 10 words) : ")
date = raw_input("Enter date (dd/mm/yyyy) : ")
error_flag, error_msg = dash_utils.check_name(title)
if (error_flag == 1):
print error_msg
return
error_flag, error_msg = dash_utils.check_date(date)
if (error_flag == 1):
print error_msg
return
date = date.split('/')
data = {
'title': title,
'short_desc': short_desc,
'date' : {
'day': date[0],
'month' : date[1],
'year' : date[2]
}
}
try:
db_ref = db.reference (db_ref_string % (date[2],title))
db_ref.set(data)
except:
network_error
print "\nEvent added successfully\n"
elif(choice == 5):
return
else :
print("Invalid Input")
# Function for editing About page
def edit_about():
db_ref_string = 'about'
try:
db_ref = db.reference(db_ref_string)
except:
network_error()
while(1):
print("\nEnter 1 to edit About Us")
print("Enter 2 to edit Vision")
print("Enter 3 to edit How we Work")
print("Enter 4 to edit Join us")
print("Enter 5 to exit")
choice = raw_input()
try:
choice = int(choice)
except ValueError:
print "\nError! Invalid input.\n"
continue
if(choice == 1):
html = raw_input('Enter about html : ')
try:
db_ref.update({
'about' : html
})
except:
network_error()
elif(choice == 2):
html = raw_input('Enter vision html : ')
try:
db_ref.update({
'vision' : html
})
except:
network_error()
elif(choice == 3):
html = raw_input('Enter how we work html : ')
try:
db_ref.update({
'how' : html
})
except:
network_error()
elif(choice == 4):
html = raw_input('Enter join us html : ')
try:
db_ref.update({
'join_us' : html
})
except:
network_error()
elif(choice == 5):
return
else :
print("Invalid input")
# Function for adding members and toggling flag_for_inductions
def edit_members():
while(1):
print ("\nEnter 1 to add faculty")
print ("Enter 2 to add student")
print ("Enter 3 to toggle flag_for_inductions")
print ("Enter 4 to exit")
choice = raw_input()
try:
choice = int(choice)
except ValueError:
print "\nError! Invalid input.\n"
continue
if(choice == 1):
db_ref_string = 'members/faculty/%s'
blob_ref_string = 'members/faculty/%s'
name = raw_input("\nEnter name : ")
email = raw_input("Enter email : ")
file_dir = raw_input("Enter path to image : ")
error_flag, error_msg = dash_utils.check_name(name)
if (error_flag == 1):
print error_msg
continue
error_flag, error_msg = dash_utils.file_exist(file_dir)
if (error_flag == 1):
print error_msg
continue
try:
blob = bucket.blob(blob_ref_string % name)
blob.upload_from_filename(file_dir)
db_ref = db.reference(db_ref_string % name)
user_data = {
'nameof' : name,
'email' : email,
'img' : blob_ref_string % name
}
db_ref.set(user_data)
except:
network_error()
elif(choice == 2):
db_ref_string = 'members/students/%(category)s/%(year)s/%(name)s'
blob_ref_String = 'members/students/%(category)s/%(year)s/%(name)s'
name = raw_input("\nEnter student name : ")
year = raw_input("Enter student admission year : ")
email = raw_input("Enter student email : ")
linkedin = raw_input("Enter linkedin profile : ")
file_dir = raw_input("Enter path to image file : ")
ch = raw_input("Enter 1 for BTech, 2 for MTech and 3 for MCA : ")
try:
ch = int(ch)
except ValueError:
print "\nError! Invalid input.\n"
continue
error_flag, error_msg = dash_utils.check_name(name)
if (error_flag == 1):
print error_msg
continue
error_flag, error_msg = dash_utils.check_year(year)
if (error_flag == 1):
print error_msg
continue
if ch == 1:
category = 'btech'
elif ch == 2:
category = 'mtech'
else:
category = 'mca'
userdata = {
'nameof': name,
'email':email,
'linkedin':linkedin,
'img': db_ref_string % {'year': year, 'category': category, 'name': name}
}
error_flag, error_msg = dash_utils.file_exist(file_dir)
if (error_flag == 1):
print error_msg
continue
try:
blob = bucket.blob(blob_ref_String % {'year': year, 'category': category, 'name': name})
blob.upload_from_filename(file_dir)
db_ref = db.reference(db_ref_string % {'year': year, 'category': category, 'name': name})
db_ref.set(userdata)
except:
network_error()
elif(choice == 3):
db_ref_string = 'members/flag_for_inductions'
try:
db_ref = db.reference(db_ref_string)
val = db_ref.get()
if val == 0:
db_ref.set(1)
print('Value toggled: Current value is 1')
else:
db_ref.set(0)
print('Value toggled: Current value is 0')
except:
network_error()
elif(choice == 4):
return;
else:
print "Invalid input"
# Function to add an activity
def edit_activity():
db_ref_string = 'activities/%s/%s'
blob_ref_string = 'activities/%s/%s'
title = raw_input("\nEnter activity title : ")
short_desc = raw_input("Enter short decsription : ")
long_desc = raw_input("Enter long description : ")
date = raw_input("Enter date (dd/mm/yyyy) : ")
file_dir = raw_input("Enter path to image : ")
choice = raw_input("Enter 1 for Course/Workshop, 2 for talk or 3 for others : ")
try:
choice = int(choice)
except ValueError:
print "\nError! Invalid input.\n"
return
error_flag, error_msg = dash_utils.check_name(title)
if (error_flag == 1):
print error_msg
return
error_flag, error_msg = dash_utils.check_date(date)
if (error_flag == 1):
print error_msg
return
date = date.split('/')
category = 'others'
if(choice == 1):
category = 'workshop'
elif(choice == 2):
category = 'talk'
data = {
'title' : title,
'short_desc' : short_desc,
'long_desc' : long_desc,
'date' : {
'day': date[0],
'month' : date[1],
'year' : date[2]
},
'category' : category,
'img' : blob_ref_string % (date[2],title)
}
error_flag, error_msg = dash_utils.file_exist(file_dir)
if (error_flag == 1):
print error_msg
return
try:
blob = bucket.blob(blob_ref_string % (date[2],title) )
blob.upload_from_filename(file_dir)
db_ref = db.reference(db_ref_string % (date[2],title) )
db_ref.set(data)
except:
network_error()
# Function to add an event to gallery
def edit_gallery():
db_ref_string = 'gallery/%s/%s' # gallery/<year>/<event_name>
blob_ref_string = 'gallery/%s/%s/%s' # gallery/<year>/<event_name>/<img_no>
title = raw_input("\nEnter activity title : ")
short_desc = raw_input("Enter short decsription : ")
date = raw_input("Enter date (dd/mm/yyyy) : ")
img_count = raw_input("Enter image count : ")
drive_link = raw_input("Enter drive link : ")
error_flag, error_msg = dash_utils.check_name(title)
if (error_flag == 1):
print error_msg
return
error_flag, error_msg = dash_utils.check_date(date)
if (error_flag == 1):
print error_msg
return
date = date.split('/')
data = {
'title' : title,
'short_desc' : short_desc,
'date' : {
'day': date[0],
'month' : date[1],
'year' : date[2]
},
'img_count' : img_count,
'drive_link' : drive_link
}
img_count = int(img_count)
i = 1
img = {}
while(i <= img_count):
img_dir = raw_input("Enter path to image %s : " %str(i))
error_flag, error_msg = dash_utils.file_exist(img_dir)
if (error_flag == 1):
print error_msg
continue
try:
blob = bucket.blob(blob_ref_string % (date[2],title,str(i)) )
blob.upload_from_filename(img_dir)
except:
network_error()
img[i] = blob_ref_string % (date[2],title,str(i))
i = i + 1;
data['img'] = img
try:
db_ref = db.reference(db_ref_string % (date[2], title) )
db_ref.set(data)
except:
network_error()
# Function to edit the contents of the notice box
def edit_notice():
db_ref_string = 'notice_box'
html = raw_input("\nEnter notice box html : ")
try:
db_ref = db.reference(db_ref_string)
db_ref.set(html)
except:
network_error()
# Function to backup/restore all HTML fileds in the database. Backup/Restore is done using backup_html.txt file in the working directory.
def backup_restore_html():
print("\nEnter 1 to backup HTML fields")
print("Enter 2 restore HTML fileds")
print("Enter 3 to exit")
choice = raw_input()
try:
choice = int(choice)
except ValueError:
print "\nError! Invalid input.\n"
return
if choice == 1:
try:
notice_box_ref = db.reference('notice_box')
right_pane_ref = db.reference('home/right_pane')
center_pane_ref = db.reference('home/center_pane')
about_ref = db.reference('about')
notice_box = notice_box_ref.get()
right_pane = right_pane_ref.get()
center_pane = center_pane_ref.get()
about = about_ref.get()
date = datetime.now().strftime('%d-%m-%Y %H:%M:%S')
except:
network_error()
backup = {
'notice_box' : notice_box,
'right_pane' : right_pane,
'center_pane' : center_pane,
'about' : about,
'date' : date
};
try:
with open('./backup_html.txt','w') as backup_file:
json.dump(backup,backup_file)
except:
print ("\nError! Could not create backup file. Make sure working directory is read-only.\n\n")
return
print("\nBackup written to backup_html.txt on %s\n\n" % date)
elif choice == 2:
try:
with open('./backup_html.txt','r') as backup_file:
backup = json.load(backup_file)
except:
print ("\nError! Backup file appears to be missing or corrupted.\n\n")
return
try:
notice_box_ref = db.reference('notice_box')
right_pane_ref = db.reference('home/right_pane')
center_pane_ref = db.reference('home/center_pane')
about_ref = db.reference('about')
notice_box_ref.set(backup['notice_box'])
right_pane_ref.set(backup['right_pane'])
center_pane_ref.set(backup['center_pane'])
about_ref.set(backup['about'])
except:
network_error()
print("\nRestored backup from %s\n\n" % backup['date'])
else :
print("Invalid Input")
#============================================ MAIN FUNCTION STARTS HERE ===================================================
choice = 0
print ("Welcome to the Admin console!\n\n")
while(choice != 8):
print ("\nEnter 1 to edit Home Page")
print ("Enter 2 to edit About Page")
print ("Enter 3 to edit Members Page")
print ("Enter 4 to edit Activities Page")
print ("Enter 5 to edit Gallery")
print ("Enter 6 to edit Notice HTML")
print ("Enter 7 to backup/restore HTML fields")
print ("Enter 8 to exit")
choice = raw_input()
try:
choice = int(choice)
except ValueError:
print "\nError! Invalid input.\n"
choice = 0
continue
if(choice == 1):
edit_home()
elif(choice == 2):
edit_about()
elif(choice == 3):
edit_members()
elif(choice == 4):
edit_activity()
elif(choice == 5):
edit_gallery()
elif(choice == 6):
edit_notice()
elif(choice == 7):
backup_restore_html()
elif(choice == 8):
continue;
else:
print "\nError! Invalid input.\n"