# -*- coding: utf-8 -*-
# Generated by Django 1.11.11 on 2018-03-12 11:19
from __future__ import unicode_literals

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


def set_identifiers(apps, schema_editor):
    Question = apps.get_model('pretixbase', 'Question')
    QuestionOption = apps.get_model('pretixbase', 'QuestionOption')

    for q in Question.objects.select_related('event'):
        if not q.identifier:
            charset = list('ABCDEFGHJKLMNPQRSTUVWXYZ3789')
            while True:
                code = get_random_string(length=8, allowed_chars=charset)
                if not Question.objects.filter(event=q.event, identifier=code).exists():
                    q.identifier = code
                    q.save()
                    break

    for q in QuestionOption.objects.select_related('question', 'question__event'):
        if not q.identifier:
            charset = list('ABCDEFGHJKLMNPQRSTUVWXYZ3789')
            while True:
                code = get_random_string(length=8, allowed_chars=charset)
                if not QuestionOption.objects.filter(question__event=q.question.event, identifier=code).exists():
                    q.identifier = code
                    q.save()
                    break


class Migration(migrations.Migration):

    dependencies = [
        ('pretixbase', '0084_questionoption_position'),
    ]

    operations = [
        migrations.AddField(
            model_name='question',
            name='identifier',
            field=models.CharField(default='', max_length=190),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='questionoption',
            name='identifier',
            field=models.CharField(default='', max_length=190),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='user',
            name='locale',
            field=models.CharField(choices=[('en', 'English'), ('de', 'German'), ('de-informal', 'German (informal)'), ('nl', 'Dutch'), ('da', 'Danish'), ('pt-br', 'Portuguese (Brazil)')], default='en', max_length=50, verbose_name='Language'),
        ),
        migrations.RunPython(set_identifiers, migrations.RunPython.noop)
    ]
