|
1
|
|
|
from django.contrib.auth.decorators import login_required |
|
2
|
|
|
from django.core.urlresolvers import reverse, reverse_lazy |
|
3
|
|
|
# from django.db.models import Q |
|
4
|
|
|
from django.contrib.sites.models import Site |
|
5
|
|
|
from django.http import HttpResponseRedirect |
|
6
|
|
|
from django.shortcuts import get_object_or_404 |
|
7
|
|
|
from django.shortcuts import redirect, render |
|
8
|
|
|
from django.template import loader |
|
9
|
|
|
from django.views import generic |
|
10
|
|
|
|
|
11
|
|
|
from braces import views |
|
12
|
|
|
from wye.base.emailer_html import send_email_to_list |
|
13
|
|
|
from wye.organisations.models import Organisation |
|
14
|
|
|
from wye.profiles.models import Profile |
|
15
|
|
|
# from wye.regions.models import RegionalLead |
|
16
|
|
|
from wye.base.constants import WorkshopStatus |
|
17
|
|
|
|
|
18
|
|
|
from wye.social.sites.twitter import send_tweet |
|
19
|
|
|
from wye.base.views import verify_user_profile |
|
20
|
|
|
from .forms import WorkshopForm, WorkshopEditForm, WorkshopFeedbackForm, WorkshopListForm |
|
21
|
|
|
from .mixins import ( |
|
22
|
|
|
WorkshopEmailMixin, |
|
23
|
|
|
WorkshopAccessMixin |
|
24
|
|
|
) |
|
25
|
|
|
from .models import Workshop |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
@login_required |
|
29
|
|
|
@verify_user_profile |
|
30
|
|
|
def workshop_list(request): |
|
31
|
|
|
template_name = 'workshops/workshop_list.html' |
|
32
|
|
|
user_profile, created = Profile.objects.get_or_create( |
|
33
|
|
|
user__id=request.user.id) |
|
34
|
|
|
if not user_profile.is_profile_filled: |
|
35
|
|
|
return redirect('profiles:profile-edit', slug=request.user.username) |
|
36
|
|
|
context_dict = {} |
|
37
|
|
|
workshop_list = Workshop.objects.all().order_by('-expected_date') |
|
38
|
|
|
workshop_list = workshop_list.filter( |
|
39
|
|
|
requester__location__id__in=[ |
|
40
|
|
|
x.id for x in request.user.profile.interested_locations.all()] |
|
41
|
|
|
) |
|
42
|
|
|
|
|
43
|
|
|
location_list = request.GET.getlist("location") |
|
44
|
|
|
if location_list: |
|
45
|
|
|
workshop_list = workshop_list.filter( |
|
46
|
|
|
location__id__in=location_list |
|
47
|
|
|
) |
|
48
|
|
|
|
|
49
|
|
|
presenter_list = request.GET.getlist("presenter") |
|
50
|
|
|
if presenter_list: |
|
51
|
|
|
workshop_list = workshop_list.filter( |
|
52
|
|
|
presenter__id__in=presenter_list |
|
53
|
|
|
) |
|
54
|
|
|
|
|
55
|
|
|
workshop_level_list = request.GET.getlist("level") |
|
56
|
|
|
if workshop_level_list: |
|
57
|
|
|
workshop_list = workshop_list.filter( |
|
58
|
|
|
workshop_level__in=workshop_level_list |
|
59
|
|
|
) |
|
60
|
|
|
|
|
61
|
|
|
workshop_section_list = request.GET.getlist("section") |
|
62
|
|
|
if workshop_section_list: |
|
63
|
|
|
workshop_list = workshop_list.filter( |
|
64
|
|
|
workshop_section__id__in=workshop_section_list |
|
65
|
|
|
) |
|
66
|
|
|
|
|
67
|
|
|
status_list = request.GET.getlist("status") |
|
68
|
|
|
if status_list: |
|
69
|
|
|
workshop_list = workshop_list.filter( |
|
70
|
|
|
status__in=status_list |
|
71
|
|
|
) |
|
72
|
|
|
|
|
73
|
|
|
context_dict['workshop_list'] = workshop_list |
|
74
|
|
|
context_dict['user'] = request.user |
|
75
|
|
|
# need to improve the part |
|
76
|
|
|
context_dict['is_not_tutor'] = False |
|
77
|
|
|
# as user can be tutor and regional lead hence we need to verify like |
|
78
|
|
|
# this |
|
79
|
|
|
if (Profile.is_regional_lead(request.user) or |
|
80
|
|
|
Profile.is_organiser(request.user) or |
|
81
|
|
|
Profile.is_admin(request.user)): |
|
82
|
|
|
context_dict['is_not_tutor'] = True |
|
83
|
|
|
context_dict['form'] = WorkshopListForm(user=request.user) |
|
84
|
|
|
|
|
85
|
|
|
return render(request, template_name, context_dict) |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
def workshop_details(request, pk): |
|
89
|
|
|
template_name = 'workshops/workshop_detail.html' |
|
90
|
|
|
workshop_obj = get_object_or_404(Workshop, id=pk) |
|
91
|
|
|
show_contact_flag = False |
|
92
|
|
|
display_edit_button = False |
|
93
|
|
|
user = request.user |
|
94
|
|
|
user_is_presenter = [u for u in workshop_obj.presenter.all() if user == u] |
|
95
|
|
|
user_is_requester = [ |
|
96
|
|
|
u for u in workshop_obj.requester.user.all() if user == u] |
|
97
|
|
|
if (user_is_presenter or user_is_requester or |
|
98
|
|
|
user.is_superuser or ((not user.is_anonymous()) and Profile.is_coordinator(user))): |
|
99
|
|
|
show_contact_flag = True |
|
100
|
|
|
if (user_is_presenter): |
|
101
|
|
|
display_edit_button = True |
|
102
|
|
|
|
|
103
|
|
|
context_dict = { |
|
104
|
|
|
'workshop': workshop_obj, |
|
105
|
|
|
'show_contact_flag': show_contact_flag, |
|
106
|
|
|
'display_edit_button': display_edit_button |
|
107
|
|
|
} |
|
108
|
|
|
return render(request, template_name, context_dict) |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
@login_required |
|
112
|
|
|
@verify_user_profile |
|
113
|
|
|
def workshop_create(request): |
|
114
|
|
|
template_name = 'workshops/workshop_create.html' |
|
115
|
|
|
context_dict = {} |
|
116
|
|
|
if not Organisation.list_user_organisations(request.user).exists(): |
|
117
|
|
|
msg = """ |
|
118
|
|
|
To request workshop you need to create organisaiton.\n\n |
|
119
|
|
|
Please use organisation tab above to create your organisation |
|
120
|
|
|
""" |
|
121
|
|
|
return render(request, 'error.html', {'message': msg}) |
|
122
|
|
|
if request.method == 'GET': |
|
123
|
|
|
form = WorkshopForm(user=request.user) |
|
124
|
|
|
context_dict['form'] = form |
|
125
|
|
|
return render(request, template_name, context_dict) |
|
126
|
|
|
form = WorkshopForm(user=request.user, data=request.POST) |
|
127
|
|
|
if not form.is_valid(): |
|
128
|
|
|
context_dict['form'] = form |
|
129
|
|
|
context_dict['errors'] = form.errors |
|
130
|
|
|
return render(request, template_name, context_dict) |
|
131
|
|
|
workshop = form.save() |
|
132
|
|
|
domain = Site.objects.get_current().domain |
|
133
|
|
|
context = { |
|
134
|
|
|
'workshop': workshop, |
|
135
|
|
|
'date': workshop.expected_date, |
|
136
|
|
|
'workshop_url': domain + '/workshop/{}/'.format(workshop.id) |
|
137
|
|
|
} |
|
138
|
|
|
# Collage POC and admin email |
|
139
|
|
|
poc_admin_user = Profile.get_user_with_type( |
|
140
|
|
|
user_type=['Collage POC', 'admin'] |
|
141
|
|
|
).values_list('email', flat=True) |
|
142
|
|
|
|
|
143
|
|
|
org_user_emails = workshop.requester.user.filter( |
|
144
|
|
|
is_active=True).values_list('email', flat=True) |
|
145
|
|
|
# all presenter if any |
|
146
|
|
|
all_presenter_email = workshop.presenter.values_list( |
|
147
|
|
|
'email', flat=True) |
|
148
|
|
|
# List of tutor who have shown interest in that location |
|
149
|
|
|
region_interested_member = Profile.objects.filter( |
|
150
|
|
|
interested_locations=workshop.requester.location, |
|
151
|
|
|
usertype__slug='tutor' |
|
152
|
|
|
).values_list('user__email', flat=True) |
|
153
|
|
|
all_email = [] |
|
154
|
|
|
all_email.extend(org_user_emails) |
|
155
|
|
|
all_email.extend(all_presenter_email) |
|
156
|
|
|
all_email.extend(poc_admin_user) |
|
157
|
|
|
all_email.extend(region_interested_member) |
|
158
|
|
|
all_email = set(all_email) |
|
159
|
|
|
send_tweet(context) |
|
160
|
|
|
|
|
161
|
|
|
subject = '[PythonExpress] Workshop request status.' |
|
162
|
|
|
email_body = loader.get_template( |
|
163
|
|
|
'email_messages/workshop/create_workshop/message.html').render(context) |
|
164
|
|
|
text_body = loader.get_template( |
|
165
|
|
|
'email_messages/workshop/create_workshop/message.txt').render(context) |
|
166
|
|
|
send_email_to_list( |
|
167
|
|
|
subject, |
|
168
|
|
|
body=email_body, |
|
169
|
|
|
users_list=all_email, |
|
170
|
|
|
text_body=text_body) |
|
171
|
|
|
success_url = reverse_lazy('workshops:workshop_list') |
|
172
|
|
|
return HttpResponseRedirect(success_url) |
|
173
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
class WorkshopUpdate(views.LoginRequiredMixin, WorkshopAccessMixin, |
|
176
|
|
|
generic.UpdateView): |
|
177
|
|
|
model = Workshop |
|
178
|
|
|
form_class = WorkshopEditForm |
|
179
|
|
|
template_name = 'workshops/workshop_update.html' |
|
180
|
|
|
|
|
181
|
|
|
def get_success_url(self): |
|
182
|
|
|
# pk = self.kwargs.get(self.pk_url_kwarg, None) |
|
183
|
|
|
self.success_url = reverse("workshops:workshop_list") |
|
184
|
|
|
return super(WorkshopUpdate, self).get_success_url() |
|
185
|
|
|
|
|
186
|
|
|
def get_initial(self): |
|
187
|
|
|
return { |
|
188
|
|
|
"requester": self.object.requester.name, |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
def get_form_kwargs(self): |
|
192
|
|
|
kwargs = super(WorkshopUpdate, self).get_form_kwargs() |
|
193
|
|
|
kwargs['request'] = self.request |
|
194
|
|
|
return kwargs |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
class WorkshopToggleActive(views.LoginRequiredMixin, views.CsrfExemptMixin, |
|
198
|
|
|
views.JSONResponseMixin, WorkshopAccessMixin, |
|
199
|
|
|
generic.UpdateView): |
|
200
|
|
|
model = Workshop |
|
201
|
|
|
fields = ('is_active', 'id') |
|
202
|
|
|
|
|
203
|
|
|
def post(self, request, *args, **kwargs): |
|
204
|
|
|
self.object = self.get_object() |
|
205
|
|
|
response = self.object.toggle_active(request.user, **kwargs) |
|
206
|
|
|
return self.render_json_response(response) |
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
class WorkshopAction(views.CsrfExemptMixin, views.LoginRequiredMixin, |
|
210
|
|
|
views.JSONResponseMixin, WorkshopEmailMixin, |
|
211
|
|
|
generic.UpdateView): |
|
212
|
|
|
|
|
213
|
|
|
model = Workshop |
|
214
|
|
|
email_dir = 'email_messages/workshop/assign_me/' |
|
215
|
|
|
|
|
216
|
|
|
def post(self, request, *args, **kwargs): |
|
217
|
|
|
self.object = self.get_object() |
|
218
|
|
|
response = self.object.manage_action(request.user, **kwargs) |
|
219
|
|
|
|
|
220
|
|
|
if response['status'] and response.get('notify') is not None: |
|
221
|
|
|
self.send_mail(request.user, response['assigned']) |
|
222
|
|
|
del response['notify'] |
|
223
|
|
|
return self.render_json_response(response) |
|
224
|
|
|
|
|
225
|
|
|
def send_mail(self, user, assigned): |
|
226
|
|
|
"""Send email to presenter and org users.""" |
|
227
|
|
|
|
|
228
|
|
|
workshop = self.object |
|
229
|
|
|
context = { |
|
230
|
|
|
'presenter': True, |
|
231
|
|
|
'assigned': assigned, |
|
232
|
|
|
'date': workshop.expected_date, |
|
233
|
|
|
'presenter_name': user.username, |
|
234
|
|
|
'workshop_organization': workshop.requester, |
|
235
|
|
|
'workshop_url': self.request.build_absolute_uri(reverse( |
|
236
|
|
|
'workshops:workshop_detail', args=[workshop.pk] |
|
237
|
|
|
)) |
|
238
|
|
|
} |
|
239
|
|
|
# email to presenter and group |
|
240
|
|
|
self.send_mail_to_presenter(user, context) |
|
241
|
|
|
context['presenter'] = False |
|
242
|
|
|
self.send_mail_to_group(context, exclude_emails=[user.email]) |
|
243
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
class WorkshopFeedbackView(views.LoginRequiredMixin, |
|
246
|
|
|
generic.FormView): |
|
247
|
|
|
form_class = WorkshopFeedbackForm |
|
248
|
|
|
template_name = "workshops/workshop_feedback.html" |
|
249
|
|
|
success_url = reverse_lazy('workshops:workshop_list') |
|
250
|
|
|
|
|
251
|
|
|
def form_valid(self, form): |
|
252
|
|
|
workshop_id = self.kwargs.get('pk') |
|
253
|
|
|
form.save(self.request.user, workshop_id) |
|
254
|
|
|
return super(WorkshopFeedbackView, self).form_valid(form) |
|
255
|
|
|
|
|
256
|
|
|
def get_context_data(self, *args, **kwargs): |
|
257
|
|
|
context = super(WorkshopFeedbackView, self).get_context_data( |
|
258
|
|
|
*args, **kwargs) |
|
259
|
|
|
context['workshop'] = Workshop.objects.get(pk=self.kwargs.get('pk')) |
|
260
|
|
|
return context |
|
261
|
|
|
|
|
262
|
|
|
|
|
263
|
|
|
def upcoming_workshops(request): |
|
264
|
|
|
template_name = 'upcoming.html' |
|
265
|
|
|
workshop_list = Workshop.objects.filter(is_active=True).filter( |
|
266
|
|
|
status__in=[WorkshopStatus.REQUESTED, |
|
267
|
|
|
WorkshopStatus.ACCEPTED]).order_by('expected_date') |
|
268
|
|
|
for workshop in workshop_list: |
|
269
|
|
|
print(workshop.presenter) |
|
270
|
|
|
context_dict = {} |
|
271
|
|
|
context_dict['workshop_list'] = workshop_list |
|
272
|
|
|
|
|
273
|
|
|
return render(request, template_name, context_dict) |
|
274
|
|
|
|