views.py
7.59 KB
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
from django.shortcuts import get_object_or_404,redirect
from django.db.models import Q
from django.views import generic
from django.contrib import messages
from rolepermissions.mixins import HasRoleMixin
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from rolepermissions.shortcuts import assign_role
from rolepermissions.verifications import has_role
from itertools import chain
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .models import User
from .forms import UserForm, UpdateProfileForm
#API IMPORTS
from rest_framework import viewsets
from .serializers import UserSerializer
from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly
# ================ ADMIN =======================
class UsersListView(HasRoleMixin, LoginRequiredMixin, generic.ListView):
allowed_roles = ['system_admin']
login_url = reverse_lazy("core:home")
redirect_field_name = 'next'
template_name = 'list_users.html'
context_object_name = 'users'
paginate_by = 10
def get_queryset(self):
search = self.request.GET.get('search', None)
if search is None:
users = User.objects.all().order_by('name').exclude( username = self.request.user.username)
else:
users = User.objects.filter(Q(username = search) | Q(name = search) | Q(name__icontains = search) | Q(username__icontains = search)).exclude( username = self.request.user.username)
return users
def get_context_data (self, **kwargs):
context = super(UsersListView, self).get_context_data(**kwargs)
context['title'] = 'Manage Users'
return context
class Create(HasRoleMixin, LoginRequiredMixin, generic.edit.CreateView):
allowed_roles = ['system_admin']
login_url = reverse_lazy("core:home")
redirect_field_name = 'next'
template_name = 'users/create.html'
form_class = UserForm
context_object_name = 'acc'
success_url = reverse_lazy('users:manage')
def form_valid(self, form):
self.object = form.save()
if self.object.type_profile == 2:
assign_role(self.object, 'student')
elif self.object.type_profile == 1:
assign_role(self.object, 'professor')
elif self.object.is_staff:
assign_role(self.object, 'system_admin')
self.object.save()
messages.success(self.request, ('User ')+self.object.name+(' created successfully!'))
return super(Create, self).form_valid(form)
def get_context_data (self, **kwargs):
context = super(Create, self).get_context_data(**kwargs)
context['title'] = "Add User"
return context
class Update(HasRoleMixin, LoginRequiredMixin, generic.UpdateView):
allowed_roles = ['system_admin']
login_url = reverse_lazy("core:home")
redirect_field_name = 'next'
template_name = 'users/update.html'
slug_field = 'username'
slug_url_kwarg = 'username'
context_object_name = 'acc'
model = User
form_class = UserForm
success_url = reverse_lazy('users:manage')
def form_valid(self, form):
self.object = form.save(commit = False)
if self.object.type_profile == 2:
assign_role(self.object, 'student')
elif self.object.type_profile == 1:
assign_role(self.object, 'professor')
elif self.object.is_staff:
assign_role(self.object, 'system_admin')
self.object.save()
messages.success(self.request, _('User ')+self.object.name+_(' updated successfully!'))
return super(Update, self).form_valid(form)
def get_context_data (self, **kwargs):
context = super(Update, self).get_context_data(**kwargs)
context['title'] = "Update User"
return context
class View(LoginRequiredMixin, generic.DetailView):
login_url = reverse_lazy("core:home")
redirect_field_name = 'next'
model = User
context_object_name = 'acc'
template_name = 'users/view.html'
slug_field = 'username'
slug_url_kwarg = 'username'
def get_context_data (self, **kwargs):
context = super(View, self).get_context_data(**kwargs)
context['title'] = "User"
return context
def delete_user(request,username):
user = get_object_or_404(User,username = username)
user.delete()
messages.success(request,_("User deleted Successfully!"))
return redirect('users:manage')
def remove_account(request,username):
user = get_object_or_404(User,username = username)
user.delete()
messages.success(request,_("User deleted Successfully!"))
return redirect('core:logout')
class Change_password(generic.TemplateView):
template_name = 'users/change_password.html'
def get_context_data (self, **kwargs):
context = super(Change_password, self).get_context_data(**kwargs)
context['title'] = "Change Password"
return context
class Remove_account(generic.TemplateView):
template_name = 'users/remove_account.html'
def get_context_data (self, **kwargs):
context = super(Remove_account, self).get_context_data(**kwargs)
context['title'] = "Remove Account"
return context
class UpdateProfile(LoginRequiredMixin, generic.edit.UpdateView):
login_url = reverse_lazy("core:home")
template_name = 'users/edit_profile.html'
form_class = UpdateProfileForm
success_url = reverse_lazy('users:profile')
def get_object(self):
user = get_object_or_404(User, username = self.request.user.username)
return user
def get_context_data(self, **kwargs):
context = super(UpdateProfile, self).get_context_data(**kwargs)
context['title'] = 'Update Profile'
context['form'] = UpdateProfileForm(instance = self.object)
return context
def form_valid(self, form):
form.save()
messages.success(self.request, _('Profile edited successfully!'))
return super(UpdateProfile, self).form_valid(form)
class DeleteUser(LoginRequiredMixin, generic.edit.DeleteView):
allowed_roles = ['student']
login_url = reverse_lazy("core:home")
model = User
success_url = reverse_lazy('core:index')
success_message = "Deleted Successfully"
def get_queryset(self):
user = get_object_or_404(User, username = self.request.user.username)
return user
class Profile(LoginRequiredMixin, generic.DetailView):
login_url = reverse_lazy("core:home")
redirect_field_name = 'next'
context_object_name = 'user'
template_name = 'users/profile.html'
def get_object(self):
user = get_object_or_404(User, username = self.request.user.username)
return user
def get_context_data (self, **kwargs):
context = super(Profile, self).get_context_data(**kwargs)
context['title'] = "Profile"
return context
class SearchView(LoginRequiredMixin, generic.ListView):
login_url = reverse_lazy("core:home")
redirect_field_name = 'next'
queryset = None
template_name = 'users/search.html'
paginate_by = 10
def get_context_data(self, **kwargs):
context = super(SearchView, self).get_context_data(**kwargs)
search = self.request.GET.get('search', None)
return context
def login(self, request):
context = {}
context['title'] = 'Log In'
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
login_user(request, user)
return redirect(reverse("users:login"))
else:
messages.add_message(request, messages.ERROR, _('E-mail or password are incorrect.'))
context["username"] = username
elif request.user.is_authenticated:
return redirect(reverse('users:login'))
return render(request,"index.html",context)
# API VIEWS
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
permissions_classes = (IsAuthenticatedOrReadOnly,)