From 23b814569a0c94b57e79cf39560ef24e20c32ba1 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 13 Sep 2019 22:44:50 +1000 Subject: [PATCH] Manager for importing StockLocation data --- InvenTree/part/admin.py | 8 +++----- InvenTree/stock/admin.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/InvenTree/part/admin.py b/InvenTree/part/admin.py index 04d00a7e66..12ebe77ca6 100644 --- a/InvenTree/part/admin.py +++ b/InvenTree/part/admin.py @@ -52,10 +52,10 @@ class PartCategoryResource(ModelResource): 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) + default_location = Field(attribute='default_location', widget=widgets.ForeignKeyWidget(StockLocation)) + class Meta: model = PartCategory skip_unchanged = True @@ -70,10 +70,8 @@ class PartCategoryResource(ModelResource): super().after_import(dataset, result, using_transactions, dry_run, **kwargs) - print("Rebuilding PartCategory tree") - # Rebuild teh PartCategory tree + # Rebuild the PartCategory tree(s) PartCategory.objects.rebuild() - print("Done!") class PartCategoryAdmin(ImportExportModelAdmin): diff --git a/InvenTree/stock/admin.py b/InvenTree/stock/admin.py index 8f4e18461e..c2975dfd0c 100644 --- a/InvenTree/stock/admin.py +++ b/InvenTree/stock/admin.py @@ -1,11 +1,46 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + from django.contrib import admin + from import_export.admin import ImportExportModelAdmin +from import_export.resources import ModelResource +from import_export.fields import Field +import import_export.widgets as widgets from .models import StockLocation, StockItem from .models import StockItemTracking +class LocationResource(ModelResource): + """ Class for managing StockLocation data import/export """ + + parent = Field(attribute='parent', widget=widgets.ForeignKeyWidget(StockLocation)) + + parent_name = Field(attribute='parent__name', readonly=True) + + class Meta: + model = StockLocation + 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) + + # Rebuild the StockLocation tree(s) + StockLocation.objects.rebuild() + + class LocationAdmin(ImportExportModelAdmin): + + resource_class = LocationResource + list_display = ('name', 'pathstring', 'description')