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 InvenTree . forms import HelperForm
2020-02-11 10:43:17 +00:00
from InvenTree . helpers import GetExportFormats
2020-05-27 00:33:47 +00:00
from InvenTree . fields import RoundingDecimalFormField
2018-04-14 15:18:12 +00:00
2019-09-17 04:06:11 +00:00
from mptt . fields import TreeNodeChoiceField
2019-04-13 12:46:26 +00:00
from django import forms
2019-09-03 12:00:43 +00:00
from django . utils . translation import ugettext as _
2019-04-13 12:46:26 +00:00
2020-10-15 21:58:39 +00:00
from . models import Part , PartCategory , PartAttachment , PartRelated
2019-05-02 07:29:21 +00:00
from . models import BomItem
2019-08-20 04:14:21 +00:00
from . models import PartParameterTemplate , PartParameter
2020-10-30 22:17:18 +00:00
from . models import PartCategoryParameterTemplate
2020-05-17 06:50:34 +00:00
from . models import PartTestTemplate
2020-09-18 11:49:56 +00:00
from . models import PartSellPriceBreak
2019-09-03 12:00:43 +00:00
from common . models import Currency
2018-04-14 15:18:12 +00:00
2020-10-29 23:08:06 +00:00
class PartModelChoiceField ( forms . ModelChoiceField ) :
""" Extending string representation of Part instance with available stock """
def label_from_instance ( self , part ) :
return f ' { part } - { part . available_stock } '
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 ' ,
]
2020-05-17 06:50:34 +00:00
class EditPartTestTemplateForm ( HelperForm ) :
""" Class for creating / editing a PartTestTemplate object """
class Meta :
model = PartTestTemplate
fields = [
' part ' ,
' test_name ' ,
2020-05-18 09:11:43 +00:00
' description ' ,
' required ' ,
' requires_value ' ,
' requires_attachment ' ,
2020-05-17 06:50:34 +00:00
]
2020-02-11 10:43:17 +00:00
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 " ) )
2020-08-19 04:05:16 +00:00
cascading = forms . BooleanField ( label = _ ( " Cascading " ) , required = False , initial = True , help_text = _ ( " Download cascading / multi-level BOM " ) )
2020-02-11 10:43:17 +00:00
2020-08-15 22:29:36 +00:00
levels = forms . IntegerField ( label = _ ( " Levels " ) , required = True , initial = 0 , help_text = _ ( " Select maximum number of BOM levels to export (0 = all levels) " ) )
2020-08-20 18:53:27 +00:00
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 " ) )
supplier_data = forms . BooleanField ( label = _ ( " Include Supplier Data " ) , required = False , initial = True , help_text = _ ( " Include part supplier data in exported BOM " ) )
2020-08-19 04:05:16 +00:00
2020-02-11 10:43:17 +00:00
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 ( )
2020-10-29 23:08:06 +00:00
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 ,
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
"""
2019-09-03 12:00:43 +00:00
validate = forms . BooleanField ( required = False , initial = False , help_text = _ ( ' Confirm that the BOM is correct ' ) )
2019-05-12 06:27:50 +00:00
class Meta :
model = Part
fields = [
' validate '
]
2019-06-27 13:49:01 +00:00
class BomUploadSelectFile ( HelperForm ) :
2019-05-24 13:56:36 +00:00
""" Form for importing a BOM. Provides a file input box for upload """
2019-04-13 12:46:26 +00:00
2019-09-03 12:00:43 +00:00
bom_file = forms . FileField ( label = ' BOM file ' , required = True , help_text = _ ( " Select BOM file to upload " ) )
2019-04-13 12:46:26 +00:00
2019-06-27 13:49:01 +00:00
class Meta :
model = Part
fields = [
' bom_file ' ,
2019-06-27 12:16:24 +00:00
]
2019-06-27 13:49:01 +00:00
2020-10-15 21:58:39 +00:00
class CreatePartRelatedForm ( HelperForm ) :
""" Form for creating a PartRelated object """
class Meta :
model = PartRelated
fields = [
' part_1 ' ,
' part_2 ' ,
]
labels = {
' part_2 ' : _ ( ' Related Part ' ) ,
}
def save ( self ) :
""" Disable model saving """
return super ( CreatePartRelatedForm , self ) . save ( commit = False )
2019-05-02 07:29:21 +00:00
class EditPartAttachmentForm ( HelperForm ) :
""" Form for editing a PartAttachment object """
class Meta :
model = PartAttachment
fields = [
' part ' ,
' attachment ' ,
' comment '
]
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 ) :
2019-04-27 12:18:07 +00:00
""" Form for editing a Part object """
2018-04-14 15:18:12 +00:00
2020-05-16 01:55:10 +00:00
field_prefix = {
' keywords ' : ' fa-key ' ,
' link ' : ' fa-link ' ,
' IPN ' : ' fa-hashtag ' ,
}
2020-09-04 19:02:12 +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 ,
2020-09-05 10:10:18 +00:00
help_text = _ ( " Duplicate all parameter data for this part " ) ,
2020-09-04 19:02:12 +00:00
label = _ ( ' Copy Parameters ' ) ,
widget = forms . HiddenInput ( ) )
2019-05-13 11:41:32 +00:00
confirm_creation = forms . BooleanField ( required = False ,
initial = False ,
2019-09-03 12:00:43 +00:00
help_text = _ ( ' Confirm part creation ' ) ,
2019-05-13 11:41:32 +00:00
widget = forms . HiddenInput ( ) )
2019-05-11 02:29:02 +00:00
2020-11-02 18:14:31 +00:00
category_templates = forms . BooleanField ( required = False ,
initial = False ,
2020-11-02 20:05:37 +00:00
help_text = _ ( ' Create parameters based on default category templates ' ) ,
label = _ ( ' Copy category parameter templates ' ) ,
2020-11-02 18:14:31 +00:00
widget = forms . HiddenInput ( ) )
2018-04-14 15:18:12 +00:00
class Meta :
model = Part
fields = [
2020-09-04 19:02:12 +00:00
' bom_copy ' ,
' parameters_copy ' ,
2019-05-11 02:29:02 +00:00
' confirm_creation ' ,
2018-04-14 15:18:12 +00:00
' category ' ,
2020-11-02 18:14:31 +00:00
' category_templates ' ,
2018-04-14 15:18:12 +00:00
' name ' ,
2019-05-14 07:23:20 +00:00
' IPN ' ,
2018-04-14 15:18:12 +00:00
' description ' ,
2019-06-20 11:46:16 +00:00
' revision ' ,
2019-05-14 07:23:20 +00:00
' keywords ' ,
2019-06-02 10:07:30 +00:00
' variant_of ' ,
2020-04-06 01:16:39 +00:00
' link ' ,
2018-04-17 08:23:24 +00:00
' default_location ' ,
' default_supplier ' ,
2019-04-15 14:01:15 +00:00
' units ' ,
2018-04-14 15:18:12 +00:00
' minimum_stock ' ,
2018-04-15 01:40:03 +00:00
]
2019-08-20 04:33:18 +00:00
class EditPartParameterTemplateForm ( HelperForm ) :
""" Form for editing a PartParameterTemplate object """
class Meta :
model = PartParameterTemplate
fields = [
' name ' ,
' units '
]
2019-08-20 04:14:21 +00:00
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 """
2018-04-15 01:40:03 +00:00
2020-05-16 01:55:10 +00:00
field_prefix = {
' default_keywords ' : ' fa-key ' ,
}
2018-04-15 01:40:03 +00:00
class Meta :
model = PartCategory
fields = [
' parent ' ,
' name ' ,
2019-05-04 09:03:32 +00:00
' description ' ,
2019-05-14 07:30:24 +00:00
' default_location ' ,
' default_keywords ' ,
2018-04-15 11:29:24 +00:00
]
2020-10-30 22:17:18 +00:00
class EditCategoryParameterTemplateForm ( HelperForm ) :
2020-10-31 17:55:52 +00:00
""" Form for editing a PartCategoryParameterTemplate object """
2020-10-30 22:17:18 +00:00
2020-11-02 17:20:29 +00:00
add_to_all_categories = forms . BooleanField ( required = False ,
initial = False ,
help_text = _ ( ' Add parameter template to all categories ' ) )
2020-10-30 22:17:18 +00:00
class Meta :
model = PartCategoryParameterTemplate
fields = [
' category ' ,
' parameter_template ' ,
' default_value ' ,
2020-11-02 17:20:29 +00:00
' add_to_all_categories ' ,
2020-10-30 22:17:18 +00:00
]
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 """
2018-04-15 11:29:24 +00:00
2020-05-27 00:33:47 +00:00
quantity = RoundingDecimalFormField ( max_digits = 10 , decimal_places = 5 )
2020-08-24 16:41:14 +00:00
sub_part = PartModelChoiceField ( queryset = Part . objects . all ( ) )
2018-04-15 11:29:24 +00:00
class Meta :
model = BomItem
fields = [
' part ' ,
' sub_part ' ,
2019-04-15 15:41:01 +00:00
' quantity ' ,
2019-06-27 13:57:21 +00:00
' reference ' ,
2019-05-14 14:16:34 +00:00
' overage ' ,
2020-10-04 13:42:09 +00:00
' note ' ,
' optional ' ,
2018-04-15 15:02:17 +00:00
]
2019-04-27 23:53:42 +00:00
# Prevent editing of the part associated with this BomItem
2019-04-15 15:41:01 +00:00
widgets = { ' part ' : forms . HiddenInput ( ) }
2019-05-18 12:58:11 +00:00
class PartPriceForm ( forms . Form ) :
""" Simple form for viewing part pricing information """
quantity = forms . IntegerField (
required = True ,
initial = 1 ,
2019-09-03 12:00:43 +00:00
help_text = _ ( ' Input quantity for price calculation ' )
2019-05-18 12:58:11 +00:00
)
2019-09-17 10:17:25 +00:00
currency = forms . ModelChoiceField ( queryset = Currency . objects . all ( ) , label = ' Currency ' , help_text = _ ( ' Select currency for price calculation ' ) )
2019-09-03 12:00:43 +00:00
2019-05-18 12:58:11 +00:00
class Meta :
model = Part
fields = [
2019-09-03 12:00:43 +00:00
' quantity ' ,
' currency ' ,
2019-05-18 12:58:11 +00:00
]
2020-09-18 11:49:56 +00:00
class EditPartSalePriceBreakForm ( HelperForm ) :
"""
Form for creating / editing a sale price for a part
"""
quantity = RoundingDecimalFormField ( max_digits = 10 , decimal_places = 5 )
cost = RoundingDecimalFormField ( max_digits = 10 , decimal_places = 5 )
class Meta :
model = PartSellPriceBreak
fields = [
' part ' ,
' quantity ' ,
' cost ' ,
' currency ' ,
2020-09-19 09:52:48 +00:00
]