# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2018-01-22 15:34
from __future__ import unicode_literals

from django.db import migrations

from enterprise.utils import decrypt_string


def migrate_passwords(apps, schema_editor):
    """
    Migrate password fields to new django-fernet-fields encrypted fields.
    """
    EnterpriseCustomerReportingConfiguration = apps.get_model(
        'enterprise',
        'EnterpriseCustomerReportingConfiguration'
    )
    for config in EnterpriseCustomerReportingConfiguration.objects.all():
        if config.password:
            config.decrypted_password = decrypt_string(
                config.password,
                config.initialization_vector
            )
        if config.sftp_password:
            config.decrypted_sftp_password = decrypt_string(
                config.sftp_password,
                config.initialization_vector
            )
        config.save()

class Migration(migrations.Migration):

    dependencies = [
        ('enterprise', '0038_auto_20180122_1427'),
    ]

    operations = [
        migrations.RunPython(migrate_passwords),
    ]
