2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-11 07:24:15 +00:00

BOM upload: improve generation of "import" template

- Better field naming
- Remove some fields which are not required here
- Better description of what is going to happen
This commit is contained in:
Oliver Walters
2020-08-18 11:47:27 +10:00
parent 89c7f72caf
commit 476cc5f661
3 changed files with 73 additions and 7 deletions

View File

@ -139,16 +139,71 @@ class BomItemResource(ModelResource):
bom_id = Field(attribute='pk')
# ID of the parent part
parent_part_id = Field(attribute='part', widget=widgets.ForeignKeyWidget(Part))
parent_part_name = Field(attribute='part__full_name', readonly=True)
# IPN of the parent part
parent_part_ipn = Field(attribute='part__IPN', readonly=True)
sub_part_id = Field(attribute='sub_part', widget=widgets.ForeignKeyWidget(Part))
# Name of the parent part
parent_part_name = Field(attribute='part__name', readonly=True)
sub_part_name = Field(attribute='sub_part__full_name', readonly=True)
# ID of the sub-part
part_id = Field(attribute='sub_part', widget=widgets.ForeignKeyWidget(Part))
# IPN of the sub-part
part_ipn = Field(attribute='sub_part__IPN', readonly=True)
# Name of the sub-part
part_name = Field(attribute='sub_part__name', readonly=True)
# Description of the sub-part
part_description = Field(attribute='sub_part__description', readonly=True)
# Is the sub-part itself an assembly?
sub_assembly = Field(attribute='sub_part__assembly', readonly=True)
def before_export(self, queryset, *args, **kwargs):
self.is_importing = kwargs.get('importing', False)
def get_fields(self, **kwargs):
"""
If we are exporting for the purposes of generating
a 'bom-import' template, there are some fields which
we are not interested in.
"""
fields = super().get_fields(**kwargs)
# If we are not generating an "import" template,
# just return the complete list of fields
if not self.is_importing:
return fields
# Otherwise, remove some fields we are not interested in
idx = 0
to_remove = [
'level',
'bom_id',
'parent_part_id',
'parent_part_ipn',
'parent_part_name',
'part_description',
'sub_assembly'
]
while idx < len(fields):
if fields[idx].column_name.lower() in to_remove:
del fields[idx]
else:
idx += 1
return fields
class Meta:
model = BomItem
skip_unchanged = True