from __future__ import unicode_literals

from django.db import migrations
import logging

logger = logging.getLogger(__file__)


def update_comment_root_target(state, *args, **kwargs):
    Comment = state.get_model('osf', 'comment')
    comments = Comment.objects.filter(is_deleted=False)
    count = 0
    for comment in comments:
        if comment.root_target:
            if hasattr(comment.root_target.referent, 'is_deleted') and comment.root_target.referent.is_deleted:
                comment.root_target = None
                comment.save()
                count += 1
            if hasattr(comment.root_target.referent, 'deleted') and comment.root_target.referent.deleted:
                comment.root_target = None
                comment.save()
                count += 1
    logger.info('Total download number of commnet migrated is {}.'.format(count))


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0094_update_preprintprovider_group_auth'),
    ]

    operations = [
        migrations.RunPython(update_comment_root_target)
    ]
from __future__ import unicode_literals
import logging

from django.db import migrations
from django_bulk_update.helper import bulk_update

logger = logging.getLogger(__file__)


def update_comment_root_target(state, *args, **kwargs):
    Comment = state.get_model('osf', 'comment')
    comments = Comment.objects.exclude(is_deleted=True).select_related('root_target')
    logger.info('{} comments to check'.format(comments.count()))
    comments_to_update = []
    for comment in comments:
        if comment.root_target:
            root_target_ctype = comment.root_target.content_type
            root_target_model_cls = state.get_model(root_target_ctype.app_label, root_target_ctype.model)
            root_target = root_target_model_cls.objects.get(pk=comment.root_target.object_id)
            if hasattr(root_target, 'is_deleted') and root_target.is_deleted:
                logger.info('{} is deleted. Setting Comment {} root_target to None'.format(root_target, comment.pk))
                comment.root_target = None
                comments_to_update.append(comment)
            if hasattr(root_target, 'deleted') and root_target.deleted:
                logger.info('{} is deleted. Setting Comment {} root_target to None'.format(root_target, comment.pk))
                comment.root_target = None
                comments_to_update.append(comment)
    bulk_update(comments_to_update, update_fields=['root_target'])
    logger.info('Total comments migrated: {}.'.format(len(comments_to_update)))


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0094_update_preprintprovider_group_auth'),
    ]

    operations = [
        migrations.RunPython(update_comment_root_target)
    ]

Fix loading root_target in migration
Fix loading root_target in migration

Migrations don't allow accessing generic foreign keys, so we
need to load root_target manually.

Also:

* add more logging
* Bulk-update comments

  from __future__ import unicode_literals
+ import logging
  
  from django.db import migrations
- import logging
+ from django_bulk_update.helper import bulk_update
  
  logger = logging.getLogger(__file__)
  
  
  def update_comment_root_target(state, *args, **kwargs):
      Comment = state.get_model('osf', 'comment')
-     comments = Comment.objects.filter(is_deleted=False)
-     count = 0
+     comments = Comment.objects.exclude(is_deleted=True).select_related('root_target')
+     logger.info('{} comments to check'.format(comments.count()))
+     comments_to_update = []
      for comment in comments:
          if comment.root_target:
+             root_target_ctype = comment.root_target.content_type
+             root_target_model_cls = state.get_model(root_target_ctype.app_label, root_target_ctype.model)
+             root_target = root_target_model_cls.objects.get(pk=comment.root_target.object_id)
-             if hasattr(comment.root_target.referent, 'is_deleted') and comment.root_target.referent.is_deleted:
+             if hasattr(root_target, 'is_deleted') and root_target.is_deleted:
+                 logger.info('{} is deleted. Setting Comment {} root_target to None'.format(root_target, comment.pk))
                  comment.root_target = None
-                 comment.save()
-                 count += 1
-             if hasattr(comment.root_target.referent, 'deleted') and comment.root_target.referent.deleted:
+                 comments_to_update.append(comment)
+             if hasattr(root_target, 'deleted') and root_target.deleted:
+                 logger.info('{} is deleted. Setting Comment {} root_target to None'.format(root_target, comment.pk))
                  comment.root_target = None
-                 comment.save()
-                 count += 1
-     logger.info('Total download number of commnet migrated is {}.'.format(count))
+                 comments_to_update.append(comment)
+     bulk_update(comments_to_update, update_fields=['root_target'])
+     logger.info('Total comments migrated: {}.'.format(len(comments_to_update)))
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('osf', '0094_update_preprintprovider_group_auth'),
      ]
  
      operations = [
          migrations.RunPython(update_comment_root_target)
      ]
+ 
Fix loading root_target in migration
## Code Before:
from __future__ import unicode_literals

from django.db import migrations
import logging

logger = logging.getLogger(__file__)


def update_comment_root_target(state, *args, **kwargs):
    Comment = state.get_model('osf', 'comment')
    comments = Comment.objects.filter(is_deleted=False)
    count = 0
    for comment in comments:
        if comment.root_target:
            if hasattr(comment.root_target.referent, 'is_deleted') and comment.root_target.referent.is_deleted:
                comment.root_target = None
                comment.save()
                count += 1
            if hasattr(comment.root_target.referent, 'deleted') and comment.root_target.referent.deleted:
                comment.root_target = None
                comment.save()
                count += 1
    logger.info('Total download number of commnet migrated is {}.'.format(count))


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0094_update_preprintprovider_group_auth'),
    ]

    operations = [
        migrations.RunPython(update_comment_root_target)
    ]
## Instruction:
Fix loading root_target in migration
## Code After:
from __future__ import unicode_literals
import logging

from django.db import migrations
from django_bulk_update.helper import bulk_update

logger = logging.getLogger(__file__)


def update_comment_root_target(state, *args, **kwargs):
    Comment = state.get_model('osf', 'comment')
    comments = Comment.objects.exclude(is_deleted=True).select_related('root_target')
    logger.info('{} comments to check'.format(comments.count()))
    comments_to_update = []
    for comment in comments:
        if comment.root_target:
            root_target_ctype = comment.root_target.content_type
            root_target_model_cls = state.get_model(root_target_ctype.app_label, root_target_ctype.model)
            root_target = root_target_model_cls.objects.get(pk=comment.root_target.object_id)
            if hasattr(root_target, 'is_deleted') and root_target.is_deleted:
                logger.info('{} is deleted. Setting Comment {} root_target to None'.format(root_target, comment.pk))
                comment.root_target = None
                comments_to_update.append(comment)
            if hasattr(root_target, 'deleted') and root_target.deleted:
                logger.info('{} is deleted. Setting Comment {} root_target to None'.format(root_target, comment.pk))
                comment.root_target = None
                comments_to_update.append(comment)
    bulk_update(comments_to_update, update_fields=['root_target'])
    logger.info('Total comments migrated: {}.'.format(len(comments_to_update)))


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0094_update_preprintprovider_group_auth'),
    ]

    operations = [
        migrations.RunPython(update_comment_root_target)
    ]
