コード例 #1
0
    def test_form_functions(self):
        from invenio_deposit.loader import \
            deposition_metadata
        from invenio_deposit 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.modules.workflows.models import Workflow

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

        user_id = self.login_user()

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

        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(user_id, 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(user_id, deposition_workflow.get_uuid())
        assert form is not None

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

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

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

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

        set_form_status(user_id, uuid, CFG_DRAFT_STATUS['finished'])
        form_status = get_form_status(user_id, deposition_workflow.get_uuid())
        assert form_status == CFG_DRAFT_STATUS['finished']
コード例 #2
0
    def test_form_functions(self):
        from invenio_deposit.loader import \
            deposition_metadata
        from invenio_deposit 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.modules.workflows.models import Workflow

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

        user_id = self.login_user()

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

        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(user_id, 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(user_id, deposition_workflow.get_uuid())
        assert form is not None

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

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

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

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

        set_form_status(user_id, uuid, CFG_DRAFT_STATUS['finished'])
        form_status = get_form_status(user_id, deposition_workflow.get_uuid())
        assert form_status == CFG_DRAFT_STATUS['finished']
コード例 #3
0
    def get_output(self, form_validation=None):
        """ Returns a representation of the current state of the workflow
            (a dict with the variables to fill the jinja template)
        """
        user_id = self.user_id
        uuid = self.get_uuid()

        from invenio.webdeposit_utils import get_form, \
            draft_field_get_all
        form = get_form(user_id, uuid)

        deposition_type = self.obj['deposition_type']
        drafts = draft_field_get_all(user_id, deposition_type)

        if form_validation:
            form.validate()

        # Get the template from configuration for this form
        template = form.config.get_template() or 'webdeposit_add.html'

        return dict(template_name_or_list=template,
                    workflow=self,
                    deposition_type=deposition_type,
                    form=form,
                    drafts=drafts,
                    uuid=uuid)
コード例 #4
0
    def get_output(self, form=None, form_validation=False):
        """ Returns a representation of the current state of the workflow
            (a dict with the variables to fill the jinja template)
        """
        from invenio.webdeposit_utils import get_form, \
            draft_field_get_all

        user_id = self.user_id
        uuid = self.get_uuid()

        if form is None:
            form = get_form(user_id, uuid, validate_draft=not form_validation)

        drafts = draft_field_get_all(user_id, self.deposition_type)

        if form_validation:
            form.validate()

        # Get the template for this form
        template = form.get_template()

        return dict(template_name_or_list=template,
                    workflow=self,
                    deposition_type=self.deposition_type,
                    form=form,
                    drafts=drafts,
                    uuid=uuid)
コード例 #5
0
    def export(obj, eng):
        user_id = obj.data['user_id']
        uuid = eng.uuid
        steps_num = obj.data['steps_num']

        from invenio.webdeposit_utils import get_form
        json_reader = JsonReader()
        for step in range(steps_num):
            form = get_form(user_id, uuid, step)
            # Insert the fields' values in bibfield's rec_json dictionary
            if form is not None:  # some steps don't have any form ...
                json_reader = form.cook_json(json_reader)

        deposition_type = \
            db.session.query(Workflow.name).\
            filter(Workflow.user_id == user_id,
                   Workflow.uuid == uuid).\
            one()[0]

        # Get the collection from configuration
        deposition_conf = WebDepositConfiguration(
            deposition_type=deposition_type)
        # or if it's not there, name the collection after the deposition type
        json_reader['collection.primary'] = \
            deposition_conf.get_collection() or deposition_type

        if 'recid' in json_reader or 'record ID' in json_reader:
            obj.data['update_record'] = True
        else:
            obj.data['update_record'] = False
        marc = json_reader.legacy_export_as_marc()
        obj.data['marc'] = marc
コード例 #6
0
    def cook_json(self):
        user_id = self.obj['user_id']
        uuid = self.get_uuid()

        from invenio.webdeposit_utils import get_form

        json_reader = JsonReader()
        for step in range(self.steps_num):
            try:
                form = get_form(user_id, uuid, step)
                json_reader = form.cook_json(json_reader)
            except:
                # some steps don't have any form ...
                pass

        return json_reader
コード例 #7
0
    def test_record_creation(self):
        import os
        from wtforms import TextAreaField
        from datetime import datetime

        from invenio.legacy.search_engine import record_exists
        from invenio.cache import cache
        from invenio.config import CFG_PREFIX
        from invenio.modules.workflows.models import Workflow
        from invenio.modules.workflows.config import CFG_WORKFLOW_STATUS
        from invenio.modules.scheduler.models import SchTASK

        from invenio.webdeposit_utils import get_form, create_workflow, \
            set_form_status, CFG_DRAFT_STATUS
        from invenio_deposit.loader import \
            deposition_metadata
        from invenio.webdeposit_workflow_utils import \
            create_record_from_marc
        from invenio.modules.record.api import get_record

        user_id = self.login_user()
        for deposition_type in deposition_metadata.keys():

            deposition = create_workflow(deposition_type, user_id)
            assert deposition is not None

            # Check if deposition creates a record
            create_rec = create_record_from_marc()
            function_exists = False
            for workflow_function in deposition.workflow:
                if create_rec.func_code == workflow_function .func_code:
                    function_exists = True
            if not function_exists:
                # if a record is not created,
                # continue with the next deposition
                continue

            uuid = deposition.get_uuid()

            cache.delete_many("1:current_deposition_type", "1:current_uuid")
            cache.add("1:current_deposition_type", deposition_type)
            cache.add("1:current_uuid", uuid)

            # Run the workflow
            deposition.run()

            # Create form's json based on the field name
            form = get_form(user_id, uuid=uuid)
            webdeposit_json = {}

            # Fill the json with dummy data
            for field in form:
                if isinstance(field, TextAreaField):
                    # If the field is associated with a marc field
                    if field.has_recjson_key() or field.has_cook_function():
                        webdeposit_json[field.name] = "test " + field.name

            draft = dict(form_type=form.__class__.__name__,
                         form_values=webdeposit_json,
                         step=0,  # dummy step
                         status=CFG_DRAFT_STATUS['finished'],
                         timestamp=str(datetime.now()))

            # Add a draft for the first step
            Workflow.set_extra_data(user_id=user_id, uuid=uuid,
                                    key='drafts', value={0: draft})

            workflow_status = CFG_WORKFLOW_STATUS.RUNNING
            while workflow_status != CFG_WORKFLOW_STATUS.COMPLETED:
                # Continue workflow
                deposition.run()
                set_form_status(user_id, uuid, CFG_DRAFT_STATUS['finished'])
                workflow_status = deposition.get_status()

            # Workflow is finished. Test if record is created
            recid = deposition.get_data('recid')
            assert recid is not None
            # Test that record id exists
            assert record_exists(recid) == 1

            # Test that the task exists
            task_id = deposition.get_data('task_id')
            assert task_id is not None

            bibtask = SchTASK.query.filter(SchTASK.id == task_id).first()
            assert bibtask is not None

            # Run bibupload, bibindex, webcoll manually
            cmd = "%s/bin/bibupload %s" % (CFG_PREFIX, task_id)
            assert not os.system(cmd)
            rec = get_record(recid)
            marc = rec.legacy_export_as_marc()
            for field in form:
                if isinstance(field, TextAreaField):
                    # If the field is associated with a marc field
                    if field.has_recjson_key() or field.has_cook_function():
                        assert "test " + field.name in marc
コード例 #8
0
    def test_record_creation(self):
        import os
        from wtforms import TextAreaField
        from datetime import datetime

        from invenio.search_engine import record_exists
        from invenio.cache import cache
        from invenio.config import CFG_PREFIX
        from invenio.webuser_flask import login_user
        from invenio.bibworkflow_model import Workflow
        from invenio.bibworkflow_config import CFG_WORKFLOW_STATUS
        from invenio.bibsched_model import SchTASK

        from invenio.webdeposit_utils import get_form, create_workflow, \
            set_form_status, CFG_DRAFT_STATUS
        from invenio.webdeposit_load_deposition_types import \
            deposition_metadata
        from invenio.webdeposit_workflow_utils import \
            create_record_from_marc
        from invenio.bibfield import get_record

        login_user(1)
        for deposition_type in deposition_metadata.keys():

            deposition = create_workflow(deposition_type, 1)
            assert deposition is not None

            # Check if deposition creates a record
            create_rec = create_record_from_marc()
            function_exists = False
            for workflow_function in deposition.workflow:
                if create_rec.func_code == workflow_function.func_code:
                    function_exists = True
            if not function_exists:
                # if a record is not created,
                #continue with the next deposition
                continue

            uuid = deposition.get_uuid()

            cache.delete_many("1:current_deposition_type", "1:current_uuid")
            cache.add("1:current_deposition_type", deposition_type)
            cache.add("1:current_uuid", uuid)

            # Run the workflow
            deposition.run()

            # Create form's json based on the field name
            form = get_form(1, uuid=uuid)
            webdeposit_json = {}

            # Fill the json with dummy data
            for field in form:
                if isinstance(field, TextAreaField):
                    # If the field is associated with a marc field
                    if field.has_recjson_key() or field.has_cook_function():
                        webdeposit_json[field.name] = "test " + field.name

            draft = dict(
                form_type=form.__class__.__name__,
                form_values=webdeposit_json,
                step=0,  # dummy step
                status=CFG_DRAFT_STATUS['finished'],
                timestamp=str(datetime.now()))

            # Add a draft for the first step
            Workflow.set_extra_data(user_id=1,
                                    uuid=uuid,
                                    key='drafts',
                                    value={0: draft})

            workflow_status = CFG_WORKFLOW_STATUS.RUNNING
            while workflow_status != CFG_WORKFLOW_STATUS.COMPLETED:
                # Continue workflow
                deposition.run()
                set_form_status(1, uuid, CFG_DRAFT_STATUS['finished'])
                workflow_status = deposition.get_status()

            # Workflow is finished. Test if record is created
            recid = deposition.get_data('recid')
            assert recid is not None
            # Test that record id exists
            assert record_exists(recid) == 1

            # Test that the task exists
            task_id = deposition.get_data('task_id')
            assert task_id is not None

            bibtask = SchTASK.query.filter(SchTASK.id == task_id).first()
            assert bibtask is not None

            # Run bibupload, bibindex, webcoll manually
            cmd = "%s/bin/bibupload %s" % (CFG_PREFIX, task_id)
            assert not os.system(cmd)
            rec = get_record(recid)
            marc = rec.legacy_export_as_marc()
            for field in form:
                if isinstance(field, TextAreaField):
                    # If the field is associated with a marc field
                    if field.has_recjson_key() or field.has_cook_function():
                        assert "test " + field.name in marc
コード例 #9
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'))
コード例 #10
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_model import WebDepositDraft
        from invenio.webdeposit_workflow import DepositionWorkflow
        from invenio.webdeposit_utils import get_current_form, get_form, \
            get_form_status, CFG_DRAFT_STATUS
        from invenio.sqlalchemyutils import db
        from invenio.webdeposit_workflow_utils import render_form, \
            wait_for_submission
        from invenio.cache import cache

        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)

        wf = [render_form(forms.values()[0]), wait_for_submission()]
        deposition_workflow = DepositionWorkflow(
            deposition_type='TestWorkflow', workflow=wf, user_id=1)

        uuid = deposition_workflow.get_uuid()
        cache.delete_many("1:current_deposition_type", "1:current_uuid")
        cache.add("1:current_deposition_type", 'TestWorkflow')
        cache.add("1:current_uuid", uuid)

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

        # There is only one form in the db
        drafts = db.session.query(WebDepositDraft)
        assert len(drafts.all()) == 1

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

        # Test that the current form has the right type
        uuid, form = get_current_form(1,
                                      deposition_type='TestWorkflow',
                                      uuid=deposition_workflow.get_uuid())
        assert isinstance(form, forms.values()[0])
        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

        db.session.query(WebDepositDraft).\
            update({'status': CFG_DRAFT_STATUS['finished']})

        form_status = get_form_status(1, deposition_workflow.get_uuid())
        assert form_status == CFG_DRAFT_STATUS['finished']
コード例 #11
0
    def export(obj, eng):
        user_id = obj.data['user_id']
        uuid = eng.uuid
        steps_num = obj.data['steps_num']

        from invenio.webdeposit_utils import get_form
        from invenio.webdeposit_load_deposition_types import deposition_metadata
        json_reader = JsonReader()

        try:
            pop_obj = Workflow.get_extra_data(user_id=user_id,
                                              uuid=uuid,
                                              key='pop_obj')
        except KeyError:
            pop_obj = None

        form_data = {}
        if 'form_values' in obj.data or pop_obj is not None:

            # copy the form values to be able to
            # delete the fields in the workflow object during iteration
            form_data = pop_obj or obj.data['form_values']

        # Populate the form with data
        for step in range(steps_num):
            form = get_form(user_id, uuid, step)

            # Insert the fields' values in bibfield's rec_json dictionary
            if form is not None:  # some steps don't have any form ...
                # Populate preingested data
                for field in form:
                    if field.name in form_data:
                        field.data = form_data.pop(field.name)
                json_reader = form.cook_json(json_reader)

        deposition_type = \
            db.session.query(Workflow.name).\
            filter(Workflow.id_user == user_id,
                   Workflow.uuid == uuid).\
            one()[0]

        # Get the collection from configuration
        if 'collection' in deposition_metadata[deposition_type]:
            json_reader['collection.primary'] = \
                deposition_metadata[deposition_type]['collection']
        else:
            json_reader['collection.primary'] = deposition_type

        if 'recid' not in json_reader or 'record ID' not in json_reader:
            # Record is new, reserve record id
            recid = run_sql(
                "INSERT INTO bibrec (creation_date, modification_date) VALUES (NOW(), NOW())"
            )
            json_reader['recid'] = recid
            obj.data['recid'] = recid
        else:
            obj.data['recid'] = json_reader['recid']
            obj.data['title'] = json_reader['title.title']

        Workflow.set_extra_data(user_id=user_id,
                                uuid=uuid,
                                key='recid',
                                value=obj.data['recid'])

        obj.data['marc'] = json_reader.legacy_export_as_marc()