Update po files
[mailman-postorious.git] / src / postorius / utils.py
blob710a360d625a6895c6410d647db05c38f9ae69a9
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)
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.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.
35 """
36 return render(request, 'postorius/errors/generic.html',
37 {'error': _('Mailman REST API not available. Please start Mailman core.')}, # noqa: E501
38 status=503)
41 def render_client_error(request, error):
42 return render(request, 'postorius/errors/generic.html',
43 {'error': str(error)},
44 status=error.code)
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.
62 """
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:
70 addr.verify()
71 mm_user.preferred_address = primary_email.email
72 return primary_email.email
73 return None
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.
82 """
83 try:
84 member = mlist.get_member(email)
85 except ValueError:
86 # Not a Member, try getting non-member.
87 try:
88 member = mlist.get_nonmember(email)
89 except ValueError:
90 member = None
91 return member
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 []
112 django_email = None
113 for addr in addresses:
114 try:
115 django_email = EmailAddress.objects.get(email=addr.email)
116 except EmailAddress.DoesNotExist:
117 continue
118 else:
119 break
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.
124 return None
125 return django_email.user
128 LANGUAGES = (
129 ('ar', 'Arabic'),
130 ('ast', 'Asturian'),
131 ('ca', 'Catalan'),
132 ('cs', 'Czech'),
133 ('da', 'Danish'),
134 ('de', 'German'),
135 ('el', 'Greek'),
136 ('es', 'Spanish'),
137 ('et', 'Estonian'),
138 ('eu', 'Euskara'),
139 ('fi', 'Finnish'),
140 ('fr', 'French'),
141 ('gl', 'Galician'),
142 ('he', 'Hebrew'),
143 ('hr', 'Croatian'),
144 ('hu', 'Hungarian'),
145 ('ia', 'Interlingua'),
146 ('it', 'Italian'),
147 ('ja', 'Japanese'),
148 ('ko', 'Korean'),
149 ('lt', 'Lithuanian'),
150 ('nl', 'Dutch'),
151 ('no', 'Norwegian'),
152 ('pl', 'Polish'),
153 ('pt', 'Portuguese'),
154 ('pt_BR', 'Portuguese (Brazil)'),
155 ('ro', 'Romanian'),
156 ('ru', 'Russian'),
157 ('sk', 'Slovak'),
158 ('sl', 'Slovenian'),
159 ('sr', 'Serbian'),
160 ('sv', 'Swedish'),
161 ('tr', 'Turkish'),
162 ('uk', 'Ukrainian'),
163 ('vi', 'Vietnamese'),
164 ('zh_CN', 'Chinese'),
165 ('zh_TW', 'Chinese (Taiwan)'),
166 ('en', 'English (USA)'),