From a68f480ec4969d053543785357e4e81d4d524530 Mon Sep 17 00:00:00 2001 From: Gabriel von Heijne Date: Thu, 20 Aug 2015 10:09:01 +0200 Subject: [PATCH] [#1633] Migration removing unused keywords Keywords that aren't used in any project are deleted. --- .../rsr/migrations/0025_auto_20150820_0144.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 akvo/rsr/migrations/0025_auto_20150820_0144.py diff --git a/akvo/rsr/migrations/0025_auto_20150820_0144.py b/akvo/rsr/migrations/0025_auto_20150820_0144.py new file mode 100644 index 0000000000..13bc9a0483 --- /dev/null +++ b/akvo/rsr/migrations/0025_auto_20150820_0144.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations + +def remove_unused_keys(apps, schema_editor): + Keyword = apps.get_model('rsr', 'Keyword') + Project = apps.get_model('rsr', 'Project') + print + + for keyword in Keyword.objects.all(): + projects = Project.objects.filter(keywords__exact=keyword) + if not projects: + print "Deleting keyword {}".format(keyword.label) + keyword.delete() + + print "Keyword distribution after migration:" + for keyword in Keyword.objects.all(): + projects = Project.objects.filter(keywords__exact=keyword) + print keyword.label, projects.count() + +class Migration(migrations.Migration): + + dependencies = [ + ('rsr', '0024_auto_20150819_1149'), + ] + + operations = [ + migrations.RunPython( + remove_unused_keys + ), + ]