inherited setup method

This commit is contained in:
Matthias 2021-05-18 01:55:47 +02:00
parent eafaf92ae2
commit 92f8bd36f1
4 changed files with 65 additions and 47 deletions

View File

@ -87,56 +87,16 @@ class FileManager:
self.HEADERS = self.REQUIRED_HEADERS + self.ITEM_MATCH_HEADERS + self.OPTIONAL_MATCH_HEADERS + self.OPTIONAL_HEADERS self.HEADERS = self.REQUIRED_HEADERS + self.ITEM_MATCH_HEADERS + self.OPTIONAL_MATCH_HEADERS + self.OPTIONAL_HEADERS
def setup(self): def setup(self):
""" Setup headers depending on the file name """ """
Setup headers
should be overriden in usage to set the Different Headers
"""
if not self.name: if not self.name:
return return
if self.name == 'order': # Update headers
self.REQUIRED_HEADERS = [ self.update_headers()
'Quantity',
]
self.ITEM_MATCH_HEADERS = [
'Manufacturer_MPN',
'Supplier_SKU',
]
self.OPTIONAL_HEADERS = [
'Purchase_Price',
'Reference',
'Notes',
]
# Update headers
self.update_headers()
# TODO maybe not here but in an own function?
if self.name == 'part':
self.REQUIRED_HEADERS = [
'Name',
'Description',
]
self.OPTIONAL_MATCH_HEADERS = [
'Category',
'default_location',
'default_supplier',
]
self.OPTIONAL_HEADERS = [
'Keywords',
'IPN',
'Revision',
'Link',
'default_expiry',
'minimum_stock',
'Units',
'Notes',
]
# Update headers
self.update_headers()
def guess_header(self, header, threshold=80): def guess_header(self, header, threshold=80):
""" Try to match a header (from the file) to a list of known headers """ Try to match a header (from the file) to a list of known headers

View File

@ -189,6 +189,15 @@ class FileManagementFormView(MultiStepFormView):
media_folder = 'file_upload/' media_folder = 'file_upload/'
extra_context_data = {} extra_context_data = {}
def __init__(self, *args, **kwargs):
""" initialize the FormView """
# perform all checks and inits from MultiStepFormView
super().__init__(*args, **kwargs)
# Check
if not(hasattr(self, 'file_manager_class') and issubclass(self.file_manager_class, FileManager)):
raise NotImplementedError('A subclass of a file manager class needs to be set!')
def get_context_data(self, form, **kwargs): def get_context_data(self, form, **kwargs):
context = super().get_context_data(form=form, **kwargs) context = super().get_context_data(form=form, **kwargs)
@ -228,7 +237,7 @@ class FileManagementFormView(MultiStepFormView):
# Get file # Get file
file = upload_files.get('upload-file', None) file = upload_files.get('upload-file', None)
if file: if file:
self.file_manager = FileManager(file=file, name=self.name) self.file_manager = self.file_manager_class(file=file, name=self.name)
def get_form_kwargs(self, step=None): def get_form_kwargs(self, step=None):
""" Update kwargs to dynamically build forms """ """ Update kwargs to dynamically build forms """

View File

@ -31,6 +31,7 @@ from part.models import Part
from common.models import InvenTreeSetting from common.models import InvenTreeSetting
from common.views import FileManagementFormView from common.views import FileManagementFormView
from common.files import FileManager
from . import forms as order_forms from . import forms as order_forms
from part.views import PartPricing from part.views import PartPricing
@ -591,6 +592,26 @@ class PurchaseOrderUpload(FileManagementFormView):
'reference': 'reference', 'reference': 'reference',
'notes': 'notes', 'notes': 'notes',
} }
class MyManger(FileManager):
def setup(self):
self.REQUIRED_HEADERS = [
'Quantity',
]
self.ITEM_MATCH_HEADERS = [
'Manufacturer_MPN',
'Supplier_SKU',
]
self.OPTIONAL_HEADERS = [
'Purchase_Price',
'Reference',
'Notes',
]
return super().setup()
file_manager_class = MyManger
def get_order(self): def get_order(self):
""" Get order or return 404 """ """ Get order or return 404 """

View File

@ -40,6 +40,7 @@ from .models import PartSellPriceBreak
from common.models import InvenTreeSetting from common.models import InvenTreeSetting
from company.models import SupplierPart from company.models import SupplierPart
from common.files import FileManager
from common.views import FileManagementFormView from common.views import FileManagementFormView
from stock.models import StockLocation from stock.models import StockLocation
@ -752,6 +753,33 @@ class PartImport(FileManagementFormView):
'default_location': 'default_location', 'default_location': 'default_location',
'default_supplier': 'default_supplier', 'default_supplier': 'default_supplier',
} }
class MyManger(FileManager):
def setup(self):
self.REQUIRED_HEADERS = [
'Name',
'Description',
]
self.OPTIONAL_MATCH_HEADERS = [
'Category',
'default_location',
'default_supplier',
]
self.OPTIONAL_HEADERS = [
'Keywords',
'IPN',
'Revision',
'Link',
'default_expiry',
'minimum_stock',
'Units',
'Notes',
]
return super().setup()
file_manager_class = MyManger
def get_field_selection(self): def get_field_selection(self):
""" Fill the form fields for step 3 """ """ Fill the form fields for step 3 """