<commit_before># This admin command is to save having to do a series of manual steps
# when merging to people in Pombola.

import sys

from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand, CommandError
from django.core.urlresolvers import reverse
from django.db import transaction

import pombola.core.models as core_models
from pombola.images.models import Image
from pombola.core.views import PersonSpeakerMappingsMixin
from pombola.slug_helpers.models import SlugRedirect

from django.conf import settings

from optparse import make_option

from django.core.exceptions import ObjectDoesNotExist

def check_basic_fields(basic_fields, to_keep, to_delete):
    """Return False if any data might be lost on merging"""

    safe_to_delete = True
    for basic_field in basic_fields:
        delete_value = getattr(to_delete, basic_field)
        keep_value = getattr(to_keep, basic_field)
        if delete_value and (keep_value != delete_value):
            # i.e. there's some data that might be lost:
            safe_to_delete = False
            message = "Mismatch in '%s': '%s' ({%d}) and '%s' (%d)"
            print >> sys.stderr, message % (basic_field,
                                            keep_value,
                                            to_keep.id,
                                            delete_value,
                                            to_delete.id)
    return safe_to_delete


class Command(PersonSpeakerMappingsMixin, BaseCommand):

    help = "Merge two Person records into one, deleting one of the originals"
    option_list = BaseCommand.option_list + (
        make_option("--keep-person", dest="keep_person", type="string",
                    help="The ID or slug of the person to retain",
                    metavar="PERSON-ID"),
        make_option("--delete-person", dest="delete_person", type="string",
                    help="The ID or slug of the person to delete",
                    metavar="PERSON-ID"),<commit_msg>Fix equality check for summary fields.<commit_after># This admin command is to save having to do a series of manual steps
# when merging to people in Pombola.

import sys

from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand, CommandError
from django.core.urlresolvers import reverse
from django.db import transaction

import pombola.core.models as core_models
from pombola.images.models import Image
from pombola.core.views import PersonSpeakerMappingsMixin
from pombola.slug_helpers.models import SlugRedirect

from django.conf import settings

from optparse import make_option

from django.core.exceptions import ObjectDoesNotExist


def check_basic_fields(basic_fields, to_keep, to_delete):
    """Return False if any data might be lost on merging"""

    safe_to_delete = True
    for basic_field in basic_fields:
        if basic_field == 'summary':
            # We can't just check equality of summary fields because
            # they are MarkupField fields which don't have equality
            # helpfully defined (and they're always different objects
            # between two different speakers), so instead, check for
            # equality of the rendered content of the summary.

            delete_value = to_delete.summary.rendered
            keep_value = to_keep.summary.rendered
        else:
            delete_value = getattr(to_delete, basic_field)
            keep_value = getattr(to_keep, basic_field)

        if delete_value and (keep_value != delete_value):
            # i.e. there's some data that might be lost:
            safe_to_delete = False
            message = "Mismatch in '%s': '%s' ({%d}) and '%s' (%d)"
            print >> sys.stderr, message % (basic_field,
                                            keep_value,
                                            to_keep.id,
                                            delete_value,
                                            to_delete.id)
    return safe_to_delete


class Command(PersonSpeakerMappingsMixin, BaseCommand):

    help = "Merge two Person records into one, deleting one of the originals"
    option_list = BaseCommand.option_list + (
        make_option("--keep-person", dest="keep_person", type="string",
                    help="The ID or slug of the person to retain",
                    metavar="PERSON-ID"),
        make_option("--delete-person", dest="delete_person", type="string",
                    help="The ID or slug of the person to delete",
                    metavar="PERSON-ID"),
{'fraction_non_alphanumeric': 0.07307525010874293, 'fraction_numerical': 0.0, 'mean_word_length': 2.3447272727272725, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4887834', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1152, 'n_words': 394}