2022-05-31 18:10:00 +00:00
|
|
|
"""Example_data."""
|
2022-05-25 20:38:03 +00:00
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
|
|
|
|
from flask import current_app
|
2022-05-31 18:10:00 +00:00
|
|
|
|
2022-06-01 15:17:25 +00:00
|
|
|
from spiffworkflow_backend.models.process_model import ProcessModelInfo
|
|
|
|
from spiffworkflow_backend.services.process_model_service import ProcessModelService
|
|
|
|
from spiffworkflow_backend.services.spec_file_service import SpecFileService
|
2022-05-25 20:38:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ExampleDataLoader:
|
|
|
|
"""ExampleDataLoader."""
|
|
|
|
|
2022-05-31 18:10:00 +00:00
|
|
|
def create_spec(
|
|
|
|
self,
|
2022-06-10 22:27:06 +00:00
|
|
|
id: str,
|
2022-06-12 11:39:30 +00:00
|
|
|
display_name: str = "",
|
|
|
|
description: str = "",
|
|
|
|
filepath: None = None,
|
|
|
|
master_spec: bool = False,
|
|
|
|
process_group_id: str = "",
|
|
|
|
display_order: int = 0,
|
|
|
|
from_tests: bool = False,
|
|
|
|
standalone: bool = False,
|
|
|
|
library: bool = False,
|
2022-06-10 22:27:06 +00:00
|
|
|
) -> ProcessModelInfo:
|
2022-05-25 20:38:03 +00:00
|
|
|
"""Assumes that a directory exists in static/bpmn with the same name as the given id.
|
|
|
|
|
|
|
|
further assumes that the [id].bpmn is the primary file for the process model.
|
|
|
|
returns an array of data models to be added to the database.
|
|
|
|
"""
|
|
|
|
global file
|
2022-05-31 18:10:00 +00:00
|
|
|
spec = ProcessModelInfo(
|
|
|
|
id=id,
|
|
|
|
display_name=display_name,
|
|
|
|
description=description,
|
|
|
|
process_group_id=process_group_id,
|
|
|
|
display_order=display_order,
|
|
|
|
is_master_spec=master_spec,
|
|
|
|
standalone=standalone,
|
|
|
|
library=library,
|
|
|
|
primary_file_name="",
|
|
|
|
primary_process_id="",
|
|
|
|
is_review=False,
|
|
|
|
libraries=[],
|
|
|
|
)
|
2022-05-25 20:38:03 +00:00
|
|
|
workflow_spec_service = ProcessModelService()
|
|
|
|
workflow_spec_service.add_spec(spec)
|
|
|
|
|
|
|
|
if not filepath and not from_tests:
|
2022-05-31 18:10:00 +00:00
|
|
|
filepath = os.path.join(current_app.root_path, "static", "bpmn", id, "*.*")
|
2022-05-25 20:38:03 +00:00
|
|
|
if not filepath and from_tests:
|
2022-05-31 18:10:00 +00:00
|
|
|
filepath = os.path.join(
|
2022-06-21 04:34:40 +00:00
|
|
|
current_app.instance_path, "..", "..", "tests", "data", id, "*.*"
|
2022-05-31 18:10:00 +00:00
|
|
|
)
|
2022-05-25 20:38:03 +00:00
|
|
|
|
|
|
|
files = glob.glob(filepath)
|
|
|
|
for file_path in files:
|
|
|
|
if os.path.isdir(file_path):
|
|
|
|
continue # Don't try to process sub directories
|
|
|
|
|
|
|
|
noise, file_extension = os.path.splitext(file_path)
|
|
|
|
filename = os.path.basename(file_path)
|
2022-05-31 18:10:00 +00:00
|
|
|
is_primary = filename.lower() == id + ".bpmn"
|
2022-05-25 20:38:03 +00:00
|
|
|
file = None
|
|
|
|
try:
|
2022-05-31 18:10:00 +00:00
|
|
|
file = open(file_path, "rb")
|
2022-05-25 20:38:03 +00:00
|
|
|
data = file.read()
|
2022-05-31 18:10:00 +00:00
|
|
|
SpecFileService.add_file(
|
|
|
|
workflow_spec=spec, file_name=filename, binary_data=data
|
|
|
|
)
|
2022-05-25 20:38:03 +00:00
|
|
|
if is_primary:
|
|
|
|
SpecFileService.set_primary_bpmn(spec, filename, data)
|
|
|
|
workflow_spec_service = ProcessModelService()
|
|
|
|
workflow_spec_service.update_spec(spec)
|
|
|
|
except IsADirectoryError:
|
|
|
|
# Ignore sub directories
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
if file:
|
|
|
|
file.close()
|
|
|
|
return spec
|