InvenTree/InvenTree/part/forms.py

385 lines
11 KiB
Python
Raw Normal View History

2019-04-27 12:18:07 +00:00
"""
Django Forms for interacting with Part objects
"""
2018-04-29 14:59:36 +00:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django import forms
from django.utils.translation import ugettext_lazy as _
from mptt.fields import TreeNodeChoiceField
2018-04-29 14:59:36 +00:00
from InvenTree.forms import HelperForm
from InvenTree.helpers import GetExportFormats
from InvenTree.fields import RoundingDecimalFormField
import common.models
2021-07-01 04:25:14 +00:00
from .models import Part, PartCategory, PartRelated
from .models import BomItem
from .models import PartParameterTemplate, PartParameter
from .models import PartCategoryParameterTemplate
from .models import PartSellPriceBreak, PartInternalPriceBreak
2020-09-18 11:49:56 +00:00
class PartModelChoiceField(forms.ModelChoiceField):
""" Extending string representation of Part instance with available stock """
def label_from_instance(self, part):
label = str(part)
# Optionally display available part quantity
if common.models.InvenTreeSetting.get_setting('PART_SHOW_QUANTITY_IN_FORMS'):
label += f" - {part.available_stock}"
return label
2021-03-16 21:28:12 +00:00
class PartImageDownloadForm(HelperForm):
"""
Form for downloading an image from a URL
"""
url = forms.URLField(
label=_('URL'),
help_text=_('Image URL'),
required=True,
)
class Meta:
model = Part
fields = [
'url',
]
2018-04-29 14:59:36 +00:00
class PartImageForm(HelperForm):
2019-04-27 12:18:07 +00:00
""" Form for uploading a Part image """
2018-04-29 02:25:07 +00:00
class Meta:
model = Part
fields = [
'image',
]
class BomExportForm(forms.Form):
""" Simple form to let user set BOM export options,
before exporting a BOM (bill of materials) file.
"""
file_format = forms.ChoiceField(label=_("File Format"), help_text=_("Select output file format"))
cascading = forms.BooleanField(label=_("Cascading"), required=False, initial=True, help_text=_("Download cascading / multi-level BOM"))
levels = forms.IntegerField(label=_("Levels"), required=True, initial=0, help_text=_("Select maximum number of BOM levels to export (0 = all levels)"))
parameter_data = forms.BooleanField(label=_("Include Parameter Data"), required=False, initial=False, help_text=_("Include part parameters data in exported BOM"))
stock_data = forms.BooleanField(label=_("Include Stock Data"), required=False, initial=False, help_text=_("Include part stock data in exported BOM"))
manufacturer_data = forms.BooleanField(label=_("Include Manufacturer Data"), required=False, initial=True, help_text=_("Include part manufacturer data in exported BOM"))
supplier_data = forms.BooleanField(label=_("Include Supplier Data"), required=False, initial=True, help_text=_("Include part supplier data in exported BOM"))
def get_choices(self):
""" BOM export format choices """
return [(x, x.upper()) for x in GetExportFormats()]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['file_format'].choices = self.get_choices()
class BomDuplicateForm(HelperForm):
"""
Simple confirmation form for BOM duplication.
Select which parent to select from.
"""
parent = PartModelChoiceField(
label=_('Parent Part'),
help_text=_('Select parent part to copy BOM from'),
queryset=Part.objects.filter(is_template=True),
)
clear = forms.BooleanField(
required=False, initial=True,
help_text=_('Clear existing BOM items')
)
confirm = forms.BooleanField(
required=False, initial=False,
2021-04-04 20:44:14 +00:00
label=_('Confirm'),
help_text=_('Confirm BOM duplication')
)
class Meta:
model = Part
fields = [
'parent',
'clear',
'confirm',
]
2019-05-12 06:27:50 +00:00
class BomValidateForm(HelperForm):
""" Simple confirmation form for BOM validation.
User is presented with a single checkbox input,
to confirm that the BOM for this part is valid
"""
2021-04-04 20:44:14 +00:00
validate = forms.BooleanField(required=False, initial=False, label=_('validate'), help_text=_('Confirm that the BOM is correct'))
2019-05-12 06:27:50 +00:00
class Meta:
model = Part
fields = [
'validate'
]
class BomUploadSelectFile(HelperForm):
""" Form for importing a BOM. Provides a file input box for upload """
2021-04-04 20:44:14 +00:00
bom_file = forms.FileField(label=_('BOM file'), required=True, help_text=_("Select BOM file to upload"))
class Meta:
model = Part
fields = [
'bom_file',
]
class CreatePartRelatedForm(HelperForm):
""" Form for creating a PartRelated object """
class Meta:
model = PartRelated
fields = [
'part_1',
'part_2',
]
labels = {
'part_2': _('Related Part'),
}
2019-09-17 04:06:11 +00:00
class SetPartCategoryForm(forms.Form):
""" Form for setting the category of multiple Part objects """
part_category = TreeNodeChoiceField(queryset=PartCategory.objects.all(), required=True, help_text=_('Select part category'))
2018-04-29 14:59:36 +00:00
class EditPartForm(HelperForm):
"""
Form for editing a Part object.
"""
2020-05-16 01:55:10 +00:00
field_prefix = {
'keywords': 'fa-key',
'link': 'fa-link',
'IPN': 'fa-hashtag',
'default_expiry': 'fa-stopwatch',
2020-05-16 01:55:10 +00:00
}
bom_copy = forms.BooleanField(required=False,
initial=True,
help_text=_("Duplicate all BOM data for this part"),
label=_('Copy BOM'),
widget=forms.HiddenInput())
parameters_copy = forms.BooleanField(required=False,
initial=True,
help_text=_("Duplicate all parameter data for this part"),
label=_('Copy Parameters'),
widget=forms.HiddenInput())
confirm_creation = forms.BooleanField(required=False,
initial=False,
help_text=_('Confirm part creation'),
widget=forms.HiddenInput())
selected_category_templates = forms.BooleanField(required=False,
initial=False,
label=_('Include category parameter templates'),
widget=forms.HiddenInput())
parent_category_templates = forms.BooleanField(required=False,
initial=False,
label=_('Include parent categories parameter templates'),
widget=forms.HiddenInput())
class Meta:
model = Part
fields = [
'confirm_creation',
'category',
'selected_category_templates',
'parent_category_templates',
'name',
'IPN',
'description',
'revision',
'bom_copy',
'parameters_copy',
'keywords',
2019-06-02 10:07:30 +00:00
'variant_of',
'link',
'default_location',
'default_supplier',
'default_expiry',
'units',
'minimum_stock',
'component',
'assembly',
'is_template',
'trackable',
'purchaseable',
'salable',
2021-01-03 12:13:58 +00:00
'virtual',
]
class EditPartParameterTemplateForm(HelperForm):
""" Form for editing a PartParameterTemplate object """
class Meta:
model = PartParameterTemplate
fields = [
'name',
'units'
]
class EditPartParameterForm(HelperForm):
""" Form for editing a PartParameter object """
class Meta:
model = PartParameter
fields = [
'part',
'template',
'data'
]
2018-04-29 14:59:36 +00:00
class EditCategoryForm(HelperForm):
2019-04-27 12:18:07 +00:00
""" Form for editing a PartCategory object """
2020-05-16 01:55:10 +00:00
field_prefix = {
'default_keywords': 'fa-key',
}
class Meta:
model = PartCategory
fields = [
'parent',
'name',
'description',
'default_location',
'default_keywords',
]
class EditCategoryParameterTemplateForm(HelperForm):
""" Form for editing a PartCategoryParameterTemplate object """
add_to_same_level_categories = forms.BooleanField(required=False,
initial=False,
help_text=_('Add parameter template to same level categories'))
add_to_all_categories = forms.BooleanField(required=False,
initial=False,
help_text=_('Add parameter template to all categories'))
class Meta:
model = PartCategoryParameterTemplate
fields = [
'category',
'parameter_template',
'default_value',
'add_to_same_level_categories',
'add_to_all_categories',
]
2018-04-29 14:59:36 +00:00
class EditBomItemForm(HelperForm):
2019-04-27 12:18:07 +00:00
""" Form for editing a BomItem object """
2021-04-04 20:44:14 +00:00
quantity = RoundingDecimalFormField(max_digits=10, decimal_places=5, label=_('Quantity'))
2021-04-04 20:44:14 +00:00
sub_part = PartModelChoiceField(queryset=Part.objects.all(), label=_('Sub part'))
2020-08-24 16:41:14 +00:00
class Meta:
model = BomItem
fields = [
'part',
'sub_part',
'quantity',
'reference',
'overage',
'note',
2021-06-01 04:17:31 +00:00
'allow_variants',
'inherited',
'optional',
2018-04-15 15:02:17 +00:00
]
# Prevent editing of the part associated with this BomItem
2021-01-02 23:07:38 +00:00
widgets = {
'part': forms.HiddenInput()
}
class PartPriceForm(forms.Form):
""" Simple form for viewing part pricing information """
quantity = forms.IntegerField(
required=True,
initial=1,
2021-04-04 20:44:14 +00:00
label=_('Quantity'),
help_text=_('Input quantity for price calculation')
)
class Meta:
model = Part
fields = [
'quantity',
]
2020-09-18 11:49:56 +00:00
class EditPartSalePriceBreakForm(HelperForm):
"""
Form for creating / editing a sale price for a part
"""
2021-04-04 20:44:14 +00:00
quantity = RoundingDecimalFormField(max_digits=10, decimal_places=5, label=_('Quantity'))
2020-09-18 11:49:56 +00:00
class Meta:
model = PartSellPriceBreak
fields = [
'part',
'quantity',
'price',
2020-09-19 09:52:48 +00:00
]
class EditPartInternalPriceBreakForm(HelperForm):
"""
Form for creating / editing a internal price for a part
"""
quantity = RoundingDecimalFormField(max_digits=10, decimal_places=5, label=_('Quantity'))
class Meta:
model = PartInternalPriceBreak
fields = [
'part',
'quantity',
'price',
]