Translated using Weblate (Albanian)
[mailman-postorious.git] / src / postorius / utils.py
blob03dd031d726eddd2cc4ba1bb2e8ed5ecc385eb09
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)
9 # any later version.
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
14 # more details.
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/>.
20 import logging
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.
36 """
37 return render(
38 request,
39 'postorius/errors/generic.html',
41 'error': _(
42 'Mailman REST API not available. Please start Mailman core.'
44 }, # noqa: E501
45 status=503,
49 def render_client_error(request, error):
50 return render(
51 request,
52 'postorius/errors/generic.html',
53 {'error': str(error)},
54 status=error.code,
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.
73 """
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:
81 addr.verify()
82 mm_user.preferred_address = primary_email.email
83 return primary_email.email
84 return None
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.
93 """
94 try:
95 member = mlist.get_member(email)
96 except ValueError:
97 # Not a Member, try getting non-member.
98 try:
99 member = mlist.get_nonmember(email)
100 except ValueError:
101 member = None
102 return member
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 []
123 django_email = None
124 for addr in addresses:
125 try:
126 django_email = EmailAddress.objects.get(email=addr.email)
127 except EmailAddress.DoesNotExist:
128 continue
129 else:
130 break
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.
135 return None
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.
158 Parameters:
159 - key (str): The key to retrieve the value for.
160 - default: The default value to return if the key is not found.
162 Returns:
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)
169 LANGUAGES = [
170 ('ar', 'Arabic'),
171 ('ast', 'Asturian'),
172 ('bg', 'Bulgarian'),
173 ('ca', 'Catalan'),
174 ('zh_CN', 'Chinese'),
175 ('zh_TW', 'Chinese (Taiwan)'),
176 ('hr', 'Croatian'),
177 ('cs', 'Czech'),
178 ('da', 'Danish'),
179 ('nl', 'Dutch'),
180 ('en', 'English (USA)'),
181 ('et', 'Estonian'),
182 ('eu', 'Euskara'),
183 ('fi', 'Finnish'),
184 ('fr', 'French'),
185 ('gl', 'Galician'),
186 ('de', 'German'),
187 ('el', 'Greek'),
188 ('he', 'Hebrew'),
189 ('hu', 'Hungarian'),
190 ('ia', 'Interlingua'),
191 ('it', 'Italian'),
192 ('ja', 'Japanese'),
193 ('ko', 'Korean'),
194 ('lt', 'Lithuanian'),
195 ('no', 'Norwegian'),
196 ('pl', 'Polish'),
197 ('pt', 'Portuguese'),
198 ('pt_BR', 'Portuguese (Brazil)'),
199 ('ro', 'Romanian'),
200 ('ru', 'Russian'),
201 ('sr', 'Serbian'),
202 ('sk', 'Slovak'),
203 ('sl', 'Slovenian'),
204 ('es', 'Spanish'),
205 ('sv', 'Swedish'),
206 ('tr', 'Turkish'),
207 ('uk', 'Ukrainian'),
208 ('vi', 'Vietnamese'),