Ejemplo n.º 1
0
    def _resource_types(self, extra_filter):
        """Dump resource type vocabulary."""
        type_ = 'resourcetypes'
        all_resource_types = vocabulary_service.read_all(
            system_identity,
            fields=["id", "props", "title", "icon"],
            type=type_,
            # Sorry, we have over 100+ resource types entry at NU actually
            max_records=150
        )
        type_labels = {
            hit["id"]: self._get_label(hit)
            for hit in all_resource_types.to_dict()["hits"]["hits"]
        }
        subset_resource_types = vocabulary_service.read_all(
            system_identity,
            fields=["id", "props", "title", "icon"],
            type=type_,
            extra_filter=extra_filter,
            # Sorry, we have over 100+ resource types entry at NU actually
            max_records=150
        )

        return [
            {
                "icon": hit.get("icon", ""),
                "id": hit["id"],
                "subtype_name": self._get_type_subtype_label(hit, type_labels)[1],  # noqa
                "type_name": self._get_type_subtype_label(hit, type_labels)[0],
            } for hit in subset_resource_types.to_dict()["hits"]["hits"]
        ]
Ejemplo n.º 2
0
    def get_subjects(self, obj):
        """Get datacite subjects."""
        subjects = obj["metadata"].get("subjects")
        if not subjects:
            return missing

        subject_vocabulary = vocabulary_service.read_all(
            system_identity,
            ['id', 'props'],
            "subjects"
        )
        subjects_props = {h["id"]: h["props"] for h in subject_vocabulary.hits}

        def create_datacite_subject(subject, subjects_props):
            """Generate datacite subject dict."""
            subject_record = subjects_props[subject['id']]

            datacite_subject = {
                "subject": subject_record['datacite']
            }

            other_fields = ["subjectScheme", "schemeURI", "valueURI"]
            other_values = [
                subject_record.get(f) for f in other_fields
            ]
            datacite_subject.update({
                k: v for k, v in zip(other_fields, other_values) if v
            })

            return datacite_subject

        return [
            create_datacite_subject(s, subjects_props) for s in subjects
        ]
Ejemplo n.º 3
0
 def _dump_vocabulary_w_basic_fields(self, vocabulary_type):
     """Dump vocabulary with id and title field."""
     results = vocabulary_service.read_all(g.identity,
                                           fields=["id", "title"],
                                           type=vocabulary_type)
     return [{
         "text": self._get_label(hit),
         "value": hit["id"],
     } for hit in results.to_dict()["hits"]["hits"]]
Ejemplo n.º 4
0
def _dump_title_types_vocabulary():
    """Dump title type vocabulary."""
    results = vocabulary_service.read_all(
        system_identity, fields=["id", "title"], type='title_types')
    return [
        {
            "text": r["title"].get(str(current_i18n.locale)),
            "value": r["id"],
        } for r in results.to_dict()["hits"]["hits"]
    ]
Ejemplo n.º 5
0
def _dump_resource_type_vocabulary():
    """Dump resource type vocabulary."""
    results = vocabulary_service.read_all(system_identity,
                                          fields=["id", "props"],
                                          type='resourcetypes')
    return [{
        "icon": r["props"].get("type_icon", ""),
        "id": r["id"],
        "subtype_name": r["props"].get("subtype_name", ""),
        "type_name": r["props"]["type_name"],
    } for r in results.to_dict()["hits"]["hits"]]
Ejemplo n.º 6
0
def _map_type(vocabulary, fields, id_):
    """Maps an internal vocabulary type to DataCite type."""
    res = vocabulary_service.read_all(
        system_identity,
        ['id'] + fields,
        vocabulary
    )

    for h in res.hits:
        if h["id"] == id_:
            return h["props"]
Ejemplo n.º 7
0
def _dump_vocabulary_w_basic_fields(vocabulary_type):
    """Dump vocabulary with id and title field."""
    results = vocabulary_service.read_all(system_identity,
                                          fields=["id", "title"],
                                          type=vocabulary_type)
    return [{
        "text":
        gettext_from_dict(r["title"], current_i18n.locale,
                          BABEL_DEFAULT_LOCALE),
        "value":
        r["id"],
    } for r in results.to_dict()["hits"]["hits"]]
Ejemplo n.º 8
0
def get_vocabulary_props(vocabulary, fields, id_):
    """Returns props associated with a vocabulary, id_."""
    # This is ok given that read_all is cached per vocabulary+fields and
    # is reused overtime
    results = vocabulary_service.read_all(
        system_identity,
        ['id'] + fields,
        vocabulary
    )

    for h in results.hits:
        if h["id"] == id_:
            return h.get("props", {})

    raise VocabularyItemNotFoundError(
        f"The '{vocabulary}' vocabulary item '{id_}' was not found.")