From 4759fd6b0f944a43bd1c40d52f0b0c4616dc834b Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Mon, 23 Nov 2020 23:47:14 +0100 Subject: [PATCH] publik-scripts: add script to update format of phone numbers --- ...ge_phone_kind_from_string_to_phone_number.py | 6 ++++-- ...-23_authentic_update_phone_numbers_format.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 publik-scripts/2020-11-23_authentic_update_phone_numbers_format.py diff --git a/publik-scripts/2020-11-21_hobo_change_phone_kind_from_string_to_phone_number.py b/publik-scripts/2020-11-21_hobo_change_phone_kind_from_string_to_phone_number.py index 2fcabe8..7554365 100644 --- a/publik-scripts/2020-11-21_hobo_change_phone_kind_from_string_to_phone_number.py +++ b/publik-scripts/2020-11-21_hobo_change_phone_kind_from_string_to_phone_number.py @@ -13,22 +13,24 @@ from hobo.profile.models import AttributeDefinition fake = '--fake' in sys.argv + class Fake(Exception): pass + tenant_name = connection.tenant.domain_url names = ['mobile', 'phone'] kind = 'phone_number' -qs = AttributeDefinition.objects.filter(name__in=names).exclude(kind=kind) +qs = AttributeDefinition.objects.filter(name__in=names).exclude(kind=kind, searchable=True) count = qs.count() if count: old_values = list(qs.values_list('name', 'kind')) try: with atomic(): - qs.update(kind=kind, last_update_timestamp=now()) + qs.update(kind=kind, searchable=True, last_update_timestamp=now()) print(tenant_name, ': Updated attributes %s to kind=%s' % (old_values, kind)) tls.MUST_NOTIFY = True if fake: diff --git a/publik-scripts/2020-11-23_authentic_update_phone_numbers_format.py b/publik-scripts/2020-11-23_authentic_update_phone_numbers_format.py new file mode 100644 index 0000000..0bb97de --- /dev/null +++ b/publik-scripts/2020-11-23_authentic_update_phone_numbers_format.py @@ -0,0 +1,17 @@ +from django.core.exceptions import ValidationError + +from authentic2.models import AttributeValue +from authentic2.attribute_kinds import PhoneNumberField + +clean = PhoneNumberField().clean + +for at in AttributeValue.objects.filter(attribute__name__in=['mobile', 'phone']): + content = at.content + try: + content = clean(content) + except ValidationError: + continue + else: + if at.content != content: + at.content = content + at.save(update_fields=['content'])