Skip to content

Commit

Permalink
Merge pull request #1838 from akvo/#1825_keywords_cleanup
Browse files Browse the repository at this point in the history
[#1825] Management command removing duplicate keywords
  • Loading branch information
KasperBrandt committed Oct 5, 2015
2 parents 342dad6 + d0f70c7 commit abd57ad
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
6 changes: 6 additions & 0 deletions akvo/rsr/management/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-

# 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 >.

6 changes: 6 additions & 0 deletions akvo/rsr/management/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-

# 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 >.

51 changes: 51 additions & 0 deletions akvo/rsr/management/commands/merge_keywords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-

# 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 >.

from django.core.management.base import BaseCommand
from ...models import Project, Partnership, Keyword


class Command(BaseCommand):
help = "Script for merging and purging keywords"

def handle(self, *args, **options):
# This dict holds the old keywords as keys and the new ones as values
mergers = {
'2150:WvW 2015': 'wvw2015', '912:SRHR Alliance': 'SRHR Alliance',
'2151:Walking for Water 2015': 'wfw2015', '1151:WvW 2014': 'wvw2014',
'272:Connect4Change': 'Connect4Change', '1684:YEP Water': 'YEP', '444:WvW 2012': 'WvW',
'1061:WASH Liberia': 'WASH Liberia', '487:Walking for Water 2012': 'WfW',
'wfw2012': 'WfW', 'wfw2013': 'WfW', '829:Walking for Water 2013': 'WfW',
'1060:Football for Water': 'Football for Water',
'275:Dutch WASH Alliance': 'WASH Alliance', '1443:Walking for Water 2014': 'wfw2014',
'815:WvW 2013': 'WvW', '2925:WvW 2016': 'wvw2016', 'wvw2012': 'WvW', 'wvw2013': 'WvW',
'ttc': False, 'ttc-c4c': False
}

#setup a dict with all keyword objects
keyword_objects = {}
for key in mergers.keys():
keyword_objects[key], _ = Keyword.objects.get_or_create(label=key)
if mergers[key]:
keyword_objects[mergers[key]], _ = Keyword.objects.get_or_create(label=mergers[key])

for project in Project.objects.all().prefetch_related('keywords'):
for keyword in project.keywords.all():
if mergers.get(keyword.label, False):
self.stdout.write(u"Adding keyword {} to project {}:{}".format(
keyword_objects[mergers[keyword.label]], project.id, project.title
))
project.keywords.add(keyword_objects[mergers[keyword.label]])
self.stdout.write(u"Removing keyword {} from project {}:{}".format(
keyword.label, project.id, project.title
))
project.keywords.remove(keyword)

for key in mergers.keys():
self.stdout.write(u"Deleting keyword {}".format(key))
Keyword.objects.get(label=key).delete()


0 comments on commit abd57ad

Please sign in to comment.