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
|
from django.shortcuts import render, redirect
from forms import UserRegistrationForm, UserLoginForm, ProfileForm
from .models import Profile, User, has_profile
from django.template import RequestContext
from django.contrib.auth import login, logout, authenticate
from django.contrib.auth.decorators import login_required
from django.contrib import messages
def index(request):
'''Landing Page'''
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('/view_profile/')
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('/view_profile/')
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():
data = form.cleaned_data
username, password = form.save()
new_user = authenticate(username=username, password=password)
login(request, new_user)
return redirect('/home')
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/')
return render(request, "workshop_app/booking.html")
else:
return redirect('/login/')
def manage(request):
user = request.user
if user.is_authenticated():
if user.groups.filter(name='instructor').count() > 0:
return render(request, "workshop_app/manage.html")
return redirect('/book/')
else:
return redirect('/login/')
@login_required
def view_profile(request):
""" view moderators and users 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):
return render(request, 'workshop_app/create_workshop.html')
else:
return redirect('/book/')
|