# Generated by Django 1.9.2 on 2016-05-18 12:05

from django.db import migrations, models
import django.db.models.deletion
from django.utils.translation import get_language


def copy_service_data(apps, schema_editor):
    fields = ('service_name', 'description', 'keywords', 'group')
    source_data = list(apps.get_model("issues", "Service").objects.all().values('id', *fields))
    for ent in source_data:
        master_id = ent.pop('id')
        apps.get_model("issues", "ServiceTranslation").objects.create(
            master_id=master_id,
            language_code=(get_language() or "en"),
            **ent
        )


class Migration(migrations.Migration):
    dependencies = [
        ('issues', '0002_mod'),
    ]

    operations = [
        migrations.CreateModel(
            name='ServiceTranslation',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('language_code', models.CharField(db_index=True, max_length=15, verbose_name='Language')),
                ('service_name', models.CharField(max_length=120)),
                ('description', models.TextField(blank=True)),
                ('keywords', models.TextField(blank=True)),
                ('group', models.CharField(blank=True, default='', max_length=140)),
            ],
            options={
                'managed': True,
                'verbose_name': 'service Translation',
                'default_permissions': (),
                'db_tablespace': '',
                'db_table': 'issues_service_translation',
            },
        ),
        migrations.AddField(
            model_name='servicetranslation',
            name='master',
            field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE,
                                    related_name='translations', to='issues.Service'),
        ),
        migrations.AlterUniqueTogether(
            name='servicetranslation',
            unique_together={('language_code', 'master')},
        ),
        migrations.RunPython(
            copy_service_data,
        ),
        migrations.RemoveField(
            model_name='service',
            name='description',
        ),
        migrations.RemoveField(
            model_name='service',
            name='group',
        ),
        migrations.RemoveField(
            model_name='service',
            name='keywords',
        ),
        migrations.RemoveField(
            model_name='service',
            name='service_name',
        ),
    ]
