mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-30 04:26:44 +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,9 +129,16 @@ class EditPartForm(HelperForm):
|
|||||||
'IPN': 'fa-hashtag',
|
'IPN': 'fa-hashtag',
|
||||||
}
|
}
|
||||||
|
|
||||||
deep_copy = forms.BooleanField(required=False,
|
bom_copy = forms.BooleanField(required=False,
|
||||||
initial=True,
|
initial=True,
|
||||||
help_text=_("Perform 'deep copy' which will duplicate all BOM data for this part"),
|
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())
|
widget=forms.HiddenInput())
|
||||||
|
|
||||||
confirm_creation = forms.BooleanField(required=False,
|
confirm_creation = forms.BooleanField(required=False,
|
||||||
@ -142,7 +149,8 @@ class EditPartForm(HelperForm):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = Part
|
model = Part
|
||||||
fields = [
|
fields = [
|
||||||
'deep_copy',
|
'bom_copy',
|
||||||
|
'parameters_copy',
|
||||||
'confirm_creation',
|
'confirm_creation',
|
||||||
'category',
|
'category',
|
||||||
'name',
|
'name',
|
||||||
|
@ -1041,6 +1041,7 @@ class Part(MPTTModel):
|
|||||||
Keyword Args:
|
Keyword Args:
|
||||||
image: If True, copies Part image (default = True)
|
image: If True, copies Part image (default = True)
|
||||||
bom: If True, copies BOM data (default = False)
|
bom: If True, copies BOM data (default = False)
|
||||||
|
parameters: If True, copies Parameters data (default = True)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Copy the part image
|
# Copy the part image
|
||||||
@ -1058,6 +1059,17 @@ class Part(MPTTModel):
|
|||||||
item.pk = None
|
item.pk = None
|
||||||
item.save()
|
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
|
# Copy the fields that aren't available in the duplicate form
|
||||||
self.salable = other.salable
|
self.salable = other.salable
|
||||||
self.assembly = other.assembly
|
self.assembly = other.assembly
|
||||||
@ -1402,6 +1414,13 @@ class PartParameter(models.Model):
|
|||||||
|
|
||||||
data = models.CharField(max_length=500, help_text=_('Parameter Value'))
|
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):
|
class BomItem(models.Model):
|
||||||
""" A BomItem links a part to its component items.
|
""" A BomItem links a part to its component items.
|
||||||
|
@ -330,7 +330,7 @@ class MakePartVariant(AjaxCreateView):
|
|||||||
data['url'] = part.get_absolute_url()
|
data['url'] = part.get_absolute_url()
|
||||||
|
|
||||||
# Copy relevent information from the template part
|
# 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)
|
return self.renderJsonResponse(request, form, data, context=context)
|
||||||
|
|
||||||
@ -377,15 +377,19 @@ class PartDuplicate(AjaxCreateView):
|
|||||||
def get_form(self):
|
def get_form(self):
|
||||||
form = super(AjaxCreateView, self).get_form()
|
form = super(AjaxCreateView, self).get_form()
|
||||||
|
|
||||||
# Force display of the 'deep_copy' widget
|
# Force display of the 'bom_copy' widget
|
||||||
form.fields['deep_copy'].widget = CheckboxInput()
|
form.fields['bom_copy'].widget = CheckboxInput()
|
||||||
|
|
||||||
|
# Force display of the 'parameters_copy' widget
|
||||||
|
form.fields['parameters_copy'].widget = CheckboxInput()
|
||||||
|
|
||||||
return form
|
return form
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
""" Capture the POST request for part duplication
|
""" 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()
|
form = self.get_form()
|
||||||
@ -428,12 +432,13 @@ class PartDuplicate(AjaxCreateView):
|
|||||||
data['pk'] = part.pk
|
data['pk'] = part.pk
|
||||||
data['text'] = str(part)
|
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()
|
original = self.get_part_to_copy()
|
||||||
|
|
||||||
if original:
|
if original:
|
||||||
part.deepCopy(original, bom=deep_copy)
|
part.deepCopy(original, bom=bom_copy, parameters=parameters_copy)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data['url'] = part.get_absolute_url()
|
data['url'] = part.get_absolute_url()
|
||||||
@ -456,7 +461,9 @@ class PartDuplicate(AjaxCreateView):
|
|||||||
else:
|
else:
|
||||||
initials = super(AjaxCreateView, self).get_initial()
|
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
|
return initials
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user