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
|
from .forms import (
UserRegistrationForm, UserLoginForm,
ProfileForm, CreateWorkshop
)
from .models import Profile, User, has_profile, Workshop, Course
from django.template import RequestContext
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.core.mail import send_mail
from django.shortcuts import render, redirect
from django.db import IntegrityError
from collections import OrderedDict
from dateutil.parser import parse
from workshop_portal.settings import (
EMAIL_HOST,
EMAIL_PORT,
EMAIL_HOST_USER,
EMAIL_HOST_PASSWORD,
EMAIL_USE_TLS
)
def index(request):
'''Landing Page'''
user = request.user
if user.is_authenticated():
if user.groups.filter(name='instructor').count() > 0:
return redirect('/manage/')
return redirect('/book/')
return render(request, "workshop_app/index.html")
def is_instructor(user):
'''Check if the user is having instructor rights'''
if user.groups.filter(name='instructor').exists():
return True
def user_login(request):
'''Login'''
user = request.user
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 user_register(request):
'''User Registeration form'''
if request.method == 'POST':
form = UserRegistrationForm(request.POST)
if form.is_valid():
try:
data = form.cleaned_data
username, password = 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
)
return redirect('/view_profile/')
except IntegrityError as e:
return render(
request,
"workshop_app/registeration_error.html"
)
else:
return render(
request, "workshop_app/register.html",
{"form": form}
)
else:
form = UserRegistrationForm()
return render(request, "workshop_app/register.html", {"form": form})
def book(request):
user = request.user
if user.is_authenticated():
if user.groups.filter(name='instructor').count() > 0:
return redirect('/manage/')
workshop_details = Workshop.objects.all()
workshop_occurence = {}
for workshops in workshop_details:
dates = workshops.recurrences.between(
datetime(2017, 3, 12, 0, 0, 0),
datetime(2017, 12, 31, 0, 0, 0), #Needs to be changed
inc=True
)
for d in range(len(dates)):
workshop_occurence[dates[d].strftime("%d-%m-%Y")] = [
workshops.workshop_instructor,
workshops.workshop_title
]
# workshop_occurence = OrderedDict(sorted(workshop_occurence.items()))
workshop_occurence = list(workshop_occurence.items())
#Show upto 3 Workshops per page
paginator = Paginator(workshop_occurence, 6)
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('/login/')
@login_required
def manage(request):
user = request.user
if user.is_authenticated():
#Move user to the group via admin
if user.groups.filter(name='instructor').count() > 0:
try:
workshop_details = Workshop.objects.get(workshop_instructor=user)
workshop_occurence_list = workshop_details.recurrences.between(
datetime(2017, 3, 12, 0, 0, 0),
datetime(2017, 12, 31, 0, 0, 0),
inc=True
)
for i in range(len(workshop_occurence_list)):
workshop_occurence_list[i] = [{
"user": str(user),
"workshop": workshop_details,
"date": workshop_occurence_list[i].date
}]
#Show upto 3 Workshops per page
paginator = Paginator(workshop_occurence_list, 3)
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('/login/')
@login_required
def view_profile(request):
""" view instructor and coordinator profile """
return render(request, "workshop_app/view_profile.html")
@login_required
def edit_profile(request):
""" edit profile details facility for instructor and coordinator """
user = request.user
if is_instructor(user):
template = 'workshop_app/manage.html'
else:
template = 'workshop_app/booking.html'
context = {'template': template}
if has_profile(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',
context
)
else:
context['form'] = form
return render(request, 'workshop_app/edit_profile.html', context)
else:
form = ProfileForm(user=user, instance=profile)
context['form'] = form
return render(request, 'workshop_app/edit_profile.html', context)
@login_required
def create_workshop(request):
'''Instructor creates workshops'''
user = request.user
if is_instructor(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/')
@login_required
def view_course_list(request):
'''Gives the course details '''
user = request.user
if is_instructor(user):
course_list = Course.objects.all()
paginator = Paginator(course_list, 9) #Show upto 12 Courses per page
page = request.GET.get('page')
try:
courses = paginator.page(page)
except PageNotAnInteger:
#If page is not an integer, deliver first page.
courses = paginator.page(1)
except EmptyPage:
#If page is out of range(e.g 999999), deliver last page.
courses = paginator.page(paginator.num_pages)
return render(
request, 'workshop_app/view_course_list.html', \
{'courses': courses}
)
else:
return redirect('/book/')
@login_required
def view_course_details(request):
'''Gives the course details '''
user = request.user
if is_instructor(user):
return redirect('/')
else:
return redirect('/book/')
def send_email(request, call_on, user_position=None):
'''
Email sending function while registration and
booking confirmation.
'''
if call_on == 'Registration':
if user_position == 'instructor':
message = 'Thank You for Registering on this platform. \n \
Since you have ask for Instructor Profile, \n \
we will get back to you soon after verifying your \n \
profile. \
In case if you don\'t get any response within 3days, \
Please contact us at '
send_mail(
'Welcome to FOSSEE', message, EMAIL_HOST_USER,
[request.user.email], fail_silently=False
)
#Send a mail to admin as well as a notification.
else:
message = 'Thank You for Registering on this platform.\n \
Rules. \n \
If you face any issue during your session please contact \
fossee.'
send_mail(
'Welcome to FOSSEE', message, EMAIL_HOST_USER,
[request.user.email], fail_silently=False
)
elif call_on == 'Booking':
pass
|