diff --git a/crc/models/workflow.py b/crc/models/workflow.py index c8fa6bc7..bf7867f7 100644 --- a/crc/models/workflow.py +++ b/crc/models/workflow.py @@ -21,13 +21,15 @@ class WorkflowSpecCategorySchema(ma.Schema): class WorkflowSpecInfo(object): - def __init__(self, id, display_name, description, category_name, is_master_spec, + def __init__(self, id, display_name, description, category_name, + display_order, is_master_spec, standalone, library, primary_file_name, primary_process_id, is_review, libraries): self.id = id # Sting unqiue id self.display_name = display_name self.description = description self.category_name = category_name + self.display_order = display_order self.is_master_spec = is_master_spec self.standalone = standalone self.library = library diff --git a/crc/services/workflow_spec_service.py b/crc/services/workflow_spec_service.py index 64719508..138c0a03 100644 --- a/crc/services/workflow_spec_service.py +++ b/crc/services/workflow_spec_service.py @@ -1,5 +1,7 @@ import json import os +import shutil +from typing import List from SpiffWorkflow.bpmn.parser.ValidationException import ValidationException from lxml import etree @@ -29,14 +31,30 @@ class WorkflowSpecService(FileSystemService): self.standalone = {} self.scan_file_system() - def save_spec(self, spec:WorkflowSpecInfo): + def add_spec(self, spec: WorkflowSpecInfo): + self.update_spec(spec) + + def update_spec(self, spec:WorkflowSpecInfo): spec_path = self.workflow_path(spec) + os.makedirs(os.path.dirname(spec_path), exist_ok=True) json_path = os.path.join(spec_path, self.WF_JSON_FILE) with open(json_path, "w") as wf_json: json.dump(self.WF_SCHEMA.dump(spec), wf_json, indent=4) self.scan_file_system() - def reorder_workflow_spec(self, spec:WorkflowSpecInfo, direction): + def delete_spec(self, spec_id: str): + if spec_id in self.specs: + spec = self.specs[spec_id] + path = self.workflow_path(spec) + shutil.rmtree(path) + self.scan_file_system() + + def get_spec(self, spec_id: str): + if spec_id not in self.specs: + raise ApiError('unknown spec', 'unable to find a spec with id:' + spec_id) + return self.specs[spec_id] + + def reorder_spec(self, spec:WorkflowSpecInfo, direction): workflows = spec.category.workflows workflows.sort(key=lambda w: w.display_order) index = workflows.index_of(spec) @@ -51,9 +69,17 @@ class WorkflowSpecService(FileSystemService): index += 1 return workflows - def get_categories(self): - """Returns a list of categories in the correct order.""" - return list(self.categories.values()).sort(key=lambda x: x.display_order) + def get_libraries(self) -> List[WorkflowSpecInfo]: + # fixme + pass + + # get_categories() + # get_category(category_name) + # add_category(body: WorkflowCategory) + # update_category(category, body) + # delete_category(category) + + def reorder_workflow_spec_category(self, spec:WorkflowSpecInfo, direction): # Fixme: Resort Workflow categories @@ -185,21 +211,4 @@ class WorkflowSpecService(FileSystemService): return process_elements[0].attrib['id'] - # TODO Methods i would add... - # delete_workflow_spec(spec_id) - # get_workflow_spec(spec_id) - # update_spec(spec_id, body) - # add_spec(body) - - # Other methods i would add, maybe not here.. - # add_library(body) - # get_libraries() - # get_library(library) - # delete_library(library) - - # get_workflow_categories() - # get_workflow_category(category, body) - # add_workflow_category(body) - # update_workflow_category(category, body) - # delete_workflow_category(category) diff --git a/example_data.py b/example_data.py index 2568759b..82177e41 100644 --- a/example_data.py +++ b/example_data.py @@ -3,7 +3,7 @@ import os from crc import app, db, session from crc.models.file import CONTENT_TYPES -from crc.models.workflow import WorkflowSpecModel +from crc.models.workflow import WorkflowSpecInfo from crc.services.document_service import DocumentService from crc.services.reference_file_service import ReferenceFileService from crc.services.spec_file_service import SpecFileService @@ -28,14 +28,18 @@ class ExampleDataLoader: further assumes that the [id].bpmn is the primary file for the workflow. returns an array of data models to be added to the database.""" global file - spec = WorkflowSpecModel(id=id, - display_name=display_name, - description=description, - is_master_spec=master_spec, - category_id=category_id, - display_order=display_order, - standalone=standalone, - library=library) + spec = WorkflowSpecInfo(id=id, + display_name=display_name, + description=description, + category_name=category_id, + display_order=display_order, + is_master_spec=master_spec, + standalone=standalone, + library=library, + primary_file_name="", + primary_process_id="", + is_review=False, + libraries=[]) db.session.add(spec) db.session.commit() if not filepath and not from_tests: @@ -46,7 +50,7 @@ class ExampleDataLoader: files = glob.glob(filepath) for file_path in files: if os.path.isdir(file_path): - continue # Don't try to process sub directories + continue # Don't try to process sub directories noise, file_extension = os.path.splitext(file_path) filename = os.path.basename(file_path) @@ -79,4 +83,3 @@ class ExampleDataLoader: ReferenceFileService.add_reference_file(StudyService.INVESTIGATOR_LIST, file.read()) file.close() -