<gh_stars>1-10
# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2017-10-24 08:18
from __future__ import unicode_literals

from django.db import migrations, connection

from bluebottle.clients import properties


def migrate_project_settings(apps, schema_editor):
    ProjectPlatformSettings = apps.get_model('projects', 'ProjectPlatformSettings')
    ProjectSearchFilter = apps.get_model('projects', 'ProjectSearchFilter')
    Client = apps.get_model('clients', 'Client')

    project_settings, _ = ProjectPlatformSettings.objects.get_or_create()

    tenant = Client.objects.get(schema_name=connection.tenant.schema_name)
    properties.set_tenant(tenant)

    project_settings.create_types = properties.PROJECT_CREATE_TYPES
    project_settings.create_flow = properties.PROJECT_CREATE_FLOW
    project_settings.contact_method = properties.PROJECT_CONTACT_METHOD
    project_settings.contact_types = properties.PROJECT_CONTACT_TYPES

    # Remove all filters
    ProjectSearchFilter.objects.all().delete()

    for filt in properties.SEARCH_OPTIONS['filters']['projects']:
        project_filter = ProjectSearchFilter.objects.create(
            project_settings=project_settings,
            name=filt['name']
        )
        for k,v in list(filt.items()):
            if filt['name'] == 'type' and k == 'values':
                v = ",".join(v)
            setattr(project_filter, k, v)
        project_filter.save()

    project_settings.save()


def dummy(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('projects', '0048_auto_20171024_1052'),
    ]

    operations = [
        migrations.RunPython(migrate_project_settings, dummy)
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2017-10-24 08:18
from __future__ import unicode_literals

from django.db import migrations, connection

from bluebottle.clients import properties


def migrate_project_settings(apps, schema_editor):
    ProjectPlatformSettings = apps.get_model('projects', 'ProjectPlatformSettings')
    ProjectSearchFilter = apps.get_model('projects', 'ProjectSearchFilter')
    Client = apps.get_model('clients', 'Client')

    project_settings, _ = ProjectPlatformSettings.objects.get_or_create()

    tenant = Client.objects.get(schema_name=connection.tenant.schema_name)
    properties.set_tenant(tenant)

    project_settings.create_types = properties.PROJECT_CREATE_TYPES
    project_settings.create_flow = properties.PROJECT_CREATE_FLOW
    project_settings.contact_method = properties.PROJECT_CONTACT_METHOD
    project_settings.contact_types = properties.PROJECT_CONTACT_TYPES

    # Remove all filters
    ProjectSearchFilter.objects.all().delete()

    for filt in properties.SEARCH_OPTIONS['filters']['projects']:
        project_filter = ProjectSearchFilter.objects.create(
            project_settings=project_settings,
            name=filt['name']
        )
        for k,v in list(filt.items()):
            if filt['name'] == 'type' and k == 'values':
                v = ",".join(v)
            setattr(project_filter, k, v)
        project_filter.save()

    project_settings.save()


def dummy(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('projects', '0048_auto_20171024_1052'),
    ]

    operations = [
        migrations.RunPython(migrate_project_settings, dummy)
    ]
# -*- coding: utf-8 -*- # Generated by Django 1.10.8 on 2017-10-24 08:18 # Remove all filters