views.py
8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# encoding: utf-8
from collections import OrderedDict
from django.conf import settings
from django.contrib import messages
from django.db import transaction
from django.db.models import Count
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext as _
from django.shortcuts import render, redirect, get_object_or_404
from django.core.urlresolvers import reverse
from django.core.exceptions import PermissionDenied
from django.views.generic import DetailView, UpdateView, TemplateView
from conversejs import xmpp
from conversejs.models import XMPPAccount
from colab.super_archives.models import (EmailAddress, Message,
EmailAddressValidation)
from colab.search.utils import get_collaboration_data
from .forms import (UserCreationForm, UserForm, ListsForm,
UserUpdateForm, ChangeXMPPPasswordForm)
from .utils import mailman
class LoginView(TemplateView):
template_name = "accounts/login.html"
class UserProfileBaseMixin(object):
model = get_user_model()
slug_field = 'username'
slug_url_kwarg = 'username'
context_object_name = 'user_'
class UserProfileUpdateView(UserProfileBaseMixin, UpdateView):
template_name = 'accounts/user_update_form.html'
form_class = UserUpdateForm
def get_success_url(self):
return reverse('user_profile', kwargs={'username':
self.object.username})
def get_object(self, *args, **kwargs):
obj = super(UserProfileUpdateView, self).get_object(*args, **kwargs)
if self.request.user != obj and not self.request.user.is_superuser:
raise PermissionDenied
return obj
def get_context_data(self, **kwargs):
context = super(UserProfileUpdateView, self).get_context_data(**kwargs)
context['CONVERSEJS_ENABLED'] = getattr(settings, 'CONVERSEJS_ENABLED')
return context
class UserProfileDetailView(UserProfileBaseMixin, DetailView):
template_name = 'accounts/user_detail.html'
def get_context_data(self, **kwargs):
user = self.object
context = {}
count_types = OrderedDict()
# TODO: remove when mailman becomes a proxied plugin
messages = Message.objects.filter(from_address__user__pk=user.pk)
count_types[_('Emails')] = messages.count()
collaborations, count_types_extras = get_collaboration_data(user)
collaborations.extend(messages)
collaborations.sort(key=lambda elem: elem.modified, reverse=True)
count_types.update(count_types_extras)
context['type_count'] = count_types
context['results'] = collaborations[:10]
email_pks = [addr.pk for addr in user.emails.iterator()]
query = Message.objects.filter(from_address__in=email_pks)
query = query.order_by('-received_time')
context['emails'] = query[:10]
count_by = 'thread__mailinglist__name'
context['list_activity'] = dict(messages.values_list(count_by)
.annotate(Count(count_by))
.order_by(count_by))
context.update(kwargs)
return super(UserProfileDetailView, self).get_context_data(**context)
def signup(request):
BROWSERID_ENABLED = getattr(settings, 'BROWSERID_ENABLED', False)
if BROWSERID_ENABLED:
# If the user is not authenticated, redirect to login
if not request.user.is_authenticated():
return redirect('login')
if request.user.is_authenticated():
# If the user doesn't need to update its main data,
# redirect to its profile
# It happens when user is created by browserid
# and didn't set his/her main data
if not request.user.needs_update:
return redirect('user_profile', username=request.user.username)
# If the user is authenticated in Persona, but not in the Colab then he
# will be redirected to the register form.
if request.method == 'GET':
if BROWSERID_ENABLED:
user_form = UserForm()
else:
user_form = UserCreationForm()
lists_form = ListsForm()
return render(request, 'accounts/user_create_form.html',
{'user_form': user_form, 'lists_form': lists_form})
if BROWSERID_ENABLED:
user_form = UserForm(request.POST, instance=request.user)
else:
user_form = UserCreationForm(request.POST)
lists_form = ListsForm(request.POST)
if not user_form.is_valid() or not lists_form.is_valid():
return render(request, 'accounts/user_create_form.html',
{'user_form': user_form, 'lists_form': lists_form})
user = user_form.save(commit=False)
user.needs_update = False
if not BROWSERID_ENABLED:
user.is_active = False
user.save()
EmailAddressValidation.create(user.email, user)
else:
user.save()
# Check if the user's email have been used previously
# in the mainling lists to link the user to old messages
email_addr, created = EmailAddress.objects.get_or_create(
address=user.email)
if created:
email_addr.real_name = user.get_full_name()
email_addr.user = user
email_addr.save()
mailing_lists = lists_form.cleaned_data.get('lists')
mailman.update_subscription(user.email, mailing_lists)
messages.success(request, _('Your profile has been created!'))
return redirect('user_profile', username=user.username)
class ManageUserSubscriptionsView(UserProfileBaseMixin, DetailView):
http_method_names = [u'get', u'post']
template_name = u'accounts/manage_subscriptions.html'
def get_object(self, *args, **kwargs):
obj = super(ManageUserSubscriptionsView, self).get_object(*args,
**kwargs)
if self.request.user != obj and not self.request.user.is_superuser:
raise PermissionDenied
return obj
def post(self, request, *args, **kwargs):
user = self.get_object()
for email in user.emails.values_list('address', flat=True):
lists = self.request.POST.getlist(email)
user.update_subscription(email, lists)
return redirect('user_profile', username=user.username)
def get_context_data(self, **kwargs):
context = {}
context['membership'] = {}
user = self.get_object()
emails = user.emails.values_list('address', flat=True)
all_lists = mailman.all_lists(description=True)
for email in emails:
lists = []
lists_for_address = mailman.address_lists(email)
for listname, description in all_lists:
if listname in lists_for_address:
checked = True
else:
checked = False
lists.append((
{'listname': listname, 'description': description},
checked
))
context['membership'].update({email: lists})
context.update(kwargs)
return super(ManageUserSubscriptionsView,
self).get_context_data(**context)
class ChangeXMPPPasswordView(UpdateView):
model = XMPPAccount
form_class = ChangeXMPPPasswordForm
fields = ['password', ]
template_name = 'accounts/change_password.html'
def get_success_url(self):
return reverse('user_profile', kwargs={
'username': self.request.user.username
})
def get_object(self, queryset=None):
obj = get_object_or_404(XMPPAccount, user=self.request.user.pk)
self.old_password = obj.password
return obj
def form_valid(self, form):
transaction.set_autocommit(False)
response = super(ChangeXMPPPasswordView, self).form_valid(form)
changed = xmpp.change_password(
self.object.jid,
self.old_password,
form.cleaned_data['password1']
)
if not changed:
messages.error(
self.request,
_(u'Could not change your password. Please, try again later.')
)
transaction.rollback()
return response
else:
transaction.commit()
messages.success(
self.request,
_("You've changed your password successfully!")
)
return response
def password_changed(request):
messages.success(request, _('Your password was changed.'))
user = request.user
return redirect('user_profile_update', username=user.username)
def password_reset_done_custom(request):
msg = _(("We've emailed you instructions for setting "
"your password. You should be receiving them shortly."))
messages.success(request, msg)
return redirect('home')
def password_reset_complete_custom(request):
msg = _('Your password has been set. You may go ahead and log in now.')
messages.success(request, msg)
return redirect('home')