<commit_before><commit_msg>[#4978] Remove Dutch foreign ministry org<commit_after>#!/usr/bin/env python3

# Akvo Reporting is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
import warnings

from django.core.management.base import BaseCommand
from django.db import transaction

from akvo.rsr.models import (
    Organisation,
)


class Command(BaseCommand):
    help = "Remove Netherlands Foreign Ministry which is a duplicate of the Dutch Foreign Ministry"

    def add_arguments(self, parser):
        parser.add_argument(
            "--dry-run", action="store_true", help="No action will be taken in the DB"
        )

    def handle(self, *args, **options):
        try:
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                self._handle(*args, **options)
        except InterruptedError:
            print("DRY RUN: No action was taken")

    @transaction.atomic()
    def _handle(self, *args, **options):
        dry_run = options.get("dry_run")
        if dry_run:
            print("DRY RUN: No action will be taken")

        original_org = Organisation.objects.get(id=464)
        duplicate_org = Organisation.objects.get(id=4596)

        self.stdout.write("Migrating partnerships of '%s' to '%s'" % (duplicate_org, original_org))
        for partnership in duplicate_org.partnerships.all().order_by("project__id"):
            partnership.organisation = original_org
            partnership.save()
            project = partnership.project
            self.stdout.write("\tPartnership(%s): %s - %s" % (partnership.id, project.id, project.title))

        self.stdout.write("Deleting org '%s'" % (duplicate_org, ))
        duplicate_org.delete()

        if dry_run:
            raise InterruptedError()
{'fraction_non_alphanumeric': 0.1054508405501783, 'fraction_numerical': 0.006113092205807438, 'mean_word_length': 2.8585461689587426, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '2561448', 'n_tokens_mistral': 556, 'n_tokens_neox': 535, 'n_words': 159}