2022-05-31 18:10:00 +00:00
|
|
|
"""Example_data."""
|
2022-05-25 20:38:03 +00:00
|
|
|
import glob
|
|
|
|
import os
|
2022-08-30 16:19:45 +00:00
|
|
|
from typing import Optional
|
2022-08-30 16:48:04 +00:00
|
|
|
|
2022-05-25 20:38:03 +00:00
|
|
|
from flask import current_app
|
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-11-01 20:48:04 +00:00
|
|
|
@staticmethod
|
2022-05-31 18:10:00 +00:00
|
|
|
def create_spec(
|
2022-08-30 16:19:45 +00:00
|
|
|
process_model_id: str,
|
2022-06-12 11:39:30 +00:00
|
|
|
display_name: str = "",
|
|
|
|
description: str = "",
|
|
|
|
display_order: int = 0,
|
2022-08-30 16:19:45 +00:00
|
|
|
bpmn_file_name: Optional[str] = None,
|
2022-11-09 18:45:16 +00:00
|
|
|
process_model_source_directory: Optional[str] = None,
|
2022-06-10 22:27:06 +00:00
|
|
|
) -> ProcessModelInfo:
|
2022-11-01 20:48:04 +00:00
|
|
|
"""Assumes that process_model_source_directory exists in static/bpmn and contains bpmn_file_name.
|
2022-05-25 20:38:03 +00:00
|
|
|
|
2022-11-01 20:48:04 +00:00
|
|
|
further assumes that bpmn_file_name is the primary file for the process model.
|
|
|
|
|
|
|
|
if bpmn_file_name is None we load all files in process_model_source_directory,
|
|
|
|
otherwise, we only load bpmn_file_name
|
2022-05-25 20:38:03 +00:00
|
|
|
"""
|
2022-11-01 20:48:04 +00:00
|
|
|
if process_model_source_directory is None:
|
|
|
|
raise Exception("You must include `process_model_source_directory`.")
|
|
|
|
|
2022-05-31 18:10:00 +00:00
|
|
|
spec = ProcessModelInfo(
|
2022-08-30 16:19:45 +00:00
|
|
|
id=process_model_id,
|
2022-05-31 18:10:00 +00:00
|
|
|
display_name=display_name,
|
|
|
|
description=description,
|
|
|
|
display_order=display_order,
|
|
|
|
is_review=False,
|
|
|
|
)
|
2022-05-25 20:38:03 +00:00
|
|
|
workflow_spec_service = ProcessModelService()
|
|
|
|
workflow_spec_service.add_spec(spec)
|
|
|
|
|
2022-08-30 16:19:45 +00:00
|
|
|
bpmn_file_name_with_extension = bpmn_file_name
|
|
|
|
if not bpmn_file_name_with_extension:
|
|
|
|
bpmn_file_name_with_extension = process_model_id
|
|
|
|
|
2022-08-30 16:48:04 +00:00
|
|
|
if not bpmn_file_name_with_extension.endswith(".bpmn"):
|
|
|
|
bpmn_file_name_with_extension += ".bpmn"
|
2022-08-30 16:19:45 +00:00
|
|
|
|
|
|
|
process_model_source_directory_to_use = process_model_source_directory
|
|
|
|
if not process_model_source_directory_to_use:
|
|
|
|
process_model_source_directory_to_use = process_model_id
|
|
|
|
|
2022-08-30 16:48:04 +00:00
|
|
|
file_name_matcher = "*.*"
|
2022-08-30 16:19:45 +00:00
|
|
|
if bpmn_file_name:
|
|
|
|
file_name_matcher = bpmn_file_name_with_extension
|
|
|
|
|
2022-11-09 21:54:14 +00:00
|
|
|
# we need instance_path here for nox tests
|
2022-11-01 20:48:04 +00:00
|
|
|
file_glob = os.path.join(
|
2022-11-09 21:54:14 +00:00
|
|
|
current_app.instance_path,
|
2022-11-01 20:48:04 +00:00
|
|
|
"..",
|
|
|
|
"..",
|
|
|
|
"tests",
|
|
|
|
"data",
|
|
|
|
process_model_source_directory_to_use,
|
|
|
|
file_name_matcher,
|
|
|
|
)
|
2022-05-25 20:38:03 +00:00
|
|
|
|
2022-06-22 20:52:50 +00:00
|
|
|
files = glob.glob(file_glob)
|
2022-05-25 20:38:03 +00:00
|
|
|
for file_path in files:
|
|
|
|
if os.path.isdir(file_path):
|
|
|
|
continue # Don't try to process sub directories
|
|
|
|
|
|
|
|
filename = os.path.basename(file_path)
|
2022-09-08 18:46:10 +00:00
|
|
|
# since there are multiple bpmn files in a test data directory, ensure we set the correct one as the primary
|
2022-08-30 16:19:45 +00:00
|
|
|
is_primary = filename.lower() == bpmn_file_name_with_extension
|
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(
|
2022-08-19 20:17:22 +00:00
|
|
|
process_model_info=spec, file_name=filename, binary_data=data
|
2022-05-31 18:10:00 +00:00
|
|
|
)
|
2022-05-25 20:38:03 +00:00
|
|
|
if is_primary:
|
2022-09-08 22:05:50 +00:00
|
|
|
SpecFileService.process_bpmn_file(
|
|
|
|
spec, filename, data, set_primary_file=True
|
|
|
|
)
|
2022-05-25 20:38:03 +00:00
|
|
|
workflow_spec_service = ProcessModelService()
|
2022-08-23 20:18:32 +00:00
|
|
|
workflow_spec_service.save_process_model(spec)
|
2022-05-25 20:38:03 +00:00
|
|
|
finally:
|
|
|
|
if file:
|
|
|
|
file.close()
|
|
|
|
return spec
|