Skip to content

Commit

Permalink
[#1633] Migration removing unused keywords
Browse files Browse the repository at this point in the history
Keywords that aren't used in any project are deleted.
  • Loading branch information
zzgvh committed Aug 20, 2015
1 parent b0ba0a2 commit a68f480
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions akvo/rsr/migrations/0025_auto_20150820_0144.py
Original file line number Diff line number Diff line change
@@ -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
),
]

0 comments on commit a68f480

Please sign in to comment.