-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#4978] Remove Dutch foreign ministry org
The duplicate has many partnerships which are replaced by partnerships with the original org.
- Loading branch information
1 parent
4f660eb
commit 1f770ca
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
akvo/rsr/management/commands/remote_netherlands_foreign_ministry.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/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() |