def get_latest_or_new_workflow(deposition_type, user_id=None): """ Creates new workflow or returns a new one """ user_id = user_id or current_user.get_id() wf = deposition_metadata[deposition_type]["workflow"] # get latest draft in order to get workflow's uuid latest_workflow = db.session.query(Workflow).\ filter( Workflow.user_id == user_id, Workflow.name == deposition_type, Workflow.module_name == 'webdeposit', Workflow.status != CFG_WORKFLOW_STATUS.FINISHED).\ order_by(db.desc(Workflow.modified)).\ first() if latest_workflow is None: # We didn't find other workflows # Let's create a new one return DepositionWorkflow(deposition_type=deposition_type, workflow=wf) # Create a new workflow # based on the latest draft's uuid uuid = latest_workflow.uuid return DepositionWorkflow(deposition_type=deposition_type, workflow=wf, uuid=uuid)
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 create_workflow(deposition_type, user_id=None): """ Creates a new workflow and returns it """ try: wf = deposition_metadata[deposition_type]["workflow"] except KeyError: # deposition type not found return None return DepositionWorkflow(deposition_type=deposition_type, workflow=wf, user_id=user_id)
def test_field_functions(self): from invenio.webdeposit_workflow import DepositionWorkflow from invenio.webdeposit_utils import draft_field_get, draft_field_set user_id = self.login_user() workflow = DepositionWorkflow(deposition_type='Article', user_id=user_id) workflow.run() # Insert a form uuid = workflow.get_uuid() # Test for a field that's not there value = draft_field_get(user_id, uuid, 'field_that_doesnt_exist') self.assertTrue(value is None) # Test for a field that hasn't been inserted in db yet value = draft_field_get(user_id, uuid, 'publisher') self.assertTrue(value is None) draft_field_set(user_id, uuid, 'publisher', 'Test Publishers Association') value = draft_field_get(user_id, uuid, 'publisher') self.assertTrue(value is 'Test Publishers Association')
def test_field_functions(self): from invenio.webdeposit_workflow import DepositionWorkflow from invenio.webdeposit_utils import draft_field_get, draft_field_set user_id = 1 workflow = DepositionWorkflow(deposition_type='TestWorkflow', user_id=user_id) workflow.run() # Insert a form uuid = workflow.get_uuid() # Test for a field that's not there value = draft_field_get(user_id, uuid, 'field_that_doesnt_exist') self.assertTrue(value is None) # Test for a field that hasn't been inserted in db yet value = draft_field_get(user_id, uuid, 'publisher') self.assertTrue(value is None) draft_field_set(user_id, uuid, 'publisher', 'Test Publishers Association') value = draft_field_get(user_id, uuid, 'publisher') self.assertTrue(value is 'Test Publishers Association')
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 get_workflow(deposition_type, uuid): """ Returns a workflow instance with uuid=uuid or None """ try: wf = deposition_metadata[deposition_type]["workflow"] except KeyError: # deposition type not found return None # Check if uuid exists first try: db.session.query(Workflow). \ filter_by(uuid=uuid).one() except NoResultFound: return None return DepositionWorkflow(uuid=uuid, deposition_type=deposition_type, workflow=wf)
def test_field_functions(self): from datetime import datetime from invenio.sqlalchemyutils import db from invenio.webdeposit_workflow import DepositionWorkflow from invenio.webdeposit_model import WebDepositDraft from invenio.webdeposit_workflow_utils import render_form from invenio.webdeposit_utils import draft_field_get from invenio.webdeposit_deposition_forms.article_form import ArticleForm from invenio.cache import cache wf = [render_form(ArticleForm)] user_id = 1 workflow = DepositionWorkflow(workflow=wf, deposition_type='TestWorkflow', user_id=user_id) cache.delete_many("1:current_deposition_type", "1:current_uuid") cache.add("1:current_deposition_type", 'TestWorkflow') cache.add("1:current_uuid", workflow.get_uuid()) workflow.run() # Insert a form uuid = workflow.get_uuid() # Test for a field that's not there value = draft_field_get(user_id, uuid, 'field_that_doesnt_exist') assert value is None # Test for a field that hasn't been inserted in db yet value = draft_field_get(user_id, uuid, 'publisher') assert value is None values = {'publisher': 'Test Publishers Association'} db.session.query(WebDepositDraft).\ filter(WebDepositDraft.uuid == uuid, WebDepositDraft.step == 0).\ update({"form_values": values, "timestamp": datetime.now()})
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']
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']
# Check if deposition type is valid. deposition_metadata[deposition_type] except KeyError, e: # deposition type not found raise InvenioWebDepositNoDepositionType(str(e)) # get latest draft in order to get workflow's uuid try: latest_workflow = Workflow.get_most_recent( Workflow.id_user == user_id, Workflow.name == deposition_type, Workflow.module_name == 'webdeposit', Workflow.status != CFG_WORKFLOW_STATUS.COMPLETED) except NoResultFound: # We didn't find other workflows # Let's create a new one return DepositionWorkflow(deposition_type=deposition_type, user_id=user_id) # Create a new workflow # based on the latest draft's uuid return DepositionWorkflow(deposition_type=deposition_type, uuid=latest_workflow.uuid, user_id=user_id) 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:
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']