Update copyright years
[mailman-postorious.git] / src / postorius / utils.py
blob0f47d9db9a5ca11128f7ab8f4f3e8b0551bddab0
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.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.
36 """
37 return render(request, 'postorius/errors/generic.html',
38 {'error': _('Mailman REST API not available. Please start Mailman core.')}, # noqa: E501
39 status=503)
42 def render_client_error(request, error):
43 return render(request, 'postorius/errors/generic.html',
44 {'error': str(error)},
45 status=error.code)
48 def get_mailman_client():
49 # easier to patch during unit tests
50 client = Client(
51 '%s/3.1' %
52 settings.MAILMAN_REST_API_URL,
53 settings.MAILMAN_REST_API_USER,
54 settings.MAILMAN_REST_API_PASS)
55 return client
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.
68 """
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:
76 addr.verify()
77 mm_user.preferred_address = primary_email.email
78 return primary_email.email
79 return None
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.
88 """
89 try:
90 member = mlist.get_member(email)
91 except ValueError:
92 # Not a Member, try getting non-member.
93 try:
94 member = mlist.get_nonmember(email)
95 except ValueError:
96 member = None
97 return member
100 LANGUAGES = (
101 ('ar', 'Arabic'),
102 ('ast', 'Asturian'),
103 ('ca', 'Catalan'),
104 ('cs', 'Czech'),
105 ('da', 'Danish'),
106 ('de', 'German'),
107 ('el', 'Greek'),
108 ('es', 'Spanish'),
109 ('et', 'Estonian'),
110 ('eu', 'Euskara'),
111 ('fi', 'Finnish'),
112 ('fr', 'French'),
113 ('gl', 'Galician'),
114 ('he', 'Hebrew'),
115 ('hr', 'Croatian'),
116 ('hu', 'Hungarian'),
117 ('ia', 'Interlingua'),
118 ('it', 'Italian'),
119 ('ja', 'Japanese'),
120 ('ko', 'Korean'),
121 ('lt', 'Lithuanian'),
122 ('nl', 'Dutch'),
123 ('no', 'Norwegian'),
124 ('pl', 'Polish'),
125 ('pt', 'Protuguese'),
126 ('pt_BR', 'Protuguese (Brazil)'),
127 ('ro', 'Romanian'),
128 ('ru', 'Russian'),
129 ('sk', 'Slovak'),
130 ('sl', 'Slovenian'),
131 ('sr', 'Serbian'),
132 ('sv', 'Swedish'),
133 ('tr', 'Turkish'),
134 ('uk', 'Ukrainian'),
135 ('vi', 'Vietnamese'),
136 ('zh_CN', 'Chinese'),
137 ('zh_TW', 'Chinese (Taiwan)'),
138 ('en', 'English (USA)'),