1 # -*- coding: utf-8 -*-
2 # Copyright (C) 1998-2023 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_lazy
as _
26 from allauth
.account
.models
import EmailAddress
27 from django_mailman3
.lib
import mailman
30 logger
= logging
.getLogger(__name__
)
33 def render_api_error(request
):
34 """Renders an error template.
35 Use if MailmanApiError is catched.
39 'postorius/errors/generic.html',
42 'Mailman REST API not available. Please start Mailman core.'
49 def render_client_error(request
, error
):
52 'postorius/errors/generic.html',
53 {'error': str(error
)},
58 def get_mailman_client():
59 """A proxy for django_mailman3.lib.mailman.get_mailman_client."""
60 return mailman
.get_mailman_client()
63 def with_empty_choice(choices
):
64 """Add an empty Choice for unset values in dropdown."""
65 return [(None, '-----')] + list(choices
)
68 def set_preferred(user
, mm_user
):
69 """Set preferred address in Mailman Core.
71 :param user: The Django user mode to set preferred address.
72 :param mm_user: The Mailman User object to set preferred address for.
74 client
= get_mailman_client()
75 primary_email
= EmailAddress
.objects
.get_primary(user
)
76 if primary_email
is not None and primary_email
.verified
:
77 # First, make sure that the email address is verified in Core,
78 # otherwise, we can't set it as a primary address.
79 addr
= client
.get_address(primary_email
.email
)
80 if not addr
.verified_on
:
82 mm_user
.preferred_address
= primary_email
.email
83 return primary_email
.email
87 def get_member_or_nonmember(mlist
, email
):
88 """Return either a Member or a Non-member with `email` in mlist.
90 :param mlist: MailingList object to get membership for.
91 :param email: Email address of the member or nonmember.
92 :returns: Member if found otherwise None.
95 member
= mlist
.get_member(email
)
97 # Not a Member, try getting non-member.
99 member
= mlist
.get_nonmember(email
)
105 def get_django_user(mm_user
, addresses
=None):
106 """Given a Mailman user, return a Django User if One exists.
108 There is no direct way to fetch that, but we can iterate on Users's Email
109 Addresses and see if Django knows any one of them. If we find one, just
110 return the user associated with that Address.
112 Ideally, the email addresses should be *all* synchronized with Core, but
113 just in case they aren't, we iterate over all the addresses.
115 :param mm_user: Mailman User object.
116 :param addresses: user.addresses for the above mailman objects. It is
117 passed in just so that we can avoid an API call to Core to get them. It
118 is optional since we can just fetch them here if need be.
119 :returns: Django user if found, None otherwise.
121 if addresses
is None:
122 addresses
= mm_user
.addresses
or []
124 for addr
in addresses
:
126 django_email
= EmailAddress
.objects
.get(email
=addr
.email
)
127 except EmailAddress
.DoesNotExist
:
132 if django_email
is None:
133 # If none of the user's emails are registered in Django, that means
134 # they don't have a user account.
136 return django_email
.user
139 def filter_memberships_by_roles(memberships
, roles
):
140 """Given a list of roles, filter the memberships with those roles.
142 :param memberships: A list of Member objects.
143 :type memberships: List[mailmanclient.restobjects.Member]
144 :param roles: A list of roles.
145 :type roles: List[str]
146 :returns: A list of memberships filtered by roles.
147 :rtype: List[mailmanclient.restobjects.Member]
149 return [member
for member
in memberships
if member
.role
in roles
]
152 def get_postorius_default(key
, default
):
154 Retrieves the value associated with the given key from the
155 POSTORIUS_DEFAULTS dictionary. If the key is not found, returns the
156 provided default value.
159 - key (str): The key to retrieve the value for.
160 - default: The default value to return if the key is not found.
163 - The value associated with the key if found, otherwise the default value.
165 postorius_defaults
= getattr(settings
, 'POSTORIUS_DEFAULTS', {})
166 return postorius_defaults
.get(key
, default
)
174 ('zh_CN', 'Chinese'),
175 ('zh_TW', 'Chinese (Taiwan)'),
180 ('en', 'English (USA)'),
190 ('ia', 'Interlingua'),
194 ('lt', 'Lithuanian'),
197 ('pt', 'Portuguese'),
198 ('pt_BR', 'Portuguese (Brazil)'),
208 ('vi', 'Vietnamese'),