Renamed copyBomFrom to deepCopy

- Allows passing of more data (in the future) e.g. tags, etc
- Performs copy of the part image
This commit is contained in:
Oliver Walters 2019-05-13 21:54:52 +10:00
parent 00f7ece6b4
commit e1e5cde60f
2 changed files with 27 additions and 13 deletions

View File

@ -16,6 +16,7 @@ from django.core.exceptions import ValidationError
from django.urls import reverse
from django.conf import settings
from django.core.files.base import ContentFile
from django.db import models, transaction
from django.core.validators import MinValueValidator
@ -533,18 +534,32 @@ class Part(models.Model):
""" Return the number of supplier parts available for this part """
return self.supplier_parts.count()
def copyBomFrom(self, other):
""" Duplicates the BOM from another part.
This should only be called during part creation,
and it does not delete any existing BOM items for *this* part.
def deepCopy(self, other, **kwargs):
""" Duplicates non-field data from another part.
Does not alter the normal fields of this part,
but can be used to copy other data linked by ForeignKey refernce.
Keyword Args:
image: If True, copies Part image (default = True)
bom: If True, copies BOM data (default = False)
"""
for item in other.bom_items.all():
# Point the item to THIS part
item.part = self
item.pk = None
item.save()
# Copy the part image
if kwargs.get('image', True):
image_file = ContentFile(other.image.read())
image_file.name = rename_part_image(self, 'test.png')
self.image = image_file
# Copy the BOM data
if kwargs.get('bom', False):
for item in other.bom_items.all():
# Point the item to THIS part
item.part = self
item.pk = None
item.save()
self.save()
def export_bom(self, **kwargs):

View File

@ -208,8 +208,7 @@ class PartDuplicate(AjaxCreateView):
original = self.get_part_to_copy()
if original:
if deep_copy:
part.copyBomFrom(original)
part.deepCopy(original, bom=deep_copy)
try:
data['url'] = part.get_absolute_url()
@ -230,7 +229,7 @@ class PartDuplicate(AjaxCreateView):
if part:
initials = model_to_dict(part)
else:
initials = super(AjaxCreateView, self).get_initials()
initials = super(AjaxCreateView, self).get_initial()
return initials