# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-12-31 19:53
from __future__ import unicode_literals

from django.db import migrations


def transfer_profil(apps, schema_editor):
    db_alias = schema_editor.connection.alias
    port = apps.get_model("topologie", "Port")
    profil = apps.get_model("topologie", "PortProfile")
    vlan = apps.get_model("machines", "Vlan")
    port_list = port.objects.using(db_alias).all()
    profil_nothing = profil.objects.using(db_alias).create(name='nothing', profil_default='nothing', radius_type='NO')
    profil_uplink = profil.objects.using(db_alias).create(name='uplink', profil_default='uplink', radius_type='NO')
    profil_machine = profil.objects.using(db_alias).create(name='asso_machine', profil_default='asso_machine', radius_type='NO')
    profil_room = profil.objects.using(db_alias).create(name='room', profil_default='room', radius_type='NO')
    profil_borne = profil.objects.using(db_alias).create(name='accesspoint', profil_default='accesspoint', radius_type='NO')
    for vlan_instance in vlan.objects.using(db_alias).all():
        if port.objects.using(db_alias).filter(vlan_force=vlan_instance):
            custom_profil = profil.objects.using(db_alias).create(name='vlan-force-' + str(vlan_instance.vlan_id), radius_type='NO', vlan_untagged=vlan_instance)
            port.objects.using(db_alias).filter(vlan_force=vlan_instance).update(custom_profil=custom_profil)
    if port.objects.using(db_alias).filter(room__isnull=False).filter(radius='STRICT').count() > port.objects.using(db_alias).filter(room__isnull=False).filter(radius='NO').count() and port.objects.using(db_alias).filter(room__isnull=False).filter(radius='STRICT').count() > port.objects.using(db_alias).filter(room__isnull=False).filter(radius='COMMON').count():
        profil_room.radius_type = 'MAC-radius'
        profil_room.radius_mode = 'STRICT'
        common_profil = profil.objects.using(db_alias).create(name='mac-radius-common', radius_type='MAC-radius', radius_mode='COMMON')
        no_rad_profil = profil.objects.using(db_alias).create(name='no-radius', radius_type='NO')
        port.objects.using(db_alias).filter(room__isnull=False).filter(radius='COMMON').update(custom_profil=common_profil)
        port.objects.using(db_alias).filter(room__isnull=False).filter(radius='NO').update(custom_profil=no_rad_profil)
    elif port.objects.using(db_alias).filter(room__isnull=False).filter(radius='COMMON').count() > port.objects.using(db_alias).filter(room__isnull=False).filter(radius='NO').count() and port.objects.using(db_alias).filter(room__isnull=False).filter(radius='COMMON').count() > port.objects.using(db_alias).filter(room__isnull=False).filter(radius='STRICT').count():
        profil_room.radius_type = 'MAC-radius'
        profil_room.radius_mode = 'COMMON'
        strict_profil = profil.objects.using(db_alias).create(name='mac-radius-strict', radius_type='MAC-radius', radius_mode='STRICT')
        no_rad_profil = profil.objects.using(db_alias).create(name='no-radius', radius_type='NO')
        port.objects.using(db_alias).filter(room__isnull=False).filter(radius='STRICT').update(custom_profil=strict_profil)
        port.objects.using(db_alias).filter(room__isnull=False).filter(radius='NO').update(custom_profil=no_rad_profil)
    else:
        strict_profil = profil.objects.using(db_alias).create(name='mac-radius-strict', radius_type='MAC-radius', radius_mode='STRICT')
        common_profil = profil.objects.using(db_alias).create(name='mac-radius-common', radius_type='MAC-radius', radius_mode='COMMON')
        port.objects.using(db_alias).filter(room__isnull=False).filter(radius='STRICT').update(custom_profil=strict_profil)
        port.objects.using(db_alias).filter(room__isnull=False).filter(radius='NO').update(custom_profil=common_profil)
    profil_room.save()


class Migration(migrations.Migration):

    dependencies = [
        ('topologie', '0063_port_custom_profil'),
    ]

    operations = [
    migrations.RunPython(transfer_profil),
    ]
