spiffworkflow-backend/tests/spiffworkflow_backend/helpers/example_data.py

84 lines
2.9 KiB
Python
Raw Normal View History

2022-05-31 18:10:00 +00:00
"""Example_data."""
import glob
import os
from flask import current_app
2022-05-31 18:10:00 +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
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:
"""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=[],
)
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, "*.*")
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
)
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"
file = None
try:
2022-05-31 18:10:00 +00:00
file = open(file_path, "rb")
data = file.read()
2022-05-31 18:10:00 +00:00
SpecFileService.add_file(
workflow_spec=spec, file_name=filename, binary_data=data
)
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