publik-scripts: add script to update format of phone numbers

This commit is contained in:
Benjamin Dauvergne 2020-11-23 23:47:14 +01:00
parent 78c780aa47
commit 4759fd6b0f
2 changed files with 21 additions and 2 deletions

View File

@ -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:

View File

@ -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'])