inventree-wireviz-plugin 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- inventree_wireviz/__init__.py +0 -0
- inventree_wireviz/processing.py +442 -0
- inventree_wireviz/serializers.py +150 -0
- inventree_wireviz/static/WirevizDashboard.js +6 -0
- inventree_wireviz/static/WirevizPanel.js +16 -0
- inventree_wireviz/static/WirevizSettings.js +6 -0
- inventree_wireviz/static/assets/IconTrash-CZbkoT-X.js +6 -0
- inventree_wireviz/static/assets/Plugins-BMhs_tNe.js +1 -0
- inventree_wireviz/static/assets/createReactComponent-CHpQc-nV.js +27 -0
- inventree_wireviz/version.py +3 -0
- inventree_wireviz/views.py +74 -0
- inventree_wireviz/wireviz.py +337 -0
- inventree_wireviz_plugin-1.0.0.dist-info/METADATA +169 -0
- inventree_wireviz_plugin-1.0.0.dist-info/RECORD +18 -0
- inventree_wireviz_plugin-1.0.0.dist-info/WHEEL +5 -0
- inventree_wireviz_plugin-1.0.0.dist-info/entry_points.txt +2 -0
- inventree_wireviz_plugin-1.0.0.dist-info/licenses/LICENSE +21 -0
- inventree_wireviz_plugin-1.0.0.dist-info/top_level.txt +1 -0
|
File without changes
|
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
"""Wireviz file processing functionality."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
import yaml
|
|
6
|
+
|
|
7
|
+
from django.conf import settings
|
|
8
|
+
from django.core.files.base import ContentFile
|
|
9
|
+
from rest_framework.exceptions import ValidationError
|
|
10
|
+
|
|
11
|
+
from company.models import ManufacturerPart, SupplierPart
|
|
12
|
+
from InvenTree.helpers import str2bool
|
|
13
|
+
from part.models import BomItem, Part
|
|
14
|
+
from plugin.registry import registry
|
|
15
|
+
|
|
16
|
+
from wireviz.Harness import Harness
|
|
17
|
+
from wireviz.wireviz import parse as parse_wireviz
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
logger = logging.getLogger("inventree")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def get_unit_registry():
|
|
24
|
+
"""Return the pint unit registry"""
|
|
25
|
+
|
|
26
|
+
from InvenTree.api_version import INVENTREE_API_VERSION
|
|
27
|
+
|
|
28
|
+
if INVENTREE_API_VERSION >= 117:
|
|
29
|
+
# Modern version of InvenTree supports custom unit registry
|
|
30
|
+
import InvenTree.conversion
|
|
31
|
+
return InvenTree.conversion.get_unit_registry()
|
|
32
|
+
|
|
33
|
+
# Fallback to the default pint unit registry
|
|
34
|
+
import pint
|
|
35
|
+
return pint.UnitRegistry()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class WirevizImportManager:
|
|
39
|
+
"""Class for managing a wireviz file import session."""
|
|
40
|
+
|
|
41
|
+
def __init__(self):
|
|
42
|
+
"""Initialize the WirevizImportManager."""
|
|
43
|
+
|
|
44
|
+
# Grab a reference to the wireviz plugin
|
|
45
|
+
self.plugin = registry.get_plugin('wireviz')
|
|
46
|
+
|
|
47
|
+
# Get the unit registry
|
|
48
|
+
self.ureg = get_unit_registry()
|
|
49
|
+
|
|
50
|
+
self.part = None
|
|
51
|
+
self.errors = []
|
|
52
|
+
self.warnings = []
|
|
53
|
+
self.part_map = {}
|
|
54
|
+
|
|
55
|
+
def create_attachment(self, part, attachment, comment, user):
|
|
56
|
+
"""Upload a file attachment to the specified part.
|
|
57
|
+
|
|
58
|
+
Note: We support both the "legacy" and "modern" attachment table.
|
|
59
|
+
|
|
60
|
+
Ref: https://github.com/inventree/InvenTree/pull/7420
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
# First, try the "modern" attachment table
|
|
64
|
+
try:
|
|
65
|
+
from common.models import Attachment
|
|
66
|
+
|
|
67
|
+
return Attachment.objects.create(
|
|
68
|
+
model_type='part',
|
|
69
|
+
model_id=part.pk,
|
|
70
|
+
attachment=attachment,
|
|
71
|
+
comment=comment,
|
|
72
|
+
upload_user=user
|
|
73
|
+
)
|
|
74
|
+
except Exception:
|
|
75
|
+
pass
|
|
76
|
+
|
|
77
|
+
# Second, try the "legacy" attachment table
|
|
78
|
+
try:
|
|
79
|
+
from part.models import PartAttachment
|
|
80
|
+
|
|
81
|
+
return PartAttachment.objects.create(
|
|
82
|
+
part=part,
|
|
83
|
+
attachment=attachment,
|
|
84
|
+
comment=comment,
|
|
85
|
+
user=user
|
|
86
|
+
)
|
|
87
|
+
except Exception:
|
|
88
|
+
pass
|
|
89
|
+
|
|
90
|
+
# Attachment not created
|
|
91
|
+
raise ValidationError("Error creating attachment file")
|
|
92
|
+
|
|
93
|
+
def prepend_templates(self):
|
|
94
|
+
"""Prepend the contents of the wireviz template files to the wireviz file."""
|
|
95
|
+
|
|
96
|
+
prepend_data = ''
|
|
97
|
+
|
|
98
|
+
for template in self.plugin.get_template_files():
|
|
99
|
+
tf = os.path.abspath(os.path.join(settings.MEDIA_ROOT, template))
|
|
100
|
+
|
|
101
|
+
with open(tf, 'r') as f:
|
|
102
|
+
|
|
103
|
+
template_data = f.read()
|
|
104
|
+
|
|
105
|
+
try:
|
|
106
|
+
yaml.safe_load(template_data)
|
|
107
|
+
except Exception as exc:
|
|
108
|
+
self.add_error(f"Invalid YAML data in template file '{template}'")
|
|
109
|
+
self.add_error(f"YAML parsing error: {exc}")
|
|
110
|
+
continue
|
|
111
|
+
|
|
112
|
+
prepend_data += template_data
|
|
113
|
+
prepend_data += '\n\n'
|
|
114
|
+
|
|
115
|
+
return prepend_data
|
|
116
|
+
|
|
117
|
+
def parse_wireviz_file(self, wv_file) -> Harness:
|
|
118
|
+
"""Process the provided wireviz file.
|
|
119
|
+
|
|
120
|
+
Returns a wireviz Harness object (if the file is valid)
|
|
121
|
+
|
|
122
|
+
Raises:
|
|
123
|
+
ValidationError: If the file is invalid (for some reason)
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
wv_file.seek(0)
|
|
127
|
+
wv_data = wv_file.read().decode('utf-8')
|
|
128
|
+
|
|
129
|
+
try:
|
|
130
|
+
yaml.safe_load(wv_data)
|
|
131
|
+
except Exception as exc:
|
|
132
|
+
raise ValidationError([
|
|
133
|
+
{str(exc)},
|
|
134
|
+
"Not a valid YAML file",
|
|
135
|
+
])
|
|
136
|
+
|
|
137
|
+
# Prepend data from existing templates
|
|
138
|
+
wv_data = self.prepend_templates() + wv_data
|
|
139
|
+
|
|
140
|
+
try:
|
|
141
|
+
harness = parse_wireviz(wv_data, return_types='harness')
|
|
142
|
+
except Exception as exc:
|
|
143
|
+
raise ValidationError([
|
|
144
|
+
{str(exc)},
|
|
145
|
+
"Failed to parse wireviz file:",
|
|
146
|
+
])
|
|
147
|
+
|
|
148
|
+
return harness
|
|
149
|
+
|
|
150
|
+
def cleanup_old_files(self, part: Part):
|
|
151
|
+
"""Remove old files from an existing Part instance."""
|
|
152
|
+
|
|
153
|
+
metadata = part.get_metadata('wireviz')
|
|
154
|
+
|
|
155
|
+
if not metadata:
|
|
156
|
+
return
|
|
157
|
+
|
|
158
|
+
file_keys = [
|
|
159
|
+
'source_file',
|
|
160
|
+
'svg_file',
|
|
161
|
+
]
|
|
162
|
+
|
|
163
|
+
filenames = []
|
|
164
|
+
|
|
165
|
+
for key in file_keys:
|
|
166
|
+
if key in metadata:
|
|
167
|
+
filenames.append(metadata[key])
|
|
168
|
+
|
|
169
|
+
for attachment in part.attachments.all():
|
|
170
|
+
fn = attachment.attachment.name
|
|
171
|
+
|
|
172
|
+
if fn in filenames or os.path.basename(fn) in filenames:
|
|
173
|
+
logger.info("WireViz: Deleting old file '%s'", fn)
|
|
174
|
+
attachment.delete()
|
|
175
|
+
|
|
176
|
+
def import_harness(self, wv_file, part: Part, user):
|
|
177
|
+
"""Import a wireviz file into the specified part."""
|
|
178
|
+
|
|
179
|
+
logger.info("Importing wireviz harness file")
|
|
180
|
+
|
|
181
|
+
# Determine runtime settings
|
|
182
|
+
delete_old_files = str2bool(self.plugin.get_setting('DELETE_OLD_FILES'))
|
|
183
|
+
save_bom_data = str2bool(self.plugin.get_setting('EXTRACT_BOM'))
|
|
184
|
+
clear_bom_data = str2bool(self.plugin.get_setting('CLEAR_BOM_DATA'))
|
|
185
|
+
|
|
186
|
+
self.part = part
|
|
187
|
+
|
|
188
|
+
if delete_old_files:
|
|
189
|
+
self.cleanup_old_files(part)
|
|
190
|
+
|
|
191
|
+
# Extract harness information
|
|
192
|
+
harness = self.parse_wireviz_file(wv_file.file)
|
|
193
|
+
|
|
194
|
+
self.extract_bom_data(harness)
|
|
195
|
+
|
|
196
|
+
if save_bom_data:
|
|
197
|
+
if clear_bom_data:
|
|
198
|
+
self.part.bom_items.all().delete()
|
|
199
|
+
|
|
200
|
+
# Bulk create new Bom Items
|
|
201
|
+
BomItem.objects.bulk_create(self.bom_items)
|
|
202
|
+
|
|
203
|
+
wv_file.file.seek(0)
|
|
204
|
+
wv_data = wv_file.file.read().decode('utf-8')
|
|
205
|
+
|
|
206
|
+
wv_filename = os.path.basename(wv_file.name)
|
|
207
|
+
|
|
208
|
+
# Save the uploaded wireviz file as an attachment for the Part instance
|
|
209
|
+
wv_attachment = self.create_attachment(
|
|
210
|
+
self.part,
|
|
211
|
+
ContentFile(wv_data, name=wv_filename),
|
|
212
|
+
'Wireviz Harness File',
|
|
213
|
+
user,
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
# Generate SVG output
|
|
217
|
+
try:
|
|
218
|
+
svg_file = self.generate_svg_output(harness, wv_filename, user)
|
|
219
|
+
svg_file = svg_file.attachment.name
|
|
220
|
+
except Exception as exc:
|
|
221
|
+
svg_file = None
|
|
222
|
+
self.add_error(f"Failed to generate SVG file: {exc}")
|
|
223
|
+
|
|
224
|
+
# Update the part metadata
|
|
225
|
+
wireviz_data = {
|
|
226
|
+
'source_file': wv_attachment.attachment.name,
|
|
227
|
+
'svg_file': svg_file,
|
|
228
|
+
'bom_data': self.bom_lines,
|
|
229
|
+
'errors': self.errors,
|
|
230
|
+
'warnings': self.warnings
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
self.part.set_metadata('wireviz', wireviz_data, overwrite=True)
|
|
234
|
+
|
|
235
|
+
def extract_bom_data(self, harness: Harness):
|
|
236
|
+
"""Extract BOM data from the provided harness file."""
|
|
237
|
+
|
|
238
|
+
logger.info("WireViz: Extracting BOM data from wireviz file")
|
|
239
|
+
|
|
240
|
+
bom = harness.bom()
|
|
241
|
+
|
|
242
|
+
self.bom_items = []
|
|
243
|
+
self.bom_lines = []
|
|
244
|
+
|
|
245
|
+
for line in bom:
|
|
246
|
+
designators = line.get('designators', [])
|
|
247
|
+
description = line.get('description', None)
|
|
248
|
+
pn = line.get('pn', None)
|
|
249
|
+
mpn = line.get('mpn', None)
|
|
250
|
+
spn = line.get('spn', None)
|
|
251
|
+
quantity = line.get('qty', None)
|
|
252
|
+
unit = line.get('unit', None)
|
|
253
|
+
|
|
254
|
+
try:
|
|
255
|
+
quantity = float(quantity)
|
|
256
|
+
except (TypeError, ValueError):
|
|
257
|
+
self.add_error(f"Invalid quantity for line: {line}")
|
|
258
|
+
continue
|
|
259
|
+
|
|
260
|
+
sub_part = self.match_part(line)
|
|
261
|
+
|
|
262
|
+
# Add line to internally stored BOM data
|
|
263
|
+
self.bom_lines.append({
|
|
264
|
+
'idx': len(self.bom_lines) + 1,
|
|
265
|
+
'description': description,
|
|
266
|
+
'designators': ', '.join(designators),
|
|
267
|
+
'quantity': quantity,
|
|
268
|
+
'unit': unit,
|
|
269
|
+
'sub_part': sub_part.pk if sub_part else None,
|
|
270
|
+
'pn': pn,
|
|
271
|
+
'mpn': mpn,
|
|
272
|
+
'spn': spn,
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
if not sub_part:
|
|
276
|
+
# No matching part can be found
|
|
277
|
+
continue
|
|
278
|
+
|
|
279
|
+
if sub_part == self.part:
|
|
280
|
+
self.add_error(f"Part {sub_part} is the same as the parent part")
|
|
281
|
+
continue
|
|
282
|
+
|
|
283
|
+
# Check that it is a *valid* option for the BOM
|
|
284
|
+
if not sub_part.check_add_to_bom(self.part):
|
|
285
|
+
self.add_error(f"Part {sub_part} is not a valid option for the BOM")
|
|
286
|
+
continue
|
|
287
|
+
|
|
288
|
+
# Associate the internal part with the designators
|
|
289
|
+
for designator in designators:
|
|
290
|
+
self.part_map[designator] = sub_part
|
|
291
|
+
|
|
292
|
+
if unit or sub_part.units:
|
|
293
|
+
quantity = self.convert_quantity(quantity, unit, sub_part.units)
|
|
294
|
+
|
|
295
|
+
if not description:
|
|
296
|
+
self.add_error(f"No description for line: {line}")
|
|
297
|
+
continue
|
|
298
|
+
|
|
299
|
+
# Construct a new BomItem object
|
|
300
|
+
# Prevent zero-quantity BOM Items
|
|
301
|
+
if quantity > 0:
|
|
302
|
+
self.bom_items.append(BomItem(
|
|
303
|
+
part=self.part,
|
|
304
|
+
sub_part=sub_part,
|
|
305
|
+
quantity=quantity,
|
|
306
|
+
reference=' '.join(designators),
|
|
307
|
+
note="Wireviz BOM item"
|
|
308
|
+
))
|
|
309
|
+
|
|
310
|
+
def convert_quantity(self, quantity, unit, base_unit):
|
|
311
|
+
"""Convert a provided physical quantity into the "base units" of the part.
|
|
312
|
+
|
|
313
|
+
Args:
|
|
314
|
+
quantity: The quantity to convert
|
|
315
|
+
unit: The unit of the quantity
|
|
316
|
+
base_unit: The base unit of the part
|
|
317
|
+
|
|
318
|
+
Returns:
|
|
319
|
+
The converted quantity, or the original quantity if conversion failed
|
|
320
|
+
"""
|
|
321
|
+
|
|
322
|
+
# Ignore unit if quantity not given
|
|
323
|
+
# Will be specified in the part units anyway
|
|
324
|
+
if not unit:
|
|
325
|
+
return quantity
|
|
326
|
+
|
|
327
|
+
logger.debug(f"WirevizPlugin: Converting quantity {quantity} {unit} to {base_unit}")
|
|
328
|
+
|
|
329
|
+
q = f"{quantity} {unit}"
|
|
330
|
+
|
|
331
|
+
try:
|
|
332
|
+
val = self.ureg.Quantity(q)
|
|
333
|
+
|
|
334
|
+
if base_unit:
|
|
335
|
+
val = val.to(base_unit)
|
|
336
|
+
|
|
337
|
+
return float(val.magnitude)
|
|
338
|
+
except Exception:
|
|
339
|
+
self.add_error(f"Could not convert quantity {quantity} {unit} to {base_unit}")
|
|
340
|
+
return quantity
|
|
341
|
+
|
|
342
|
+
def add_error(self, msg: str):
|
|
343
|
+
"""Add an error message."""
|
|
344
|
+
self.errors.append(msg)
|
|
345
|
+
logger.error(f"WireViz: {msg}")
|
|
346
|
+
|
|
347
|
+
def add_warning(self, msg: str):
|
|
348
|
+
"""Add a warning message."""
|
|
349
|
+
self.warnings.append(msg)
|
|
350
|
+
logger.warning(f"WireViz: {msg}")
|
|
351
|
+
|
|
352
|
+
def match_part(self, line: dict):
|
|
353
|
+
"""Attempt to match a BOM line item to an InvenTree part.
|
|
354
|
+
|
|
355
|
+
Arguments:
|
|
356
|
+
line: A dictionary of BOM line item data
|
|
357
|
+
|
|
358
|
+
Returns:
|
|
359
|
+
A Part instance, or None
|
|
360
|
+
"""
|
|
361
|
+
|
|
362
|
+
# Extract data from BOM entry
|
|
363
|
+
pn = line.get('pn', None)
|
|
364
|
+
description = line.get('description', None)
|
|
365
|
+
mpn = line.get('mpn', None)
|
|
366
|
+
spn = line.get('spn', None)
|
|
367
|
+
|
|
368
|
+
# Match pn -> part.IPN
|
|
369
|
+
if pn:
|
|
370
|
+
results = Part.objects.filter(IPN=pn)
|
|
371
|
+
if results.count() == 1:
|
|
372
|
+
return results.first()
|
|
373
|
+
|
|
374
|
+
# Match pn -> part.name
|
|
375
|
+
results = Part.objects.filter(name=pn)
|
|
376
|
+
if results.count() == 1:
|
|
377
|
+
return results.first()
|
|
378
|
+
|
|
379
|
+
# Match description -> part.description
|
|
380
|
+
if description:
|
|
381
|
+
results = Part.objects.filter(description=description)
|
|
382
|
+
if results.count() == 1:
|
|
383
|
+
return results.first()
|
|
384
|
+
|
|
385
|
+
# Match mpn -> manufacturer_part.MPN
|
|
386
|
+
if mpn:
|
|
387
|
+
results = ManufacturerPart.objects.filter(MPN=mpn)
|
|
388
|
+
if results.count() == 1:
|
|
389
|
+
return results.first().part
|
|
390
|
+
|
|
391
|
+
# Match spn -> supplier_part.SKU
|
|
392
|
+
if spn:
|
|
393
|
+
results = SupplierPart.objects.filter(SKU=spn)
|
|
394
|
+
if results.count() == 1:
|
|
395
|
+
return results.first().part
|
|
396
|
+
|
|
397
|
+
# For a 'wire', append the wire color and try again
|
|
398
|
+
if pn and description and description.startswith('Wire, '):
|
|
399
|
+
wire_data = [x.strip() for x in description.split(',')]
|
|
400
|
+
|
|
401
|
+
"""
|
|
402
|
+
For individual wires, the PN does not include the color.
|
|
403
|
+
For example, a wire might have a PN "26AWG-PTFE"
|
|
404
|
+
To fully quality the wire, we need to append the color.
|
|
405
|
+
So, we might get a value like "26AWG-PTFE-YE" for a yellow wire.
|
|
406
|
+
"""
|
|
407
|
+
|
|
408
|
+
if len(wire_data) >= 3:
|
|
409
|
+
color = wire_data[2]
|
|
410
|
+
wire_pn = f"{pn}-{color}"
|
|
411
|
+
else:
|
|
412
|
+
wire_pn = pn
|
|
413
|
+
|
|
414
|
+
# Match wire_pn -> part.IPN
|
|
415
|
+
results = Part.objects.filter(IPN=wire_pn)
|
|
416
|
+
|
|
417
|
+
if results.count() == 1:
|
|
418
|
+
return results.first()
|
|
419
|
+
|
|
420
|
+
# Match wire_pn -> part.name
|
|
421
|
+
results = Part.objects.filter(name=wire_pn)
|
|
422
|
+
|
|
423
|
+
if results.count() == 1:
|
|
424
|
+
return results.first()
|
|
425
|
+
|
|
426
|
+
def generate_svg_output(self, harness: Harness, filename: str, user):
|
|
427
|
+
"""Generate SVG output from a wireviz harness."""
|
|
428
|
+
|
|
429
|
+
logger.info("WirevizPlugin: Generating SVG output for wireviz harness")
|
|
430
|
+
|
|
431
|
+
graph = harness.create_graph()
|
|
432
|
+
svg_data = graph.pipe(format='svg')
|
|
433
|
+
|
|
434
|
+
return self.create_attachment(
|
|
435
|
+
self.part,
|
|
436
|
+
ContentFile(
|
|
437
|
+
svg_data.decode('utf-8'),
|
|
438
|
+
name='wireviz_harness.svg',
|
|
439
|
+
),
|
|
440
|
+
f"Wireviz Harness (autogenerated from {filename})",
|
|
441
|
+
user
|
|
442
|
+
)
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"""DRF serializers for the wireviz plugin."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import yaml
|
|
5
|
+
|
|
6
|
+
from django.conf import settings
|
|
7
|
+
from rest_framework import serializers
|
|
8
|
+
from rest_framework.exceptions import ValidationError
|
|
9
|
+
|
|
10
|
+
from part.models import Part
|
|
11
|
+
|
|
12
|
+
from .processing import WirevizImportManager
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def template_path(template):
|
|
16
|
+
"""Return the fully qualified template path from a template string."""
|
|
17
|
+
|
|
18
|
+
template = os.path.basename(template)
|
|
19
|
+
|
|
20
|
+
return os.path.abspath(os.path.join(settings.MEDIA_ROOT, 'wireviz', template))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class WirevizDeleteSerializer(serializers.Serializer):
|
|
24
|
+
"""Serializer for deleting a wireviz file."""
|
|
25
|
+
|
|
26
|
+
part = serializers.PrimaryKeyRelatedField(
|
|
27
|
+
queryset=Part.objects.all(),
|
|
28
|
+
many=False,
|
|
29
|
+
required=True, allow_null=False,
|
|
30
|
+
label="Part",
|
|
31
|
+
help_text="Select part"
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
def save(self, **kwargs):
|
|
35
|
+
"""Remove wireviz harness from the specified part."""
|
|
36
|
+
|
|
37
|
+
part = self.validated_data['part']
|
|
38
|
+
part.set_metadata('wireviz', None)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class WirevizUploadSerializer(serializers.Serializer):
|
|
42
|
+
"""Serializer for uploading a wireviz file"""
|
|
43
|
+
|
|
44
|
+
part = serializers.PrimaryKeyRelatedField(
|
|
45
|
+
queryset=Part.objects.all(),
|
|
46
|
+
many=False,
|
|
47
|
+
required=True, allow_null=False,
|
|
48
|
+
label="Part",
|
|
49
|
+
help_text="Select part"
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
file = serializers.FileField(
|
|
53
|
+
label="Wireviz file",
|
|
54
|
+
help_text="Upload a wireviz file",
|
|
55
|
+
required=True,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
def validate_file(self, file):
|
|
59
|
+
"""Validate the uploaded wireviz file."""
|
|
60
|
+
|
|
61
|
+
try:
|
|
62
|
+
mgr = WirevizImportManager()
|
|
63
|
+
mgr.parse_wireviz_file(file.file)
|
|
64
|
+
except ValidationError as e:
|
|
65
|
+
raise e
|
|
66
|
+
except Exception:
|
|
67
|
+
raise ValidationError("Error parsing wireviz file")
|
|
68
|
+
|
|
69
|
+
return file
|
|
70
|
+
|
|
71
|
+
def save(self, user=None, **kwargs):
|
|
72
|
+
"""Save the validated serializer."""
|
|
73
|
+
|
|
74
|
+
data = self.validated_data
|
|
75
|
+
|
|
76
|
+
wv_file = data['file']
|
|
77
|
+
part = data['part']
|
|
78
|
+
|
|
79
|
+
mgr = WirevizImportManager()
|
|
80
|
+
mgr.import_harness(wv_file, part, user)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class UploadTemplateSerializer(serializers.Serializer):
|
|
84
|
+
"""Serializer for uploading a wireviz template file."""
|
|
85
|
+
|
|
86
|
+
template = serializers.FileField(
|
|
87
|
+
label="Template file",
|
|
88
|
+
help_text="Upload a wireviz template file",
|
|
89
|
+
required=True,
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
def validate_template(self, template):
|
|
93
|
+
"""Validate template file."""
|
|
94
|
+
|
|
95
|
+
if not template.name.endswith('.wireviz'):
|
|
96
|
+
raise ValidationError("File must be .wireviz file")
|
|
97
|
+
|
|
98
|
+
data = template.file.read().decode('utf-8')
|
|
99
|
+
|
|
100
|
+
try:
|
|
101
|
+
yaml.safe_load(data)
|
|
102
|
+
except yaml.YAMLError:
|
|
103
|
+
raise ValidationError("Invalid YAML file")
|
|
104
|
+
|
|
105
|
+
return template
|
|
106
|
+
|
|
107
|
+
def save(self, **kwargs):
|
|
108
|
+
"""Save the uploaded wireviz template file."""
|
|
109
|
+
|
|
110
|
+
# Ensure directory exists first
|
|
111
|
+
os.makedirs(os.path.join(settings.MEDIA_ROOT, 'wireviz'), exist_ok=True)
|
|
112
|
+
template = self.validated_data['template']
|
|
113
|
+
filename = template_path(template.name)
|
|
114
|
+
|
|
115
|
+
with open(filename, 'w') as output:
|
|
116
|
+
template.file.seek(0)
|
|
117
|
+
output.write(template.file.read().decode('utf-8'))
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class DeleteTemplateSerializer(serializers.Serializer):
|
|
121
|
+
"""Serializer for deleting a wireviz template file."""
|
|
122
|
+
|
|
123
|
+
template = serializers.CharField(
|
|
124
|
+
label="Template file name",
|
|
125
|
+
help_text="Select wireviz template file",
|
|
126
|
+
required=True, allow_blank=False
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
def validate_template(self, template):
|
|
130
|
+
"""Ensure that the specified wireviz template file exists."""
|
|
131
|
+
|
|
132
|
+
path = template_path(template)
|
|
133
|
+
|
|
134
|
+
if not path:
|
|
135
|
+
raise serializers.ValidationError("Invalid wireviz template file")
|
|
136
|
+
|
|
137
|
+
if not os.path.exists(path):
|
|
138
|
+
raise serializers.ValidationError("Wireviz template file does not exist")
|
|
139
|
+
|
|
140
|
+
return template
|
|
141
|
+
|
|
142
|
+
def save(self, **kwargs):
|
|
143
|
+
"""Delete the specified wireviz template file."""
|
|
144
|
+
|
|
145
|
+
template = self.validated_data['template']
|
|
146
|
+
|
|
147
|
+
path = template_path(template)
|
|
148
|
+
|
|
149
|
+
if os.path.exists(path) and os.path.isfile(path):
|
|
150
|
+
os.remove(path)
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import{c as o,a as n,g as i,M as l}from"./assets/createReactComponent-CHpQc-nV.js";import{c as s}from"./assets/Plugins-BMhs_tNe.js";/**
|
|
2
|
+
* @license @tabler/icons-react v3.31.0 - MIT
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license.
|
|
5
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/var c=o("outline","topology-star","IconTopologyStar",[["path",{d:"M8 18a2 2 0 1 0 -4 0a2 2 0 0 0 4 0z",key:"svg-0"}],["path",{d:"M20 6a2 2 0 1 0 -4 0a2 2 0 0 0 4 0z",key:"svg-1"}],["path",{d:"M8 6a2 2 0 1 0 -4 0a2 2 0 0 0 4 0z",key:"svg-2"}],["path",{d:"M20 18a2 2 0 1 0 -4 0a2 2 0 0 0 4 0z",key:"svg-3"}],["path",{d:"M14 12a2 2 0 1 0 -4 0a2 2 0 0 0 4 0z",key:"svg-4"}],["path",{d:"M7.5 7.5l3 3",key:"svg-5"}],["path",{d:"M7.5 16.5l3 -3",key:"svg-6"}],["path",{d:"M13.5 13.5l3 3",key:"svg-7"}],["path",{d:"M16.5 7.5l-3 3",key:"svg-8"}]]);const p=window.MantineCore.Button,g=window.MantineCore.Group,m=window.MantineCore.Stack,d=window.MantineCore.Text;function u({context:e}){const t=e.forms.create({title:"Upload New Wireviz Diagram",url:n("/plugin/wireviz/upload/"),method:"POST",fields:{part:{filters:{active:!0,assembly:!0}},file:{}},successMessage:"Wireviz diagram uploaded",onFormSuccess:a=>{if(a!=null&&a.part){const r=i(l.part,a.part);e.navigate(`${r}/wireviz`)}}});return React.createElement(React.Fragment,null,t.modal,React.createElement(m,{gap:"xs"},React.createElement(d,{size:"lg"},"Upload Wireviz Diagram"),React.createElement(g,{gap:"xs"},React.createElement(c,null),React.createElement(p,{onClick:()=>t.open()},"Upload New Diagram"))))}function y(e){return s(e),React.createElement(u,{context:e})}export{y as renderWirevizDashboard};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import{c as v,a as M,g as f,M as z,n as D}from"./assets/createReactComponent-CHpQc-nV.js";import{c as C}from"./assets/Plugins-BMhs_tNe.js";import{I}from"./assets/IconTrash-CZbkoT-X.js";/**
|
|
2
|
+
* @license @tabler/icons-react v3.31.0 - MIT
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license.
|
|
5
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/var b=v("outline","dots-vertical","IconDotsVertical",[["path",{d:"M12 12m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0",key:"svg-0"}],["path",{d:"M12 19m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0",key:"svg-1"}],["path",{d:"M12 5m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0",key:"svg-2"}]]);/**
|
|
7
|
+
* @license @tabler/icons-react v3.31.0 - MIT
|
|
8
|
+
*
|
|
9
|
+
* This source code is licensed under the MIT license.
|
|
10
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
11
|
+
*/var W=v("outline","file","IconFile",[["path",{d:"M14 3v4a1 1 0 0 0 1 1h4",key:"svg-0"}],["path",{d:"M17 21h-10a2 2 0 0 1 -2 -2v-14a2 2 0 0 1 2 -2h7l5 5v11a2 2 0 0 1 -2 2z",key:"svg-1"}]]);/**
|
|
12
|
+
* @license @tabler/icons-react v3.31.0 - MIT
|
|
13
|
+
*
|
|
14
|
+
* This source code is licensed under the MIT license.
|
|
15
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
16
|
+
*/var S=v("outline","upload","IconUpload",[["path",{d:"M4 17v2a2 2 0 0 0 2 2h12a2 2 0 0 0 2 -2v-2",key:"svg-0"}],["path",{d:"M7 9l5 -5l5 5",key:"svg-1"}],["path",{d:"M12 4l0 12",key:"svg-2"}]]);const _=window.MantineCore.ActionIcon,s=window.MantineCore.Alert,y=window.MantineCore.Anchor,k=window.MantineCore.Divider,g=window.MantineCore.Group,A=window.MantineCore.Image,i=window.MantineCore.Menu,E=window.MantineCore.Paper,P=window.MantineCore.SimpleGrid,o=window.MantineCore.Stack,t=window.MantineCore.Table,c=window.MantineCore.Text,T=window.MantineCore.Title,r=window.React.useMemo;function F({context:a,row:e}){const l=e.sub_part&&e.pn?f(z.part,e.sub_part,!0):"";return React.createElement(t.Tr,{key:`bom-${e.idx}`},React.createElement(t.Td,{key:"col-idx"},e.idx),React.createElement(t.Td,{key:"col-dsg"},e.designators),React.createElement(t.Td,{key:"col-dsc"},e.description),React.createElement(t.Td,{key:"col-qua"},React.createElement(g,{gap:"xs",wrap:"nowrap"},React.createElement(c,{key:"quantity"},e.quantity),e.unit&&React.createElement(c,{key:"unit",size:"sm"},e.unit))),React.createElement(t.Td,{key:"col-prt"},l?React.createElement(y,{href:l,onClick:m=>D(l,a.navigate,m)},e.pn):React.createElement(c,null,"-")))}function U({context:a}){const e=r(()=>(a==null?void 0:a.context)??{},[a]),l=r(()=>e.wireviz_bom_data??[],[e]),m=r(()=>e.wireviz_svg_file??null,[e]),d=r(()=>e.wireviz_errors??[],[e]),u=r(()=>e.wireviz_warnings??[],[e]),p=r(()=>e.wireviz_source_file??null,[e]),R=r(()=>(e==null?void 0:e.can_edit)??!1,[e]),w=a.forms.create({title:"Remove Wireviz Diagram",url:M("/plugin/wireviz/delete/"),method:"POST",fields:{part:{value:a.id,hidden:!0}},preFormContent:React.createElement(s,{color:"red",title:"Delete Diagram"},"Delete the Wireviz diagram from this part?"),successMessage:"Diagram removed",onFormSuccess:()=>{window.location.reload()}}),h=a.forms.create({title:"Upload New Wireviz Diagram",url:M("/plugin/wireviz/upload/"),method:"POST",fields:{part:{value:a.id,hidden:!0},file:{}},successMessage:"Diagram uploaded",onFormSuccess:()=>{window.location.reload()}});return React.createElement(React.Fragment,null,R&&h.modal,R&&w.modal,React.createElement(o,{gap:"xs",w:"100%"},d&&d.length>0&&React.createElement(s,{color:"red",title:"Wireviz Errors"},React.createElement(o,{gap:"xs"},d.map(n=>React.createElement(c,{key:n},n)))),u&&u.length>0&&React.createElement(s,{color:"yellow",title:"Wireviz Warnings"},React.createElement(o,{gap:"xs"},u.map(n=>React.createElement(c,{key:n},n)))),React.createElement(P,{cols:2},React.createElement(E,{shadow:"lg",p:"md"},React.createElement(o,{gap:"xs"},React.createElement(g,{justify:"space-between"},React.createElement(T,{order:4},"Harness Diagram"),R&&React.createElement(i,{position:"bottom-end",withArrow:!0},React.createElement(i.Target,null,React.createElement(_,{variant:"transparent"},React.createElement(b,null))),React.createElement(i.Dropdown,null,React.createElement(i.Item,{onClick:h.open,leftSection:React.createElement(S,{color:"green"})},"Upload New Diagram"),p&&React.createElement(i.Item,{onClick:w.open,leftSection:React.createElement(I,{color:"red"})},"Remove Diagram")))),React.createElement(k,null),m?React.createElement(A,{src:m,alt:"Wireviz diagram"}):React.createElement(s,{color:"red",title:"No Diagram Available"},React.createElement(c,null,"No Wireviz diagram available for this part")))),React.createElement(E,{shadow:"lg",p:"md"},React.createElement(o,{gap:"xs"},React.createElement(T,{order:4},"Bill of Materials"),React.createElement(k,null),React.createElement(t,{striped:!0},React.createElement(t.Thead,null,React.createElement(t.Tr,null,React.createElement(t.Th,null,"ID"),React.createElement(t.Th,null,"Designators"),React.createElement(t.Th,null,"Description"),React.createElement(t.Th,null,"Quantity"),React.createElement(t.Th,null,"Part"))),React.createElement(t.Tbody,null,l.map(n=>React.createElement(F,{row:n,context:a}))))))),p&&React.createElement(E,{shadow:"lg",p:"md"},React.createElement(y,{href:p,target:"_blank"},React.createElement(g,{gap:"xs"},React.createElement(W,null),React.createElement(c,null,"Wireviz Source File"))))))}function V(a){return C(a),React.createElement(U,{context:a})}export{V as renderWirevizPanel};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import{c as T,a as n,g as E,M as g}from"./assets/createReactComponent-CHpQc-nV.js";import{I as M}from"./assets/IconTrash-CZbkoT-X.js";/**
|
|
2
|
+
* @license @tabler/icons-react v3.31.0 - MIT
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license.
|
|
5
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/var v=T("outline","circle-plus","IconCirclePlus",[["path",{d:"M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0",key:"svg-0"}],["path",{d:"M9 12h6",key:"svg-1"}],["path",{d:"M12 9v6",key:"svg-2"}]]);const p=window.MantineCore.Alert,c=window.MantineCore.Button,r=window.MantineCore.Group,f=window.MantineCore.Stack,t=window.MantineCore.Table,l=window.MantineCore.Text,C=window.MantineCore.Title,o=window.MantineCore.Tooltip,b=window.React.useMemo,h=window.React.useState;function y({context:a}){const i=a.forms.create({title:"Upload New Wireviz Diagram",url:n("/plugin/wireviz/upload/"),method:"POST",fields:{part:{filters:{active:!0,assembly:!0}},file:{}},successMessage:"Wireviz diagram uploaded",onFormSuccess:e=>{if(e!=null&&e.part){const w=E(g.part,e.part);a.navigate(`${w}/wireviz`)}}}),s=a.forms.create({title:"Upload New Template",url:n("/plugin/wireviz/upload-template/"),fields:{template:{}},successMessage:"Template uploaded",onFormSuccess:()=>{}}),[d,R]=h(null),m=a.forms.create({title:"Delete Template",url:n("/plugin/wireviz/delete-template/"),fields:{template:{value:d,hidden:!0}},preFormContent:React.createElement(p,{color:"orange",title:"Delete Template"},React.createElement(l,null,"Are you sure you want to delete this template?"),React.createElement(l,null,"This action cannot be undone!")),submitColor:"red",submitText:"Delete",successMessage:"Template deleted",onFormSuccess:()=>{}}),u=b(()=>{var e;return((e=a.context)==null?void 0:e.templates)??[]},[a.context]);return React.createElement(React.Fragment,null,i.modal,s.modal,m.modal,React.createElement(f,{gap:"xs"},React.createElement(r,{gap:"xs"},React.createElement(o,{label:"Upload new diagram"},React.createElement(c,{onClick:()=>i.open()},"Upload New Diagram"))),React.createElement(r,{justify:"space-between"},React.createElement(C,{order:4},"Templates"),React.createElement(o,{label:"Upload new template"},React.createElement(c,{variant:"outline",color:"green",onClick:s.open},React.createElement(v,null)))),u.length>0?React.createElement(t,{striped:!0},React.createElement(t.Thead,null,React.createElement(t.Tr,null,React.createElement(t.Td,null,"Template File"),React.createElement(t.Td,null))),React.createElement(t.Tbody,null,u.map(e=>React.createElement(t.Tr,{key:e},React.createElement(t.Td,null,React.createElement(l,null,e)),React.createElement(t.Td,null,React.createElement(r,{justify:"right"},React.createElement(o,{label:"Delete template"},React.createElement(c,{variant:"outline",color:"red",onClick:()=>{R(e),m.open()}},React.createElement(M,null))))))))):React.createElement(p,{color:"blue",title:"No templates found"},React.createElement(l,null,"Upload a template to get started!"))))}function D(a){return React.createElement(y,{context:a})}export{D as renderPluginSettings};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import{c as a}from"./createReactComponent-CHpQc-nV.js";/**
|
|
2
|
+
* @license @tabler/icons-react v3.31.0 - MIT
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license.
|
|
5
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/var e=a("outline","trash","IconTrash",[["path",{d:"M4 7l16 0",key:"svg-0"}],["path",{d:"M10 11l0 6",key:"svg-1"}],["path",{d:"M14 11l0 6",key:"svg-2"}],["path",{d:"M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12",key:"svg-3"}],["path",{d:"M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3",key:"svg-4"}]]);export{e as I};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e="0.1.0";function s(n){var i;const o=((i=n==null?void 0:n.version)==null?void 0:i.inventree)||"";e!=o&&console.info(`Plugin version mismatch! Expected version ${e}, got ${o}`)}export{s as c};
|