Skip to content

Commit

Permalink
Bugfix: copying multiple attachments (#961)
Browse files Browse the repository at this point in the history
* Copying multiple attachments using the `part.copy()` method caused the temporary directory to be removed.

* Updated version to 3.10.1
  • Loading branch information
JelleBoersma authored Apr 1, 2021
1 parent ae32646 commit 5bb28be
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
==========

v3.10.1 (01APR21)
-----------------

* :bug: Copying multiple attachments using the `part.copy()` method caused the temporary directory to be removed.

v3.10.0 (29MAR21)
-----------------

Expand Down
2 changes: 1 addition & 1 deletion pykechain/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = 'pykechain'
description = 'KE-chain Python SDK'

version = '3.10.0'
version = '3.10.1'

author = 'KE-works BV'
email = 'support+pykechain@ke-works.com'
12 changes: 5 additions & 7 deletions pykechain/extra_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,15 +580,13 @@ def _copy_part(

mapping = get_mapping_dictionary()
attachment_properties = get_attachments()
if attachment_properties:
temp_dir = tempfile.TemporaryDirectory()
for prop_original in attachment_properties:
prop_new = mapping[prop_original.id]
if prop_original.has_value():
full_path = os.path.join(temp_dir.name or os.getcwd(), prop_original.filename)
for prop_original in attachment_properties:
prop_new = mapping[prop_original.id]
if prop_original.has_value():
with tempfile.TemporaryDirectory() as target_dir:
full_path = os.path.join(target_dir, prop_original.filename)
prop_original.save_as(filename=full_path)
prop_new.upload(full_path)
temp_dir.cleanup()

_update_references()
Property.update_values(client=copied_model._client)
Expand Down

Large diffs are not rendered by default.

31 changes: 20 additions & 11 deletions tests/test_parts_copy_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,21 +293,30 @@ def test_copy_too_many_target_parent_instances(self):
)

def test_copy_attachments(self):
name = "__Property attachment"
self.model_to_be_copied.add_property(
name=name,
property_type=PropertyType.ATTACHMENT_VALUE,
)
self.instance_to_be_copied.refresh()
names = [
"__Property attachment_1",
"__Property attachment_2",
]

for name in names:
self.model_to_be_copied.add_property(
name=name,
property_type=PropertyType.ATTACHMENT_VALUE,
)

p_attachment = self.instance_to_be_copied.property(name)
file = os.path.join(os.path.dirname(os.path.dirname(__file__)), "requirements.txt")
self.instance_to_be_copied.refresh()

p_attachment.upload(file)
for name in names:
p_attachment = self.instance_to_be_copied.property(name)
file = os.path.join(os.path.dirname(os.path.dirname(__file__)), "requirements.txt")
p_attachment.upload(file)

self.dump_part = self.model_to_be_copied.copy(
target_parent=self.model_target_parent,
)

copied_prop = self.dump_part.instance().property(name)
self.assertTrue(copied_prop.has_value())
copied_instance = self.dump_part.instance()

for name in names:
copied_prop = copied_instance.property(name)
self.assertTrue(copied_prop.has_value())

0 comments on commit 5bb28be

Please sign in to comment.