1 # -*- coding: utf-8 -*-
2 # Copyright (C) 1998-2021 by the Free Software Foundation, Inc.
4 # This file is part of Postorius.
6 # Postorius is free software: you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option)
11 # Postorius is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 # You should have received a copy of the GNU General Public License along with
17 # Postorius. If not, see <http://www.gnu.org/licenses/>.
22 from django
.conf
import settings
23 from django
.shortcuts
import render
24 from django
.utils
.translation
import gettext
as _
26 from allauth
.account
.models
import EmailAddress
27 from mailmanclient
import Client
30 logger
= logging
.getLogger(__name__
)
33 def render_api_error(request
):
34 """Renders an error template.
35 Use if MailmanApiError is catched.
37 return render(request
, 'postorius/errors/generic.html',
38 {'error': _('Mailman REST API not available. Please start Mailman core.')}, # noqa: E501
42 def render_client_error(request
, error
):
43 return render(request
, 'postorius/errors/generic.html',
44 {'error': str(error
)},
48 def get_mailman_client():
49 # easier to patch during unit tests
52 settings
.MAILMAN_REST_API_URL
,
53 settings
.MAILMAN_REST_API_USER
,
54 settings
.MAILMAN_REST_API_PASS
)
58 def with_empty_choice(choices
):
59 """Add an empty Choice for unset values in dropdown."""
60 return [(None, '-----')] + list(choices
)
63 def set_preferred(user
, mm_user
):
64 """Set preferred address in Mailman Core.
66 :param user: The Django user mode to set preferred address.
67 :param mm_user: The Mailman User object to set preferred address for.
69 client
= get_mailman_client()
70 primary_email
= EmailAddress
.objects
.get_primary(user
)
71 if primary_email
is not None and primary_email
.verified
:
72 # First, make sure that the email address is verified in Core,
73 # otherwise, we can't set it as a primary address.
74 addr
= client
.get_address(primary_email
.email
)
75 if not addr
.verified_on
:
77 mm_user
.preferred_address
= primary_email
.email
78 return primary_email
.email
82 def get_member_or_nonmember(mlist
, email
):
83 """Return either a Member or a Non-member with `email` in mlist.
85 :param mlist: MailingList object to get membership for.
86 :param email: Email address of the member or nonmember.
87 :returns: Member if found otherwise None.
90 member
= mlist
.get_member(email
)
92 # Not a Member, try getting non-member.
94 member
= mlist
.get_nonmember(email
)
117 ('ia', 'Interlingua'),
121 ('lt', 'Lithuanian'),
125 ('pt', 'Protuguese'),
126 ('pt_BR', 'Protuguese (Brazil)'),
135 ('vi', 'Vietnamese'),
136 ('zh_CN', 'Chinese'),
137 ('zh_TW', 'Chinese (Taiwan)'),
138 ('en', 'English (USA)'),