# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-07-17 19:29
from __future__ import unicode_literals

from django.db import migrations, models
from django.db.models import deletion


def fwd(app, schema_editor):
    Event = app.get_model('pretixbase', 'Event')
    for e in Event.objects.select_related('organizer').all():
        e.invoices.all().update(prefix=e.slug.upper() + '-', organizer=e.organizer)


class Migration(migrations.Migration):
    dependencies = [
        ('pretixbase', '0068_subevent_frontpage_text'),
    ]

    operations = [
        migrations.AddField(
            model_name='invoice',
            name='prefix',
            field=models.CharField(db_index=True, default='', max_length=160),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='invoice',
            name='organizer',
            field=models.ForeignKey(null=True,
                                    on_delete=deletion.PROTECT,
                                    related_name='invoices', to='pretixbase.Organizer'),
            preserve_default=False,
        ),
        migrations.RunPython(
            fwd, migrations.RunPython.noop
        ),
        migrations.AlterUniqueTogether(
            name='invoice',
            unique_together=set([('organizer', 'prefix', 'invoice_no')]),
        ),
        migrations.AlterField(
            model_name='invoice',
            name='organizer',
            field=models.ForeignKey(on_delete=deletion.PROTECT, related_name='invoices', to='pretixbase.Organizer'),
        ),
    ]
