From bacd70687d63c89d40547c9643410502377429ba Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Fri, 13 Sep 2019 22:20:08 +1000
Subject: [PATCH] Management class for PartCategory import / export

---
 InvenTree/part/admin.py | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/InvenTree/part/admin.py b/InvenTree/part/admin.py
index 8b5e415019..bed6779f8e 100644
--- a/InvenTree/part/admin.py
+++ b/InvenTree/part/admin.py
@@ -41,8 +41,38 @@ class PartAdmin(ImportExportModelAdmin):
     list_display = ('full_name', 'description', 'total_stock', 'category')
 
 
+class PartCategoryResource(ModelResource):
+    """ Class for managing PartCategory data import/export """
+
+    parent = Field(attribute='parent', widget=widgets.ForeignKeyWidget(PartCategory))
+
+    default_location = Field(attribute='default_location', widget=widgets.ForeignKeyWidget(StockLocation))
+
+    parent_name = Field(attribute='parent__name', readonly=True)
+
+    class Meta:
+        model = PartCategory
+        skip_unchanged = True
+        report_skipped = False
+
+        exclude = [
+            # Exclude MPTT internal model fields
+            'lft', 'rght', 'tree_id', 'level',
+        ]
+
+    def after_import(self, dataset, result, using_transactions, dry_run, **kwargs):
+
+        super().after_import(dataset, result, using_transactions, dry_run, **kwargs)
+
+        print("Rebuilding PartCategory tree")
+        # Rebuild teh PartCategory tree
+        PartCategory.objects.rebuild()
+        print("Done!")
+
 class PartCategoryAdmin(ImportExportModelAdmin):
 
+    resource_class = PartCategoryResource
+
     list_display = ('name', 'pathstring', 'description')