# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-01-10 21:24
from __future__ import unicode_literals

from django.db import migrations, models


def create_user_permissions(apps, schema_editor):
    Permission = apps.get_model("dataentry", "Permission")
    UserLocationPermission = apps.get_model("dataentry", "UserLocationPermission")
    Account = apps.get_model("accounts", "Account")
    
    perms = Permission.objects.all();
    accounts = Account.objects.all();
    
    for account in accounts:
        for perm in perms:
            if perm.account_permission_name is None:
                continue
            
            val = getattr(account, perm.account_permission_name)
            if val:
                ulp = UserLocationPermission()
                ulp.account = account
                ulp.permission = perm
                ulp.country = None
                ulp.station = None
                ulp.save()
                    

class Migration(migrations.Migration):

    dependencies = [
        ('dataentry', '0045_permission_userlocationpermission'),
    ]               

    operations = [
        migrations.AddField(
            model_name='permission',
            name='account_permission_name',
            field=models.CharField(max_length=100, null=True),
        ),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('IRF', 'VIEW','GLOBAL','permission_irf_view');"),
        migrations.RunSQL("INSERT INTO dataentry_permission  (permission_group, action, min_level, account_permission_name) values ('IRF', 'ADD','GLOBAL','permission_irf_add');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('IRF', 'EDIT','GLOBAL','permission_irf_edit');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('IRF', 'DELETE','GLOBAL','permission_irf_delete');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('IRF', 'APPROVE','GLOBAL',null);"),
        
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('VIF', 'VIEW','GLOBAL','permission_vif_view');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('VIF', 'ADD','GLOBAL','permission_vif_add');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('VIF', 'EDIT','GLOBAL','permission_vif_edit');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('VIF', 'DELETE','GLOBAL','permission_vif_edit');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('VIF', 'APPROVE','GLOBAL',null);"),
        
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('STATIONS', 'VIEW','GLOBAL','permission_border_stations_view');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('STATIONS', 'ADD','GLOBAL','permission_border_stations_add');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('STATIONS', 'EDIT','GLOBAL','permission_border_stations_edit');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('STATIONS', 'DELETE','GLOBAL','permission_border_stations_delete');"),
        
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('BUDGETS', 'VIEW','GLOBAL','permission_budget_view');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('BUDGETS', 'ADD','GLOBAL','permission_budget_add');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('BUDGETS', 'EDIT','GLOBAL','permission_budget_edit');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('BUDGETS', 'DELETE','GLOBAL','permission_budget_delete');"),
        
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('NOTIFICATIONS', 'INVESTIGATION','GLOBAL','permission_receive_investigation_alert');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('NOTIFICATIONS', 'LEGAL','GLOBAL','permission_receive_legal_alert');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('NOTIFICATIONS', 'MDF','GLOBAL','permission_can_receive_mdf');"),
        
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('PERSONS', 'TRAFFICKER MATCH','GLOBAL','permission_person_match');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('ACCOUNTS', 'MANAGE','GLOBAL','permission_accounts_manage');"),
        migrations.RunSQL("INSERT INTO dataentry_permission (permission_group, action, min_level, account_permission_name) values ('ADDRESSES', 'MANAGE','GLOBAL','permission_address2_manage');"),
        
        migrations.RunPython(create_user_permissions),
    ]
