1
|
|
|
from django.conf import settings |
2
|
|
|
from django.core.urlresolvers import reverse_lazy |
3
|
|
|
from django.http.response import HttpResponseRedirect |
4
|
|
|
from django.shortcuts import redirect, render |
5
|
|
|
from django.template import Context, loader |
6
|
|
|
from django.views import generic |
7
|
|
|
|
8
|
|
|
from braces import views |
9
|
|
|
from wye.base.emailer_html import send_email_to_id, send_email_to_list |
10
|
|
|
from wye.profiles.models import Profile |
11
|
|
|
from wye.regions.models import RegionalLead |
12
|
|
|
from .forms import OrganisationForm |
13
|
|
|
from .models import Organisation |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class OrganisationList(views.LoginRequiredMixin, generic.ListView): |
17
|
|
|
model = Organisation |
18
|
|
|
template_name = 'organisation/list.html' |
19
|
|
|
|
20
|
|
|
def dispatch(self, request, *args, **kwargs): |
21
|
|
|
user_profile, created = Profile.objects.get_or_create( |
22
|
|
|
user__id=self.request.user.id) |
23
|
|
|
if not user_profile.get_user_type: |
24
|
|
|
return redirect('profiles:profile-edit', slug=request.user.username) |
25
|
|
|
return super(OrganisationList, self).dispatch(request, *args, **kwargs) |
26
|
|
|
|
27
|
|
|
def get_queryset(self): |
28
|
|
|
return Organisation.objects.filter(active=True) |
29
|
|
|
|
30
|
|
|
def get_context_data(self, *args, **kwargs): |
31
|
|
|
context = super(OrganisationList, self).get_context_data( |
32
|
|
|
*args, **kwargs) |
33
|
|
|
if Profile.is_organiser(self.request.user): |
34
|
|
|
context['org_created_list'] = self.get_queryset().filter( |
35
|
|
|
created_by=self.request.user) |
36
|
|
|
context['org_belongs_list'] = self.get_queryset().exclude( |
37
|
|
|
created_by=self.request.user).filter( |
38
|
|
|
user=self.request.user) |
39
|
|
|
elif Profile.is_regional_lead(self.request.user): |
40
|
|
|
regions = RegionalLead.objects.filter(leads=self.request.user) |
41
|
|
|
context['regional_org_list'] = self.get_queryset().filter( |
42
|
|
|
location__id__in=[x.location.id for x in regions]) |
43
|
|
|
# elif Profile.is_presenter(self.request.user): |
44
|
|
|
# pass |
45
|
|
|
context['user'] = self.request.user |
46
|
|
|
# need to improve the part |
47
|
|
|
context['is_not_tutor'] = False |
48
|
|
|
# as user can be tutor and regional lead hence we need to verify like |
49
|
|
|
# this |
50
|
|
|
if (Profile.is_regional_lead(self.request.user) or |
51
|
|
|
Profile.is_organiser(self.request.user) or |
52
|
|
|
Profile.is_admin(self.request.user)): |
53
|
|
|
context['is_not_tutor'] = True |
54
|
|
|
return context |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
class OrganisationCreate(views.LoginRequiredMixin, generic.CreateView): |
58
|
|
|
model = Organisation |
59
|
|
|
form_class = OrganisationForm |
60
|
|
|
template_name = 'organisation/create.html' |
61
|
|
|
success_url = reverse_lazy('organisations:organisation_list') |
62
|
|
|
|
63
|
|
|
# def get_queryset(self): |
64
|
|
|
# return Organisation.objects.filter(user=self.request.user) |
65
|
|
|
|
66
|
|
|
def post(self, request, *args, **kwargs): |
67
|
|
|
form = OrganisationForm(data=request.POST) |
68
|
|
|
if form.is_valid(): |
69
|
|
|
form.instance.modified_by = request.user |
70
|
|
|
form.instance.created_by = request.user |
71
|
|
|
form.instance.save() |
72
|
|
|
form.instance.user.add(request.user) |
73
|
|
|
form.instance.save() |
74
|
|
|
host = '{}://{}'.format(settings.SITE_PROTOCOL, |
75
|
|
|
request.META['HTTP_HOST']) |
76
|
|
|
email_context = Context({ |
77
|
|
|
'full_name': '%s %s' % (request.user.first_name, |
78
|
|
|
request.user.last_name), |
79
|
|
|
'org_id': form.instance.id, |
80
|
|
|
'host': host |
81
|
|
|
|
82
|
|
|
}) |
83
|
|
|
subject = "%s organisation for region %s is created" % ( |
84
|
|
|
form.instance.name, form.instance.location.name) |
85
|
|
|
email_body = loader.get_template( |
86
|
|
|
'email_messages/organisation/new.html').render(email_context) |
87
|
|
|
text_body = loader.get_template( |
88
|
|
|
'email_messages/organisation/new.txt').render(email_context) |
89
|
|
|
|
90
|
|
|
regional_lead = Profile.objects.filter( |
91
|
|
|
interested_locations=form.instance.location, |
92
|
|
|
usertype__slug='lead').values_list('user__email', flat=True) |
93
|
|
|
send_email_to_id(subject, |
94
|
|
|
body=email_body, |
95
|
|
|
email_id=request.user.email, |
96
|
|
|
text_body=text_body) |
97
|
|
|
|
98
|
|
|
send_email_to_list(subject, |
99
|
|
|
body=email_body, |
100
|
|
|
users_list=regional_lead, |
101
|
|
|
text_body=text_body) |
102
|
|
|
|
103
|
|
|
return HttpResponseRedirect(self.success_url) |
104
|
|
|
else: |
105
|
|
|
return render(request, self.template_name, {'form': form}) |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
class OrganisationDetail(views.LoginRequiredMixin, generic.DetailView): |
109
|
|
|
model = Organisation |
110
|
|
|
template_name = 'organisation/detail.html' |
111
|
|
|
success_url = reverse_lazy('organisations:organisation_list') |
112
|
|
|
|
113
|
|
|
def get_queryset(self): |
114
|
|
|
return Organisation.objects.filter( |
115
|
|
|
user=self.request.user, |
116
|
|
|
id=self.kwargs['pk']) |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
class OrganisationUpdate(views.LoginRequiredMixin, generic.UpdateView): |
120
|
|
|
model = Organisation |
121
|
|
|
form_class = OrganisationForm |
122
|
|
|
template_name = 'organisation/edit.html' |
123
|
|
|
success_url = reverse_lazy('organisations:organisation_list') |
124
|
|
|
|
125
|
|
|
def get_object(self, queryset=None): |
126
|
|
|
return Organisation.objects.get(user=self.request.user, id=self.kwargs['pk']) |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
class OrganisationDeactive(views.CsrfExemptMixin, |
130
|
|
|
views.LoginRequiredMixin, |
131
|
|
|
views.JSONResponseMixin, |
132
|
|
|
generic.UpdateView): |
133
|
|
|
model = Organisation |
134
|
|
|
fields = ('active', 'id') |
135
|
|
|
|
136
|
|
|
def post(self, request, *args, **kwargs): |
137
|
|
|
self.object = self.get_object() |
138
|
|
|
response = self.object.toggle_active(request.user, **kwargs) |
139
|
|
|
return self.render_json_response(response) |
140
|
|
|
|