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
.shortcuts
import render
23 from django
.utils
.translation
import gettext
as _
25 from allauth
.account
.models
import EmailAddress
26 from django_mailman3
.lib
import mailman
29 logger
= logging
.getLogger(__name__
)
32 def render_api_error(request
):
33 """Renders an error template.
34 Use if MailmanApiError is catched.
36 return render(request
, 'postorius/errors/generic.html',
37 {'error': _('Mailman REST API not available. Please start Mailman core.')}, # noqa: E501
41 def render_client_error(request
, error
):
42 return render(request
, 'postorius/errors/generic.html',
43 {'error': str(error
)},
47 def get_mailman_client():
48 """A proxy for django_mailman3.lib.mailman.get_mailman_client."""
49 return mailman
.get_mailman_client()
52 def with_empty_choice(choices
):
53 """Add an empty Choice for unset values in dropdown."""
54 return [(None, '-----')] + list(choices
)
57 def set_preferred(user
, mm_user
):
58 """Set preferred address in Mailman Core.
60 :param user: The Django user mode to set preferred address.
61 :param mm_user: The Mailman User object to set preferred address for.
63 client
= get_mailman_client()
64 primary_email
= EmailAddress
.objects
.get_primary(user
)
65 if primary_email
is not None and primary_email
.verified
:
66 # First, make sure that the email address is verified in Core,
67 # otherwise, we can't set it as a primary address.
68 addr
= client
.get_address(primary_email
.email
)
69 if not addr
.verified_on
:
71 mm_user
.preferred_address
= primary_email
.email
72 return primary_email
.email
76 def get_member_or_nonmember(mlist
, email
):
77 """Return either a Member or a Non-member with `email` in mlist.
79 :param mlist: MailingList object to get membership for.
80 :param email: Email address of the member or nonmember.
81 :returns: Member if found otherwise None.
84 member
= mlist
.get_member(email
)
86 # Not a Member, try getting non-member.
88 member
= mlist
.get_nonmember(email
)
94 def get_django_user(mm_user
, addresses
=None):
95 """Given a Mailman user, return a Django User if One exists.
97 There is no direct way to fetch that, but we can iterate on Users's Email
98 Addresses and see if Django knows any one of them. If we find one, just
99 return the user associated with that Address.
101 Ideally, the email addresses should be *all* synchronized with Core, but
102 just in case they aren't, we iterate over all the addresses.
104 :param mm_user: Mailman User object.
105 :param addresses: user.addresses for the above mailman objects. It is
106 passed in just so that we can avoid an API call to Core to get them. It
107 is optional since we can just fetch them here if need be.
108 :returns: Django user if found, None otherwise.
110 if addresses
is None:
111 addresses
= mm_user
.addresses
or []
113 for addr
in addresses
:
115 django_email
= EmailAddress
.objects
.get(email
=addr
.email
)
116 except EmailAddress
.DoesNotExist
:
121 if django_email
is None:
122 # If none of the user's emails are registered in Django, that means
123 # they don't have a user account.
125 return django_email
.user
145 ('ia', 'Interlingua'),
149 ('lt', 'Lithuanian'),
153 ('pt', 'Portuguese'),
154 ('pt_BR', 'Portuguese (Brazil)'),
163 ('vi', 'Vietnamese'),
164 ('zh_CN', 'Chinese'),
165 ('zh_TW', 'Chinese (Taiwan)'),
166 ('en', 'English (USA)'),