# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-03-12 18:25
from __future__ import unicode_literals

from django.db import migrations
from django.db import transaction

def divorce_preprints_from_nodes(apps, schema_editor):
    Preprint = apps.get_model('osf', 'PreprintService')
    PreprintContributor = apps.get_model('osf', 'PreprintContributor')
    # tried to use F() function here but F() doesn't support table joins
    # instead, using the following to make this transaction atomic
    with transaction.atomic():
        for preprint in Preprint.objects.filter(node__isnull=False):
            preprint.title = preprint.node.title
            preprint.description = preprint.node.description
            preprint.creator = preprint.node.creator
            preprint.save()
    for preprint in Preprint.objects.all():
        if preprint.node:
            # preprint.title = preprint.node.title
            # preprint.description = preprint.node.description
            # preprint.creator = preprint.node.creator
            # use bulk create
            for contrib in preprint.node._contributors:
                # make a PreprintContributor that points to the pp instead of the node
                new_contrib = PreprintContributor.objects.create()
                new_contrib.primary_identifier_name = contrib.primary_identifier_name
                new_contrib.read = contrib.read
                new_contrib.write = contrib.write
                new_contrib.admin = contrib.admin
                new_contrib.visible = contrib.visible
                new_contrib.user = contrib.user
                new_contrib.preprint = preprint
                new_contrib.save()
                preprint._contributors.add(new_contrib)
            # will existing nodes attached to preprints still by accessible? A: yes!
            preprint.save()


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0083_update_preprint_model_for_divorce'),
    ]

    operations = [
        migrations.RunPython(divorce_preprints_from_nodes)
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-03-12 18:25
from __future__ import unicode_literals

from django.db import migrations
from django.db import transaction

def divorce_preprints_from_nodes(apps, schema_editor):
    Preprint = apps.get_model('osf', 'PreprintService')
    PreprintContributor = apps.get_model('osf', 'PreprintContributor')
    # tried to use F() function here but F() doesn't support table joins
    # instead, using the following to make this transaction atomic
    with transaction.atomic():
        for preprint in Preprint.objects.filter(node__isnull=False):
            preprint.title = preprint.node.title
            preprint.description = preprint.node.description
            preprint.creator = preprint.node.creator
            preprint.save()
    for preprint in Preprint.objects.all():
        if preprint.node:
            # preprint.title = preprint.node.title
            # preprint.description = preprint.node.description
            # preprint.creator = preprint.node.creator
            # use bulk create
            for contrib in preprint.node.contributor_set.all():
                # make a PreprintContributor that points to the pp instead of the node
                new_contrib = PreprintContributor.objects.create(
                    preprint=preprint,
                    user=contrib.user,
                    read=contrib.read,
                    write=contrib.write,
                    admin=contrib.admin,
                    visible=contrib.visible
                )

                new_contrib.save()


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0083_update_preprint_model_for_divorce'),
    ]

    operations = [
        migrations.RunPython(divorce_preprints_from_nodes)
    ]

update the script