# -*- coding: utf-8 -*-
# Generated by Django 1.11.11 on 2018-04-30 18:34
from __future__ import unicode_literals

from django.db import migrations, models ,connection

from osf.models import AbstractNode


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0099_merge_20180427_1109'),
    ]

    def add_default_access_requests_enabled(self, *args, **kwargs):
        # Get the date the original noderequest migration was applied
        sql = "SELECT applied from django_migrations WHERE app = 'osf' AND name = '0077_add_noderequest_model';"
        with connection.cursor() as cursor:
            cursor.execute(sql)
            date_noderequest_migration = cursor.fetchall()[0][0]

        # Get all projects created before that
        AbstractNode.objects.filter(created__lte=date_noderequest_migration).update(access_requests_enabled=True)

    def remove_default_access_requests_enabled(self, *args, **kwargs):
        # Get the date the original noderequest migration was applied
        sql = "SELECT applied from django_migrations WHERE app = 'osf' AND name = '0077_add_noderequest_model';"
        with connection.cursor() as cursor:
            cursor.execute(sql)
            date_noderequest_migration = cursor.fetchall()[0][0]

        # Get all projects created before that
        AbstractNode.objects.filter(created__lte=date_noderequest_migration).update(access_requests_enabled=None)

    operations = [
        migrations.AlterField(
            model_name='noderequestaction',
            name='permissions',
            field=models.CharField(choices=[(b'read', b'Read'), (b'write', b'Write'), (b'admin', b'Admin')], default=b'read', max_length=5),
        ),
        migrations.RunPython(add_default_access_requests_enabled, remove_default_access_requests_enabled),
    ]

Add default value of access requests enabled to exsisting projects made before model added
Add default value of access requests enabled to exsisting projects made before model added

[#PLAT-835]

Also alter permissions on noderequestaction model, since choices were altered from when the original migration was made
