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
|
fake = '--fake' in sys.argv
|
||||||
|
|
||||||
|
|
||||||
class Fake(Exception):
|
class Fake(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
tenant_name = connection.tenant.domain_url
|
tenant_name = connection.tenant.domain_url
|
||||||
|
|
||||||
names = ['mobile', 'phone']
|
names = ['mobile', 'phone']
|
||||||
kind = 'phone_number'
|
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()
|
count = qs.count()
|
||||||
if count:
|
if count:
|
||||||
old_values = list(qs.values_list('name', 'kind'))
|
old_values = list(qs.values_list('name', 'kind'))
|
||||||
try:
|
try:
|
||||||
with atomic():
|
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))
|
print(tenant_name, ': Updated attributes %s to kind=%s' % (old_values, kind))
|
||||||
tls.MUST_NOTIFY = True
|
tls.MUST_NOTIFY = True
|
||||||
if fake:
|
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