# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-05-30 15:29
from __future__ import unicode_literals

import random
import string

from django.db import IntegrityError, migrations, models, transaction
from django.template.defaultfilters import slugify


class Migration(migrations.Migration):

    def slugify_event(apps, schema_editor):
        """Generate slug from date and venue."""
        Event = apps.get_model('workshops', 'Event')
        for event in Event.objects.filter(slug__isnull=True):
            try:
                with transaction.atomic():
                    event.slug = slugify('{}-{}'.format(event.start,
                                                        event.venue))
                    event.save()
            except IntegrityError:
                population = string.ascii_letters + string.digits
                random_suffix = ''.join(random.sample(population, 20))
                event.slug = slugify('{}-{}-{}'.format(event.start,
                                                       event.venue,
                                                       random_suffix))
                event.save()

    dependencies = [
        ('workshops', '0087_renaming_column_repository_tags_squashed_0092_alter_help_text_of_metadata_changed'),
    ]

    operations = [
        migrations.AlterField(
            model_name='event',
            name='reg_key',
            field=models.CharField(blank=True, default='', max_length=20, verbose_name='Eventbrite key'),
            preserve_default=False,
        ),
        migrations.RunPython(slugify_event),
        migrations.AlterField(
            model_name='event',
            name='slug',
            field=models.SlugField(max_length=100, unique=True),
        ),
        migrations.AlterField(
            model_name='eventrequest',
            name='travel_reimbursement',
            field=models.CharField(blank=True, choices=[('', "Don't know yet."), ('book', 'Book travel through our university or program.'), ('reimburse', 'Book their own travel and be reimbursed.'), ('', 'Other (type below)')], default='', max_length=40, verbose_name="How will instructors' travel and accommodations be managed?"),
        ),
        migrations.AlterField(
            model_name='membership',
            name='contribution_type',
            field=models.CharField(blank=True, choices=[('financial', 'Financial'), ('person-days', 'Person-days'), ('other', 'Other')], default='other', max_length=40),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='person',
            name='middle',
            field=models.CharField(blank=True, default='', max_length=100),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='person',
            name='url',
            field=models.CharField(blank=True, default='', max_length=100),
            preserve_default=False,
        ),
    ]
