Use EmailAddress instead of set_other_emails
1 parent e791374 commit 24df95b27b8f5b5522186d05cd00990dc7ce0a43
@Aurélien Bompard Aurélien Bompard authored on 10 Aug 2016
Showing 5 changed files
View
8
src/postorius/auth/utils.py
"""
Authentication and authorization-related utilities.
"""
 
from allauth.account.models import EmailAddress
from django.utils import six
from postorius.utils import set_other_emails
from postorius.models import List
 
 
def user_is_in_list_roster(user, mailing_list, roster):
if not user.is_authenticated():
return False
if not hasattr(user, 'other_emails'):
set_other_emails(user)
addresses = set([user.email]) | set(user.other_emails)
addresses = set(EmailAddress.objects.filter(
user=user, verified=True).values_list("email", flat=True))
if addresses & set(getattr(mailing_list, roster)):
return True # At least one address is in the roster
return False
 
View
3
■■
src/postorius/middleware.py
 
 
class PostoriusMiddleware(object):
 
def process_request(self, request):
utils.set_other_emails(request.user)
 
def process_exception(self, request, exception):
if isinstance(exception, MailmanApiError):
return utils.render_api_error(request)
View
1
■■■■
src/postorius/views/generic.py
# get the list object.
if 'list_id' in kwargs:
self.mailing_list = self._get_list(kwargs['list_id'],
int(kwargs.get('page', 1)))
utils.set_other_emails(request.user)
set_user_access_props(request.user, self.mailing_list)
# set the template
if 'template' in kwargs:
self.template = kwargs['template']
View
30
src/postorius/views/list.py
import csv
import email.utils
import logging
 
from allauth.account.models import EmailAddress
from django.http import HttpResponse, HttpResponseNotAllowed, Http404
 
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.core.validators import validate_email
data = {'list': self.mailing_list,
'userSubscribed': False,
'subscribed_address': None}
if request.user.is_authenticated():
user_emails = [request.user.email] + request.user.other_emails
user_emails = EmailAddress.objects.filter(
user=request.user, verified=True).order_by(
"email").values_list("email", flat=True)
for address in user_emails:
try:
self.mailing_list.get_member(address)
except ValueError:
 
@method_decorator(login_required)
def post(self, request, list_id):
try:
user_emails = [request.user.email] + request.user.other_emails
user_emails = EmailAddress.objects.filter(
user=request.user, verified=True).order_by(
"email").values_list("email", flat=True)
form = ListSubscribe(user_emails, request.POST)
# Find the currently subscribed email
old_email = None
for address in user_emails:
Subscribes an email address to a mailing list via POST and
redirects to the `list_summary` view.
"""
try:
user_addresses = [request.user.email] + request.user.other_emails
form = ListSubscribe(user_addresses, request.POST)
user_emails = EmailAddress.objects.filter(
user=request.user, verified=True).order_by(
"email").values_list("email", flat=True)
form = ListSubscribe(user_emails, request.POST)
if form.is_valid():
email = request.POST.get('email')
response = self.mailing_list.subscribe(
email, pre_verified=True, pre_confirmed=True)
if role == 'owner':
if len(roster) == 1:
messages.error(request, _('Removing the last owner is impossible'))
return redirect('list_members', the_list.list_id, role)
# the user may not have a other_emails property if it's a superuser
user_addresses = set([request.user.email]) | \
set(getattr(request.user, 'other_emails', []))
if address in user_addresses:
user_emails = EmailAddress.objects.filter(
user=request.user, verified=True).order_by(
"email").values_list("email", flat=True)
if address in user_emails:
# The user is removing themselves, redirect to the list info page
# because they won't have access to the members page anyway.
redirect_on_success = redirect('list_summary', the_list.list_id)
 
View
src/postorius/views/user.py