publik-scripts: add script to update format of phone numbers
This commit is contained in:
parent
78c780aa47
commit
4759fd6b0f
|
@ -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:
|
||||
|
|
|
@ -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'])
|
Loading…
Reference in New Issue