1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
from django.conf import settings |
3
|
|
|
from django.core.exceptions import PermissionDenied |
4
|
|
|
from django.core.urlresolvers import reverse |
5
|
|
|
from django.http import HttpResponseRedirect |
6
|
|
|
from django.shortcuts import redirect, render, get_object_or_404 |
7
|
|
|
from django.template import Context, loader |
8
|
|
|
from django.views.generic import UpdateView |
9
|
|
|
from django.views.generic.list import ListView |
10
|
|
|
from django.contrib.auth import logout |
11
|
|
|
from django.contrib.auth.decorators import login_required |
12
|
|
|
from wye.base.constants import WorkshopStatus |
13
|
|
|
from wye.base.emailer_html import send_email_to_id, send_email_to_list |
14
|
|
|
from wye.organisations.models import Organisation |
15
|
|
|
from wye.profiles.models import Profile |
16
|
|
|
from wye.workshops.models import Workshop |
17
|
|
|
|
18
|
|
|
from .forms import ContactUsForm, UserProfileForm, PartnerForm |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
@login_required |
22
|
|
|
def account_redirect(request): |
23
|
|
|
return redirect('/profile/{}/'.format(request.user.profile.slug)) |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def profile_view(request, slug): |
27
|
|
|
try: |
28
|
|
|
p = Profile.objects.get(user__username=slug) |
29
|
|
|
workshops = Workshop.objects.filter(is_active=True).filter( |
30
|
|
|
presenter=p.user).filter(status__in=[ |
31
|
|
|
WorkshopStatus.ACCEPTED, |
32
|
|
|
WorkshopStatus.REQUESTED, |
33
|
|
|
WorkshopStatus.FEEDBACK_PENDING, |
34
|
|
|
WorkshopStatus.COMPLETED]).order_by('-expected_date') |
35
|
|
|
return render( |
36
|
|
|
request, 'profile/index.html', |
37
|
|
|
{'object': p, 'workshops': workshops}) |
38
|
|
|
except Profile.DoesNotExist: |
39
|
|
|
return render(request, 'error.html', { |
40
|
|
|
"message": "Profile does not exist"}) |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def user_dashboad(request): |
44
|
|
|
profile, created = Profile.objects.get_or_create( |
45
|
|
|
user__id=request.user.id) |
46
|
|
|
if not profile.is_profile_filled: |
47
|
|
|
return redirect('profiles:profile-edit', slug=request.user.username) |
48
|
|
|
workshop_all = Workshop.objects.filter( |
49
|
|
|
is_active=True).order_by('-expected_date') |
50
|
|
|
|
51
|
|
|
accept_workshops = workshop_all.filter( |
52
|
|
|
status=WorkshopStatus.ACCEPTED).filter( |
53
|
|
|
presenter__id__in=[request.user.id]) |
54
|
|
|
requested_workshops = workshop_all.filter( |
55
|
|
|
status=WorkshopStatus.REQUESTED).filter( |
56
|
|
|
presenter__id__in=[request.user.id]) |
57
|
|
|
requested_workshops = workshop_all.filter( |
58
|
|
|
status=WorkshopStatus.REQUESTED).filter( |
59
|
|
|
presenter__id__in=[request.user.id]) |
60
|
|
|
|
61
|
|
|
organisation_all = Organisation.objects.all() |
62
|
|
|
context = {} |
63
|
|
|
for each_type in profile.get_user_type: |
64
|
|
|
if each_type == 'poc': |
65
|
|
|
context['is_college_poc'] = True |
66
|
|
|
context['users_organisation'] = organisation_all.filter( |
67
|
|
|
user=request.user) |
68
|
|
|
context['workshop_requested_under_poc'] = workshop_all.filter( |
69
|
|
|
requester__id__in=organisation_all.values_list( |
70
|
|
|
'id', flat=True)) |
71
|
|
|
context['workshops_accepted_under_poc'] = workshop_all.filter( |
72
|
|
|
status=WorkshopStatus.ACCEPTED, |
73
|
|
|
requester__id__in=organisation_all.values_list( |
74
|
|
|
'id', flat=True)) |
75
|
|
|
else: |
76
|
|
|
context['is_tutor'] = True |
77
|
|
|
context['workshop_requested_tutor'] = accept_workshops.filter( |
78
|
|
|
presenter=request.user) |
79
|
|
|
context['workshop_completed_tutor'] = \ |
80
|
|
|
requested_workshops.filter( |
81
|
|
|
presenter=request.user) |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
class UserDashboard(ListView): |
85
|
|
|
model = Profile |
86
|
|
|
template_name = 'profile/dashboard.html' |
87
|
|
|
|
88
|
|
|
def dispatch(self, request, *args, **kwargs): |
89
|
|
|
user_profile = get_object_or_404( |
90
|
|
|
Profile, user__id=self.request.user.id) |
91
|
|
|
if not user_profile.get_user_type: |
92
|
|
|
return redirect('profiles:profile-edit', |
93
|
|
|
slug=request.user.username) |
94
|
|
|
return super(UserDashboard, self).dispatch(request, *args, **kwargs) |
95
|
|
|
|
96
|
|
|
def get_context_data(self, **kwargs): |
97
|
|
|
context = super(UserDashboard, self).get_context_data(**kwargs) |
98
|
|
|
profile = Profile.objects.get(user=self.request.user) |
99
|
|
|
workshop_all = Workshop.objects.filter(is_active=True) |
100
|
|
|
accept_workshops = workshop_all.filter( |
101
|
|
|
status=WorkshopStatus.ACCEPTED) |
102
|
|
|
requested_workshops = workshop_all.filter( |
103
|
|
|
status=WorkshopStatus.REQUESTED) |
104
|
|
|
organisation_all = Organisation.objects.all() |
105
|
|
|
for each_type in profile.get_user_type: |
106
|
|
|
if each_type == 'tutor': |
107
|
|
|
context['is_tutor'] = True |
108
|
|
|
context['workshop_requested_tutor'] = accept_workshops.filter( |
109
|
|
|
presenter=self.request.user) |
110
|
|
|
context['workshop_completed_tutor'] = \ |
111
|
|
|
requested_workshops.filter( |
112
|
|
|
presenter=self.request.user) |
113
|
|
|
if each_type == 'lead': |
114
|
|
|
context['is_regional_lead'] = True |
115
|
|
|
context['workshops_accepted_under_rl'] = accept_workshops |
116
|
|
|
context['workshops_pending_under_rl'] = requested_workshops |
117
|
|
|
context['interested_tutors'] = Profile.objects.filter( |
118
|
|
|
usertype__slug='tutor', |
119
|
|
|
interested_locations__name__in=profile.get_interested_locations)\ |
120
|
|
|
.exclude(user=self.request.user).count() |
121
|
|
|
context['interested_locations'] = organisation_all.filter( |
122
|
|
|
location__name__in=profile.get_interested_locations)\ |
123
|
|
|
.count() |
124
|
|
|
|
125
|
|
|
if each_type == 'poc': |
126
|
|
|
context['is_college_poc'] = True |
127
|
|
|
context['users_organisation'] = organisation_all.filter( |
128
|
|
|
user=self.request.user) |
129
|
|
|
context['workshop_requested_under_poc'] = workshop_all.filter( |
130
|
|
|
requester__id__in=organisation_all.values_list( |
131
|
|
|
'id', flat=True)) |
132
|
|
|
context['workshops_accepted_under_poc'] = workshop_all.filter( |
133
|
|
|
status=WorkshopStatus.ACCEPTED, |
134
|
|
|
requester__id__in=organisation_all.values_list( |
135
|
|
|
'id', flat=True)) |
136
|
|
|
|
137
|
|
|
if each_type == 'admin': |
138
|
|
|
context['is_admin'] = True |
139
|
|
|
context['workshops_by_status'] = workshop_all.order_by( |
140
|
|
|
'status') |
141
|
|
|
context['workshops_by_region'] = workshop_all.order_by( |
142
|
|
|
'location') |
143
|
|
|
|
144
|
|
|
return context |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
class ProfileEditView(UpdateView): |
148
|
|
|
model = Profile |
149
|
|
|
template_name = 'profile/update.html' |
150
|
|
|
form_class = UserProfileForm |
151
|
|
|
slug_field = 'user__username' |
152
|
|
|
|
153
|
|
|
def form_valid(self, form): |
154
|
|
|
return super(ProfileEditView, self).form_valid(form) |
155
|
|
|
|
156
|
|
|
def get_success_url(self): |
157
|
|
|
return reverse('profiles:profile-page', kwargs={ |
158
|
|
|
'slug': self.object.slug}) |
159
|
|
|
|
160
|
|
|
def dispatch(self, *args, **kwargs): |
161
|
|
|
if self.request.user.pk == self.get_object().pk: |
162
|
|
|
return super(ProfileEditView, self).dispatch(*args, **kwargs) |
163
|
|
|
else: |
164
|
|
|
raise PermissionDenied |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
def contact(request): |
168
|
|
|
if request.method == 'POST': |
169
|
|
|
form = ContactUsForm(request.POST) |
170
|
|
|
if form.is_valid(): |
171
|
|
|
email_context = Context({ |
172
|
|
|
'contact_name': form.cleaned_data['name'], |
173
|
|
|
'contact_email': form.cleaned_data['email'], |
174
|
|
|
'comments': form.cleaned_data['comments'], |
175
|
|
|
'conatct_number': form.cleaned_data['contact_number'], |
176
|
|
|
'feedback_type': form.cleaned_data['feedback_type'] |
177
|
|
|
}) |
178
|
|
|
|
179
|
|
|
subject = "PythonExpress Feedback by %s" % ( |
180
|
|
|
form.cleaned_data['name']) |
181
|
|
|
text_body = loader.get_template( |
182
|
|
|
'email_messages/contactus/message.txt').render(email_context) |
183
|
|
|
email_body = loader.get_template( |
184
|
|
|
'email_messages/contactus/message.html').render(email_context) |
185
|
|
|
user_subject = '[PythonExpress] Feedback Received' |
186
|
|
|
user_text_body = loader.get_template( |
187
|
|
|
'email_messages/contactus/message_user.txt').render( |
188
|
|
|
email_context) |
189
|
|
|
user_email_body = loader.get_template( |
190
|
|
|
'email_messages/contactus/message_user.html').render( |
191
|
|
|
email_context) |
192
|
|
|
site_admins = [ |
193
|
|
|
email for name, email in settings.MANAGERS] # @UnusedVariable |
194
|
|
|
try: |
195
|
|
|
regional_lead = Profile.objects.filter( |
196
|
|
|
usertype__slug__in=['lead', 'admin']).values_list( |
197
|
|
|
'user__email', flat=True) |
198
|
|
|
regional_lead.extend(site_admins) |
199
|
|
|
send_email_to_list( |
200
|
|
|
subject, |
201
|
|
|
users_list=regional_lead, |
202
|
|
|
body=email_body, |
203
|
|
|
text_body=text_body) |
204
|
|
|
|
205
|
|
|
send_email_to_id( |
206
|
|
|
user_subject, |
207
|
|
|
body=user_email_body, |
208
|
|
|
email_id=form.cleaned_data['email'], |
209
|
|
|
text_body=user_text_body) |
210
|
|
|
except Exception as e: |
211
|
|
|
print(e) |
212
|
|
|
return HttpResponseRedirect('/thankyou') |
213
|
|
|
else: |
214
|
|
|
if request.user.is_authenticated(): |
215
|
|
|
form = ContactUsForm(initial={'name': request.user.first_name + |
216
|
|
|
" " + request.user.last_name, |
217
|
|
|
'email': request.user.email |
218
|
|
|
}) |
219
|
|
|
else: |
220
|
|
|
form = ContactUsForm() |
221
|
|
|
return render(request, 'contact.html', {'form': form}) |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
def partner_view(request): |
225
|
|
|
if request.method == 'POST': |
226
|
|
|
form = PartnerForm(request.POST) |
227
|
|
|
if form.is_valid(): |
228
|
|
|
email_context = Context({ |
229
|
|
|
'org_name': form.cleaned_data['org_name'], |
230
|
|
|
'org_url': form.cleaned_data['org_url'], |
231
|
|
|
'partner_type': form.cleaned_data['partner_type'], |
232
|
|
|
'description': form.cleaned_data['description'], |
233
|
|
|
'python_use': form.cleaned_data['python_use'], |
234
|
|
|
'comments': form.cleaned_data['comments'], |
235
|
|
|
'name': form.cleaned_data['name'], |
236
|
|
|
'email': form.cleaned_data['email'], |
237
|
|
|
'contact_name': form.cleaned_data['name'], |
238
|
|
|
'contact_email': form.cleaned_data['email'], |
239
|
|
|
'conatct_number': form.cleaned_data['contact_number'], |
240
|
|
|
|
241
|
|
|
}) |
242
|
|
|
|
243
|
|
|
subject = "[PythonExpress] Partnership request by %s" % ( |
244
|
|
|
form.cleaned_data['org_name']) |
245
|
|
|
text_body = loader.get_template( |
246
|
|
|
'email_messages/partner/partner_message.txt').render( |
247
|
|
|
email_context) |
248
|
|
|
email_body = loader.get_template( |
249
|
|
|
'email_messages/partner/partner_message.html').render( |
250
|
|
|
email_context) |
251
|
|
|
user_subject = '[PythonExpress] Partnership request Received' |
252
|
|
|
user_text_body = loader.get_template( |
253
|
|
|
'email_messages/partner/message_user.txt').render( |
254
|
|
|
email_context) |
255
|
|
|
user_email_body = loader.get_template( |
256
|
|
|
'email_messages/partner/message_user.html').render( |
257
|
|
|
email_context) |
258
|
|
|
|
259
|
|
|
try: |
260
|
|
|
send_email_to_id( |
261
|
|
|
subject, |
262
|
|
|
email_id='[email protected]', |
263
|
|
|
body=email_body, |
264
|
|
|
text_body=text_body) |
265
|
|
|
|
266
|
|
|
send_email_to_id( |
267
|
|
|
user_subject, |
268
|
|
|
body=user_email_body, |
269
|
|
|
email_id=form.cleaned_data['email'], |
270
|
|
|
text_body=user_text_body) |
271
|
|
|
except Exception as e: |
272
|
|
|
print(e) |
273
|
|
|
return HttpResponseRedirect('/thankyou') |
274
|
|
|
else: |
275
|
|
|
form = PartnerForm() |
276
|
|
|
return render(request, 'partner.html', {'form': form}) |
277
|
|
|
|
278
|
|
|
|
279
|
|
|
def account_deactivate(request, slug): |
280
|
|
|
user = request.user |
281
|
|
|
user.is_active = False |
282
|
|
|
user.save() |
283
|
|
|
logout(request) |
284
|
|
|
return HttpResponseRedirect('/') |
285
|
|
|
|