|
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 WorkshopEmailMixin(object): |
|
37
|
|
|
|
|
38
|
|
|
def send_mail_to_presenter(self, user, context): |
|
39
|
|
|
""" |
|
40
|
|
|
Send email to presenter. |
|
41
|
|
|
@param user: Is user object |
|
42
|
|
|
@param context: Is dict of data required by email template. |
|
43
|
|
|
""" |
|
44
|
|
|
|
|
45
|
|
|
# Send email to presenter |
|
46
|
|
|
return send_mail([user.email], context, self.email_dir) |
|
47
|
|
|
|
|
48
|
|
|
def send_mail_to_group(self, context, exclude_emails=None): |
|
49
|
|
|
""" |
|
50
|
|
|
Send email to org/group users. |
|
51
|
|
|
@param context: Is dict of data required by email template. |
|
52
|
|
|
@exclude_emails: Is list of email to be excluded from |
|
53
|
|
|
email update. |
|
54
|
|
|
""" |
|
55
|
|
|
|
|
56
|
|
|
if exclude_emails is None: |
|
57
|
|
|
exclude_emails = [] |
|
58
|
|
|
|
|
59
|
|
|
# Collage POC and admin email |
|
60
|
|
|
poc_admin_user = Profile.get_user_with_type( |
|
61
|
|
|
user_type=['Collage POC', 'admin'] |
|
62
|
|
|
).values_list('email', flat=True) |
|
63
|
|
|
# Org user email |
|
64
|
|
|
org_user_emails = self.object.requester.user.filter( |
|
65
|
|
|
is_active=True |
|
66
|
|
|
).values_list('email', flat=True) |
|
67
|
|
|
# all presenter if any |
|
68
|
|
|
all_presenter_email = self.object.presenter.values_list( |
|
69
|
|
|
'email', flat=True |
|
70
|
|
|
) |
|
71
|
|
|
# List of tutor who have shown interest in that location |
|
72
|
|
|
region_interested_member = Profile.objects.filter( |
|
73
|
|
|
interested_locations=self.object.requester.location, |
|
74
|
|
|
usertype__slug='tutor' |
|
75
|
|
|
).values_list('user__email', flat=True) |
|
76
|
|
|
|
|
77
|
|
|
all_email = [] |
|
78
|
|
|
all_email.extend(org_user_emails) |
|
79
|
|
|
all_email.extend(all_presenter_email) |
|
80
|
|
|
all_email.extend(poc_admin_user) |
|
81
|
|
|
all_email.extend(region_interested_member) |
|
82
|
|
|
all_email = set(all_email) |
|
83
|
|
|
all_email = list(all_email.difference(exclude_emails)) |
|
84
|
|
|
send_mail(all_email, context, self.email_dir) |
|
85
|
|
|
|