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

Handle modern attachment API #41

Merged
merged 2 commits into from
Jun 24, 2024
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
60 changes: 49 additions & 11 deletions inventree_wireviz/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from company.models import ManufacturerPart, SupplierPart
from InvenTree.helpers import str2bool
from part.models import BomItem, Part, PartAttachment
from part.models import BomItem, Part
from plugin.registry import registry

from wireviz.Harness import Harness
Expand Down Expand Up @@ -52,6 +52,44 @@ def __init__(self):
self.warnings = []
self.part_map = {}

def create_attachment(self, part, attachment, comment, user):
"""Upload a file attachment to the specified part.

Note: We support both the "legacy" and "modern" attachment table.

Ref: https://github.com/inventree/InvenTree/pull/7420
"""

# First, try the "modern" attachment table
try:
from common.models import Attachment

return Attachment.objects.create(
model_type='part',
model_id=part.pk,
attachment=attachment,
comment=comment,
user=user
)
except Exception:
pass

# Second, try the "legacy" attachment table
try:
from part.models import PartAttachment

return PartAttachment.objects.create(
part=part,
attachment=attachment,
comment=comment,
user=user
)
except Exception:
pass

# Attachment not created
return None

def prepend_templates(self):
"""Prepend the contents of the wireviz template files to the wireviz file."""

Expand Down Expand Up @@ -168,11 +206,11 @@ def import_harness(self, wv_file, part: Part, user):
wv_filename = os.path.basename(wv_file.name)

# Save the uploaded wireviz file as an attachment for the Part instance
wv_attachment = PartAttachment.objects.create(
part=self.part,
attachment=ContentFile(wv_data, name=wv_filename),
comment='Wireviz Harness File',
user=user,
wv_attachment = self.create_attachment(
self.part,
ContentFile(wv_data, name=wv_filename),
'Wireviz Harness File',
user,
)

# Generate SVG output
Expand Down Expand Up @@ -390,12 +428,12 @@ def generate_svg_output(self, harness: Harness, filename: str, user):
graph = harness.create_graph()
svg_data = graph.pipe(format='svg')

return PartAttachment.objects.create(
part=self.part,
attachment=ContentFile(
return self.create_attachment(
self.part,
ContentFile(
svg_data.decode('utf-8'),
name='wireviz_harness.svg',
),
comment=f"Wireviz Harness (autogenerated from {filename})",
user=user
f"Wireviz Harness (autogenerated from {filename})",
user
)
2 changes: 1 addition & 1 deletion inventree_wireviz/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Version information for the inventree-wireviz plugin"""

PLUGIN_VERSION = "0.5.0"
PLUGIN_VERSION = "0.6.0"
Loading