|
1
|
|
|
import uuid |
|
2
|
|
|
|
|
3
|
|
|
from django.conf import settings |
|
4
|
|
|
from django.conf.urls import patterns, url |
|
5
|
|
|
from django.contrib.auth.tokens import PasswordResetTokenGenerator |
|
6
|
|
|
from django.core.urlresolvers import reverse_lazy |
|
7
|
|
|
from django.http import Http404 |
|
8
|
|
|
from django.shortcuts import redirect, render |
|
9
|
|
|
from django.template import Context, loader |
|
10
|
|
|
from django.views import generic |
|
11
|
|
|
|
|
12
|
|
|
from braces import views |
|
13
|
|
|
from django.http.response import HttpResponseRedirect |
|
14
|
|
|
from wye.base.emailer_html import send_email_to_id, send_email_to_list |
|
15
|
|
|
from wye.profiles.models import Profile, UserType |
|
16
|
|
|
from wye.regions.models import RegionalLead |
|
17
|
|
|
|
|
18
|
|
|
from .forms import ( |
|
19
|
|
|
OrganisationForm, OrganisationMemberAddForm, |
|
20
|
|
|
UserRegistrationForm |
|
21
|
|
|
) |
|
22
|
|
|
from .models import Organisation, User |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class OrganisationList(views.LoginRequiredMixin, generic.ListView): |
|
26
|
|
|
model = Organisation |
|
27
|
|
|
template_name = 'organisation/list.html' |
|
28
|
|
|
|
|
29
|
|
|
def dispatch(self, request, *args, **kwargs): |
|
30
|
|
|
user_profile = Profile.objects.get( |
|
31
|
|
|
user__id=self.request.user.id) |
|
32
|
|
|
if not user_profile.is_profile_filled: |
|
33
|
|
|
return redirect('profiles:profile-edit', slug=request.user.username) |
|
34
|
|
|
return super(OrganisationList, self).dispatch(request, *args, **kwargs) |
|
35
|
|
|
|
|
36
|
|
|
def get_queryset(self): |
|
37
|
|
|
return Organisation.objects.filter(active=True) |
|
38
|
|
|
|
|
39
|
|
|
def get_context_data(self, *args, **kwargs): |
|
40
|
|
|
context = super(OrganisationList, self).get_context_data( |
|
41
|
|
|
*args, **kwargs) |
|
42
|
|
|
if Profile.is_organiser(self.request.user): |
|
43
|
|
|
context['org_created_list'] = self.get_queryset().filter( |
|
44
|
|
|
created_by=self.request.user) |
|
45
|
|
|
context['org_belongs_list'] = self.get_queryset().exclude( |
|
46
|
|
|
created_by=self.request.user).filter( |
|
47
|
|
|
user=self.request.user) |
|
48
|
|
|
elif Profile.is_regional_lead(self.request.user): |
|
49
|
|
|
regions = RegionalLead.objects.filter(leads=self.request.user) |
|
50
|
|
|
context['regional_org_list'] = self.get_queryset().filter( |
|
51
|
|
|
location__id__in=[x.location.id for x in regions]) |
|
52
|
|
|
context['user'] = self.request.user |
|
53
|
|
|
# need to improve the part |
|
54
|
|
|
# context['is_not_tutor'] = False |
|
55
|
|
|
# as user can be tutor and regional lead hence we need to verify like |
|
56
|
|
|
# this |
|
57
|
|
|
if (Profile.is_regional_lead(self.request.user) or |
|
58
|
|
|
Profile.is_organiser(self.request.user) or |
|
59
|
|
|
Profile.is_admin(self.request.user)): |
|
60
|
|
|
context['is_not_tutor'] = True |
|
61
|
|
|
return context |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
class OrganisationCreate(views.LoginRequiredMixin, generic.CreateView): |
|
65
|
|
|
model = Organisation |
|
66
|
|
|
form_class = OrganisationForm |
|
67
|
|
|
template_name = 'organisation/create.html' |
|
68
|
|
|
success_url = reverse_lazy('organisations:organisation_list') |
|
69
|
|
|
|
|
70
|
|
|
def dispatch(self, request, *args, **kwargs): |
|
71
|
|
|
user_profile = Profile.objects.get( |
|
72
|
|
|
user__id=self.request.user.id) |
|
73
|
|
|
if not user_profile.is_profile_filled: |
|
74
|
|
|
return redirect('profiles:profile-edit', slug=request.user.username) |
|
75
|
|
|
return super(OrganisationCreate, self).dispatch(request, *args, **kwargs) |
|
76
|
|
|
|
|
77
|
|
|
def post(self, request, *args, **kwargs): |
|
78
|
|
|
form = OrganisationForm(data=request.POST) |
|
79
|
|
|
if form.is_valid(): |
|
80
|
|
|
form.instance.modified_by = request.user |
|
81
|
|
|
form.instance.created_by = request.user |
|
82
|
|
|
form.instance.save() |
|
83
|
|
|
form.instance.user.add(request.user) |
|
84
|
|
|
form.instance.save() |
|
85
|
|
|
user_profile = Profile.objects.get( |
|
86
|
|
|
user__id=self.request.user.id) |
|
87
|
|
|
if not ('poc' in user_profile.get_user_type): |
|
88
|
|
|
poc_type = UserType.objects.get(slug='poc') |
|
89
|
|
|
user_profile.usertype.add(poc_type) |
|
90
|
|
|
user_profile.save() |
|
91
|
|
|
|
|
92
|
|
|
host = '{}://{}'.format(settings.SITE_PROTOCOL, |
|
93
|
|
|
request.META['HTTP_HOST']) |
|
94
|
|
|
email_context = Context({ |
|
95
|
|
|
'full_name': '%s %s' % (request.user.first_name, |
|
96
|
|
|
request.user.last_name), |
|
97
|
|
|
'org_id': form.instance.id, |
|
98
|
|
|
'host': host |
|
99
|
|
|
|
|
100
|
|
|
}) |
|
101
|
|
|
subject = "%s organisation for region %s is created" % ( |
|
102
|
|
|
form.instance.name, form.instance.location.name) |
|
103
|
|
|
email_body = loader.get_template( |
|
104
|
|
|
'email_messages/organisation/new.html').render(email_context) |
|
105
|
|
|
text_body = loader.get_template( |
|
106
|
|
|
'email_messages/organisation/new.txt').render(email_context) |
|
107
|
|
|
|
|
108
|
|
|
regional_lead = Profile.objects.filter( |
|
109
|
|
|
interested_locations=form.instance.location, |
|
110
|
|
|
usertype__slug='lead').values_list('user__email', flat=True) |
|
111
|
|
|
send_email_to_id(subject, |
|
112
|
|
|
body=email_body, |
|
113
|
|
|
email_id=request.user.email, |
|
114
|
|
|
text_body=text_body) |
|
115
|
|
|
|
|
116
|
|
|
send_email_to_list(subject, |
|
117
|
|
|
body=email_body, |
|
118
|
|
|
users_list=regional_lead, |
|
119
|
|
|
text_body=text_body) |
|
120
|
|
|
|
|
121
|
|
|
return HttpResponseRedirect(self.success_url) |
|
122
|
|
|
else: |
|
123
|
|
|
return render(request, self.template_name, {'form': form}) |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
class OrganisationDetail(views.LoginRequiredMixin, generic.DetailView): |
|
127
|
|
|
model = Organisation |
|
128
|
|
|
template_name = 'organisation/detail.html' |
|
129
|
|
|
success_url = reverse_lazy('organisations:organisation_list') |
|
130
|
|
|
|
|
131
|
|
|
def get_queryset(self): |
|
132
|
|
|
return Organisation.objects.filter( |
|
133
|
|
|
user=self.request.user, |
|
134
|
|
|
id=self.kwargs['pk']) |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
class OrganisationUpdate(views.LoginRequiredMixin, generic.UpdateView): |
|
138
|
|
|
model = Organisation |
|
139
|
|
|
form_class = OrganisationForm |
|
140
|
|
|
template_name = 'organisation/edit.html' |
|
141
|
|
|
success_url = reverse_lazy('organisations:organisation_list') |
|
142
|
|
|
|
|
143
|
|
|
def get_object(self, queryset=None): |
|
144
|
|
|
org = Organisation.objects.get(user=self.request.user, id=self.kwargs['pk']) |
|
145
|
|
|
if org.created_by == self.request.user: |
|
146
|
|
|
return Organisation.objects.get(user=self.request.user, id=self.kwargs['pk']) |
|
147
|
|
|
else: |
|
148
|
|
|
self.template_name = "403.html" |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
class OrganisationMemberAdd(views.LoginRequiredMixin, generic.UpdateView): |
|
152
|
|
|
model = Organisation |
|
153
|
|
|
form_class = OrganisationMemberAddForm |
|
154
|
|
|
template_name = 'organisation/member-add.html' |
|
155
|
|
|
success_url = reverse_lazy('organisations:organisation_list') |
|
156
|
|
|
|
|
157
|
|
|
def get_username(self, email): |
|
158
|
|
|
""" |
|
159
|
|
|
Returns a UUID-based 'random' and unique username. |
|
160
|
|
|
|
|
161
|
|
|
This is required data for user models with a username field. |
|
162
|
|
|
""" |
|
163
|
|
|
uuid_str = str(uuid.uuid4()) |
|
164
|
|
|
username = email.split("@")[0] |
|
165
|
|
|
uuid_str = uuid_str[:30 - len(username)] |
|
166
|
|
|
return username + uuid_str |
|
167
|
|
|
|
|
168
|
|
|
def get_token(self, user, **kwargs): |
|
169
|
|
|
"""Returns a unique token for the given user""" |
|
170
|
|
|
return PasswordResetTokenGenerator().make_token(user) |
|
171
|
|
|
|
|
172
|
|
|
def get_urls(self): |
|
173
|
|
|
return patterns('', |
|
174
|
|
|
url(r'^(?P<user_id>[\d]+)-(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/', |
|
175
|
|
|
view=self.activate_view, name="invitation_register") |
|
176
|
|
|
) |
|
177
|
|
|
|
|
178
|
|
|
def post(self, request, *args, **kwargs): |
|
179
|
|
|
form = OrganisationMemberAddForm(data=request.POST) |
|
180
|
|
|
if form.is_valid(): |
|
181
|
|
|
existing_user = form.cleaned_data['existing_user'] |
|
182
|
|
|
new_user = form.cleaned_data['new_user'] |
|
183
|
|
|
|
|
184
|
|
|
org = Organisation.objects.get(id=self.kwargs['pk']) |
|
185
|
|
|
host = '{}://{}'.format(settings.SITE_PROTOCOL, |
|
186
|
|
|
request.META['HTTP_HOST']) |
|
187
|
|
|
|
|
188
|
|
|
context = { |
|
189
|
|
|
'full_name': '%s %s' % (request.user.first_name, |
|
190
|
|
|
request.user.last_name), |
|
191
|
|
|
'org_name': org.name, |
|
192
|
|
|
'host': host |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
if existing_user: |
|
196
|
|
|
# add user to organisation |
|
197
|
|
|
user = existing_user |
|
198
|
|
|
org.user.add(user) |
|
199
|
|
|
org.save() |
|
200
|
|
|
|
|
201
|
|
|
# set email user's name in context |
|
202
|
|
|
context['new_member_name'] = '%s %s' % (user.first_name, |
|
203
|
|
|
user.last_name) |
|
204
|
|
|
email_context = Context(context) |
|
205
|
|
|
|
|
206
|
|
|
# send mail to user being added |
|
207
|
|
|
subject = "You are added in %s organisation" % ( |
|
208
|
|
|
org.location.name) |
|
209
|
|
|
email_body = loader.get_template( |
|
210
|
|
|
'email_messages/organisation/to_new_member_existing.html').render( |
|
211
|
|
|
email_context) |
|
212
|
|
|
text_body = loader.get_template( |
|
213
|
|
|
'email_messages/organisation/to_new_member_existing.txt').render(email_context) |
|
214
|
|
|
|
|
215
|
|
|
send_email_to_id(subject, |
|
216
|
|
|
body=email_body, |
|
217
|
|
|
email_id=user.email, |
|
218
|
|
|
text_body=text_body) |
|
219
|
|
|
|
|
220
|
|
|
elif new_user: |
|
221
|
|
|
# generate a random password |
|
222
|
|
|
random_password = User.objects.make_random_password() |
|
223
|
|
|
|
|
224
|
|
|
# create a user with the email from form |
|
225
|
|
|
user = User(username=self.get_username(new_user), |
|
226
|
|
|
email=new_user, |
|
227
|
|
|
password=random_password) |
|
228
|
|
|
|
|
229
|
|
|
# user is inactive initialy |
|
230
|
|
|
user.is_active = False |
|
231
|
|
|
user.save() |
|
232
|
|
|
|
|
233
|
|
|
# add the user to organisation |
|
234
|
|
|
org.user.add(user.id) |
|
235
|
|
|
org.save() |
|
236
|
|
|
|
|
237
|
|
|
# set the email context, the token will be used to generate a unique varification |
|
238
|
|
|
# link |
|
239
|
|
|
token = self.get_token(user) |
|
240
|
|
|
|
|
241
|
|
|
context['new_member_name'] = '%s' % (user.email) |
|
242
|
|
|
context['token'] = token |
|
243
|
|
|
context['user'] = user |
|
244
|
|
|
email_context = Context(context) |
|
245
|
|
|
|
|
246
|
|
|
# set the meta |
|
247
|
|
|
subject = "[Python Express]:You are added in %s organisation" % ( |
|
248
|
|
|
org.location.name) |
|
249
|
|
|
email_body = loader.get_template( |
|
250
|
|
|
'email_messages/organisation/to_new_member.html').render(email_context) |
|
251
|
|
|
text_body = loader.get_template( |
|
252
|
|
|
'email_messages/organisation/to_new_member.txt').render(email_context) |
|
253
|
|
|
|
|
254
|
|
|
# send the mail to new user |
|
255
|
|
|
send_email_to_id(subject, |
|
256
|
|
|
body=email_body, |
|
257
|
|
|
email_id=new_user, |
|
258
|
|
|
text_body=text_body) |
|
259
|
|
|
|
|
260
|
|
|
# These mails will be sent in both cases. |
|
261
|
|
|
subject = "user %s %s added in %s organisation" % ( |
|
262
|
|
|
user.first_name, user.last_name, org.location.name) |
|
263
|
|
|
email_body = loader.get_template( |
|
264
|
|
|
'email_messages/organisation/member_addition_to_user.html').render( |
|
265
|
|
|
email_context) |
|
266
|
|
|
text_body = loader.get_template( |
|
267
|
|
|
'email_messages/organisation/member_addition_to_user.txt').render( |
|
268
|
|
|
email_context) |
|
269
|
|
|
|
|
270
|
|
|
# send mail to the user who added the new member |
|
271
|
|
|
send_email_to_id(subject, |
|
272
|
|
|
body=email_body, |
|
273
|
|
|
email_id=request.user.email, |
|
274
|
|
|
text_body=text_body) |
|
275
|
|
|
|
|
276
|
|
|
regional_lead = Profile.objects.filter( |
|
277
|
|
|
interested_locations=org.location, |
|
278
|
|
|
usertype__slug='lead').values_list('user__email', flat=True) |
|
279
|
|
|
|
|
280
|
|
|
email_body = loader.get_template( |
|
281
|
|
|
'email_messages/organisation/member_addition_to_lead.html').render( |
|
282
|
|
|
email_context) |
|
283
|
|
|
text_body = loader.get_template( |
|
284
|
|
|
'email_messages/organisation/member_addition_to_lead.txt').render( |
|
285
|
|
|
email_context) |
|
286
|
|
|
|
|
287
|
|
|
# send mail to the regional leads |
|
288
|
|
|
send_email_to_list(subject, |
|
289
|
|
|
body=email_body, |
|
290
|
|
|
users_list=regional_lead, |
|
291
|
|
|
text_body=text_body) |
|
292
|
|
|
|
|
293
|
|
|
return HttpResponseRedirect(self.success_url) |
|
294
|
|
|
|
|
295
|
|
|
else: |
|
296
|
|
|
return render(request, self.template_name, {'form': form}) |
|
297
|
|
|
|
|
298
|
|
|
|
|
299
|
|
|
def activate_view(request, user_id, token): |
|
300
|
|
|
""" |
|
301
|
|
|
View function that activates the given User by setting `is_active` to |
|
302
|
|
|
true if the provided information is verified. |
|
303
|
|
|
""" |
|
304
|
|
|
try: |
|
305
|
|
|
user = User.objects.get(id=user_id, is_active=False) |
|
306
|
|
|
except(User.DoesNotExist): |
|
307
|
|
|
raise Http404("Your URL may have expired.") |
|
308
|
|
|
|
|
309
|
|
|
if not PasswordResetTokenGenerator().check_token(user, token): |
|
310
|
|
|
raise Http404("Your URL may have expired.") |
|
311
|
|
|
|
|
312
|
|
|
form = UserRegistrationForm(data=request.POST or None, instance=user) |
|
313
|
|
|
if form.is_valid(): |
|
314
|
|
|
user.is_active = True |
|
315
|
|
|
user.username = form.cleaned_data['username'] |
|
316
|
|
|
user.first_name = form.cleaned_data['first_name'] |
|
317
|
|
|
user.last_name = form.cleaned_data['last_name'] |
|
318
|
|
|
user.set_password(form.cleaned_data['password']) |
|
319
|
|
|
user.save() |
|
320
|
|
|
return redirect(reverse_lazy('organisations:organisation_list')) |
|
321
|
|
|
else: |
|
322
|
|
|
return render(request, 'organisation/register_form.html', |
|
323
|
|
|
{'form': form}) |
|
324
|
|
|
|
|
325
|
|
|
|
|
326
|
|
|
class OrganisationDeactive(views.CsrfExemptMixin, |
|
327
|
|
|
views.LoginRequiredMixin, |
|
328
|
|
|
views.JSONResponseMixin, |
|
329
|
|
|
generic.UpdateView): |
|
330
|
|
|
model = Organisation |
|
331
|
|
|
fields = ('active', 'id') |
|
332
|
|
|
|
|
333
|
|
|
def get_object(self, queryset=None): |
|
334
|
|
|
return Organisation.objects.get(user=self.request.user, |
|
335
|
|
|
id=self.kwargs['pk']) |
|
336
|
|
|
|
|
337
|
|
|
def post(self, request, *args, **kwargs): |
|
338
|
|
|
self.object = self.get_object() |
|
339
|
|
|
response = self.object.toggle_active(request.user, **kwargs) |
|
340
|
|
|
return self.render_json_response(response) |
|
341
|
|
|
|