# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2017-01-31 21:34
from __future__ import unicode_literals

from django.db.models import Q
from django.db import migrations
from django.contrib.auth.models import Group
from django.contrib.auth.models import Permission

import logging

logger = logging.getLogger(__file__)


def get_read_only_permissions():
    return Permission.objects.filter(
        Q(codename='view_node') |
        Q(codename='view_user') |
        Q(codename='view_conference') |
        Q(codename='view_spam') |
        Q(codename='view_metrics') |
        Q(codename='view_desk')
    )


def get_admin_permissions():
    return Permission.objects.filter(
        Q(codename='change_node') |
        Q(codename='delete_node') |
        Q(codename='change_user') |
        Q(codename='change_conference') |
        Q(codename='mark_spam')
    )


def add_group_permissions(*args):

    # Rename nodes_and_users group to read_only which makes more sense
    try:
        group = Group.objects.get(name='nodes_and_users')
        group.name = 'read_only'
        group.save()
        logger.info('nodes_and_users renamed to read_only')
    except Group.DoesNotExist:
        group, created = Group.objects.get_or_create(name='read_only')
        if created:
            logger.info('read_only group created')

    # Read only for nodes, users, meetings, and spam
    [group.permissions.add(perm) for perm in get_read_only_permissions()]
    group.save()
    logger.info('Node, user, spam and meeting permissions added to read only group')

    # Admin for nodes, users, meetings, and spam - reuse previous osf_admiin group
    admin_group, created = Group.objects.get_or_create(name='osf_admin')
    if created:
        logger.info('admin_user Group created')
    [admin_group.permissions.add(perm) for perm in get_read_only_permissions()]
    [admin_group.permissions.add(perm) for perm in get_admin_permissions()]
    group.save()
    logger.info('Administrator permissions for Node, user, spam and meeting permissions added to admin group')

    # Remove superfluous osf_group
    try:
        Group.objects.get(name='osf_group').delete()
    except Group.DoesNotExist:
        pass


def remove_group_permissions(*args):

    # reverse the naming for nodes_and_users
    group = Group.objects.get(name='read_only')
    [group.permissions.remmove(perm) for perm in get_read_only_permissions()]
    group.name = 'nodes_and_users'
    group.save()

    # remove permissions for osf admin group
    admin_group = Group.objects.get(name='osf_admin')
    [admin_group.permissions.remove(perm) for perm in get_read_only_permissions()]
    [admin_group.permissions.remove(perm) for perm in get_admin_permissions()]

    # re-create osf_group
    group, created = Group.objects.get_or_create(name='osf_group')
    if created:
        logger.info('osf_group created')


class Migration(migrations.Migration):

    dependencies = [
        ('base', '0002_groups'),
    ]

    operations = [
        migrations.RunPython(add_group_permissions, remove_group_permissions),
    ]

@@ -862,24 +862,194 @@
 m')%0A    )%0A%0A%0A
+def get_prereg_admin_permissions():%0A    return Permission.objects.filter(%0A        Q(codename='admin.view_prereg') %7C%0A        Q(codename='admin.administer_prereg')%0A    )%0A%0A%0A
 def add_grou
@@ -1066,24 +1066,24 @@
 ons(*args):%0A
-
 %0A    # Renam
@@ -1363,33 +1363,24 @@
        group
-, created
  = Group.obj
@@ -1420,32 +1420,8 @@
 y')%0A
-        if created:%0A    
     
@@ -2195,24 +2195,599 @@
 in group')%0A%0A
+    # rename prereg group to prereg_admin%0A    try:%0A        prereg_group = Group.objects.get(name='prereg')%0A        prereg_group.name = 'prereg_admin'%0A        prereg_group.save()%0A        logger.info('prereg renamed to prereg_admin')%0A    except Group.DoesNotExist:%0A        prereg_group = Group.objects.create(name='prereg_admin')%0A        logger.info('read_only group created')%0A%0A    %5Bprereg_group.permissions.add(perm) for perm in get_prereg_admin_permissions()%5D%0A    prereg_group.save()%0A    logger.info('Prereg read and administer permissions added to the prereg_admin group')%0A%0A
     # Remove
@@ -3353,32 +3353,32 @@
 _permissions()%5D%0A
-
     %5Badmin_group
@@ -3439,16 +3439,295 @@
 sions()%5D
+%0A    admin_group.save()%0A%0A    # reverse the naming for prereg to prereg_admin%0A    prereg_group = Group.objects.get(name='prereg_admin')%0A    %5Bprereg_group.permissions.remmove(perm) for perm in get_prereg_admin_permissions()%5D%0A    prereg_group.name = 'prereg'%0A    prereg_group.save()
 %0A%0A    # 

Add prereg permissions and group to migration