mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Merge pull request #961 from eeintech/parameters_copy
Implemented part parameters copy from template/original part
This commit is contained in:
commit
6ef4390e29
@ -129,10 +129,17 @@ class EditPartForm(HelperForm):
|
||||
'IPN': 'fa-hashtag',
|
||||
}
|
||||
|
||||
deep_copy = forms.BooleanField(required=False,
|
||||
initial=True,
|
||||
help_text=_("Perform 'deep copy' which will duplicate all BOM data for this part"),
|
||||
widget=forms.HiddenInput())
|
||||
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 parameters data for this part"),
|
||||
label=_('Copy Parameters'),
|
||||
widget=forms.HiddenInput())
|
||||
|
||||
confirm_creation = forms.BooleanField(required=False,
|
||||
initial=False,
|
||||
@ -142,7 +149,8 @@ class EditPartForm(HelperForm):
|
||||
class Meta:
|
||||
model = Part
|
||||
fields = [
|
||||
'deep_copy',
|
||||
'bom_copy',
|
||||
'parameters_copy',
|
||||
'confirm_creation',
|
||||
'category',
|
||||
'name',
|
||||
|
@ -1041,6 +1041,7 @@ class Part(MPTTModel):
|
||||
Keyword Args:
|
||||
image: If True, copies Part image (default = True)
|
||||
bom: If True, copies BOM data (default = False)
|
||||
parameters: If True, copies Parameters data (default = True)
|
||||
"""
|
||||
|
||||
# Copy the part image
|
||||
@ -1058,6 +1059,17 @@ class Part(MPTTModel):
|
||||
item.pk = None
|
||||
item.save()
|
||||
|
||||
# Copy the parameters data
|
||||
if kwargs.get('parameters', True):
|
||||
# Get template part parameters
|
||||
parameters = other.get_parameters()
|
||||
# Copy template part parameters to new variant part
|
||||
for parameter in parameters:
|
||||
PartParameter.create(part=self,
|
||||
template=parameter.template,
|
||||
data=parameter.data,
|
||||
save=True)
|
||||
|
||||
# Copy the fields that aren't available in the duplicate form
|
||||
self.salable = other.salable
|
||||
self.assembly = other.assembly
|
||||
@ -1402,6 +1414,13 @@ class PartParameter(models.Model):
|
||||
|
||||
data = models.CharField(max_length=500, help_text=_('Parameter Value'))
|
||||
|
||||
@classmethod
|
||||
def create(cls, part, template, data, save=False):
|
||||
part_parameter = cls(part=part, template=template, data=data)
|
||||
if save:
|
||||
part_parameter.save()
|
||||
return part_parameter
|
||||
|
||||
|
||||
class BomItem(models.Model):
|
||||
""" A BomItem links a part to its component items.
|
||||
|
@ -330,7 +330,7 @@ class MakePartVariant(AjaxCreateView):
|
||||
data['url'] = part.get_absolute_url()
|
||||
|
||||
# Copy relevent information from the template part
|
||||
part.deepCopy(part_template, bom=True)
|
||||
part.deepCopy(part_template, bom=True, parameters=True)
|
||||
|
||||
return self.renderJsonResponse(request, form, data, context=context)
|
||||
|
||||
@ -377,15 +377,19 @@ class PartDuplicate(AjaxCreateView):
|
||||
def get_form(self):
|
||||
form = super(AjaxCreateView, self).get_form()
|
||||
|
||||
# Force display of the 'deep_copy' widget
|
||||
form.fields['deep_copy'].widget = CheckboxInput()
|
||||
# Force display of the 'bom_copy' widget
|
||||
form.fields['bom_copy'].widget = CheckboxInput()
|
||||
|
||||
# Force display of the 'parameters_copy' widget
|
||||
form.fields['parameters_copy'].widget = CheckboxInput()
|
||||
|
||||
return form
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
""" Capture the POST request for part duplication
|
||||
|
||||
- If the deep_copy object is set, copy all the BOM items too!
|
||||
- If the bom_copy object is set, copy all the BOM items too!
|
||||
- If the parameters_copy object is set, copy all the parameters too!
|
||||
"""
|
||||
|
||||
form = self.get_form()
|
||||
@ -428,12 +432,13 @@ class PartDuplicate(AjaxCreateView):
|
||||
data['pk'] = part.pk
|
||||
data['text'] = str(part)
|
||||
|
||||
deep_copy = str2bool(request.POST.get('deep_copy', False))
|
||||
bom_copy = str2bool(request.POST.get('bom_copy', False))
|
||||
parameters_copy = str2bool(request.POST.get('parameters_copy', False))
|
||||
|
||||
original = self.get_part_to_copy()
|
||||
|
||||
if original:
|
||||
part.deepCopy(original, bom=deep_copy)
|
||||
part.deepCopy(original, bom=bom_copy, parameters=parameters_copy)
|
||||
|
||||
try:
|
||||
data['url'] = part.get_absolute_url()
|
||||
@ -456,7 +461,9 @@ class PartDuplicate(AjaxCreateView):
|
||||
else:
|
||||
initials = super(AjaxCreateView, self).get_initial()
|
||||
|
||||
initials['deep_copy'] = str2bool(InvenTreeSetting.get_setting('part_deep_copy', True))
|
||||
initials['bom_copy'] = str2bool(InvenTreeSetting.get_setting('part_deep_copy', True))
|
||||
# Create new entry in InvenTree/common/kvp.yaml?
|
||||
initials['parameters_copy'] = str2bool(InvenTreeSetting.get_setting('part_deep_copy', True))
|
||||
|
||||
return initials
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user