예제 #1
0
def get_form_status(user_id, uuid, step=None):
    try:
        webdeposit_draft = \
            Workflow.get_extra_data(user_id=user_id,
                                    uuid=uuid,
                                    getter=draft_getter(step))
    except ValueError:
        # No drafts found
        raise NoResultFound
    except NoResultFound:
        return None

    return webdeposit_draft['status']
예제 #2
0
def draft_field_get(user_id, uuid, field_name, subfield_name=None):
    """ Returns the value of a field
        or, in case of error, None
    """

    values = \
        Workflow.get_extra_data(user_id=user_id, uuid=uuid,
                                getter=draft_getter())['form_values']
    try:
        if subfield_name is not None:
            return values[field_name][subfield_name]
        return values[field_name]
    except KeyError:
        return None
예제 #3
0
    def get_data(self, key):
        try:
            return Workflow.get_extra_data(user_id=self.user_id,
                                           uuid=self.uuid,
                                           key=key)
        except (NoResultFound, KeyError):
            pass

        bib_obj = BibWorkflowObject.query.\
            filter(BibWorkflowObject.id_workflow == self.uuid,
                   BibWorkflowObject.id_parent != None).\
            order_by(desc(BibWorkflowObject.modified)).first()
        data = bib_obj.get_data()
        if key in data:
            return data[key]

        return None
예제 #4
0
def get_preingested_form_data(user_id, uuid=None, key=None, cached_data=False):
    def get_preingested_data(key):
        def getter(json):
            if 'pop_obj' in json:
                if key is None:
                    return json['pop_obj']
                else:
                    return json['pop_obj'][key]
            else:
                return {}

        return getter

    if cached_data:
        return cache.get(str(user_id) + ':cached_form_data')
    try:
        return Workflow.get_extra_data(user_id,
                                       uuid=uuid,
                                       getter=get_preingested_data(key))
    except NoResultFound:
        return None
예제 #5
0
def get_form(user_id,
             uuid,
             step=None,
             formdata=None,
             load_draft=True,
             validate_draft=False):
    """
    Returns the current state of the workflow in a form or a previous
    state (step)

    @param user_id: the id of the user.
    @type user_id: int

    @param uuid: the unique identifier of the workflow that the form belongs to.
    @type uuid: str or UUID

    @param step: the step inside the workflow that the form was rendered. Used to
    get previous forms. If not defined, it returns the last one.
    @type: int

    @param formdata:
        Dictionary of formdata.
    @param validate_draft:
        If draft data exists, and no formdata is provided, the form will be
        validated if this parameter is set to true.
    """
    # Get draft data
    if load_draft:
        try:

            webdeposit_draft = \
                Workflow.get_extra_data(user_id=user_id,
                                        uuid=uuid,
                                        getter=draft_getter(step))
        except (ValueError, NoResultFound):
            # No drafts found
            return None

    # If a field is not present in formdata, Form.process() will assume it is
    # blank instead of using the draft_data value. Most of the time we are only
    # submitting a single field in JSON via AJAX requests. We therefore reset
    # non-submitted fields to the draft_data value.
    draft_data = webdeposit_draft['form_values'] if load_draft else {}
    if formdata:
        formdata = MultiDict(formdata)
    form = forms[webdeposit_draft['form_type']](formdata=formdata,
                                                **draft_data)
    if formdata:
        form.reset_field_data(exclude=formdata.keys())

    # Set field flags
    if load_draft and 'form_field_flags' in webdeposit_draft:
        form.set_flags(webdeposit_draft['form_field_flags'])

    # Process files
    if 'files' in draft_data:
        # FIXME: sql alchemy(0.8.0) returns the value from the
        #        column form_values with keys and values in unicode.
        #        This creates problem when the dict is rendered
        #        in the page to be used by javascript functions. There must
        #        be a more elegant way than decoding the dict from unicode.

        draft_data['files'] = decode_dict_from_unicode(draft_data['files'])
        for file_metadata in draft_data['files']:
            # Replace the path with the unique filename
            if isinstance(file_metadata, basestring):
                import json
                file_metadata = json.loads(file_metadata)
            filepath = file_metadata['file'].split('/')
            unique_filename = filepath[-1]
            file_metadata['unique_filename'] = unique_filename
        form.__setattr__('files', draft_data['files'])
    else:
        form.__setattr__('files', {})

    if validate_draft and draft_data and formdata is None:
        form.validate()

    return form
예제 #6
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()