old_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.11.9 on 2018-03-12 18:25
  from __future__ import unicode_literals
  
  from django.conf import settings
  from django.db import migrations, models
  import django.db.models.deletion
  import osf.models.validators
  
  
  def divorce_preprints_from_nodes(apps, schema_editor):
      Preprint = apps.get_model('osf', 'PreprintService')
      PreprintContributor = apps.get_model('osf', 'PreprintContributor')
      for preprint in Preprint.objects.all():
          if preprint.node:
              preprint.title = preprint.node.title
              preprint.description = preprint.node.description
              preprint.creator = preprint.node.creator
              for contrib in preprint.node._contributors:
                  # make a PreprintContributor that points to the pp instead of the node
                      # primary_identifier_name = 'user__guids___id'
                      #
                      # read = models.BooleanField(default=False)
                      # write = models.BooleanField(default=False)
                      # admin = models.BooleanField(default=False)
                      # visible = models.BooleanField(default=False)
                      # user = models.ForeignKey('OSFUser', on_delete=models.CASCADE)
                  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?
  
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('osf', '0083_update_preprint_model_for_divorce'),
      ]
  
      operations = [
          migrations.RunPython(divorce_preprints_from_nodes)
      ]
  
new_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.11.9 on 2018-03-12 18:25
  from __future__ import unicode_literals
  
  from django.conf import settings
  from django.db import migrations, models
  import django.db.models.deletion
  import osf.models.validators
  
  
  def divorce_preprints_from_nodes(apps, schema_editor):
      Preprint = apps.get_model('osf', 'PreprintService')
      PreprintContributor = apps.get_model('osf', 'PreprintContributor')
      Preprint.objects.filter(node__isnull=False).update(title=F('node__title'), description=F('node__description'), creator=F('node__creator'))
      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)
      ]
  
subject: Optimize preprint updates