Skip to content

Commit

Permalink
Handle modern attachment API
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingersGat committed Jun 24, 2024
1 parent 4d79284 commit ede633d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
61 changes: 50 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,45 @@ 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 +207,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 +429,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"

0 comments on commit ede633d

Please sign in to comment.