# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2018-10-31 23:40
from __future__ import unicode_literals

from django.db import migrations
from config.restype import RESTYPE


def updatesdstorageaddress(apps, schema_editor):
    ConfResource = apps.get_model('config', 'ConfResource')
    applied = ConfResource.objects.filter(type_id=RESTYPE['SDAddresses']).count()
    if applied == 0:
        ConfComponent = apps.get_model('config', 'ConfComponent')
        ConfParameter = apps.get_model('config', 'ConfParameter')
        sdcompid = ConfComponent.objects.filter(type='S')
        for sd in sdcompid:
            sdresid = ConfResource.objects.filter(compid=sd, type__name='Storage')
            for sdid in sdresid:
                addrquery = ConfParameter.objects.filter(resid=sdid, name='SDAddress')
                portquery = ConfParameter.objects.filter(resid=sdid, name='SDPort')
                address = addrquery[0].value
                addrquery.delete()
                portquery.delete()
                sdaddrsid = ConfResource(compid=sd, name='', sub=sdid.resid, type_id=RESTYPE['SDAddresses'], description='')
                sdaddrsid.save()
                ipsid = ConfResource(compid=sd, name='', sub=sdaddrsid.resid, type_id=RESTYPE['IP'], description='')
                ipsid.save()
                a = ConfParameter(resid=ipsid, name='Addr', value=address)
                a.save()
                p = ConfParameter(resid=ipsid, name='Port', value=9103)
                p.save()


class Migration(migrations.Migration):

    dependencies = [
        ('config', '0002_confrtype_equ'),
    ]

    operations = [
        migrations.RunPython(updatesdstorageaddress),
    ]

Add automatic SDAddress migration to new version.
Add automatic SDAddress migration to new version.
