InvenTree/InvenTree/part/part.py
bloemp 92f5601e78
Fixes and improvements for the part import wizard (#4127)
Made changes that resemble PR #4050 to the part import wizard to make the correct form show.
Added option to download a part import template file.
Increased the number of allowable formfield because the importer creates a lot of table fields when importing multiple parts at once.
2023-01-01 22:03:43 +11:00

38 lines
928 B
Python

"""Functionality for Part import template.
Primarily Part import tools.
"""
from InvenTree.helpers import DownloadFile, GetExportFormats
from .admin import PartImportResource
from .models import Part
def IsValidPartFormat(fmt):
"""Test if a file format specifier is in the valid list of part import template file formats."""
return fmt.strip().lower() in GetExportFormats()
def MakePartTemplate(fmt):
"""Generate a part import template file (for user download)."""
fmt = fmt.strip().lower()
if not IsValidPartFormat(fmt):
fmt = 'csv'
# Create an "empty" queryset, essentially.
# This will then export just the row headers!
query = Part.objects.filter(pk=None)
dataset = PartImportResource().export(
queryset=query,
importing=True
)
data = dataset.export(fmt)
filename = 'InvenTree_Part_Template.' + fmt
return DownloadFile(data, filename)