예제 #1
0
    def clear_tables(self):
        from invenio.bibworkflow_model import Workflow, BibWorkflowObject
        from invenio.sqlalchemyutils import db

        dep_workflows = Workflow.get(
            Workflow.module_name == "webdeposit").all()
        for workflow in dep_workflows:
            BibWorkflowObject.query.filter(
                BibWorkflowObject.id_workflow == workflow.uuid).delete()
        Workflow.get(Workflow.module_name == "webdeposit").delete()
        db.session.commit()
예제 #2
0
    def tearDown(self):
        """ Clean up created objects """
        from invenio.bibworkflow_model import (BibWorkflowObject,
                                               Workflow,
                                               BibWorkflowObjectLogging)
        from invenio.bibworkflow_utils import get_redis_keys, set_up_redis

        workflows = Workflow.get(Workflow.module_name == "unit_tests").all()
        for workflow in workflows:
            BibWorkflowObject.query.filter(
                BibWorkflowObject.id_workflow == workflow.uuid
            ).delete()

            objects = BibWorkflowObjectLogging.query.filter(
                BibWorkflowObject.id_workflow == workflow.uuid
            ).all()
            for obj in objects:
                db.session.delete(obj)
            db.session.delete(workflow)
        # Deleting dumy object created in tests
        db.session.query(BibWorkflowObject).filter(
            BibWorkflowObject.id_workflow.in_([11, 123, 253])
        ).delete(synchronize_session='fetch')
        Workflow.query.filter(Workflow.module_name == "unit_tests").delete()
        db.session.commit()

        rs = set_up_redis()
        keys = get_redis_keys()
        for key in keys:
            keys2 = get_redis_keys(key)
            for key2 in keys2:
                rs.delete("holdingpen_sort:%s:%s" % (key, key2,))
            rs.delete("holdingpen_sort:%s" % (key,))
        rs.delete("holdingpen_sort")
예제 #3
0
def draft_field_get_all(user_id, deposition_type):
    """ Returns a list with values of the field_names specified
        containing all the latest drafts
        of deposition of type=deposition_type
    """

    ## Select drafts with max step workflow.
    workflows = Workflow.get(Workflow.status != CFG_WORKFLOW_STATUS.COMPLETED,
                             Workflow.id_user == user_id,
                             Workflow.name == deposition_type,
                             Workflow.module_name == 'webdeposit').all()

    drafts = []
    get_max_draft = draft_getter()

    class Draft(object):
        def __init__(self, dictionary, workflow):
            for k, v in dictionary.items():
                setattr(self, k, v)
            setattr(self, 'workflow', workflow)
            setattr(self, 'uuid', workflow.uuid)

    for workflow in workflows:
        max_draft = get_max_draft(workflow.extra_data)
        if max_draft is not None:
            drafts.append(Draft(max_draft, workflow))

    drafts = sorted(drafts, key=lambda d: d.timestamp, reverse=True)
    return drafts
예제 #4
0
    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, InvenioWebDepositNoDepositionType
        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)
            self.assertTrue(workflow is not None)
            # The just created workflow is retrieved as latest
            workflow2 = get_latest_or_new_workflow(deposition_type, user_id=1)
            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)
예제 #5
0
def get_draft(user_id, uuid, field_name=None):
    """ Returns draft values in a field_name => field_value dictionary
        or if field_name is defined, returns the associated value
    """

    draft = Workflow.get(user_id=user_id, uuid=uuid)

    form_values = draft['form_values']

    if field_name is None:
        return form_values
    else:
        try:
            return form_values[field_name]
        except KeyError:  # field_name doesn't exist
            return form_values  # return whole row
예제 #6
0
def get_workflow(uuid, deposition_type=None):
    """ Returns a workflow instance with uuid=uuid or None """

    # Check if uuid exists first and get the deposition_type if None
    try:
        workflow = Workflow.get(uuid=uuid).one()
        if deposition_type is None:
            deposition_type = workflow.name
    except NoResultFound:
        return None

    try:
        # Check if deposition type is valid.
        deposition_metadata[deposition_type]
    except KeyError, e:
        # deposition type not found
        raise InvenioWebDepositNoDepositionType(str(e))
def index(deposition_type):
    if deposition_type not in deposition_metadata:
        flash(_('Invalid deposition type `%s`.' % deposition_type), 'error')
        return redirect(url_for('.index_deposition_types'))
    current_app.config['breadcrumbs_map'][request.endpoint] = [
        (_('Home'), '')] + blueprint.breadcrumbs + [(deposition_type, None)]
    user_id = current_user.get_id()
    drafts = draft_field_get_all(user_id, deposition_type)

    from invenio.bibworkflow_model import Workflow
    past_depositions = \
        Workflow.get(Workflow.name == deposition_type,
                     Workflow.id_user == user_id,
                     Workflow.status == CFG_WORKFLOW_STATUS.COMPLETED).\
        all()

    return render_template('webdeposit_index.html', drafts=drafts,
                           deposition_type=deposition_type,
                           deposition_types=deposition_types,
                           past_depositions=past_depositions)
예제 #8
0
    def __init__(self, name=None, uuid=None, curr_obj=0,
                 workflow_object=None, id_user=0, module_name="Unknown",
                 **kwargs):
        super(BibWorkflowEngine, self).__init__()
        self.db_obj = None
        if isinstance(workflow_object, Workflow):
            self.db_obj = workflow_object
        else:
            # If uuid is defined we try to get the db object from DB.
            if uuid is not None:
                self.db_obj = \
                    Workflow.get(Workflow.uuid == uuid).first()
            else:
                uuid = new_uuid()

            if self.db_obj is None:
                self.db_obj = Workflow(name=name, id_user=id_user,
                                       current_object=curr_obj,
                                       module_name=module_name, uuid=uuid)
                self._create_db_obj()

        self.set_workflow_by_name(name)
        self.set_extra_data_params(**kwargs)
예제 #9
0
    def test_form_functions(self):
        from invenio.webdeposit_load_deposition_types import \
            deposition_metadata
        from invenio.webdeposit_load_forms import forms
        from invenio.webdeposit_workflow import DepositionWorkflow
        from invenio.webdeposit_utils import get_form, \
            get_form_status, set_form_status, CFG_DRAFT_STATUS
        from invenio.bibworkflow_model import Workflow

        for metadata in deposition_metadata.values():
            for wf_function in metadata['workflow']:
                if 'render_form' == wf_function.func_name:
                    break

        from invenio.webuser_flask import login_user
        login_user(1)

        deposition_workflow = DepositionWorkflow(deposition_type='Article',
                                                 user_id=1)

        uuid = deposition_workflow.get_uuid()

        # Run the workflow to insert a form in the db
        deposition_workflow.run()

        # There is only one form in the db
        workflows = Workflow.get(module_name='webdeposit')
        assert len(workflows.all()) == 1
        assert len(workflows[0].extra_data['drafts']) == 1

        # Test that guest user doesn't have access to the form
        form = get_form(0, uuid=uuid)
        assert form is None

        # Test that the current form has the right type
        form = get_form(1, uuid=deposition_workflow.get_uuid())
        assert isinstance(form, forms['ArticleForm'])
        assert str(uuid) == str(deposition_workflow.get_uuid())

        # Test that form is returned with get_form function
        form = get_form(1, deposition_workflow.get_uuid())
        assert form is not None

        form = get_form(1, deposition_workflow.get_uuid(), step=0)
        assert form is not None

        # Second step doesn't have a form
        form = get_form(1, deposition_workflow.get_uuid(), step=1)
        assert form is None

        form_status = get_form_status(1, deposition_workflow.get_uuid())
        assert form_status == CFG_DRAFT_STATUS['unfinished']

        form_status = get_form_status(1,
                                      deposition_workflow.get_uuid(),
                                      step=2)
        assert form_status is None

        set_form_status(1, uuid, CFG_DRAFT_STATUS['finished'])
        form_status = get_form_status(1, deposition_workflow.get_uuid())
        assert form_status == CFG_DRAFT_STATUS['finished']
예제 #10
0
def get_current_step(uuid):
    webdep_workflow = Workflow.get(Workflow.uuid == uuid).one()
    steps = webdep_workflow.task_counter

    return get_last_step(steps)
예제 #11
0
 def get_workflow_from_db(self):
     return Workflow.get(Workflow.uuid == self.get_uuid()).first()