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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
|
from .forms import (
UserRegistrationForm, UserLoginForm,
ProfileForm, CreateWorkshop,
ProposeWorkshopDateForm, ProfileCommentsForm
)
from .models import (
Profile, User,
has_profile, Workshop,
WorkshopType, RequestedWorkshop,
BookedWorkshop, ProposeWorkshopDate,
Testimonial, ProfileComments
)
from datetime import datetime, date
from django.contrib.auth import login, logout, authenticate
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, redirect
from django.utils import timezone
from .send_mails import send_email
from django.http import HttpResponse, HttpResponseRedirect
from textwrap import dedent
from django.conf import settings
from os import listdir, path, sep
from zipfile import ZipFile
from django.contrib import messages
import datetime as dt
import csv
import logging
try:
from StringIO import StringIO as string_io
except ImportError:
from io import BytesIO as string_io
__author__ = "Akshen Doke"
__credits__ = ["Mahesh Gudi", "Aditya P.", "Ankit Javalkar",
"Prathamesh Salunke", "Kiran Kishore",
"KhushalSingh Rajput", "Prabhu Ramachandran",
"Arun KP"]
def is_email_checked(user):
if hasattr(user, 'profile'):
return True if user.profile.is_email_verified else False
else:
return False
def is_superuser(user):
return True if user.is_superuser else False
def index(request):
'''Landing Page'''
user = request.user
form = UserLoginForm()
testimonials = Testimonial.objects.all()
if user.is_authenticated() and is_email_checked(user):
if user.groups.filter(name='instructor').count() > 0:
return redirect('/manage/')
return redirect('/book/')
elif request.method == "POST":
form = UserLoginForm(request.POST)
if form.is_valid():
user = form.cleaned_data
login(request, user)
if is_superuser(user):
return redirect("/admin")
if user.groups.filter(name='instructor').count() > 0:
return redirect('/manage/')
return redirect('/book/')
return render(request, "workshop_app/index.html", {"form": form,
"testimonials": testimonials })
def is_instructor(user):
'''Check if the user is having instructor rights'''
return True if user.groups.filter(name='instructor').count() > 0 else False
def user_login(request):
'''User Login'''
user = request.user
if is_superuser(user):
return redirect('/admin')
if user.is_authenticated():
if user.groups.filter(name='instructor').count() > 0:
return redirect('/manage/')
return redirect('/book/')
if request.method == "POST":
form = UserLoginForm(request.POST)
if form.is_valid():
user = form.cleaned_data
login(request, user)
if user.groups.filter(name='instructor').count() > 0:
return redirect('/manage/')
return redirect('/book/')
else:
return render(request, 'workshop_app/login.html', {"form": form})
else:
form = UserLoginForm()
return render(request, 'workshop_app/login.html', {"form": form})
def user_logout(request):
'''Logout'''
logout(request)
return render(request, 'workshop_app/logout.html')
def activate_user(request, key=None):
user = request.user
if is_superuser(user):
return redirect("/admin")
if key is None:
if user.is_authenticated() and user.profile.is_email_verified==0 and \
timezone.now() > user.profile.key_expiry_time:
status = "1"
Profile.objects.get(user_id=user.profile.user_id).delete()
User.objects.get(id=user.profile.user_id).delete()
return render(request, 'workshop_app/activation.html',
{'status':status})
elif user.is_authenticated() and user.profile.is_email_verified==0:
return render(request, 'workshop_app/activation.html')
elif user.is_authenticated() and user.profile.is_email_verified:
status = "2"
return render(request, 'workshop_app/activation.html',
{'status':status})
else:
return redirect('/register/')
try:
user = Profile.objects.get(activation_key=key)
except:
return redirect('/register/')
if key == user.activation_key:
user.is_email_verified = True
user.save()
status = "0"
else:
logout(request)
return redirect('/logout/')
return render(request, 'workshop_app/activation.html',
{"status": status})
def user_register(request):
'''User Registration form'''
if request.method == 'POST':
form = UserRegistrationForm(request.POST)
if form.is_valid():
data = form.cleaned_data
username, password, key = form.save()
new_user = authenticate(username=username, password=password)
login(request, new_user)
user_position = request.user.profile.position
send_email(
request, call_on='Registration',
user_position=user_position,
key=key
)
return render(request, 'workshop_app/activation.html')
else:
if request.user.is_authenticated():
return redirect('/view_profile/')
return render(
request, "workshop_app/registration/register.html",
{"form": form}
)
else:
if request.user.is_authenticated() and is_email_checked(request.user):
return redirect('/my_workshops/')
elif request.user.is_authenticated():
return render(request, 'workshop_app/activation.html')
form = UserRegistrationForm()
return render(request, "workshop_app/registration/register.html", {"form": form})
# This is shown to coordinator for booking workshops
def book(request):
user = request.user
if user.is_authenticated():
if is_email_checked(user):
if user.groups.filter(name='instructor').count() > 0:
return redirect('/manage/')
workshop_details = Workshop.objects.all()
workshop_occurence_list = []
today = datetime.now() + dt.timedelta(days=3)
upto = datetime.now() + dt.timedelta(weeks=52)
for workshops in workshop_details:
dates = workshops.recurrences.between(
today,
upto,
inc=True
)
for d in range(len(dates)):
workshop_occurence = [
dates[d].strftime("%d-%m-%Y"),
workshops.workshop_instructor,
workshops.workshop_title,
workshops.workshop_instructor_id,
workshops.workshop_title_id,
workshops.workshop_title.workshoptype_description
]
workshop_occurence_list.append(workshop_occurence)
del workshop_occurence
# Gives you the objects of BookedWorkshop
bookedworkshop = BookedWorkshop.objects.all()
if len(bookedworkshop) != 0:
for b in bookedworkshop:
'''
handles objects from bookedworkshop
-requested
-proposed
'''
try:
x = b.booked_workshop_requested.requested_workshop_date.strftime("%d-%m-%Y")
y = b.booked_workshop_requested.requested_workshop_title
except:
x = b.booked_workshop_proposed.proposed_workshop_date.strftime("%d-%m-%Y")
y = b.booked_workshop_proposed.proposed_workshop_title
for a in workshop_occurence_list:
if a[0] == x and a[2] == y:
workshop_occurence_list.remove(a)
del x, y
# Objects of RequestedWorkshop for that particular coordinator
rW_obj = RequestedWorkshop.objects.filter(
requested_workshop_coordinator=request.user
)
for r in rW_obj:
x = r.requested_workshop_date.strftime("%d-%m-%Y")
for a in workshop_occurence_list:
if a[0] == x:
workshop_occurence_list.remove(a)
del x
# Show upto 12 Workshops per page
paginator = Paginator(workshop_occurence_list, 12)
page = request.GET.get('page')
try:
workshop_occurences = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
workshop_occurences = paginator.page(1)
except EmptyPage:
# If page is out of range(e.g 999999), deliver last page.
workshop_occurences = paginator.page(paginator.num_pages)
return render(
request, "workshop_app/booking.html",
{"workshop_details": workshop_occurences}
)
else:
return redirect('/activate_user/')
else:
return redirect('/login/')
@login_required
def book_workshop(request):
'''
Function for Updating RequestedWorkshop Model
'''
if request.method == 'POST':
user_position = request.user.profile.position
client_data = request.body.decode("utf-8").split("&")
client_data = client_data[0].split("%2C")
workshop_date = client_data[0][2:]
if client_data[-1] == '0':
queue = RequestedWorkshop.objects.filter(
requested_workshop_instructor=client_data[1],
requested_workshop_date=datetime.strptime(
client_data[0][2:], "%d-%m-%Y"
),
requested_workshop_title=client_data[-2]
).count() + 1
return HttpResponse(str(queue))
workshops_list = Workshop.objects.filter(
workshop_instructor=client_data[1],
workshop_title_id=client_data[2]
)
today = datetime.now() + dt.timedelta(days=3)
upto = datetime.now() + dt.timedelta(weeks=52)
for workshop in workshops_list:
workshop_recurrence_list = workshop.recurrences.between(
today,
upto,
inc=True
)
rW_obj = RequestedWorkshop()
if RequestedWorkshop.objects.filter(
requested_workshop_instructor=workshop.workshop_instructor,
requested_workshop_date=datetime.strptime(
client_data[0][2:], "%d-%m-%Y",
),
requested_workshop_coordinator=request.user,
requested_workshop_title=client_data[-1]
).count() > 0:
return HttpResponse(dedent("""You already have a booking
for this workshop please check the
instructors response in My Workshops tab and
also check your email."""))
else:
for w in workshop_recurrence_list:
if workshop_date == (w.strftime("%d-%m-%Y")):
rW_obj.requested_workshop_instructor = workshop.workshop_instructor
rW_obj.requested_workshop_coordinator = request.user
rW_obj.requested_workshop_date = datetime.strptime(
workshop_date,"%d-%m-%Y"
)
rW_obj.requested_workshop_title = workshop.workshop_title
rW_obj.save()
queue = RequestedWorkshop.objects.filter(
requested_workshop_instructor=workshop.workshop_instructor,
requested_workshop_date=datetime.strptime(
workshop_date, "%d-%m-%Y",
),
requested_workshop_title=client_data[-1]
).count()
# Mail to instructor
send_email(request, call_on='Booking',
user_position='instructor',
workshop_date=workshop_date,
workshop_title=workshop.workshop_title.workshoptype_name,
user_name=str(request.user.get_full_name()),
other_email=workshop.workshop_instructor.email
)
phone_number = workshop.workshop_instructor.profile.phone_number
#Mail to coordinator
send_email(request, call_on='Booking',
workshop_date=workshop_date,
workshop_title=workshop.workshop_title.workshoptype_name,
user_name=workshop.workshop_instructor.profile.user.get_full_name(),
other_email=workshop.workshop_instructor.email,
phone_number=phone_number)
return HttpResponse(dedent("""\
Your request has been successful, Please check
your email for further information. Your request is number
{0} in the queue.""".format(str(queue))))
else:
logout(request)
return HttpResponse("Some Error Occurred.")
@login_required
def manage(request):
user = request.user
if user.is_authenticated() and is_email_checked(user):
#Move user to the group via admin
if user.groups.filter(name='instructor').count() > 0:
try:
#Can Handle Multiple Workshops
workshop_details = Workshop.objects.filter(
workshop_instructor=user.id
)
workshop_occurence_list = []
today = datetime.now() + dt.timedelta(days=3)
upto = datetime.now() + dt.timedelta(weeks=52)
for workshop in workshop_details:
workshop_occurence = workshop.recurrences.between(
today,
upto,
inc=True
)
for i in range(len(workshop_occurence)):
workshop_occurence_list.append({
"user": str(user),
"workshop": workshop.workshop_title,
"date": workshop_occurence[i].date()
})
requested_workshop = RequestedWorkshop.objects.filter(
requested_workshop_instructor=user.id
)
#Need to recheck logic
for j in range(len(requested_workshop)):
for i in workshop_occurence_list:
a = requested_workshop[j].requested_workshop_date
b = requested_workshop[j].requested_workshop_title
if i['date'] == a and i['workshop'] == b:
workshop_occurence_list.remove(i)
del a, b
#Show upto 12 Workshops per page
paginator = Paginator(workshop_occurence_list, 12)
page = request.GET.get('page')
try:
workshops = paginator.page(page)
except PageNotAnInteger:
#If page is not an integer, deliver first page.
workshops = paginator.page(1)
except EmptyPage:
#If page is out of range(e.g 999999), deliver last page.
workshops = paginator.page(paginator.num_pages)
except:
workshops = None
return render(
request, "workshop_app/manage.html",
{"workshop_occurence_list": workshops}
)
return redirect('/book/')
else:
return redirect('/activate_user/')
@login_required
def my_workshops(request):
user = request.user
if user.is_authenticated() and is_email_checked(user):
if is_instructor(user):
if request.method == 'POST':
user_position = request.user.profile.position
client_data = request.body.decode("utf-8").split("&")
client_data = client_data[0].split("%2C")
if client_data[-1] == 'ACCEPTED':
workshop_date = datetime.strptime(
client_data[1], "%Y-%m-%d"
)
coordinator_obj = User.objects.get(username=client_data[0][2:])
workshop_status = RequestedWorkshop.objects.get(
requested_workshop_instructor=user.id,
requested_workshop_date=workshop_date,
requested_workshop_coordinator=coordinator_obj.id,
requested_workshop_title=client_data[2]
)
workshop_status.status = client_data[-1]
workshop_status.save()
booked_workshop_obj = BookedWorkshop()
booked_workshop_obj.booked_workshop_requested = workshop_status
booked_workshop_obj.save()
ws = workshop_status
cmail = ws.requested_workshop_coordinator.email
cname = ws.requested_workshop_coordinator.profile.user.get_full_name()
cnum = ws.requested_workshop_coordinator.profile.phone_number
cinstitute = ws.requested_workshop_coordinator.profile.institute
inum = request.user.profile.phone_number
wtitle = ws.requested_workshop_title.workshoptype_name
#For Instructor
send_email(request, call_on='Booking Confirmed',
user_position='instructor',
workshop_date=str(client_data[1]),
workshop_title=wtitle,
user_name=str(cname),
other_email=cmail,
phone_number=cnum,
institute=cinstitute
)
#For Coordinator
send_email(request, call_on='Booking Confirmed',
workshop_date=str(client_data[1]),
workshop_title=wtitle,
other_email=cmail,
phone_number=inum
)
elif client_data[-1] == 'DELETED':
workshop_date = client_data[1]
workshops_list = Workshop.objects.filter(workshop_instructor=request.user.id,
workshop_title_id=client_data[2]
)
today = datetime.now() + dt.timedelta(days=3)
upto = datetime.now() + dt.timedelta(weeks=52)
for workshop in workshops_list:
workshop_recurrence_list = workshop.recurrences.between(
today,
upto,
inc=True
)
for d in workshop_recurrence_list:
if workshop_date == d.strftime("%Y-%m-%d"):
rW_obj = RequestedWorkshop()
rW_obj.requested_workshop_instructor = request.user
rW_obj.requested_workshop_coordinator = request.user
rW_obj.requested_workshop_date = workshop_date
rW_obj.requested_workshop_title = workshop.workshop_title
rW_obj.status = client_data[-1]
rW_obj.save()
bW_obj = BookedWorkshop()
bW_obj.booked_workshop_requested = rW_obj
bW_obj.save()
#For instructor
send_email(request, call_on='Workshop Deleted',
workshop_date=str(client_data[1]),
workshop_title=workshop.workshop_title
)
return HttpResponse("Workshop Deleted")
elif client_data[-1] == 'APPROVED':
workshop_date = datetime.strptime(
client_data[1], "%Y-%m-%d"
)
coordinator_obj = User.objects.get(username=client_data[0][2:])
workshop_status = ProposeWorkshopDate.objects.get(
proposed_workshop_date=workshop_date,
proposed_workshop_coordinator=coordinator_obj.id,
proposed_workshop_title=client_data[2]
)
workshop_status.status = 'ACCEPTED'
workshop_status.proposed_workshop_instructor = user
workshop_status.save()
booked_workshop_obj = BookedWorkshop()
booked_workshop_obj.booked_workshop_proposed = workshop_status
booked_workshop_obj.save()
ws = workshop_status
cmail = ws.proposed_workshop_coordinator.email
cname = ws.proposed_workshop_coordinator.profile.user.get_full_name()
cnum = ws.proposed_workshop_coordinator.profile.phone_number
cinstitute = ws.proposed_workshop_coordinator.profile.institute
inum = request.user.profile.phone_number
wtitle = ws.proposed_workshop_title.workshoptype_name
#For Instructor
send_email(request, call_on='Booking Confirmed',
user_position='instructor',
workshop_date=str(client_data[1]),
workshop_title=wtitle,
user_name=str(cname),
other_email=cmail,
phone_number=cnum,
institute=cinstitute
)
#For Coordinator
send_email(request, call_on='Booking Confirmed',
workshop_date=str(client_data[1]),
workshop_title=wtitle,
other_email=cmail,
phone_number=inum
)
elif client_data[-1] == 'CHANGE_DATE':
temp, iid = client_data[0].split("=")
temp, new_workshop_date = client_data[-2].split("%3D")
cid, workshop_title_id = client_data[1], client_data[2]
new_workshop_date = datetime.strptime(
new_workshop_date, "%Y-%m-%d"
)
workshop_date = datetime.strptime(
client_data[3], "%Y-%m-%d"
)
cemail = User.objects.get(id=cid)
today = datetime.today()
if today > new_workshop_date:
return HttpResponse("Please Give proper Date!")
else:
result = RequestedWorkshop.objects.filter(
requested_workshop_instructor=user.id,
requested_workshop_coordinator=cid,
requested_workshop_title_id=workshop_title_id,
requested_workshop_date=workshop_date).update(
requested_workshop_date=new_workshop_date)
if result:
del temp
else:
ProposeWorkshopDate.objects.filter(
proposed_workshop_instructor=user.id,
proposed_workshop_coordinator=cid,
proposed_workshop_title_id=workshop_title_id,
proposed_workshop_date=workshop_date).update(
proposed_workshop_date=new_workshop_date)
#For Instructor
send_email(request, call_on='Change Date',
user_position='instructor',
workshop_date=workshop_date.date(),
new_workshop_date=str(new_workshop_date.date())
)
#For Coordinator
send_email(request, call_on='Change Date',
new_workshop_date=str(new_workshop_date.date()),
workshop_date=str(workshop_date.date()),
other_email=cemail.email
)
return HttpResponse("Date Changed")
else:
workshop_date = datetime.strptime(
client_data[1], "%Y-%m-%d"
)
coordinator_obj = User.objects.get(username=client_data[0][2:])
workshop_status = RequestedWorkshop.objects.get(
requested_workshop_instructor=user.id,
requested_workshop_date=workshop_date,
requested_workshop_coordinator=coordinator_obj.id,
requested_workshop_title=client_data[2]
)
workshop_status.status = client_data[-1]
workshop_status.save()
ws = workshop_status
wtitle = ws.requested_workshop_title.workshoptype_name
cmail = ws.requested_workshop_coordinator.email
cname = ws.requested_workshop_coordinator.profile.user.get_full_name()
cnum = ws.requested_workshop_coordinator.profile.phone_number
cinstitute = ws.requested_workshop_coordinator.profile.institute
#For Instructor
send_email(request, call_on='Booking Request Rejected',
user_position='instructor',
workshop_date=str(client_data[1]),
workshop_title=wtitle,
user_name=str(cname),
other_email=cmail,
phone_number=cnum,
institute=cinstitute
)
#For Coordinator
send_email(request, call_on='Booking Request Rejected',
workshop_date=str(client_data[1]),
workshop_title=wtitle,
other_email=cmail
)
workshops = []
today = datetime.today().date()
workshop_occurence_list = RequestedWorkshop.objects.filter(
requested_workshop_instructor=user.id,
requested_workshop_date__gt=today,
).order_by('-requested_workshop_date')
for w in workshop_occurence_list:
workshops.append(w)
proposed_workshop = ProposeWorkshopDate.objects.filter(
proposed_workshop_instructor=user.id,
proposed_workshop_date__gt=today,
).order_by('-proposed_workshop_date')
for p in proposed_workshop:
workshops.append(p)
proposed_workshop_pending = ProposeWorkshopDate.objects.filter(
status='Pending'
).order_by('-proposed_workshop_date')
for p in proposed_workshop_pending:
workshops.append(p)
#Show upto 12 Workshops per page
paginator = Paginator(workshops, 12)
page = request.GET.get('page')
try:
workshop_occurences = paginator.page(page)
except PageNotAnInteger:
#If page is not an integer, deliver first page.
workshop_occurences = paginator.page(1)
except EmptyPage:
#If page is out of range(e.g 999999), deliver last page.
workshop_occurences = paginator.page(paginator.num_pages)
return render(request, 'workshop_app/my_workshops.html',
{ "workshop_occurences" :workshop_occurences,
"today": today})
else:
workshops = []
workshop_occurence_list = RequestedWorkshop.objects.filter(
requested_workshop_coordinator=user.id
).order_by('-requested_workshop_date')
for w in workshop_occurence_list:
workshops.append(w)
proposed_workshop = ProposeWorkshopDate.objects.filter(
proposed_workshop_coordinator=user.id
).order_by('-proposed_workshop_date')
for p in proposed_workshop:
workshops.append(p)
#Show upto 12 Workshops per page
paginator = Paginator(workshops, 12)
page = request.GET.get('page')
try:
workshop_occurences = paginator.page(page)
except PageNotAnInteger:
#If page is not an integer, deliver first page.
workshop_occurences = paginator.page(1)
except EmptyPage:
#If page is out of range(e.g 999999), deliver last page.
workshop_occurences = paginator.page(paginator.num_pages)
return render(request, 'workshop_app/my_workshops.html',
{"workshop_occurences": workshop_occurences})
else:
return redirect('/login/')
@login_required
def propose_workshop(request):
'''Coordinator proposed a workshop and date'''
user = request.user
if is_superuser(user):
return redirect("/admin")
if is_email_checked(user):
if is_instructor(user):
return redirect('/manage/')
else:
if request.method == 'POST':
form = ProposeWorkshopDateForm(request.POST)
if form.is_valid():
form_data = form.save(commit=False)
form_data.proposed_workshop_coordinator = user
#Avoiding Duplicate workshop entries for same date and workshop_title
if ProposeWorkshopDate.objects.filter(
proposed_workshop_date=form_data.proposed_workshop_date,
proposed_workshop_title=form_data.proposed_workshop_title,
proposed_workshop_coordinator=form_data.proposed_workshop_coordinator
).exists():
return redirect('/my_workshops/')
else:
form_data.proposed_workshop_coordinator.save()
form_data.save()
instructors = Profile.objects.filter(position='instructor')
for i in instructors:
send_email(request, call_on='Proposed Workshop',
user_position='instructor',
workshop_date=str(form_data.proposed_workshop_date),
workshop_title=form_data.proposed_workshop_title,
user_name=str(user.get_full_name()),
other_email=i.user.email,
phone_number=user.profile.phone_number,
institute=user.profile.institute
)
return redirect('/my_workshops/')
else:
form = ProposeWorkshopDateForm()
return render(
request, 'workshop_app/propose_workshop.html',
{"form": form }
)
else:
return render(request, 'workshop_app/activation.html')
@login_required
def view_profile(request):
""" view instructor and coordinator profile """
user = request.user
if is_superuser(user):
return redirect('/admin')
if is_email_checked(user) and user.is_authenticated():
return render(request, "workshop_app/view_profile.html")
else:
if user.is_authenticated():
return render(request, 'workshop_app/activation.html')
else:
try:
logout(request)
return redirect('/login/')
except:
return redirect('/register/')
@login_required
def edit_profile(request):
""" edit profile details facility for instructor and coordinator """
user = request.user
if is_superuser(user):
return redirect('/admin')
if is_email_checked(user):
if is_instructor(user):
template = 'workshop_app/manage.html'
else:
template = 'workshop_app/booking.html'
else:
try:
logout(request)
return redirect('/login/')
except:
return redirect('/register/')
context = {'template': template}
if has_profile(user) and is_email_checked(user):
profile = Profile.objects.get(user_id=user.id)
else:
profile = None
if request.method == 'POST':
form = ProfileForm(request.POST, user=user, instance=profile)
if form.is_valid():
form_data = form.save(commit=False)
form_data.user = user
form_data.user.first_name = request.POST['first_name']
form_data.user.last_name = request.POST['last_name']
form_data.user.save()
form_data.save()
return render(
request, 'workshop_app/profile_updated.html'
)
else:
context['form'] = form
return render(request, 'workshop_app/edit_profile.html', context)
else:
form = ProfileForm(user=user, instance=profile)
return render(request, 'workshop_app/edit_profile.html', {'form': form})
@login_required
def create_workshop(request):
'''Instructor creates workshops'''
user = request.user
if is_superuser(user):
return redirect("/admin")
if is_instructor(user) and is_email_checked(user):
if request.method == 'POST':
form = CreateWorkshop(request.POST)
if form.is_valid():
form_data = form.save(commit=False)
#form_data.profile_id = profile.id
form_data.workshop_instructor = user
form_data.workshop_instructor.save()
form_data.save()
return redirect('/manage/')
else:
form = CreateWorkshop()
return render(
request, 'workshop_app/create_workshop.html',
{"form": form }
)
else:
return redirect('/book/')
def view_workshoptype_details(request, workshoptype_id):
'''Gives the types of workshop details '''
user = request.user
if is_superuser(user):
return redirect("/admin")
view_workshoptype_details = WorkshopType.objects.get(id=workshoptype_id)
return render(
request, 'workshop_app/view_workshoptype_details.html', \
{'workshoptype': view_workshoptype_details}
)
def view_workshoptype_list(request):
'''Gives the details for types of workshops.'''
user = request.user
if is_superuser(user):
return redirect("/admin")
workshoptype_list = WorkshopType.objects.all()
paginator = Paginator(workshoptype_list, 12) #Show upto 12 workshops per page
page = request.GET.get('page')
try:
workshoptype = paginator.page(page)
except PageNotAnInteger:
#If page is not an integer, deliver first page.
workshoptype = paginator.page(1)
except EmptyPage:
#If page is out of range(e.g 999999), deliver last page.
workshoptype = paginator.page(paginator.num_pages)
return render(
request, 'workshop_app/view_workshoptype_list.html', \
{'workshoptype': workshoptype}
)
def faq(request):
return render(request, 'workshop_app/view_faq.html')
def how_to_participate(request):
return render(request, 'workshop_app/how_to_participate.html')
def file_view(request, workshop_title):
if workshop_title =='flowchart':
return render(request, 'workshop_app/how_to_participate.html')
else:
filename = WorkshopType.objects.get(id=workshop_title)
attachment_path = path.dirname(filename.workshoptype_attachments.path)
zipfile_name = string_io()
zipfile = ZipFile(zipfile_name, "w")
attachments = listdir(attachment_path)
for file in attachments:
file_path = sep.join((attachment_path, file))
zipfile.write(file_path, path.basename(file_path))
zipfile.close()
zipfile_name.seek(0)
response = HttpResponse(content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename={0}.zip'.format(
filename.workshoptype_name.replace(" ", "_")
)
response.write(zipfile_name.read())
return response
def check_workshop_type(x):
try:
y = datetime.strftime(x.proposed_workshop_date, '%d-%m-%Y')
except:
y = datetime.strftime(x.requested_workshop_date, '%d-%m-%Y')
return y
@login_required
def workshop_stats(request):
user = request.user
today = datetime.now()
upto = today + dt.timedelta(days=120)
#For Monthly Chart
workshop_count = [0] * 12
for x in range(12):
workshop_count[x] +=RequestedWorkshop.objects.filter(
requested_workshop_date__year=str(today.year),
requested_workshop_date__month=str(x+1),
status='ACCEPTED').count()
workshop_count[x] +=ProposeWorkshopDate.objects.filter(
proposed_workshop_date__year=str(today.year),
proposed_workshop_date__month=str(x+1),
status='ACCEPTED').count()
# Count Total Number of workshops for each type
workshop_titles = WorkshopType.objects.all()
workshoptype_dict = {}
for title in workshop_titles:
workshoptype_dict[title]=0
for title in workshoptype_dict.keys():
workshoptype_dict[title] += RequestedWorkshop.objects.filter(
requested_workshop_title=title,
status='ACCEPTED').count()
workshoptype_dict[title] += ProposeWorkshopDate.objects.filter(
proposed_workshop_title=title,
status='ACCEPTED').count()
#For Pie Chart
workshoptype_num = []
workshoptype_title = []
for title in workshoptype_dict.keys():
workshoptype_title.append(str(title))
for count in workshoptype_dict.values():
workshoptype_num.append(count)
workshoptype_count = [workshoptype_title, workshoptype_num]
del workshoptype_title, workshoptype_num
# For India Map
states = [
['Code', 'State', 'Number'],
["IN-AP", "Andhra Pradesh", 0],
["IN-AR", "Arunachal Pradesh", 0],
["IN-AS", "Assam", 0],
["IN-BR", "Bihar", 0],
["IN-CT", "Chhattisgarh", 0],
["IN-GA", "Goa", 0],
["IN-GJ", "Gujarat", 0],
["IN-HR", "Haryana", 0],
["IN-HP", "Himachal Pradesh", 0],
["IN-JK", "Jammu and Kashmir", 0],
["IN-JH", "Jharkhand", 0],
["IN-KA", "Karnataka", 0],
["IN-KL", "Kerala", 0],
["IN-MP", "Madhya Pradesh", 0],
["IN-MH", "Maharashtra", 0],
["IN-MN", "Manipur", 0],
["IN-ML", "Meghalaya", 0],
["IN-MZ", "Mizoram", 0],
["IN-NL", "Nagaland", 0],
["IN-OR", "Odisha", 0],
["IN-PB", "Punjab", 0],
["IN-RJ", "Rajasthan", 0],
["IN-SK", "Sikkim", 0],
["IN-TN", "Tamil Nadu", 0],
["IN-TG", "Telangana", 0],
["IN-TR", "Tripura", 0],
["IN-UT", "Uttarakhand", 0],
["IN-UP", "Uttar Pradesh", 0],
["IN-WB", "West Bengal", 0],
["IN-AN", "Andaman and Nicobar Islands", 0],
["IN-CH", "Chandigarh", 0],
["IN-DN", "Dadra and Nagar Haveli", 0],
["IN-DD", "Daman and Diu", 0],
["IN-DL", "Delhi", 0],
["IN-LD", "Lakshadweep", 0],
["IN-PY", "Puducherry", 0]
]
workshop_state = []
requestedWorkshops = RequestedWorkshop.objects.filter(status='ACCEPTED')
proposedWorkshops = ProposeWorkshopDate.objects.filter(status='ACCEPTED')
for workshop in requestedWorkshops:
for s in states:
if s[0] == workshop.requested_workshop_coordinator.profile.state:
s[2] +=1
for workshop in proposedWorkshops:
for s in states:
if s[0] == workshop.proposed_workshop_coordinator.profile.state:
s[2] +=1
#For Data Downloading and Viewing
if request.method == 'POST':
try:
from_dates = request.POST.get('from')
to_dates = request.POST.get('to')
#Fetches Accepted workshops which were proposed by Coordinators
proposed_workshops = ProposeWorkshopDate.objects.filter(
proposed_workshop_date__range=(from_dates, to_dates),
status='ACCEPTED'
)
# Fetches Accepted workshops which were Accepted by
# Instructors based on their Availability
requested_workshops = RequestedWorkshop.objects.filter(
requested_workshop_date__range=(from_dates, to_dates),
status='ACCEPTED'
)
upcoming_workshops = []
for workshop in proposed_workshops:
upcoming_workshops.append(workshop)
for workshop in requested_workshops:
upcoming_workshops.append(workshop)
upcoming_workshops = sorted(upcoming_workshops,
key=lambda x: check_workshop_type(x))
download = request.POST.get('Download')
if download:
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment;\
filename="records_from_{0}_to_{1}.csv"'.format(
from_dates,to_dates
)
writer = csv.writer(response)
header = [
'coordinator name',
'instructor name',
'workshop',
'date',
'status',
'institute name'
]
writer.writerow(header)
for workshop in upcoming_workshops:
try:
row = [
workshop.proposed_workshop_coordinator,
workshop.proposed_workshop_instructor,
workshop.proposed_workshop_title,
workshop.proposed_workshop_date,
workshop.status,
workshop.proposed_workshop_coordinator.profile.institute
]
except:
row = [
workshop.requested_workshop_coordinator,
workshop.requested_workshop_instructor,
workshop.requested_workshop_title,
workshop.requested_workshop_date,
workshop.status,
workshop.requested_workshop_coordinator.profile.institute
]
writer.writerow(row)
return response
else:
return render(request, 'workshop_app/workshop_stats.html',
{
"upcoming_workshops": upcoming_workshops,
"show_workshop_stats": settings.SHOW_WORKSHOP_STATS,
"workshop_count": workshop_count,
"workshoptype_count": workshoptype_count,
"india_map": states
})
except:
messages.info(request, 'Please enter Valid Dates')
if is_instructor(user) and is_email_checked(user):
try:
#Fetches Accepted workshops which were proposed by Coordinators
proposed_workshops = ProposeWorkshopDate.objects.filter(
proposed_workshop_date__range=(today, upto),
status='ACCEPTED'
)
#Fetches Accepted workshops which were Accepted by
# Instructors based on their Availability
requested_workshops = RequestedWorkshop.objects.filter(
requested_workshop_date__range=(today, upto),
status='ACCEPTED'
)
upcoming_workshops = []
for workshop in proposed_workshops:
upcoming_workshops.append(workshop)
for workshop in requested_workshops:
upcoming_workshops.append(workshop)
upcoming_workshops = sorted(upcoming_workshops,
key=lambda x: check_workshop_type(x))
except:
upcoming_workshops = None
paginator = Paginator(upcoming_workshops, 12)
page = request.GET.get('page')
try:
upcoming_workshops = paginator.page(page)
except PageNotAnInteger:
#If page is not an integer, deliver first page.
upcoming_workshops = paginator.page(1)
except EmptyPage:
#If page is out of range(e.g 999999), deliver last page.
upcoming_workshops = paginator.page(paginator.num_pages)
return render(request, 'workshop_app/workshop_stats.html',
{
"upcoming_workshops": upcoming_workshops,
"show_workshop_stats": settings.SHOW_WORKSHOP_STATS,
"workshop_count": workshop_count,
"workshoptype_count": workshoptype_count,
"india_map": states
})
else:
return redirect('/manage/')
def self_workshop(request):
return render(request, "workshop_app/self_workshop.html")
@login_required
def view_comment_profile(request, user_id):
'''instructor can view/post comments on coordinator profile '''
user = request.user
if is_instructor(user) and is_email_checked(user):
comment_form = ProfileCommentsForm()
coordinator_profile = Profile.objects.get(user_id=user_id)
try:
comments = ProfileComments.objects.filter(coordinator_profile_id=user_id).order_by('-created_date')
except:
comments = None
if request.method == 'POST':
comment_formpost = ProfileCommentsForm(request.POST)
if comment_formpost.is_valid():
form_data = comment_formpost.save(commit=False)
form_data.coordinator_profile_id = user_id
form_data.instructor_profile_id = user.id
form_data.save()
return render(request, "workshop_app/view_comment_profile.html",
{"coordinator_profile": coordinator_profile,
"comments": comments,
"comment_form": comment_form
})
else:
if comments is not None:
#Show upto 12 Workshops per page
paginator = Paginator(comments, 12)
page = request.GET.get('page')
try:
comments = paginator.page(page)
except PageNotAnInteger:
#If page is not an integer, deliver first page.
comments = paginator.page(1)
except EmptyPage:
#If page is out of range(e.g 999999), deliver last page.
comments = paginator.page(paginator.num_pages)
return render(request, "workshop_app/view_comment_profile.html",
{"coordinator_profile": coordinator_profile,
"comments": comments,
"comment_form": comment_form})
return redirect('/book/')
|