Example #1
0
    def create_spec(self,
                    id,
                    name,
                    display_name="",
                    description="",
                    filepath=None,
                    master_spec=False,
                    category_id=None,
                    display_order=None,
                    from_tests=False):
        """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 workflow.
           returns an array of data models to be added to the database."""
        global file
        file_service = FileService()
        spec = WorkflowSpecModel(id=id,
                                 name=name,
                                 display_name=display_name,
                                 description=description,
                                 is_master_spec=master_spec,
                                 category_id=category_id,
                                 display_order=display_order)
        db.session.add(spec)
        db.session.commit()
        if not filepath and not from_tests:
            filepath = os.path.join(app.root_path, 'static', 'bpmn', id, "*")
        if not filepath and from_tests:
            filepath = os.path.join(app.root_path, '..', 'tests', 'data', id,
                                    "*")

        files = glob.glob(filepath)
        for file_path in files:
            noise, file_extension = os.path.splitext(file_path)
            filename = os.path.basename(file_path)

            is_status = filename.lower() == 'status.bpmn'
            is_primary = filename.lower() == id + '.bpmn'
            file = None
            try:
                file = open(file_path, 'rb')
                data = file.read()
                content_type = CONTENT_TYPES[file_extension[1:]]
                file_service.add_workflow_spec_file(workflow_spec=spec,
                                                    name=filename,
                                                    content_type=content_type,
                                                    binary_data=data,
                                                    primary=is_primary,
                                                    is_status=is_status)
            except IsADirectoryError as de:
                # Ignore sub directories
                pass
            finally:
                if file:
                    file.close()
        return spec
Example #2
0
    def test_spec_versioning(self):
        self.load_example_data()
        study = session.query(StudyModel).first()
        workflow_spec_model = self.load_test_spec("decision_table")
        processor = self.get_processor(study, workflow_spec_model)
        self.assertTrue(processor.get_version_string().startswith('v1.1'))
        file_service = FileService()

        file_service.add_workflow_spec_file(workflow_spec_model,
                                            "new_file.txt", "txt", b'blahblah')
        processor = self.get_processor(study, workflow_spec_model)
        self.assertTrue(processor.get_version_string().startswith('v1.1.1'))

        file_path = os.path.join(app.root_path, '..', 'tests', 'data', 'docx',
                                 'docx.bpmn')
        file = open(file_path, "rb")
        data = file.read()

        file_model = db.session.query(FileModel).filter(
            FileModel.name == "decision_table.bpmn").first()
        file_service.update_file(file_model, data, "txt")
        processor = self.get_processor(study, workflow_spec_model)
        self.assertTrue(processor.get_version_string().startswith('v2.1.1'))
Example #3
0
def add_file(workflow_spec_id=None, workflow_id=None, form_field_key=None):
    file = connexion.request.files['file']
    if workflow_id:
        if form_field_key is None:
            raise ApiError('invalid_workflow_file',
                           'When adding a workflow related file, you must specify a form_field_key')
        file_model = FileService.add_workflow_file(workflow_id=workflow_id, irb_doc_code=form_field_key,
                                                   name=file.filename, content_type=file.content_type,
                                                   binary_data=file.stream.read())
    elif workflow_spec_id:
        workflow_spec = session.query(WorkflowSpecModel).filter_by(id=workflow_spec_id).first()
        file_model = FileService.add_workflow_spec_file(workflow_spec, file.filename, file.content_type,
                                                        file.stream.read())
    else:
        raise ApiError("invalid_file", "You must supply either a workflow spec id or a workflow_id and form_field_key.")

    return FileSchema().dump(to_file_api(file_model))
    def process_workflow_spec_file(json_file, spec_directory):
        file_path = os.path.join(spec_directory, json_file)

        with open(file_path, 'r') as json_handle:
            data = json_handle.read()
            data_obj = json.loads(data)
            spec_file_name = '.'.join(json_file.name.split('.')[:-1])
            spec_file_path = os.path.join(spec_directory, spec_file_name)

            with open(spec_file_path, 'rb') as spec_handle:
                # workflow_spec_name = spec_directory.split('/')[-1]
                # workflow_spec = session.query(WorkflowSpecModel).filter(
                #     WorkflowSpecModel.display_name == workflow_spec_name).first()

                workflow_spec_file_model = session.query(FileModel).\
                    filter(FileModel.workflow_spec_id == data_obj['workflow_spec_id']).\
                    filter(FileModel.name == spec_file_name).\
                    first()
                if workflow_spec_file_model:
                    # update workflow_spec_file_model
                    FileService.update_file(
                        workflow_spec_file_model, spec_handle.read(),
                        CONTENT_TYPES[spec_file_name.split('.')[-1]])
                else:
                    # create new model
                    workflow_spec = session.query(WorkflowSpecModel).filter(
                        WorkflowSpecModel.id ==
                        data_obj['workflow_spec_id']).first()
                    workflow_spec_file_model = FileService.add_workflow_spec_file(
                        workflow_spec,
                        name=spec_file_name,
                        content_type=CONTENT_TYPES[spec_file_name.split('.')
                                                   [-1]],
                        binary_data=spec_handle.read())

            print(f'process_workflow_spec_file: data_obj: {data_obj}')
        return workflow_spec_file_model