示例#1
0
    def update_workflow_spec_file_model(workflow_spec: WorkflowSpecModel,
                                        file_model: FileModel, binary_data,
                                        content_type):
        # Verify the extension
        file_extension = FileService.get_extension(file_model.name)
        if file_extension not in FileType._member_names_:
            raise ApiError(
                'unknown_extension',
                'The file you provided does not have an accepted extension:' +
                file_extension,
                status_code=404)
        else:
            file_model.type = FileType[file_extension]
            file_model.content_type = content_type
            file_model.archived = False  # Unarchive the file if it is archived.

        # If this is a BPMN, extract the process id.
        if file_model.type == FileType.bpmn:
            try:
                bpmn: etree.Element = etree.fromstring(binary_data)
                file_model.primary_process_id = SpecFileService.get_process_id(
                    bpmn)
                file_model.is_review = FileService.has_swimlane(bpmn)
            except etree.XMLSyntaxError as xse:
                raise ApiError("invalid_xml",
                               "Failed to parse xml: " + str(xse),
                               file_name=file_model.name)

        session.add(file_model)
        session.commit()

        return file_model
示例#2
0
    def add_workflow_spec_file(workflow_spec: WorkflowSpecModel,
                               name,
                               content_type,
                               binary_data,
                               primary=False,
                               is_status=False):
        """Create a new file and associate it with a workflow spec.
        3 steps; create file model, write file data to filesystem, write file info to file system"""
        file_model = session.query(FileModel)\
            .filter(FileModel.workflow_spec_id == workflow_spec.id)\
            .filter(FileModel.name == name).first()

        if file_model:
            if not file_model.archived:
                # Raise ApiError if the file already exists and is not archived
                raise ApiError(
                    code="duplicate_file",
                    message=
                    'If you want to replace the file, use the update mechanism.'
                )
        else:
            file_model = FileModel(
                workflow_spec_id=workflow_spec.id,
                name=name,
                primary=primary,
                is_status=is_status,
            )

        file_model = SpecFileService.update_workflow_spec_file_model(
            workflow_spec, file_model, binary_data, content_type)
        file_path = SpecFileService().write_spec_file_data_to_system(
            workflow_spec, file_model.name, binary_data)
        SpecFileService().write_spec_file_info_to_system(file_path, file_model)

        return file_model
示例#3
0
    def add_workflow_spec_file(workflow_spec: WorkflowSpecModel,
                               name, content_type, binary_data, primary=False, is_status=False):
        """Create a new file and associate it with a workflow spec."""
        file_model = FileModel(
            workflow_spec_id=workflow_spec.id,
            name=name,
            primary=primary,
            is_status=is_status,
        )

        return FileService.update_file(file_model, binary_data, content_type)
示例#4
0
 def add_reference_file(name, content_type, binary_data):
     """Create a file with the given name, but not associated with a spec or workflow.
        Only one file with the given reference name can exist."""
     file_model = session.query(FileModel). \
         filter(FileModel.is_reference == True). \
         filter(FileModel.name == name).first()
     if not file_model:
         file_model = FileModel(
             name=name,
             is_reference=True
         )
     return FileService.update_file(file_model, binary_data, content_type)
示例#5
0
    def add_workflow_file(workflow_id, irb_doc_code, task_spec_name, name,
                          content_type, binary_data):
        file_model = session.query(FileModel)\
            .filter(FileModel.workflow_id == workflow_id)\
            .filter(FileModel.name == name) \
            .filter(FileModel.task_spec == task_spec_name) \
            .filter(FileModel.irb_doc_code == irb_doc_code).first()

        if not file_model:
            file_model = FileModel(workflow_id=workflow_id,
                                   name=name,
                                   task_spec=task_spec_name,
                                   irb_doc_code=irb_doc_code)
        return FileService.update_file(file_model, binary_data, content_type)
示例#6
0
 def test_list_multiple_files_for_workflow_spec(self):
     self.load_example_data()
     spec = self.load_test_spec("random_fact")
     svgFile = FileModel(name="test.svg",
                         type=FileType.svg,
                         primary=False,
                         workflow_spec_id=spec.id)
     session.add(svgFile)
     session.flush()
     rv = self.app.get('/v1.0/file?workflow_spec_id=%s' % spec.id,
                       follow_redirects=True,
                       content_type="application/json",
                       headers=self.logged_in_headers())
     self.assert_success(rv)
     json_data = json.loads(rv.get_data(as_text=True))
     self.assertEqual(2, len(json_data))
    def add_reference_file(name, content_type, binary_data):
        """Create a file with the given name, but not associated with a spec or workflow.
           Only one file with the given reference name can exist."""
        file_model = session.query(FileModel). \
            filter(FileModel.is_reference == True). \
            filter(FileModel.name == name).first()
        if not file_model:
            file_extension = FileService.get_extension(name)
            file_type = FileType[file_extension].value

            file_model = FileModel(name=name,
                                   is_reference=True,
                                   type=file_type,
                                   content_type=content_type)
            session.add(file_model)
            session.commit()
        else:
            raise ApiError(
                code='file_already_exists',
                message=f"The reference file {name} already exists.")
        return ReferenceFileService().update_reference_file(
            file_model, binary_data)
示例#8
0
    def add_workflow_file(workflow_id, irb_doc_code, name, content_type, binary_data):
        """Create a new file and associate it with the workflow
        Please note that the irb_doc_code MUST be a known file in the irb_documents.xslx reference document."""
        if not FileService.is_allowed_document(irb_doc_code):
            raise ApiError("invalid_form_field_key",
                           "When uploading files, the form field id must match a known document in the "
                           "irb_docunents.xslx reference file.  This code is not found in that file '%s'" % irb_doc_code)

        """Assure this is unique to the workflow, task, and document code AND the Name
           Because we will allow users to upload multiple files for the same form field
            in some cases """
        file_model = session.query(FileModel)\
            .filter(FileModel.workflow_id == workflow_id)\
            .filter(FileModel.name == name)\
            .filter(FileModel.irb_doc_code == irb_doc_code).first()

        if not file_model:
            file_model = FileModel(
                workflow_id=workflow_id,
                name=name,
                irb_doc_code=irb_doc_code
            )
        return FileService.update_file(file_model, binary_data, content_type)
示例#9
0
    def test_update_file_info(self):
        self.load_example_data()
        file: FileModel = session.query(FileModel).filter(
            column('workflow_spec_id').isnot(None)).first()
        file_model = FileModel(id=file.id,
                               name="silly_new_name.bpmn",
                               type=file.type,
                               content_type=file.content_type,
                               is_reference=file.is_reference,
                               primary=file.primary,
                               primary_process_id=file.primary_process_id,
                               workflow_id=file.workflow_id,
                               workflow_spec_id=file.workflow_spec_id,
                               archived=file.archived)
        # file.name = "silly_new_name.bpmn"

        rv = self.app.put('/v1.0/spec_file/%i' % file.id,
                          content_type="application/json",
                          data=json.dumps(FileModelSchema().dump(file_model)),
                          headers=self.logged_in_headers())
        self.assert_success(rv)
        db_file = session.query(FileModel).filter_by(id=file.id).first()
        self.assertIsNotNone(db_file)
        self.assertEqual("silly_new_name.bpmn", db_file.name)
def update_or_create_current_file(remote, workflow_spec_id, updatefile):
    currentfile = file_get(workflow_spec_id, updatefile['filename'])
    if not currentfile:
        currentfile = FileModel()
        currentfile.name = updatefile['filename']
        if workflow_spec_id == 'REFERENCE_FILES':
            currentfile.workflow_spec_id = None
            currentfile.is_reference = True
        else:
            currentfile.workflow_spec_id = workflow_spec_id

    currentfile.date_created = updatefile['date_created']
    currentfile.type = updatefile['type']
    currentfile.primary = updatefile['primary']
    currentfile.content_type = updatefile['content_type']
    currentfile.primary_process_id = updatefile['primary_process_id']
    session.add(currentfile)
    try:
        content = WorkflowSyncService.get_remote_file_by_hash(
            remote, updatefile['md5_hash'])
        FileService.update_file(currentfile, content, updatefile['type'])
    except ApiError:
        # Remote files doesn't exist, don't update it.
        print("Remote file " + currentfile.name +
              " does not exist, so not syncing.")