<commit_before># -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-02-22 20:39
from __future__ import unicode_literals

import time
import logging
import progressbar
from django.db import connection, migrations
from django.db.models import Q
from django.contrib.contenttypes.models import ContentType
from bulk_update.helper import bulk_update
from addons.wiki.models import WikiPage, NodeWikiPage, WikiVersion
from osf.models import Comment, Guid, AbstractNode

logger = logging.getLogger(__name__)

# Cache of WikiPage id => guid, of the form
# {
#     <id>: <guid_pk>
#
# }
WIKI_PAGE_GUIDS = {}

def reverse_func(state, schema):
    """
    Reverses NodeWikiPage migration. Repoints guids back to each NodeWikiPage,
    repoints comment_targets, comments_viewed_timestamps, and deletes all WikiVersions and WikiPages
    """
    nwp_content_type_id = ContentType.objects.get_for_model(NodeWikiPage).id

    nodes = AbstractNode.objects.exclude(wiki_pages_versions={})
    progress_bar = progressbar.ProgressBar(maxval=nodes.count()).start()
    for i, node in enumerate(nodes, 1):
        progress_bar.update(i)
        for wiki_key, version_list in node.wiki_pages_versions.iteritems():
            if version_list:
                for index, version in enumerate(version_list):
                    nwp = NodeWikiPage.objects.filter(former_guid=version).include(None)[0]
                    wp = WikiPage.load(version)
                    guid = migrate_guid_referent(Guid.load(version), nwp, nwp_content_type_id)
                    guid.save()
                    nwp = guid.referent
                move_comment_target(Guid.load(wp._id), nwp)
                update_comments_viewed_timestamp(node, wp._id, nwp)
    progress_bar.finish()
    WikiVersion.objects.all().delete()
    WikiPage.objects.all().delete()
    logger.info('NodeWikiPages restored and WikiVersions and WikiPages removed.')


def move_comment_target(current_guid, desired_target):
    """Move the comment's target from the current target to the desired target"""
    desired_target_guid_id = Guid.load(desired_target.former_guid).id
    if Comment.objects.filter(Q(root_target=current_guid) | Q(target=current_guid)).exists():
        Comment.objects.filter(root_target=current_guid).update(root_target_id=desired_target_guid_id)
        Comment.objects.filter(target=current_guid).update(target_id=desired_target_guid_id)
    return

def update_comments_viewed_timestamp(node, current_wiki_guid, desired_wiki_object):
    """Replace the current_wiki_object keys in the comments_viewed_timestamp dict with the desired wiki_object_id """
    users_pending_save = []<commit_msg>Update docstring.<commit_after># -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-02-22 20:39
from __future__ import unicode_literals

import time
import logging
import progressbar
from django.db import connection, migrations
from django.db.models import Q
from django.contrib.contenttypes.models import ContentType
from bulk_update.helper import bulk_update
from addons.wiki.models import WikiPage, NodeWikiPage, WikiVersion
from osf.models import Comment, Guid, AbstractNode

logger = logging.getLogger(__name__)

# Cache of WikiPage id => guid, of the form
# {
#     <id>: <guid_pk>
#
# }
WIKI_PAGE_GUIDS = {}

def reverse_func(state, schema):
    """
    Reverses NodeWikiPage migration. Repoints guids back to each NodeWikiPage,
    repoints comment_targets, comments_viewed_timestamps, and deletes all WikiVersions and WikiPages
    """
    nwp_content_type_id = ContentType.objects.get_for_model(NodeWikiPage).id

    nodes = AbstractNode.objects.exclude(wiki_pages_versions={})
    progress_bar = progressbar.ProgressBar(maxval=nodes.count()).start()
    for i, node in enumerate(nodes, 1):
        progress_bar.update(i)
        for wiki_key, version_list in node.wiki_pages_versions.iteritems():
            if version_list:
                for index, version in enumerate(version_list):
                    nwp = NodeWikiPage.objects.filter(former_guid=version).include(None)[0]
                    wp = WikiPage.load(version)
                    guid = migrate_guid_referent(Guid.load(version), nwp, nwp_content_type_id)
                    guid.save()
                    nwp = guid.referent
                move_comment_target(Guid.load(wp._id), nwp)
                update_comments_viewed_timestamp(node, wp._id, nwp)
    progress_bar.finish()
    WikiVersion.objects.all().delete()
    WikiPage.objects.all().delete()
    logger.info('NodeWikiPages restored and WikiVersions and WikiPages removed.')


def move_comment_target(current_guid, desired_target):
    """
    Move the comment's target from the current target to the desired target
    Specifically for repointing WikiPage comments -> NodeWikiPage comments
    """
    desired_target_guid_id = Guid.load(desired_target.former_guid).id
    if Comment.objects.filter(Q(root_target=current_guid) | Q(target=current_guid)).exists():
        Comment.objects.filter(root_target=current_guid).update(root_target_id=desired_target_guid_id)
        Comment.objects.filter(target=current_guid).update(target_id=desired_target_guid_id)
    return

def update_comments_viewed_timestamp(node, current_wiki_guid, desired_wiki_object):
    """Replace the current_wiki_object keys in the comments_viewed_timestamp dict with the desired wiki_object_id """
    users_pending_save = []
{'fraction_non_alphanumeric': 0.08922963236652504, 'fraction_numerical': 0.007020136707925365, 'mean_word_length': 3.8951175406871608, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3628090', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1577, 'n_words': 356}