views.py
5.5 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
#!/usr/bin/env python
# encoding: utf-8
import datetime
from collections import OrderedDict
from django.contrib import messages
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
from django.core.urlresolvers import reverse
from django.core.exceptions import PermissionDenied
from django.views.generic import DetailView, UpdateView
from haystack.query import SearchQuerySet
from super_archives.models import EmailAddress, Message
from super_archives.utils.email import send_email_lists
from search.utils import trans
from .forms import UserCreationForm, ListsForm, UserUpdateForm
from .utils import mailman
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
class UserProfileDetailView(UserProfileBaseMixin, DetailView):
template_name = 'accounts/user_detail.html'
def get_context_data(self, **kwargs):
user = self.object
context = {}
count_types = OrderedDict()
fields_or_lookup = (
{'collaborators__contains': user.username},
{'fullname_and_username__contains': user.username},
)
for type in ['thread', 'ticket', 'wiki', 'changeset', 'attachment']:
sqs = SearchQuerySet()
for filter_or in fields_or_lookup:
sqs = sqs.filter_or(type=type, **filter_or)
count_types[trans(type)] = sqs.count()
context['type_count'] = count_types
sqs = SearchQuerySet()
for filter_or in fields_or_lookup:
sqs = sqs.filter_or(**filter_or).exclude(type='thread')
context['results'] = sqs.order_by('-modified', '-created')[: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'
messages = Message.objects.filter(from_address__user__pk=user.pk)
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):
# If the request method is GET just return the form
if request.method == 'GET':
user_form = UserCreationForm()
lists_form = ListsForm()
return render(request, 'accounts/user_create_form.html',
{'user_form': user_form, 'lists_form': lists_form})
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()
mailing_lists = lists_form.cleaned_data.get('lists')
if mailing_lists:
send_email_lists(user, mailing_lists)
# 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()
messages.success(request, _('Your profile has been created!'))
messages.warning(request, _('You must login to validated your profile. '
'Profiles not validated are deleted in 24h.'))
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 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()
for email in emails:
lists = []
lists_for_address = mailman.address_lists(email)
for listname in all_lists:
if listname in lists_for_address:
checked = True
else:
checked = False
lists.append((listname, checked))
context['membership'].update({email: lists})
context.update(kwargs)
return super(ManageUserSubscriptionsView, self).get_context_data(**context)