# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2017-08-16 23:07
from __future__ import unicode_literals

import django.core.validators
from django.db import migrations, models


def device_owner_to_super_user(apps, schema_editor):
    DeviceOwner = apps.get_model('kolibriauth', 'DeviceOwner')
    FacilityUser = apps.get_model('kolibriauth', 'FacilityUser')
    Facility = apps.get_model('kolibriauth', 'Facility')
    default_facility = Facility.objects.all().first()
    DevicePermissions = apps.get_model('device', 'DevicePermissions')
    DeviceSettings = apps.get_model('device', 'DeviceSettings')
    # Can't do much if no facilities exist, as no facility to FK the users onto
    if default_facility:
        for device_owner in DeviceOwner.objects.all():
            superuser = FacilityUser.objects.create(
                username=device_owner.username,
                password=device_owner.password,
                facility=default_facility,
                full_name=device_owner.full_name,
                date_joined=device_owner.date_joined,
            )
            DevicePermissions.objects.create(user=superuser, is_superuser=True)
        # Finally, set the is_provisioned flag
        settings, created = DeviceSettings.get_or_create(is_provisioned=True)


class Migration(migrations.Migration):

    dependencies = [
        ('kolibriauth', '0003_auto_20170621_0958'),
        ('device', '0001_initial')
    ]

    operations = [
        migrations.RunPython(device_owner_to_super_user, migrations.RunPython.noop),
        migrations.DeleteModel(
            name='DeviceOwner',
        ),
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2017-08-16 23:07
from __future__ import unicode_literals

import django.core.validators
from django.db import migrations, models


def device_owner_to_super_user(apps, schema_editor):
    DeviceOwner = apps.get_model('kolibriauth', 'DeviceOwner')
    FacilityUser = apps.get_model('kolibriauth', 'FacilityUser')
    Facility = apps.get_model('kolibriauth', 'Facility')
    default_facility = Facility.objects.all().first()
    DevicePermissions = apps.get_model('device', 'DevicePermissions')
    DeviceSettings = apps.get_model('device', 'DeviceSettings')
    from kolibri.auth.models import FacilityUser as RealFacilityUser, Facility as RealFacility
    real_default_facility = RealFacility.get_default_facility()
    # Can't do much if no facilities exist, as no facility to FK the users onto
    if default_facility:
        for device_owner in DeviceOwner.objects.all():
            uuid = RealFacilityUser(username=device_owner.username, facility=real_default_facility).calculate_uuid()
            dataset_id = real_default_facility.dataset_id
            superuser = FacilityUser.objects.create(
                username=device_owner.username,
                password=device_owner.password,
                facility=default_facility,
                full_name=device_owner.full_name,
                date_joined=device_owner.date_joined,
                id=uuid,
                dataset_id=dataset_id,
            )
            DevicePermissions.objects.create(user=superuser, is_superuser=True)
        # Finally, set the is_provisioned flag
        settings, created = DeviceSettings.objects.get_or_create(is_provisioned=True)


class Migration(migrations.Migration):

    dependencies = [
        ('kolibriauth', '0003_auto_20170621_0958'),
        ('device', '0001_initial')
    ]

    operations = [
        migrations.RunPython(device_owner_to_super_user, migrations.RunPython.noop),
        migrations.DeleteModel(
            name='DeviceOwner',
        ),
    ]

Fix bugs in DeviceOwner data migration.