From e1e5cde60fcec2e50905a5bc1818b7486c0d5309 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 13 May 2019 21:54:52 +1000 Subject: [PATCH] Renamed copyBomFrom to deepCopy - Allows passing of more data (in the future) e.g. tags, etc - Performs copy of the part image --- InvenTree/part/models.py | 35 +++++++++++++++++++++++++---------- InvenTree/part/views.py | 5 ++--- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index da989c525a..5a609c0e59 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -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): diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index b78fcf7239..d9540035d8 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -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