# Generated by Django 4.2.11 on 2024-04-25 13:14

import random
import string

from django.db import migrations


def populate_model(apps, schema_editor):
    """
    Move existing additional email app settings into the
    SODARUserAdditionalEmail model as verified emais. Delete the settings
    objects.
    """
    AppSetting = apps.get_model('projectroles', 'AppSetting')
    SODARUserAdditionalEmail = apps.get_model(
        'projectroles', 'SODARUserAdditionalEmail'
    )
    for a in AppSetting.objects.filter(
        app_plugin=None, name='user_email_additional'
    ):
        for v in a.value.split(';'):
            secret = ''.join(
                random.SystemRandom().choice(
                    string.ascii_lowercase + string.digits
                )
                for _ in range(32)
            )
            try:
                SODARUserAdditionalEmail.objects.create(
                    user=a.user, email=v, verified=True, secret=secret
                )
            except Exception:
                pass
        a.delete()


class Migration(migrations.Migration):

    dependencies = [
        ("projectroles", "0029_sodaruseradditionalemail"),
    ]

    operations = [
        migrations.RunPython(
            populate_model, reverse_code=migrations.RunPython.noop
        )
    ]
