# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2018-05-28 14:32
from __future__ import unicode_literals

from django.db import migrations, models
from django.utils.crypto import get_random_string


def set_pids(apps, schema_editor):
    OrderPosition = apps.get_model('pretixbase', 'OrderPosition')  # noqa
    taken = set()
    charset = list('ABCDEFGHJKLMNPQRSTUVWXYZ3789')
    for op in OrderPosition.objects.iterator():
        while True:
            code = get_random_string(length=10, allowed_chars=charset)
            if code not in taken:
                op.pseudonymization_id = code
                taken.add(code)
                break
        op.save(update_fields=['pseudonymization_id'])


class Migration(migrations.Migration):
    dependencies = [
        ('pretixbase', '0092_auto_20180511_1224'),
    ]

    operations = [
        migrations.AddField(
            model_name='orderposition',
            name='pseudonymization_id',
            field=models.CharField(db_index=True, max_length=16, null=True, unique=True),
        ),
        migrations.RunPython(
            set_pids,
            migrations.RunPython.noop,
        ),
        migrations.AlterField(
            model_name='orderposition',
            name='pseudonymization_id',
            field=models.CharField(db_index=True, default='', max_length=16, unique=True),
            preserve_default=False,
        ),
    ]
