Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: copying multiple attachments #961

Merged
merged 2 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())