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 deposition_submit(deposition_type): uuid = request.values['uuid'] user_id = current_user.get_id() try: workflow = get_workflow(uuid, deposition_type) except InvenioWebDepositNoDepositionType: return jsonify errors = validate_preingested_data(user_id, uuid, deposition_type=None) if errors: return jsonify(errors) workflow_status = CFG_WORKFLOW_STATUS.RUNNING while workflow_status != CFG_WORKFLOW_STATUS.FINISHED: # Continue workflow workflow.run() set_form_status(1, uuid, CFG_WORKFLOW_STATUS.FINISHED) workflow_status = workflow.get_status() return jsonify({})
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
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
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'))