From b65720f3497ca8efdf0bfece294a605d6defc86e Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Wed, 29 Aug 2018 15:47:54 +0100
Subject: [PATCH] Add categories to the projects

---
 .../management/commands/migrate_projects.py   | 21 +++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/opentech/public/projects/management/commands/migrate_projects.py b/opentech/public/projects/management/commands/migrate_projects.py
index 1b446bbba..3c91730ca 100644
--- a/opentech/public/projects/management/commands/migrate_projects.py
+++ b/opentech/public/projects/management/commands/migrate_projects.py
@@ -197,6 +197,24 @@ class Command(BaseCommand):
                 source=fund,
             ))
 
+        category_fields = [
+            'field_term_region',
+            'field_term_country',
+            'field_technology_attribute',
+            'field_proposal_theme',
+            'field_proposal_focus',
+            'field_proposal_beneficiaries',
+        ]
+        categories = {}
+        for category in category_fields:
+            terms = self.ensure_iterable(node[category])
+            for term in terms:
+                option = self.get_referenced_term(term['tid'])
+                if option:
+                    categories.setdefault(option.category.id, []).append(option.id)
+
+        project.categories = json.dumps(categories)
+
         try:
             if not project.get_parent():
                 self.parent_page.add_child(instance=project)
@@ -223,8 +241,7 @@ class Command(BaseCommand):
 
     def get_referenced_term(self, tid):
         try:
-            term = self.terms[tid]
-            return term.id
+            return self.terms[tid]
         except KeyError:
             return None
 
-- 
GitLab