|
1
|
|
|
# from django.contrib import messages |
|
2
|
|
|
# from django.core.exceptions import PermissionDenied |
|
3
|
|
|
# from django.core.urlresolvers import reverse |
|
4
|
|
|
# from django.http import Http404 |
|
5
|
|
|
from django.http import HttpResponseForbidden |
|
6
|
|
|
# from django.http import HttpResponseRedirect, JsonResponse |
|
7
|
|
|
# from django.shortcuts import render |
|
8
|
|
|
|
|
9
|
|
|
# from wye.base.constants import WorkshopStatus, FeedbackType |
|
10
|
|
|
from wye.base.emailer import send_mail |
|
11
|
|
|
# from wye.organisations.models import Organisation |
|
12
|
|
|
from wye.profiles.models import Profile |
|
13
|
|
|
from wye.regions.models import RegionalLead |
|
14
|
|
|
|
|
15
|
|
|
from .models import Workshop |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
class WorkshopAccessMixin(object): |
|
19
|
|
|
|
|
20
|
|
|
def dispatch(self, request, *args, **kwargs): |
|
21
|
|
|
user = request.user |
|
22
|
|
|
pk = self.kwargs.get(self.pk_url_kwarg, None) |
|
23
|
|
|
workshop = Workshop.objects.get(id=pk) |
|
24
|
|
|
is_admin = Profile.is_admin(user) |
|
25
|
|
|
is_lead = (Profile.is_regional_lead(user) and |
|
26
|
|
|
RegionalLead.is_regional_lead(user, workshop.location)) |
|
27
|
|
|
is_organiser = (Profile.is_organiser(user) and |
|
28
|
|
|
user in workshop.requester.user.all()) |
|
29
|
|
|
|
|
30
|
|
|
if not (is_admin or is_lead or is_organiser): |
|
31
|
|
|
return HttpResponseForbidden("Not sufficent permission") |
|
32
|
|
|
return super(WorkshopAccessMixin, self).dispatch( |
|
33
|
|
|
request, *args, **kwargs) |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
# class WorkshopFeedBackMixin(object): |
|
37
|
|
|
# """ |
|
38
|
|
|
# Restrict access to feedback url if |
|
39
|
|
|
# - Workshop is not completed |
|
40
|
|
|
# - If the user accessing the url is not presenter or |
|
41
|
|
|
# organiser |
|
42
|
|
|
# """ |
|
43
|
|
|
|
|
44
|
|
|
# def dispatch(self, request, *args, **kwargs): |
|
45
|
|
|
# pk = self.kwargs.get('pk') |
|
46
|
|
|
# workshop = Workshop.objects.get(id=pk) |
|
47
|
|
|
# user = self.request.user |
|
48
|
|
|
|
|
49
|
|
|
# if workshop.status != WorkshopStatus.COMPLETED: |
|
50
|
|
|
# raise Http404 |
|
51
|
|
|
# if not (workshop.is_presenter(user) or workshop.is_organiser(user)): |
|
52
|
|
|
# raise PermissionDenied |
|
53
|
|
|
# return super(WorkshopFeedBackMixin, self).dispatch(request, *args, **kwargs) |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
# class WorkshopRestrictMixin(object): |
|
57
|
|
|
# """ |
|
58
|
|
|
# Mixin to restrict |
|
59
|
|
|
# - For organisation to add workshop if no feedback is shared. |
|
60
|
|
|
# - For presenter to takeup workshop if no feedback is shared |
|
61
|
|
|
# """ |
|
62
|
|
|
|
|
63
|
|
|
# allow_presenter = False |
|
64
|
|
|
|
|
65
|
|
|
# def dispatch(self, request, *args, **kwargs): |
|
66
|
|
|
# self.user = request.user |
|
67
|
|
|
# self.feedback_required = [] |
|
68
|
|
|
|
|
69
|
|
|
# # check if user is tutor |
|
70
|
|
|
# if Profile.is_presenter(self.user) and self.allow_presenter: |
|
71
|
|
|
# self.validate_presenter_feedback() |
|
72
|
|
|
# elif (Profile.is_organiser(self.user) and |
|
73
|
|
|
# Organisation.list_user_organisations(self.user).exists()): |
|
74
|
|
|
# # if user is from organisation |
|
75
|
|
|
# self.validate_organisation_feedback() |
|
76
|
|
|
# elif (Profile.is_regional_lead(self.user) or |
|
77
|
|
|
# Profile.is_admin(self.user)): |
|
78
|
|
|
# pass # don't restrict lead and admin |
|
79
|
|
|
# else: |
|
80
|
|
|
# msg = """ |
|
81
|
|
|
# To request workshop you need to create organisaiton.\n\n |
|
82
|
|
|
# Please use organisation tab above to create your organisation""" |
|
83
|
|
|
|
|
84
|
|
|
# # return json for ajax request |
|
85
|
|
|
# return render(request, 'error.html', {'message': msg}) |
|
86
|
|
|
|
|
87
|
|
|
# if self.feedback_required: |
|
88
|
|
|
# return self.return_response(request) |
|
89
|
|
|
# return super(WorkshopRestrictMixin, self).dispatch(request, *args, **kwargs) |
|
90
|
|
|
|
|
91
|
|
|
# def validate_presenter_feedback(self): |
|
92
|
|
|
# workshops = Workshop.objects.filter( |
|
93
|
|
|
# presenter=self.user, status=WorkshopStatus.COMPLETED) |
|
94
|
|
|
|
|
95
|
|
|
# for workshop in workshops: |
|
96
|
|
|
# feedback = WorkshopFeedBack.objects.filter( |
|
97
|
|
|
# workshop=workshop, feedback_type=FeedbackType.PRESENTER |
|
98
|
|
|
# ).count() |
|
99
|
|
|
# if feedback == 0: |
|
100
|
|
|
# self.feedback_required.append(workshop) |
|
101
|
|
|
|
|
102
|
|
|
# def validate_organisation_feedback(self): |
|
103
|
|
|
# workshops = Workshop.objects.filter( |
|
104
|
|
|
# requester__user=self.user, status=WorkshopStatus.COMPLETED) |
|
105
|
|
|
|
|
106
|
|
|
# for workshop in workshops: |
|
107
|
|
|
# feedback = WorkshopFeedBack.objects.filter( |
|
108
|
|
|
# workshop=workshop, feedback_type=FeedbackType.ORGANISATION |
|
109
|
|
|
# ).count() |
|
110
|
|
|
# if feedback == 0: |
|
111
|
|
|
# self.feedback_required.append(workshop) |
|
112
|
|
|
|
|
113
|
|
|
# def return_response(self, request): |
|
114
|
|
|
# msg = "Please complete the feeback for %s" % ( |
|
115
|
|
|
# ", ".join(map(str, self.feedback_required))) |
|
116
|
|
|
|
|
117
|
|
|
# # return json for ajax request |
|
118
|
|
|
# if request.is_ajax(): |
|
119
|
|
|
# return JsonResponse({"status": False, "msg": msg}) |
|
120
|
|
|
|
|
121
|
|
|
# messages.error(request, msg) |
|
122
|
|
|
# return HttpResponseRedirect(reverse('workshops:workshop_list')) |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
class WorkshopEmailMixin(object): |
|
126
|
|
|
|
|
127
|
|
|
def send_mail_to_presenter(self, user, context): |
|
128
|
|
|
""" |
|
129
|
|
|
Send email to presenter. |
|
130
|
|
|
@param user: Is user object |
|
131
|
|
|
@param context: Is dict of data required by email template. |
|
132
|
|
|
""" |
|
133
|
|
|
|
|
134
|
|
|
# Send email to presenter |
|
135
|
|
|
return send_mail([user.email], context, self.email_dir) |
|
136
|
|
|
|
|
137
|
|
|
def send_mail_to_group(self, context, exclude_emails=None): |
|
138
|
|
|
""" |
|
139
|
|
|
Send email to org/group users. |
|
140
|
|
|
@param context: Is dict of data required by email template. |
|
141
|
|
|
@exclude_emails: Is list of email to be excluded from |
|
142
|
|
|
email update. |
|
143
|
|
|
""" |
|
144
|
|
|
|
|
145
|
|
|
if exclude_emails is None: |
|
146
|
|
|
exclude_emails = [] |
|
147
|
|
|
|
|
148
|
|
|
# Collage POC and admin email |
|
149
|
|
|
poc_admin_user = Profile.get_user_with_type( |
|
150
|
|
|
user_type=['Collage POC', 'admin'] |
|
151
|
|
|
).values_list('email', flat=True) |
|
152
|
|
|
# Org user email |
|
153
|
|
|
org_user_emails = self.object.requester.user.filter( |
|
154
|
|
|
is_active=True |
|
155
|
|
|
).values_list('email', flat=True) |
|
156
|
|
|
# all presenter if any |
|
157
|
|
|
all_presenter_email = self.object.presenter.values_list( |
|
158
|
|
|
'email', flat=True |
|
159
|
|
|
) |
|
160
|
|
|
# List of tutor who have shown interest in that location |
|
161
|
|
|
region_interested_member = Profile.objects.filter( |
|
162
|
|
|
interested_locations=self.object.requester.location, |
|
163
|
|
|
usertype__slug='tutor' |
|
164
|
|
|
).values_list('user__email', flat=True) |
|
165
|
|
|
|
|
166
|
|
|
all_email = [] |
|
167
|
|
|
all_email.extend(org_user_emails) |
|
168
|
|
|
all_email.extend(all_presenter_email) |
|
169
|
|
|
all_email.extend(poc_admin_user) |
|
170
|
|
|
all_email.extend(region_interested_member) |
|
171
|
|
|
all_email = set(all_email) |
|
172
|
|
|
all_email = list(all_email.difference(exclude_emails)) |
|
173
|
|
|
send_mail(all_email, context, self.email_dir) |
|
174
|
|
|
|