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
|
|
|
if not user_profile.can_create_organisation: |
76
|
|
|
msg = '''Exceed number of organisaiton registration. |
77
|
|
|
Use contact us form to connect to co-ordinators''' |
78
|
|
|
return render(request, 'error.html', {'message': msg}) |
79
|
|
|
return super(OrganisationCreate, self).dispatch(request, *args, **kwargs) |
80
|
|
|
|
81
|
|
|
def post(self, request, *args, **kwargs): |
82
|
|
|
form = OrganisationForm(data=request.POST) |
83
|
|
|
if form.is_valid(): |
84
|
|
|
form.instance.modified_by = request.user |
85
|
|
|
form.instance.created_by = request.user |
86
|
|
|
form.instance.save() |
87
|
|
|
form.instance.user.add(request.user) |
88
|
|
|
form.instance.save() |
89
|
|
|
user_profile = Profile.objects.get( |
90
|
|
|
user__id=self.request.user.id) |
91
|
|
|
if not ('poc' in user_profile.get_user_type): |
92
|
|
|
poc_type = UserType.objects.get(slug='poc') |
93
|
|
|
user_profile.usertype.add(poc_type) |
94
|
|
|
user_profile.save() |
95
|
|
|
|
96
|
|
|
host = '{}://{}'.format(settings.SITE_PROTOCOL, |
97
|
|
|
request.META['HTTP_HOST']) |
98
|
|
|
email_context = Context({ |
99
|
|
|
'full_name': '%s %s' % (request.user.first_name, |
100
|
|
|
request.user.last_name), |
101
|
|
|
'org_id': form.instance.id, |
102
|
|
|
'host': host |
103
|
|
|
|
104
|
|
|
}) |
105
|
|
|
subject = "%s organisation for region %s is created" % ( |
106
|
|
|
form.instance.name, form.instance.location.name) |
107
|
|
|
email_body = loader.get_template( |
108
|
|
|
'email_messages/organisation/new.html').render(email_context) |
109
|
|
|
text_body = loader.get_template( |
110
|
|
|
'email_messages/organisation/new.txt').render(email_context) |
111
|
|
|
|
112
|
|
|
regional_lead = Profile.objects.filter( |
113
|
|
|
interested_locations=form.instance.location, |
114
|
|
|
usertype__slug='lead').values_list('user__email', flat=True) |
115
|
|
|
send_email_to_id(subject, |
116
|
|
|
body=email_body, |
117
|
|
|
email_id=request.user.email, |
118
|
|
|
text_body=text_body) |
119
|
|
|
|
120
|
|
|
send_email_to_list(subject, |
121
|
|
|
body=email_body, |
122
|
|
|
users_list=regional_lead, |
123
|
|
|
text_body=text_body) |
124
|
|
|
|
125
|
|
|
return HttpResponseRedirect(self.success_url) |
126
|
|
|
else: |
127
|
|
|
return render(request, self.template_name, {'form': form}) |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
class OrganisationDetail(views.LoginRequiredMixin, generic.DetailView): |
131
|
|
|
model = Organisation |
132
|
|
|
template_name = 'organisation/detail.html' |
133
|
|
|
success_url = reverse_lazy('organisations:organisation_list') |
134
|
|
|
|
135
|
|
|
def get_queryset(self): |
136
|
|
|
return Organisation.objects.filter( |
137
|
|
|
user=self.request.user, |
138
|
|
|
id=self.kwargs['pk']) |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
class OrganisationUpdate(views.LoginRequiredMixin, generic.UpdateView): |
142
|
|
|
model = Organisation |
143
|
|
|
form_class = OrganisationForm |
144
|
|
|
template_name = 'organisation/edit.html' |
145
|
|
|
success_url = reverse_lazy('organisations:organisation_list') |
146
|
|
|
|
147
|
|
|
def get_object(self, queryset=None): |
148
|
|
|
org = Organisation.objects.get(user=self.request.user, id=self.kwargs['pk']) |
149
|
|
|
if org.created_by == self.request.user: |
150
|
|
|
return Organisation.objects.get(user=self.request.user, id=self.kwargs['pk']) |
151
|
|
|
else: |
152
|
|
|
self.template_name = "403.html" |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
class OrganisationMemberAdd(views.LoginRequiredMixin, generic.UpdateView): |
156
|
|
|
model = Organisation |
157
|
|
|
form_class = OrganisationMemberAddForm |
158
|
|
|
template_name = 'organisation/member-add.html' |
159
|
|
|
success_url = reverse_lazy('organisations:organisation_list') |
160
|
|
|
|
161
|
|
|
def get_username(self, email): |
162
|
|
|
""" |
163
|
|
|
Returns a UUID-based 'random' and unique username. |
164
|
|
|
|
165
|
|
|
This is required data for user models with a username field. |
166
|
|
|
""" |
167
|
|
|
uuid_str = str(uuid.uuid4()) |
168
|
|
|
username = email.split("@")[0] |
169
|
|
|
uuid_str = uuid_str[:30 - len(username)] |
170
|
|
|
return username + uuid_str |
171
|
|
|
|
172
|
|
|
def get_token(self, user, **kwargs): |
173
|
|
|
"""Returns a unique token for the given user""" |
174
|
|
|
return PasswordResetTokenGenerator().make_token(user) |
175
|
|
|
|
176
|
|
|
def get_urls(self): |
177
|
|
|
return patterns('', |
178
|
|
|
url(r'^(?P<user_id>[\d]+)-(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/', |
179
|
|
|
view=self.activate_view, name="invitation_register") |
180
|
|
|
) |
181
|
|
|
|
182
|
|
|
def post(self, request, *args, **kwargs): |
183
|
|
|
form = OrganisationMemberAddForm(data=request.POST) |
184
|
|
|
if form.is_valid(): |
185
|
|
|
existing_user = form.cleaned_data['existing_user'] |
186
|
|
|
new_user = form.cleaned_data['new_user'] |
187
|
|
|
|
188
|
|
|
org = Organisation.objects.get(id=self.kwargs['pk']) |
189
|
|
|
host = '{}://{}'.format(settings.SITE_PROTOCOL, |
190
|
|
|
request.META['HTTP_HOST']) |
191
|
|
|
|
192
|
|
|
context = { |
193
|
|
|
'full_name': '%s %s' % (request.user.first_name, |
194
|
|
|
request.user.last_name), |
195
|
|
|
'org_name': org.name, |
196
|
|
|
'host': host |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if existing_user: |
200
|
|
|
# add user to organisation |
201
|
|
|
user = existing_user |
202
|
|
|
org.user.add(user) |
203
|
|
|
org.save() |
204
|
|
|
|
205
|
|
|
# set email user's name in context |
206
|
|
|
context['new_member_name'] = '%s %s' % (user.first_name, |
207
|
|
|
user.last_name) |
208
|
|
|
email_context = Context(context) |
209
|
|
|
|
210
|
|
|
# send mail to user being added |
211
|
|
|
subject = "You are added in %s organisation" % ( |
212
|
|
|
org.location.name) |
213
|
|
|
email_body = loader.get_template( |
214
|
|
|
'email_messages/organisation/to_new_member_existing.html').render( |
215
|
|
|
email_context) |
216
|
|
|
text_body = loader.get_template( |
217
|
|
|
'email_messages/organisation/to_new_member_existing.txt').render(email_context) |
218
|
|
|
|
219
|
|
|
send_email_to_id(subject, |
220
|
|
|
body=email_body, |
221
|
|
|
email_id=user.email, |
222
|
|
|
text_body=text_body) |
223
|
|
|
|
224
|
|
|
elif new_user: |
225
|
|
|
# generate a random password |
226
|
|
|
random_password = User.objects.make_random_password() |
227
|
|
|
|
228
|
|
|
# create a user with the email from form |
229
|
|
|
user = User(username=self.get_username(new_user), |
230
|
|
|
email=new_user, |
231
|
|
|
password=random_password) |
232
|
|
|
|
233
|
|
|
# user is inactive initialy |
234
|
|
|
user.is_active = False |
235
|
|
|
user.save() |
236
|
|
|
|
237
|
|
|
# add the user to organisation |
238
|
|
|
org.user.add(user.id) |
239
|
|
|
org.save() |
240
|
|
|
|
241
|
|
|
# set the email context, the token will be used to generate a unique varification |
242
|
|
|
# link |
243
|
|
|
token = self.get_token(user) |
244
|
|
|
|
245
|
|
|
context['new_member_name'] = '%s' % (user.email) |
246
|
|
|
context['token'] = token |
247
|
|
|
context['user'] = user |
248
|
|
|
email_context = Context(context) |
249
|
|
|
|
250
|
|
|
# set the meta |
251
|
|
|
subject = "[Python Express]:You are added in %s organisation" % ( |
252
|
|
|
org.location.name) |
253
|
|
|
email_body = loader.get_template( |
254
|
|
|
'email_messages/organisation/to_new_member.html').render(email_context) |
255
|
|
|
text_body = loader.get_template( |
256
|
|
|
'email_messages/organisation/to_new_member.txt').render(email_context) |
257
|
|
|
|
258
|
|
|
# send the mail to new user |
259
|
|
|
send_email_to_id(subject, |
260
|
|
|
body=email_body, |
261
|
|
|
email_id=new_user, |
262
|
|
|
text_body=text_body) |
263
|
|
|
|
264
|
|
|
# These mails will be sent in both cases. |
265
|
|
|
subject = "user %s %s added in %s organisation" % ( |
266
|
|
|
user.first_name, user.last_name, org.location.name) |
267
|
|
|
email_body = loader.get_template( |
268
|
|
|
'email_messages/organisation/member_addition_to_user.html').render( |
269
|
|
|
email_context) |
270
|
|
|
text_body = loader.get_template( |
271
|
|
|
'email_messages/organisation/member_addition_to_user.txt').render( |
272
|
|
|
email_context) |
273
|
|
|
|
274
|
|
|
# send mail to the user who added the new member |
275
|
|
|
send_email_to_id(subject, |
276
|
|
|
body=email_body, |
277
|
|
|
email_id=request.user.email, |
278
|
|
|
text_body=text_body) |
279
|
|
|
|
280
|
|
|
regional_lead = Profile.objects.filter( |
281
|
|
|
interested_locations=org.location, |
282
|
|
|
usertype__slug='lead').values_list('user__email', flat=True) |
283
|
|
|
|
284
|
|
|
email_body = loader.get_template( |
285
|
|
|
'email_messages/organisation/member_addition_to_lead.html').render( |
286
|
|
|
email_context) |
287
|
|
|
text_body = loader.get_template( |
288
|
|
|
'email_messages/organisation/member_addition_to_lead.txt').render( |
289
|
|
|
email_context) |
290
|
|
|
|
291
|
|
|
# send mail to the regional leads |
292
|
|
|
send_email_to_list(subject, |
293
|
|
|
body=email_body, |
294
|
|
|
users_list=regional_lead, |
295
|
|
|
text_body=text_body) |
296
|
|
|
|
297
|
|
|
return HttpResponseRedirect(self.success_url) |
298
|
|
|
|
299
|
|
|
else: |
300
|
|
|
return render(request, self.template_name, {'form': form}) |
301
|
|
|
|
302
|
|
|
|
303
|
|
|
def activate_view(request, user_id, token): |
304
|
|
|
""" |
305
|
|
|
View function that activates the given User by setting `is_active` to |
306
|
|
|
true if the provided information is verified. |
307
|
|
|
""" |
308
|
|
|
try: |
309
|
|
|
user = User.objects.get(id=user_id, is_active=False) |
310
|
|
|
except(User.DoesNotExist): |
311
|
|
|
raise Http404("Your URL may have expired.") |
312
|
|
|
|
313
|
|
|
if not PasswordResetTokenGenerator().check_token(user, token): |
314
|
|
|
raise Http404("Your URL may have expired.") |
315
|
|
|
|
316
|
|
|
form = UserRegistrationForm(data=request.POST or None, instance=user) |
317
|
|
|
if form.is_valid(): |
318
|
|
|
user.is_active = True |
319
|
|
|
user.username = form.cleaned_data['username'] |
320
|
|
|
user.first_name = form.cleaned_data['first_name'] |
321
|
|
|
user.last_name = form.cleaned_data['last_name'] |
322
|
|
|
user.set_password(form.cleaned_data['password']) |
323
|
|
|
user.save() |
324
|
|
|
return redirect(reverse_lazy('organisations:organisation_list')) |
325
|
|
|
else: |
326
|
|
|
return render(request, 'organisation/register_form.html', |
327
|
|
|
{'form': form}) |
328
|
|
|
|
329
|
|
|
|
330
|
|
|
class OrganisationDeactive(views.CsrfExemptMixin, |
331
|
|
|
views.LoginRequiredMixin, |
332
|
|
|
views.JSONResponseMixin, |
333
|
|
|
generic.UpdateView): |
334
|
|
|
model = Organisation |
335
|
|
|
fields = ('active', 'id') |
336
|
|
|
|
337
|
|
|
def get_object(self, queryset=None): |
338
|
|
|
return Organisation.objects.get(user=self.request.user, |
339
|
|
|
id=self.kwargs['pk']) |
340
|
|
|
|
341
|
|
|
def post(self, request, *args, **kwargs): |
342
|
|
|
self.object = self.get_object() |
343
|
|
|
response = self.object.toggle_active(request.user, **kwargs) |
344
|
|
|
return self.render_json_response(response) |
345
|
|
|
|