Example #1
0
    def test_add_file_from_task_increments_version_and_replaces_on_subsequent_add(
            self):
        self.load_example_data()
        self.create_reference_document()
        workflow = self.create_workflow('file_upload_form')
        processor = WorkflowProcessor(workflow)
        task = processor.next_task()
        irb_code = "UVACompl_PRCAppr"  # The first file referenced in pb required docs.
        FileService.add_workflow_file(workflow_id=workflow.id,
                                      name="anything.png",
                                      content_type="text",
                                      binary_data=b'1234',
                                      irb_doc_code=irb_code)
        # Add the file again with different data
        FileService.add_workflow_file(workflow_id=workflow.id,
                                      name="anything.png",
                                      content_type="text",
                                      binary_data=b'5678',
                                      irb_doc_code=irb_code)

        file_models = FileService.get_workflow_files(workflow_id=workflow.id)
        self.assertEqual(1, len(file_models))

        file_data = FileService.get_workflow_data_files(
            workflow_id=workflow.id)
        self.assertEqual(1, len(file_data))
        self.assertEqual(2, file_data[0].version)
Example #2
0
    def test_replace_archive_file_unarchives_the_file_and_updates(self):
        self.load_example_data()
        workflow = self.create_workflow('file_upload_form')
        processor = WorkflowProcessor(workflow)
        task = processor.next_task()
        irb_code = "UVACompl_PRCAppr"  # The first file referenced in pb required docs.
        FileService.add_workflow_file(workflow_id=workflow.id,
                                      irb_doc_code=irb_code,
                                      task_spec_name=task.get_name(),
                                      name="anything.png",
                                      content_type="text",
                                      binary_data=b'1234')

        # Archive the file
        file_models = FileService.get_workflow_files(workflow_id=workflow.id)
        self.assertEqual(1, len(file_models))
        file_model = file_models[0]
        file_model.archived = True
        db.session.add(file_model)

        # Assure that the file no longer comes back.
        file_models = FileService.get_workflow_files(workflow_id=workflow.id)
        self.assertEqual(0, len(file_models))

        # Add the file again with different data
        FileService.add_workflow_file(workflow_id=workflow.id,
                                      irb_doc_code=irb_code,
                                      task_spec_name=task.get_name(),
                                      name="anything.png",
                                      content_type="text",
                                      binary_data=b'5678')

        file_models = FileService.get_workflow_files(workflow_id=workflow.id)
        self.assertEqual(1, len(file_models))

        file_data = FileService.get_workflow_data_files(
            workflow_id=workflow.id)

        self.assertEqual(1, len(file_data))
        self.assertEqual(2, file_data[0].version)
        self.assertEqual(b'5678', file_data[0].data)
Example #3
0
    def add_approval(study_id, workflow_id, approver_uid):
        """we might have multiple approvals for a workflow, so I would expect this
            method to get called multiple times for the same workflow.  This will
            only add a new approval if no approval already exists for the approver_uid,
            unless the workflow has changed, at which point, it will CANCEL any
            pending approvals and create a new approval for the latest version
            of the workflow."""

        # Find any existing approvals for this workflow.
        latest_approval_requests = db.session.query(ApprovalModel). \
            filter(ApprovalModel.workflow_id == workflow_id). \
            order_by(desc(ApprovalModel.version))

        latest_approver_request = latest_approval_requests.filter(
            ApprovalModel.approver_uid == approver_uid).first()

        # Construct as hash of the latest files to see if things have changed since
        # the last approval.
        workflow = db.session.query(WorkflowModel).filter(
            WorkflowModel.id == workflow_id).first()
        workflow_data_files = FileService.get_workflow_data_files(workflow_id)
        current_data_file_ids = list(data_file.id
                                     for data_file in workflow_data_files)

        if len(current_data_file_ids) == 0:
            raise ApiError(
                "invalid_workflow_approval",
                "You can't create an approval for a workflow that has"
                "no files to approve in it.")

        # If an existing approval request exists and no changes were made, do nothing.
        # If there is an existing approval request for a previous version of the workflow
        # then add a new request, and cancel any waiting/pending requests.
        if latest_approver_request:
            request_file_ids = list(
                file.file_data_id
                for file in latest_approver_request.approval_files)
            current_data_file_ids.sort()
            request_file_ids.sort()
            other_approver = latest_approval_requests.filter(
                ApprovalModel.approver_uid != approver_uid).first()
            if current_data_file_ids == request_file_ids:
                return  # This approval already exists or we're updating other approver.
            else:
                for approval_request in latest_approval_requests:
                    if (approval_request.version
                            == latest_approver_request.version
                            and approval_request.status !=
                            ApprovalStatus.CANCELED.value):
                        approval_request.status = ApprovalStatus.CANCELED.value
                        db.session.add(approval_request)
                version = latest_approver_request.version + 1
        else:
            version = 1

        model = ApprovalModel(study_id=study_id,
                              workflow_id=workflow_id,
                              approver_uid=approver_uid,
                              status=ApprovalStatus.PENDING.value,
                              message="",
                              date_created=datetime.now(),
                              version=version)
        approval_files = ApprovalService._create_approval_files(
            workflow_data_files, model)

        # Check approvals count
        approvals_count = ApprovalModel().query.filter_by(
            study_id=study_id, workflow_id=workflow_id,
            version=version).count()

        db.session.add(model)
        db.session.add_all(approval_files)
        db.session.commit()

        # Send first email
        if approvals_count == 0:
            ldap_service = LdapService()
            pi_user_info = ldap_service.user_info(
                model.study.primary_investigator_id)
            approver_info = ldap_service.user_info(approver_uid)
            # send rrp submission
            mail_result = send_ramp_up_submission_email(
                '*****@*****.**', [pi_user_info.email_address],
                f'{approver_info.display_name} - ({approver_info.uid})')
            if mail_result:
                app.logger.error(mail_result, exc_info=True)
            # send rrp approval request for first approver
            # enhance the second part in case it bombs
            approver_email = [
                approver_info.email_address
            ] if approver_info.email_address else app.config['FALLBACK_EMAILS']
            mail_result = send_ramp_up_approval_request_first_review_email(
                '*****@*****.**', approver_email,
                f'{pi_user_info.display_name} - ({pi_user_info.uid})')
            if mail_result:
                app.logger.error(mail_result, exc_info=True)