def _serialize(self, nested_obj, attr, obj):
     result = super(NestedWithoutEmptyObjects,
                    self)._serialize(nested_obj, attr, obj)
     clean = strip_empty_values(result)
     if clean is None:
         return self.default if self.default is not missing else None
     return clean
 def _serialize(self, nested_obj, attr, obj):
     result = super(NestedWithoutEmptyObjects,
                    self)._serialize(nested_obj, attr, obj)
     clean = strip_empty_values(result)
     if clean is None:
         return self.default if self.default is not missing else None
     return clean
Ejemplo n.º 3
0
    def create(cls, data, id_=None, **kwargs):
        """Override the default ``create``.

        To handle also the docmuments and figures retrieval.

        Note:

            Might create an extra revision in the record if it had to download
            any documents or figures.

        Keyword Args:

            id_(uuid): an optional uuid to assign to the created record object.
            files_src_records(List[InspireRecord]): if passed, it will try to
                get the files for the documents and figures from the first
                record in the list that has it in it's files iterator before
                downloading them, for example to merge existing
                records.
            skip_files(bool): if ``True`` it will skip the files retrieval
                described above. Note also that, if not passed, it will fall
                back to the value of the ``RECORDS_SKIP_FILES`` configuration
                variable.

        Examples:
            >>> record = {
            ...     '$schema': 'hep.json',
            ... }
            >>> record = InspireRecord.create(record)
            >>> record.commit()
        """
        files_src_records = kwargs.pop('files_src_records', [])
        skip_files = kwargs.pop(
            'skip_files', current_app.config.get('RECORDS_SKIP_FILES'))

        id_ = id_ or uuid.uuid4()
        data = strip_empty_values(data)

        with db.session.begin_nested():
            cls.mint(id_, data)
            record = super(InspireRecord, cls).create(data, id_=id_, **kwargs)

        if not skip_files:
            record.download_documents_and_figures(
                src_records=files_src_records,
            )

        return record
Ejemplo n.º 4
0
    def create(cls, data, id_=None, **kwargs):
        """Override the default ``create``.

        To handle also the docmuments and figures retrieval.

        Note:

            Might create an extra revision in the record if it had to download
            any documents or figures.

        Keyword Args:

            id_(uuid): an optional uuid to assign to the created record object.
            files_src_records(List[InspireRecord]): if passed, it will try to
                get the files for the documents and figures from the first
                record in the list that has it in it's files iterator before
                downloading them, for example to merge existing
                records.
            skip_files(bool): if ``True`` it will skip the files retrieval
                described above. Note also that, if not passed, it will fall
                back to the value of the ``RECORDS_SKIP_FILES`` configuration
                variable.

        Examples:
            >>> record = {
            ...     '$schema': 'hep.json',
            ... }
            >>> record = InspireRecord.create(record)
            >>> record.commit()
        """
        files_src_records = kwargs.pop('files_src_records', [])
        skip_files = kwargs.pop(
            'skip_files', current_app.config.get('RECORDS_SKIP_FILES'))

        id_ = id_ or uuid.uuid4()
        data = strip_empty_values(data)

        with db.session.begin_nested():
            cls.mint(id_, data)
            record = super(InspireRecord, cls).create(data, id_=id_, **kwargs)

        if not skip_files:
            record.download_documents_and_figures(
                src_records=files_src_records,
            )

        return record
Ejemplo n.º 5
0
    def parse(self):
        for report_number in self.report_numbers:
            self.builder.add_report_number(report_number)

        self.builder.add_uid(self.isbn)
        self.builder.add_uid(self.arxiv_eprint)

        for doi in self.dois:
            self.builder.add_uid(doi)

        self.builder.set_journal_issue(self.journal_issue)
        self.builder.set_journal_volume(self.journal_volume)
        self.builder.set_journal_title(self.journal_title)
        self.builder.set_page_artid(page_start=self.page_start,
                                    page_end=self.page_end)
        self.builder.set_year(self.year)
        return strip_empty_values(self.builder.obj)
Ejemplo n.º 6
0
    def create(cls, *args, **kwargs):
        """Override the default ``create``.

        To handle also the docmuments and figures retrieval.

        Note:

            Might create an extra revision in the record if it had to download
            any documents or figures.

        Keyword Args:

            files_src_records(List[InspireRecord]): if passed, it will try to
                get the files for the documents and figures from the first
                record in the list that has it in it's files iterator before
                downloading them, for example to merge existing
                records.
            skip_files(bool): if ``True`` it will skip the files retrieval
                described above. Note also that, if not passed, it will fall
                back to the value of the ``RECORDS_SKIP_FILES`` configuration
                variable.

        """
        config_skip_files = current_app.config.get('RECORDS_SKIP_FILES')

        files_src_records = kwargs.pop('files_src_records', ())
        skip_files = kwargs.pop('skip_files', config_skip_files)

        data = strip_empty_values(*args)
        new_record = super(InspireRecord, cls).create(data=data, **kwargs)

        if not skip_files:
            new_record.download_documents_and_figures(
                src_records=files_src_records,
            )
            new_record.commit()

        return new_record
Ejemplo n.º 7
0
def test_strip_empty_values():
    obj = {
        '_foo': (),
        'foo': (1, 2, 3),
        '_bar': [],
        'bar': [1, 2, 3],
        '_baz': set(),
        'baz': set([1, 2, 3]),
        'qux': True,
        'quux': False,
        'plugh': 0,
    }

    expected = {
        'foo': (1, 2, 3),
        'bar': [1, 2, 3],
        'baz': set([1, 2, 3]),
        'qux': True,
        'quux': False,
        'plugh': 0,
    }
    result = strip_empty_values(obj)

    assert expected == result
Ejemplo n.º 8
0
 def strip_empty(self, data):
     return strip_empty_values(data)
Ejemplo n.º 9
0
def delete_empty_fields(obj, eng):
    obj.data = strip_empty_values(obj.data)
Ejemplo n.º 10
0
 def process_post_dump_in_order(self, data, original_data):
     for dump_func in self.post_dumps:
         data = dump_func(data, original_data)
     return strip_empty_values(data)
Ejemplo n.º 11
0
 def strip_empty(self, data):
     return strip_empty_values(data)
Ejemplo n.º 12
0
def test_strip_empty_values_returns_none_on_none():
    assert strip_empty_values(None) is None
Ejemplo n.º 13
0
def formdata_to_model(obj, formdata):
    """Manipulate form data to match authors data model."""
    form_fields = copy.deepcopy(formdata)

    filter_empty_elements(
        form_fields,
        ['institution_history', 'advisors',
         'websites', 'experiments']
    )
    data = updateform.do(form_fields)

    # ===========
    # Collections
    # ===========
    data['_collections'] = ['Authors']

    # ======
    # Schema
    # ======

    # FIXME it's not clear whether $schema is ever present at this stage
    if '$schema' not in data and '$schema' in obj.data:
        data['$schema'] = obj.data.get('$schema')
    if '$schema' in data:
        ensure_valid_schema(data)

    author_name = ''

    if 'family_name' in form_fields and form_fields['family_name']:
        author_name = form_fields['family_name'].strip() + ', '
    if 'given_names' in form_fields and form_fields['given_names']:
        author_name += form_fields['given_names']

    if author_name:
        data.get('name', {})['value'] = author_name

    # Add comments to extra data
    if 'extra_comments' in form_fields and form_fields['extra_comments']:
        data.setdefault('_private_notes', []).append({
            'source': 'submitter',
            'value': form_fields['extra_comments']
        })

    data['stub'] = False

    # ==========
    # Submitter Info
    # ==========
    try:
        user_email = User.query.get(obj.id_user).email
    except AttributeError:
        user_email = ''
    try:
        orcid = UserIdentity.query.filter_by(
            id_user=obj.id_user,
            method='orcid'
        ).one().id
    except NoResultFound:
        orcid = ''
    data['acquisition_source'] = dict(
        email=user_email,
        datetime=datetime.datetime.utcnow().isoformat(),
        method="submitter",
        orcid=orcid,
        submission_number=str(obj.id),
        internal_uid=int(obj.id_user),
    )

    data = strip_empty_values(data)

    return data
Ejemplo n.º 14
0
def formdata_to_model(obj, formdata):
    """Manipulate form data to match authors data model."""
    form_fields = copy.deepcopy(formdata)

    filter_empty_elements(
        form_fields,
        ['institution_history', 'advisors', 'websites', 'experiments'])
    data = updateform.do(form_fields)

    # ===========
    # Collections
    # ===========
    data['_collections'] = ['Authors']

    # ======
    # Schema
    # ======
    if '$schema' not in data and '$schema' in obj.data:
        data['$schema'] = obj.data.get('$schema')

    if '$schema' in data and not data['$schema'].startswith('http'):
        data['$schema'] = url_for('invenio_jsonschemas.get_schema',
                                  schema_path="records/{0}".format(
                                      data['$schema']))

    author_name = ''

    if 'family_name' in form_fields and form_fields['family_name']:
        author_name = form_fields['family_name'].strip() + ', '
    if 'given_names' in form_fields and form_fields['given_names']:
        author_name += form_fields['given_names']

    if author_name:
        data.get('name', {})['value'] = author_name

    # Add comments to extra data
    if 'extra_comments' in form_fields and form_fields['extra_comments']:
        data.setdefault('_private_notes', []).append({
            'source':
            'submitter',
            'value':
            form_fields['extra_comments']
        })

    data['stub'] = False

    # ==========
    # Submitter Info
    # ==========
    try:
        user_email = User.query.get(obj.id_user).email
    except AttributeError:
        user_email = ''
    try:
        orcid = UserIdentity.query.filter_by(id_user=obj.id_user,
                                             method='orcid').one().id
    except NoResultFound:
        orcid = ''
    data['acquisition_source'] = dict(
        email=user_email,
        datetime=datetime.datetime.utcnow().isoformat(),
        method="submitter",
        orcid=orcid,
        submission_number=str(obj.id),
        internal_uid=int(obj.id_user),
    )

    data = strip_empty_values(data)

    validate(data, 'authors')

    return data