1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
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 |
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 wye.base.constants import WorkshopStatus |
12
|
|
|
from wye.base.emailer_html import send_email_to_id, send_email_to_list |
13
|
|
|
from wye.organisations.models import Organisation |
14
|
|
|
from wye.profiles.models import Profile |
15
|
|
|
from wye.workshops.models import Workshop |
16
|
|
|
|
17
|
|
|
from .forms import ContactUsForm, UserProfileForm |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def profile_view(request, slug): |
21
|
|
|
try: |
22
|
|
|
p = Profile.objects.get(user__username=slug) |
23
|
|
|
except Profile.DoesNotExist: |
24
|
|
|
return render(request, 'error.html', { |
25
|
|
|
"message": "Profile does not exist"}) |
26
|
|
|
return render(request, 'profile/index.html', {'object': p}) |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class UserDashboard(ListView): |
30
|
|
|
model = Profile |
31
|
|
|
template_name = 'profile/dashboard.html' |
32
|
|
|
|
33
|
|
|
def dispatch(self, request, *args, **kwargs): |
34
|
|
|
user_profile = Profile.objects.get( |
35
|
|
|
user__id=self.request.user.id) |
36
|
|
|
if not user_profile.get_user_type: |
37
|
|
|
return redirect('profiles:profile-edit', |
38
|
|
|
slug=request.user.username) |
39
|
|
|
return super(UserDashboard, self).dispatch(request, *args, **kwargs) |
40
|
|
|
|
41
|
|
|
def get_context_data(self, **kwargs): |
42
|
|
|
context = super(UserDashboard, self).get_context_data(**kwargs) |
43
|
|
|
user_profile = Profile.objects.get(user=self.request.user) |
44
|
|
|
workshop_all = Workshop.objects.all() |
45
|
|
|
accept_workshops = workshop_all.filter( |
46
|
|
|
status=WorkshopStatus.ACCEPTED) |
47
|
|
|
requested_workshops = workshop_all.filter( |
48
|
|
|
status=WorkshopStatus.REQUESTED) |
49
|
|
|
organisation_all = Organisation.objects.all() |
50
|
|
|
for each_type in user_profile.get_user_type: |
51
|
|
|
if each_type == 'tutor': |
52
|
|
|
context['is_tutor'] = True |
53
|
|
|
context['workshop_requested_tutor'] = accept_workshops.filter( |
54
|
|
|
presenter=self.request.user) |
55
|
|
|
context['workshop_completed_tutor'] = requested_workshops.filter( |
56
|
|
|
presenter=self.request.user) |
57
|
|
|
if each_type == 'lead': |
58
|
|
|
context['is_regional_lead'] = True |
59
|
|
|
context['workshops_accepted_under_rl'] = accept_workshops |
60
|
|
|
context['workshops_pending_under_rl'] = requested_workshops |
61
|
|
|
context['interested_tutors'] = Profile.objects.filter( |
62
|
|
|
usertype__slug='tutor', |
63
|
|
|
interested_locations__name__in=user_profile.get_interested_locations).exclude( |
64
|
|
|
user=self.request.user).count() |
65
|
|
|
context['interested_locations'] = organisation_all.filter( |
66
|
|
|
location__name__in=user_profile.get_interested_locations).count() |
67
|
|
|
|
68
|
|
|
if each_type == 'poc': |
69
|
|
|
context['is_college_poc'] = True |
70
|
|
|
context['users_organisation'] = organisation_all.filter( |
71
|
|
|
user=self.request.user) |
72
|
|
|
context['workshop_requested_under_poc'] = workshop_all.filter( |
73
|
|
|
requester__id__in=organisation_all.values_list( |
74
|
|
|
'id', flat=True)) |
75
|
|
|
context['workshops_accepted_under_poc'] = workshop_all.filter( |
76
|
|
|
status=WorkshopStatus.ACCEPTED, |
77
|
|
|
requester__id__in=organisation_all.values_list( |
78
|
|
|
'id', flat=True)) |
79
|
|
|
|
80
|
|
|
if each_type == 'admin': |
81
|
|
|
context['is_admin'] = True |
82
|
|
|
context['workshops_by_status'] = workshop_all.order_by( |
83
|
|
|
'status') |
84
|
|
|
context['workshops_by_region'] = workshop_all.order_by( |
85
|
|
|
'location') |
86
|
|
|
|
87
|
|
|
return context |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
class ProfileEditView(UpdateView): |
91
|
|
|
model = Profile |
92
|
|
|
template_name = 'profile/update.html' |
93
|
|
|
form_class = UserProfileForm |
94
|
|
|
slug_field = 'user__username' |
95
|
|
|
|
96
|
|
|
def form_valid(self, form): |
97
|
|
|
return super(ProfileEditView, self).form_valid(form) |
98
|
|
|
|
99
|
|
|
def get_success_url(self): |
100
|
|
|
return reverse('profiles:profile-page', kwargs={'slug': self.object.slug}) |
101
|
|
|
|
102
|
|
|
def dispatch(self, *args, **kwargs): |
103
|
|
|
if self.request.user.pk == self.get_object().pk: |
104
|
|
|
return super(ProfileEditView, self).dispatch(*args, **kwargs) |
105
|
|
|
else: |
106
|
|
|
raise PermissionDenied |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
def contact(request): |
110
|
|
|
if request.method == 'POST': |
111
|
|
|
form = ContactUsForm(request.POST) |
112
|
|
|
if form.is_valid(): |
113
|
|
|
email_context = Context({ |
114
|
|
|
'contact_name': form.cleaned_data['name'], |
115
|
|
|
'contact_email': form.cleaned_data['email'], |
116
|
|
|
'comments': form.cleaned_data['comments'], |
117
|
|
|
'conatct_number': form.cleaned_data['contact_number'], |
118
|
|
|
'feedback_type': form.cleaned_data['feedback_type'] |
119
|
|
|
}) |
120
|
|
|
|
121
|
|
|
subject = "PythonExpress Feedback by %s" % ( |
122
|
|
|
form.cleaned_data['name']) |
123
|
|
|
text_body = loader.get_template( |
124
|
|
|
'email_messages/contactus/message.txt').render(email_context) |
125
|
|
|
email_body = loader.get_template( |
126
|
|
|
'email_messages/contactus/message.html').render(email_context) |
127
|
|
|
user_subject = '[PythonExpress] Feedback Received' |
128
|
|
|
user_text_body = loader.get_template( |
129
|
|
|
'email_messages/contactus/message_user.txt').render(email_context) |
130
|
|
|
user_email_body = loader.get_template( |
131
|
|
|
'email_messages/contactus/message_user.html').render(email_context) |
132
|
|
|
|
133
|
|
|
try: |
134
|
|
|
regional_lead = Profile.objects.filter( |
135
|
|
|
usertype__slug__in=['lead', 'admin']).values_list( |
136
|
|
|
'user__email', flat=True) |
137
|
|
|
send_email_to_list( |
138
|
|
|
subject, |
139
|
|
|
users_list=regional_lead, |
140
|
|
|
body=email_body, |
141
|
|
|
text_body=text_body) |
142
|
|
|
|
143
|
|
|
send_email_to_id( |
144
|
|
|
user_subject, |
145
|
|
|
body=user_email_body, |
146
|
|
|
email_id=form.cleaned_data['email'], |
147
|
|
|
text_body=user_text_body) |
148
|
|
|
except Exception as e: |
149
|
|
|
print(e) |
150
|
|
|
return HttpResponseRedirect('/thankyou') |
151
|
|
|
else: |
152
|
|
|
form = ContactUsForm() |
153
|
|
|
return render(request, 'contact.html', {'form': form}) |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
def account_deactivate(request, slug): |
157
|
|
|
user = request.user |
158
|
|
|
user.is_active = False |
159
|
|
|
user.save() |
160
|
|
|
logout(request) |
161
|
|
|
return HttpResponseRedirect('/') |
162
|
|
|
|