def test_workflow_creation(self):
        from invenio_deposit.loader import \
            deposition_metadata
        from invenio.modules.workflows.models import Workflow
        from invenio.webdeposit_workflow import DepositionWorkflow
        from invenio.webdeposit_utils import get_latest_or_new_workflow, \
            get_workflow, delete_workflow, InvenioWebDepositNoDepositionType

        user_id = self.login_user()

        number_of_dep_types = len(deposition_metadata)
        # Test for every deposition type
        for deposition_type in deposition_metadata.keys():
            # New workflow is created
            workflow = get_latest_or_new_workflow(deposition_type,
                                                  user_id=user_id)
            self.assertTrue(workflow is not None)
            # The just created workflow is retrieved as latest
            workflow2 = get_latest_or_new_workflow(deposition_type,
                                                   user_id=user_id)
            self.assertTrue(workflow2 is not None)

            self.assertEqual(str(workflow2.uuid), str(workflow.uuid))

            # and also retrieved with its uuid
            workflow = get_workflow(workflow.uuid, deposition_type)
            self.assertTrue(workflow is not None)

        # Test get_workflow function with random arguments
        deposition_type = deposition_metadata.keys()[-1]
        workflow = get_workflow('some_uuid_that_doesnt_exist', deposition_type)
        self.assertTrue(workflow is None)

        # Create workflow without using webdeposit_utils
        workflow = DepositionWorkflow(deposition_type=deposition_type,
                                      user_id=1)

        self.assertRaises(InvenioWebDepositNoDepositionType,
                          get_workflow, workflow.get_uuid(),
                          'deposition_type_that_doesnt_exist')

        # Test that the retrieved workflow is the same and not None
        workflow2 = get_workflow(workflow.get_uuid(), deposition_type)
        self.assertTrue(workflow2 is not None)
        self.assertEqual(workflow2.get_uuid(), workflow.get_uuid())

        # Check the number of created workflows
        count_workflows = Workflow.get(
            Workflow.module_name == "webdeposit"
        ).count()
        self.assertEqual(count_workflows, number_of_dep_types + 1)

        uuid = workflow.get_uuid()
        delete_workflow(1, uuid)

        workflow = get_workflow(uuid, deposition_type)
        self.assertTrue(workflow is None)
Beispiel #2
0
    def test_workflow_creation(self):
        from invenio_deposit.loader import \
            deposition_metadata
        from invenio.modules.workflows.models import Workflow
        from invenio.webdeposit_workflow import DepositionWorkflow
        from invenio.webdeposit_utils import get_latest_or_new_workflow, \
            get_workflow, delete_workflow, InvenioWebDepositNoDepositionType

        user_id = self.login_user()

        number_of_dep_types = len(deposition_metadata)
        # Test for every deposition type
        for deposition_type in deposition_metadata.keys():
            # New workflow is created
            workflow = get_latest_or_new_workflow(deposition_type,
                                                  user_id=user_id)
            self.assertTrue(workflow is not None)
            # The just created workflow is retrieved as latest
            workflow2 = get_latest_or_new_workflow(deposition_type,
                                                   user_id=user_id)
            self.assertTrue(workflow2 is not None)

            self.assertEqual(str(workflow2.uuid), str(workflow.uuid))

            # and also retrieved with its uuid
            workflow = get_workflow(workflow.uuid, deposition_type)
            self.assertTrue(workflow is not None)

        # Test get_workflow function with random arguments
        deposition_type = deposition_metadata.keys()[-1]
        workflow = get_workflow('some_uuid_that_doesnt_exist', deposition_type)
        self.assertTrue(workflow is None)

        # Create workflow without using webdeposit_utils
        workflow = DepositionWorkflow(deposition_type=deposition_type,
                                      user_id=1)

        self.assertRaises(InvenioWebDepositNoDepositionType, get_workflow,
                          workflow.get_uuid(),
                          'deposition_type_that_doesnt_exist')

        # Test that the retrieved workflow is the same and not None
        workflow2 = get_workflow(workflow.get_uuid(), deposition_type)
        self.assertTrue(workflow2 is not None)
        self.assertEqual(workflow2.get_uuid(), workflow.get_uuid())

        # Check the number of created workflows
        count_workflows = Workflow.get(
            Workflow.module_name == "webdeposit").count()
        self.assertEqual(count_workflows, number_of_dep_types + 1)

        uuid = workflow.get_uuid()
        delete_workflow(1, uuid)

        workflow = get_workflow(uuid, deposition_type)
        self.assertTrue(workflow is None)
    def test_workflow_creation(self):
        from invenio.webdeposit_load_deposition_types import \
            deposition_metadata
        from invenio.bibworkflow_model import Workflow
        from invenio.webdeposit_workflow import DepositionWorkflow
        from invenio.webdeposit_utils import get_latest_or_new_workflow, \
            get_workflow, delete_workflow
        from invenio.sqlalchemyutils import db
        from invenio.webuser_flask import login_user

        login_user(1)

        number_of_dep_types = len(deposition_metadata)
        # Test for every deposition type
        for deposition_type in deposition_metadata.keys():
            # New workflow is created
            workflow = get_latest_or_new_workflow(deposition_type, user_id=1)
            assert workflow is not None

            # The just created workflow is retrieved as latest
            workflow2 = get_latest_or_new_workflow(deposition_type, user_id=1)
            assert workflow2 is not None
            assert str(workflow2.uuid) == str(workflow.uuid)

            # and also retrieved with its uuid
            workflow = get_workflow(deposition_type, workflow.uuid)
            assert workflow is not None

        # Test get_workflow function with random arguments
        workflow = get_workflow('deposition_type_that_doesnt_exist',
                                'some_uuid')
        assert workflow is None

        deposition_type = deposition_metadata.keys()[-1]
        workflow = get_workflow(deposition_type, 'some_uuid_that_doesnt_exist')
        assert workflow is None

        # Create workflow without using webdeposit_utils
        wf = deposition_metadata[deposition_type]["workflow"]
        workflow = DepositionWorkflow(deposition_type=deposition_type,
                                      workflow=wf,
                                      user_id=1)

        # Test that the retrieved workflow is the same and not None
        workflow2 = get_workflow(deposition_type, workflow.get_uuid())
        assert workflow2 is not None
        assert workflow2.get_uuid() == workflow.get_uuid()

        # Check the number of created workflows
        workflows = db.session.query(Workflow).all()
        assert len(workflows) == number_of_dep_types + 1

        uuid = workflow.get_uuid()
        delete_workflow(1, uuid)
        workflow = get_workflow(deposition_type, uuid)
        assert workflow is None
    def test_deposit_files(self):
        from flask import current_app, url_for
        from invenio_deposit.loader import \
            deposition_metadata
        from invenio.modules.workflows.models import Workflow
        from invenio.webdeposit_utils import create_workflow, deposit_files, \
            get_latest_or_new_workflow

        user_id = self.login_user()

        # Test for every deposition type
        for deposition_type in deposition_metadata.keys():
            workflow = create_workflow(deposition_type, user_id)
            uuid = workflow.get_uuid()

            pdffile = make_pdf_fixture("test.pdf")

            with current_app.test_request_context(
                url_for(
                    'webdeposit.upload_file', deposition_type=deposition_type,
                    uuid=uuid
                ),
                method='POST',
                data={
                    'file': pdffile,
                    'name': 'test.pdf', }):

                deposit_files(user_id, deposition_type, uuid, preingest=True)

            workflow = get_latest_or_new_workflow(deposition_type, user_id)
            workflow.run()

            draft = Workflow.get(
                Workflow.id_user == user_id, Workflow.uuid == uuid
            ).one().extra_data['drafts'][0]

            assert len(draft['form_values']['files']) == 1
            filemeta = draft['form_values']['files'][0]
            assert filemeta['name'] == 'test.pdf'
            assert filemeta['content_type'] == 'application/pdf'
Beispiel #5
0
    def test_deposit_files(self):
        from flask import current_app, url_for
        from invenio_deposit.loader import \
            deposition_metadata
        from invenio.modules.workflows.models import Workflow
        from invenio.webdeposit_utils import create_workflow, deposit_files, \
            get_latest_or_new_workflow

        user_id = self.login_user()

        # Test for every deposition type
        for deposition_type in deposition_metadata.keys():
            workflow = create_workflow(deposition_type, user_id)
            uuid = workflow.get_uuid()

            pdffile = make_pdf_fixture("test.pdf")

            with current_app.test_request_context(url_for(
                    'webdeposit.upload_file',
                    deposition_type=deposition_type,
                    uuid=uuid),
                                                  method='POST',
                                                  data={
                                                      'file': pdffile,
                                                      'name': 'test.pdf',
                                                  }):

                deposit_files(user_id, deposition_type, uuid, preingest=True)

            workflow = get_latest_or_new_workflow(deposition_type, user_id)
            workflow.run()

            draft = Workflow.get(
                Workflow.id_user == user_id,
                Workflow.uuid == uuid).one().extra_data['drafts'][0]

            assert len(draft['form_values']['files']) == 1
            filemeta = draft['form_values']['files'][0]
            assert filemeta['name'] == 'test.pdf'
            assert filemeta['content_type'] == 'application/pdf'
Beispiel #6
0
def add(deposition_type, uuid):
    """
        Runs the workflows and shows the current form/output of the workflow
        Loads the associated to the uuid workflow.

        if the current step of the workflow renders a form, it loads it.
        if the workflow is finished or in case of error,
        it redirects to the deposition types page
        flashing also the associated message.

        Moreover, it handles a form's POST request for the fields and files,
        and validates the whole form after the submission.

        @param deposition_type: the type of the deposition to be run.
        @param uuid: the universal unique identifier for the workflow.
    """

    status = 0

    if deposition_type not in deposition_metadata:
        flash(_('Invalid deposition type `%s`.' % deposition_type), 'error')
        return redirect(url_for('.index_deposition_types'))

    elif uuid is None:
        # get the latest one. if there is no workflow created
        # lets create a new workflow with given deposition type
        workflow = get_latest_or_new_workflow(deposition_type)
        uuid = workflow.get_uuid()
        #flash(_('Deposition %s') % (uuid,), 'info')
        return redirect(
            url_for('.add', deposition_type=deposition_type, uuid=uuid))
    else:
        # get workflow with specific uuid
        workflow = get_workflow(deposition_type, uuid)
        if workflow is None:
            flash(_('Deposition with uuid `') + uuid + '` not found.', 'error')
            return redirect(url_for('.index_deposition_types'))

    cache.delete_many(
        str(current_user.get_id()) + ":current_deposition_type",
        str(current_user.get_id()) + ":current_uuid")
    cache.add(
        str(current_user.get_id()) + ":current_deposition_type",
        deposition_type)
    cache.add(str(current_user.get_id()) + ":current_uuid", uuid)

    current_app.config['breadcrumbs_map'][request.endpoint] = [
        (_('Home'), '')] + blueprint.breadcrumbs + \
        [(deposition_type, 'webdeposit.index',
         {'deposition_type': deposition_type}),
         (uuid, 'webdeposit.add',
         {'deposition_type': deposition_type, 'uuid': uuid})]

    if request.method == 'POST':
        # Save the files
        for uploaded_file in request.files.values():
            filename = secure_filename(uploaded_file.filename)
            if filename == "":
                continue

            CFG_USER_WEBDEPOSIT_FOLDER = create_user_file_system(
                current_user.get_id(), deposition_type, uuid)
            unique_filename = str(new_uuid()) + filename
            file_path = os.path.join(CFG_USER_WEBDEPOSIT_FOLDER,
                                     unique_filename)
            uploaded_file.save(file_path)
            size = os.path.getsize(file_path)
            file_metadata = dict(name=filename, file=file_path, size=size)
            draft_field_list_add(current_user.get_id(), uuid, "files",
                                 file_metadata)

        # Save form values
        for (field_name, value) in request.form.items():
            if "submit" in field_name.lower():
                continue
            draft_field_set(current_user.get_id(), uuid, field_name, value)

        form = get_form(current_user.get_id(), uuid)
        # Validate form
        if not form.validate():
            # render the form with error messages
            # the `workflow.get_output` function returns also the template
            return render_template(**workflow.get_output(form_validation=True))

        #Set the latest form status to finished
        set_form_status(current_user.get_id(), uuid,
                        CFG_DRAFT_STATUS['finished'])

    workflow.run()
    status = workflow.get_status()
    if status != CFG_WORKFLOW_STATUS.FINISHED and \
            status != CFG_WORKFLOW_STATUS.ERROR:
        # render current step of the workflow
        # the `workflow.get_output` function returns also the template
        return render_template(**workflow.get_output())
    elif status == CFG_WORKFLOW_STATUS.FINISHED:
        flash(
            deposition_type + _(' deposition has been successfully finished.'),
            'success')
        return redirect(url_for('.index_deposition_types'))
    elif status == CFG_WORKFLOW_STATUS.ERROR:
        flash(deposition_type + _(' deposition %s has returned error.'),
              'error')
        current_app.logger.error('Deposition: %s has returned error. %d' %
                                 uuid)
        return redirect(url_for('.index_deposition_types'))