Projet

Général

Profil

0001-utils-add-a-lazy_join-function-41342.patch

Benjamin Dauvergne, 15 avril 2020 11:42

Télécharger (2,8 ko)

Voir les différences:

Subject: [PATCH 1/4] utils: add a lazy_join function (#41342)

Use it to join translated strings.
 src/authentic2/utils/lazy.py | 26 ++++++++++++++++++++++++++
 tests/test_utils.py          | 18 ++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 src/authentic2/utils/lazy.py
src/authentic2/utils/lazy.py
1
# authentic2 - versatile identity manager
2
# Copyright (C) 2010-2020 Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.utils.text import format_lazy
18

  
19

  
20
def lazy_join(join, args):
21
    if not args:
22
        return ''
23

  
24
    fstring = '{}' + ''.join([join + '{}'] * (len(args) - 1))
25
    return format_lazy(fstring, *args)
26

  
tests/test_utils.py
18 18
from django.contrib.auth.middleware import AuthenticationMiddleware
19 19
from django.contrib.sessions.middleware import SessionMiddleware
20 20
from django.core import mail
21
from django.utils.functional import lazy
21 22

  
22 23
from django_rbac.utils import get_ou_model
23 24

  
......
25 26
                              user_can_change_password, login,
26 27
                              get_authentication_events, authenticate,
27 28
                              send_templated_mail)
29
from authentic2.utils.lazy import lazy_join
28 30

  
29 31

  
30 32
def test_good_next_url(db, rf, settings):
......
155 157
    sent_mail = mail.outbox.pop()
156 158
    assert sent_mail.subject == specific_template_ou
157 159
    assert sent_mail.body == specific_template_ou
160

  
161

  
162
def test_lazy_join():
163
    a = 'a'
164

  
165
    def f():
166
        return a
167
    f = lazy(f, str)
168

  
169
    joined = lazy_join(',', ['a', f()])
170

  
171
    assert str(joined) == 'a,a'
172

  
173
    a = 'b'
174

  
175
    assert str(joined) == 'a,b'
158
-