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.urls import reverse
from django.conf import settings from django.conf import settings
from django.core.files.base import ContentFile
from django.db import models, transaction from django.db import models, transaction
from django.core.validators import MinValueValidator 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 the number of supplier parts available for this part """
return self.supplier_parts.count() return self.supplier_parts.count()
def copyBomFrom(self, other): def deepCopy(self, other, **kwargs):
""" Duplicates the BOM from another part. """ Duplicates non-field data from another part.
Does not alter the normal fields of this part,
This should only be called during part creation, but can be used to copy other data linked by ForeignKey refernce.
and it does not delete any existing BOM items for *this* part.
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(): # Copy the part image
# Point the item to THIS part if kwargs.get('image', True):
item.part = self image_file = ContentFile(other.image.read())
item.pk = None image_file.name = rename_part_image(self, 'test.png')
item.save()
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): def export_bom(self, **kwargs):

View File

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