# -*- 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


class Migration(migrations.Migration):

    dependencies = [
        ("users", "0065_auto_20171231_2053"),
        ("cotisations", "0028_auto_20171231_0007"),
        ("machines", "0071_auto_20171231_2100"),
        ("preferences", "0025_auto_20171231_2142"),
        ("topologie", "0033_auto_20171231_1743"),
    ]

    def transfer_permissions(apps, schema_editor):
        permission_groups = {
            "bofh": [
                "add_ban",
                "change_ban",
                "delete_ban",
                "view_ban",
                "add_club",
                "change_club",
                "delete_club",
                "add_user",
                "change_user",
                "change_user_force",
                "change_user_password",
                "change_user_shell",
                "view_user",
                "add_whitelist",
                "change_whitelist",
                "delete_whitelist",
                "view_whitelist",
            ],
            "bureau": [
                "add_logentry",
                "change_logentry",
                "delete_logentry",
                "add_group",
                "change_group",
                "delete_group",
                "add_permission",
                "change_permission",
                "delete_permission",
                "add_adherent",
                "change_adherent",
                "delete_adherent",
                "add_ban",
                "change_ban",
                "delete_ban",
                "view_ban",
                "add_club",
                "change_club",
                "delete_club",
                "add_listright",
                "change_listright",
                "delete_listright",
                "view_listright",
                "add_school",
                "change_school",
                "delete_school",
                "view_school",
                "add_user",
                "change_user",
                "change_user_force",
                "change_user_groups",
                "change_user_password",
                "change_user_shell",
                "change_user_state",
                "delete_user",
                "view_user",
                "add_whitelist",
                "change_whitelist",
                "delete_whitelist",
                "view_whitelist",
            ],
            "cableur": [
                "add_logentry",
                "view_article",
                "add_banque",
                "change_banque",
                "delete_banque",
                "view_banque",
                "add_cotisation",
                "change_cotisation",
                "delete_cotisation",
                "view_cotisation",
                "add_facture",
                "can_create",
                "can_delete",
                "can_edit",
                "can_view",
                "can_view_all",
                "change_facture",
                "delete_facture",
                "view_facture",
                "view_paiement",
                "add_vente",
                "change_vente",
                "delete_vente",
                "view_vente",
                "add_domain",
                "change_domain",
                "delete_domain",
                "view_domain",
                "use_all_extension",
                "view_extension",
                "add_interface",
                "change_interface",
                "delete_interface",
                "view_interface",
                "view_iplist",
                "view_iptype",
                "add_machine",
                "change_machine",
                "view_machine",
                "view_machinetype",
                "view_mx",
                "view_nas",
                "view_ns",
                "view_ouvertureportlist",
                "view_service",
                "view_soa",
                "view_soa",
                "view_txt",
                "view_vlan",
                "view_assooption",
                "view_generaloption",
                "view_mailmessageoption",
                "view_optionalmachine",
                "view_optionaltopologie",
                "view_optionaluser",
                "view_service",
                "view_constructorswitch",
                "view_modelswitch",
                "view_port",
                "view_room",
                "view_stack",
                "view_switch",
                "add_adherent",
                "change_adherent",
                "view_ban",
                "add_club",
                "change_club",
                "view_listright",
                "add_school",
                "change_school",
                "delete_school",
                "view_school",
                "view_serviceuser",
                "add_user",
                "change_user",
                "change_user_force",
                "change_user_password",
                "view_user",
                "add_whitelist",
                "change_whitelist",
                "delete_whitelist",
                "view_whitelist",
            ],
            "tresorier": [
                "add_article",
                "change_article",
                "delete_article",
                "view_article",
                "add_banque",
                "change_banque",
                "delete_banque",
                "view_banque",
                "add_cotisation",
                "change_all_cotisation",
                "change_cotisation",
                "delete_cotisation",
                "view_cotisation",
                "add_facture",
                "can_change_control",
                "can_change_pdf",
                "can_create",
                "can_delete",
                "can_edit",
                "can_view",
                "can_view_all",
                "change_all_facture",
                "change_facture",
                "change_facture_control",
                "change_facture_pdf",
                "delete_facture",
                "view_facture",
                "add_paiement",
                "change_paiement",
                "delete_paiement",
                "view_paiement",
                "add_vente",
                "change_all_vente",
                "change_vente",
                "delete_vente",
                "view_vente",
            ],
            "admin": [
                "add_logentry",
                "change_logentry",
                "delete_logentry",
                "add_assooption",
                "change_assooption",
                "delete_assooption",
                "view_assooption",
                "add_generaloption",
                "change_generaloption",
                "delete_generaloption",
                "view_generaloption",
                "add_mailmessageoption",
                "change_mailmessageoption",
                "delete_mailmessageoption",
                "view_mailmessageoption",
                "add_optionalmachine",
                "change_optionalmachine",
                "delete_optionalmachine",
                "view_optionalmachine",
                "add_optionaltopologie",
                "change_optionaltopologie",
                "delete_optionaltopologie",
                "view_optionaltopologie",
                "add_optionaluser",
                "change_optionaluser",
                "delete_optionaluser",
                "view_optionaluser",
                "add_service",
                "add_services",
                "change_service",
                "change_services",
                "delete_service",
                "delete_services",
                "view_service",
            ],
            "infra": [
                "add_domain",
                "change_domain",
                "delete_domain",
                "view_domain",
                "add_extension",
                "change_extension",
                "delete_extension",
                "use_all_extension",
                "view_extension",
                "add_interface",
                "change_interface",
                "delete_interface",
                "view_interface",
                "add_iplist",
                "change_iplist",
                "delete_iplist",
                "view_iplist",
                "add_iptype",
                "change_iptype",
                "delete_iptype",
                "use_all_iptype",
                "view_iptype",
                "add_machine",
                "change_machine",
                "change_machine_user",
                "delete_machine",
                "view_machine",
                "add_machinetype",
                "change_machinetype",
                "delete_machinetype",
                "use_all_machinetype",
                "view_machinetype",
                "add_mx",
                "change_mx",
                "delete_mx",
                "view_mx",
                "add_nas",
                "change_nas",
                "delete_nas",
                "view_nas",
                "add_ns",
                "change_ns",
                "delete_ns",
                "view_ns",
                "add_ouvertureport",
                "change_ouvertureport",
                "delete_ouvertureport",
                "add_ouvertureportlist",
                "change_ouvertureportlist",
                "delete_ouvertureportlist",
                "view_ouvertureportlist",
                "add_service",
                "change_service",
                "delete_service",
                "view_service",
                "add_service_link",
                "change_service_link",
                "delete_service_link",
                "add_soa",
                "change_soa",
                "delete_soa",
                "view_soa",
                "add_srv",
                "change_srv",
                "delete_srv",
                "view_soa",
                "add_text",
                "add_txt",
                "change_text",
                "change_txt",
                "delete_text",
                "delete_txt",
                "view_txt",
                "add_vlan",
                "change_vlan",
                "delete_vlan",
                "view_vlan",
                "add_constructorswitch",
                "change_constructorswitch",
                "delete_constructorswitch",
                "view_constructorswitch",
                "add_modelswitch",
                "change_modelswitch",
                "delete_modelswitch",
                "view_modelswitch",
                "add_port",
                "change_port",
                "delete_port",
                "view_port",
                "add_room",
                "change_room",
                "delete_room",
                "view_room",
                "add_stack",
                "change_stack",
                "delete_stack",
                "view_stack",
                "add_switch",
                "change_switch",
                "delete_switch",
                "view_switch",
                "add_listshell",
                "change_listshell",
                "delete_listshell",
                "add_serviceuser",
                "change_serviceuser",
                "delete_serviceuser",
                "view_serviceuser",
                "change_user",
                "view_user",
            ],
        }

        rights = apps.get_model("users", "ListRight")
        permissions = apps.get_model("auth", "Permission")
        groups = apps.get_model("auth", "Group")
        db_alias = schema_editor.connection.alias
        for group in permission_groups:
            lr_object = rights.objects.using(db_alias).filter(unix_name=group).first()
            if not lr_object:
                last = rights.objects.using(db_alias).all().order_by("gid").last()
                if last:
                    gid = last.gid + 1
                else:
                    gid = 501
                group_object = groups.objects.using(db_alias).create(name=group)
                lr_object = rights.objects.using(db_alias).create(
                    unix_name=group, gid=gid, group_ptr=group_object
                )
            lr_object = lr_object.group_ptr
            for permission in permission_groups[group]:
                perm = (
                    permissions.objects.using(db_alias)
                    .filter(codename=permission)
                    .first()
                )
                if perm:
                    lr_object.permissions.add(perm)
            lr_object.save()

    def untransfer_permissions(apps, schema_editor):
        return

    operations = [migrations.RunPython(transfer_permissions, untransfer_permissions)]
