Projet

Général

Profil

0001-profile-format-phone-numbers-at-cell-rendering-time-.patch

Paul Marillonnet, 03 janvier 2023 15:34

Télécharger (4,98 ko)

Voir les différences:

Subject: [PATCH] profile: format phone numbers at cell-rendering time (#72769)

 combo/profile/models.py |  3 +++
 combo/profile/utils.py  | 24 ++++++++++++++++++++++++
 setup.py                |  1 +
 tests/settings.py       |  8 ++++++++
 tests/test_profile.py   | 12 +++++++++++-
 5 files changed, 47 insertions(+), 1 deletion(-)
combo/profile/models.py
24 24

  
25 25
from combo.data.library import register_cell_class
26 26
from combo.data.models import JsonCellBase
27
from combo.profile import utils as profile_utils
27 28

  
28 29

  
29 30
class Profile(models.Model):
......
61 62
                if value:
62 63
                    if attribute['kind'] in ('birthdate', 'date'):
63 64
                        value = parse_date(value)
65
                    if attribute['kind'] == 'phone_number':
66
                        value = profile_utils.get_formatted_phone(value)
64 67
                extra_context['profile_fields'][attribute['name']]['value'] = value
65 68
        else:
66 69
            extra_context['error'] = 'unknown user'
combo/profile/utils.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
import phonenumbers
17 18
from django.conf import settings
18 19
from django.contrib.auth.models import User
20
from phonenumbers.phonenumberutil import region_code_for_country_code
19 21

  
20 22
if 'mellon' in settings.INSTALLED_APPS:
21 23
    from mellon.models import UserSAMLIdentifier
......
46 48
        if raise_on_missing:
47 49
            raise User.DoesNotExist()
48 50
        return ProxiedUser(name_id=name_id)
51

  
52

  
53
def get_formatted_phone(value, country_code=None):
54
    if country_code is None:
55
        country_code = settings.DEFAULT_COUNTRY_CODE
56

  
57
    region_code = 'ZZ'  # phonenumbers' default value for unknown regions
58
    try:
59
        region_code = region_code_for_country_code(int(country_code))
60
    except ValueError:
61
        pass
62
    if region_code == 'ZZ':
63
        return value
64

  
65
    try:
66
        pn = phonenumbers.parse(value, region_code)
67
    except phonenumbers.NumberParseException:
68
        return value
69

  
70
    if not phonenumbers.is_valid_number(pn):
71
        return value
72
    return phonenumbers.format_number(pn, phonenumbers.PhoneNumberFormat.NATIONAL)
setup.py
181 181
        'pywebpush',
182 182
        'pygal',
183 183
        'lxml',
184
        'phonenumbers',
184 185
    ],
185 186
    zip_safe=False,
186 187
    cmdclass={
tests/settings.py
105 105
            'label': 'Birth Date',
106 106
            'user_visible': True,
107 107
        },
108
        {
109
            'name': 'phone',
110
            'kind': 'phone_number',
111
            'label': 'Phone',
112
            'user_visible': True,
113
        },
108 114
    ]
109 115
}
110 116

  
......
121 127
        'rest_framework.authentication.BasicAuthentication',
122 128
    ]
123 129
}
130

  
131
DEFAULT_COUNTRY_CODE = '33'
tests/test_profile.py
20 20
    cell = ProfileCell(page=page, order=0)
21 21
    cell.save()
22 22

  
23
    data = {'first_name': 'Foo', 'birthdate': '2018-08-10'}
23
    data = {
24
        'first_name': 'Foo',
25
        'birthdate': '2018-08-10',
26
        'phone': '+33612345678',
27
    }
24 28
    requests_get.return_value = mock.Mock(content=json.dumps(data), json=lambda: data, status_code=200)
25 29

  
26 30
    admin_user.get_name_id = lambda: '123456'
......
28 32
    context = cell.get_cell_extra_context({'synchronous': True, 'selected_user': admin_user})
29 33
    assert context['profile_fields']['first_name']['value'] == 'Foo'
30 34
    assert context['profile_fields']['birthdate']['value'] == datetime.date(2018, 8, 10)
35
    assert context['profile_fields']['phone']['value'] == '06 12 34 56 78'
31 36
    assert requests_get.call_args[0][0] == 'http://example.org/api/users/123456/'
37

  
38
    data['phone'] = '+336a23c5678'
39
    requests_get.return_value = mock.Mock(content=json.dumps(data), json=lambda: data, status_code=200)
40
    context = cell.get_cell_extra_context({'synchronous': True, 'selected_user': admin_user})
41
    assert context['profile_fields']['phone']['value'] == '+336a23c5678'
32
-