<commit_before>from optparse import make_option

from django.db import IntegrityError

from django.core.management.base import (
    BaseCommand,
    CommandError,
)

from apps.api.models import (
    Group,
)


class Command(BaseCommand):
    help = "Merge selected gorups"
    option_list = BaseCommand.option_list + (
        make_option(
            "-o",
            "--old",
            dest="old",
            help="specify old pk",
        ),
    )
    option_list = option_list + (
        make_option(
            "-n",
            "--new",
            dest="new",
            help="specify new pk",
        ),
    )

    def handle(self, *args, **options):
        # make sure file option is present
        if options['old'] is None:
            raise CommandError("Option `--old=...` must be specified.")

        if options['new'] is None:
            raise CommandError("Option `--new=...` must be specified.")

        # make sure both groups exist
        try:
            new_group = Group.objects.get(pk=options['new'])
        except Group.DoesNotExist:
            raise CommandError("Target group does not exist.")
        try:
            old_group = Group.objects.get(pk=options['old'])
        except Group.DoesNotExist:
            raise CommandError("Subject group does not exist.")

        # perform de-dup
        contestants = old_group.contestants.all()
        if not contestants:
            old_group.delete()
            return 'No contesants to move.'

        for contestant in contestants:
            contestant.group = new_group
            try:
                contestant.save()
            except IntegrityError:
                raise CommandError(
                    "Contestant {0} already exists.  Merge manually".format(contestant.id)
                )

        # remove redundant group
        try:
            old_group.delete()
        except Exception as e:
            raise CommandError("Error deleted old group: {0}".format(e))
<commit_msg>Update merge command<commit_after>from optparse import make_option

from django.db import IntegrityError

from django.core.management.base import (
    BaseCommand,
    CommandError,
)

from apps.api.models import (
    Group,
)


class Command(BaseCommand):
    help = "Merge selected groups by name"
    option_list = BaseCommand.option_list + (
        make_option(
            "-o",
            "--old",
            dest="old",
            help="specify old name",
        ),
    )
    option_list = option_list + (
        make_option(
            "-n",
            "--new",
            dest="new",
            help="specify new name",
        ),
    )

    def handle(self, *args, **options):
        # make sure file option is present
        if options['old'] is None:
            raise CommandError("Option `--old=...` must be specified.")

        if options['new'] is None:
            raise CommandError("Option `--new=...` must be specified.")

        # make sure both groups exist
        try:
            new_group = Group.objects.get(
                name__iexact=options['new'],
            )
        except Group.DoesNotExist:
            raise CommandError("Target group does not exist.")
        try:
            old_group = Group.objects.get(
                name__iexact=options['old'],
            )
        except Group.DoesNotExist:
            raise CommandError("Subject group does not exist.")

        # perform de-dup
        contestants = old_group.contestants.all()
        if not contestants:
            old_group.delete()
            return 'No contesants to move.'

        for contestant in contestants:
            contestant.group = new_group
            try:
                contestant.save()
            except IntegrityError:
                raise CommandError(
                    "Contestant {0} already exists.  Merge manually".format(contestant)
                )

        # remove redundant group
        try:
            old_group.delete()
        except Exception as e:
            raise CommandError("Error deleted old group: {0}".format(e))

{'fraction_non_alphanumeric': 0.09907045009784736, 'fraction_numerical': 0.0009784735812133072, 'mean_word_length': 1.6725490196078432, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4259127', 'n_tokens_mistral': 1087, 'n_tokens_neox': 1061, 'n_words': 294}