Fix problem with duplicate usernames

This commit is contained in:
Lorenz Padberg 2024-02-07 14:58:06 +01:00
parent ce7ab55cba
commit 216f448137
3 changed files with 14 additions and 8 deletions

View File

@ -149,7 +149,7 @@ def migrate_snapshots():
default_content_block = get_default_content_block(objective_group_snapshot, module) default_content_block = get_default_content_block(objective_group_snapshot, module)
if default_content_block: if default_content_block:
print(f"Default content block: {default_content_block.title}") print(f"Default content block: {default_content_block.title}")
snapshot.hidden_content_blocks.add(default_content_block.id) snapshot.hidden_content_blocks.add(default_content_block.id)
if list(visible_objectives_by_ids.values()): if list(visible_objectives_by_ids.values()):
objectives = list(visible_objectives_by_ids.values())[0] objectives = list(visible_objectives_by_ids.values())[0]

View File

@ -12,17 +12,23 @@ class Command(BaseCommand):
self.stdout.write("If so, type \"YES\"") self.stdout.write("If so, type \"YES\"")
result = input() result = input()
user_model = get_user_model()
if result == 'YES': if result == 'YES':
users = get_user_model().objects.all() users = user_model.objects.all().order_by('email')
with ThreadPoolExecutor(max_workers=10) as executor: with ThreadPoolExecutor(max_workers=5) as executor:
executor.map(process_user, users) executor.map(process_user, users)
def process_user(usr): def process_user(usr):
# replace domain with skillbox.ch # replace domain with id.skillbox.ch,to ensure unique emails
usr.email = usr.email.split('@')[0] + '@skillbox.ch' usr.email = usr.email.split('@')[0] + f'@skillbox.ch'
user_model = get_user_model()
if user_model.objects.filter(email=usr.email).exists():
usr.email = usr.email.split('@')[0] + f'@{usr.id}.skillbox.ch'
usr.username = usr.email usr.username = usr.email
print(usr.email) print(usr.email)

View File

@ -4,9 +4,9 @@ python manage.py reset_all_passwords
pg_dump -Fc --no-acl -h localhost -U skillbox skillbox > latest-anonymized.dump pg_dump -Fc --no-acl -h localhost -U skillbox skillbox > latest-anonymized.dump
python manage.py migrate_objectives_to_content python manage.py migrate_objectives_to_content
pg_dump -Fc --no-acl -h localhost -U skillbox skillbox > latest-migrated-objectives.dump pg_dump -Fc --no-acl -h localhost -U skillbox skillbox > latest-migrated-objectives.dump
#python manage.py migrate_objective_snapshots python manage.py migrate_objective_snapshots
#pg_dump -Fc --no-acl -h localhost -U skillbox skillbox > latest-migrated-objectives-and-snapshots.dump pg_dump -Fc --no-acl -h localhost -U skillbox skillbox > latest-migrated-objectives-and-snapshots.dump
# Use this command to restore the database from the dump: #Use this command to restore the database from the dump:
#pg_restore --verbose --clean --no-acl --no-owner -h localhost -U skillbox -d skillbox latest-migrated-objectives.dump #pg_restore --verbose --clean --no-acl --no-owner -h localhost -U skillbox -d skillbox latest-migrated-objectives.dump