# -*- coding: utf-8 -*-
# Generated by Django 1.10.4 on 2017-06-29 21:57
from __future__ import unicode_literals

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


def copy_to_nokia(apps, schema_editor):
    WithingsUser = apps.get_model('nokiaapp', 'WithingsUser')
    NokiaUser = apps.get_model('nokiaapp', 'NokiaUser')
    for user in WithingsUser.objects.all():
        NokiaUser.objects.create(
            id=user.id,
            nokia_user_id=user.withings_user_id,
            access_token=user.access_token,
            access_token_secret=user.access_token_secret,
            last_update=user.last_update,
            user=user.user
        )


def copy_to_withings(apps, schema_editor):
    WithingsUser = apps.get_model('nokiaapp', 'WithingsUser')
    NokiaUser = apps.get_model('nokiaapp', 'NokiaUser')
    for user in NokiaUser.objects.all():
        WithingsUser.objects.create(
            id=user.id,
            withings_user_id=user.nokia_user_id,
            access_token=user.access_token,
            access_token_secret=user.access_token_secret,
            last_update=user.last_update,
            user=user.user
        )


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('nokiaapp', '0002_help_text'),
    ]

    operations = [
        migrations.CreateModel(
            name='NokiaUser',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('nokia_user_id', models.IntegerField(help_text=b'The nokia user ID')),
                ('access_token', models.TextField(help_text=b'OAuth access token')),
                ('access_token_secret', models.TextField(help_text=b'OAuth access token secret')),
                ('last_update', models.DateTimeField(blank=True, help_text=b"The datetime the user's nokia data was last updated", null=True)),
                ('user', models.OneToOneField(help_text=b'The user', on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.RunPython(copy_to_nokia, copy_to_withings),
        migrations.RemoveField(
            model_name='withingsuser',
            name='user',
        ),
        migrations.AlterField(
            model_name='measure',
            name='group',
            field=models.ForeignKey(help_text=b"The measurement's group", on_delete=django.db.models.deletion.CASCADE, related_name='measures', to='nokiaapp.MeasureGroup'),
        ),
        migrations.AlterField(
            model_name='measuregroup',
            name='grpid',
            field=models.IntegerField(help_text=b'The group ID, assigned by nokia'),
        ),
        migrations.AlterModelTable(
            name='measure',
            table=None,
        ),
        migrations.AlterModelTable(
            name='measuregroup',
            table=None,
        ),
        migrations.DeleteModel(
            name='WithingsUser',
        ),
    ]
