From 73a8f6c8e05d650933602bd80124f08006d3d381 Mon Sep 17 00:00:00 2001 From: Fatimah Date: Fri, 1 Mar 2024 10:34:05 +0100 Subject: [PATCH] custom_fields: added subject field --- .../services/custom_fields/__init__.py | 6 +- .../services/custom_fields/subject.py | 80 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 invenio_vocabularies/services/custom_fields/subject.py diff --git a/invenio_vocabularies/services/custom_fields/__init__.py b/invenio_vocabularies/services/custom_fields/__init__.py index 1ce96f3c..6044dbaa 100644 --- a/invenio_vocabularies/services/custom_fields/__init__.py +++ b/invenio_vocabularies/services/custom_fields/__init__.py @@ -7,6 +7,10 @@ """Custom Fields for InvenioRDM.""" +from .subject import SUBJECT_FIELDS, SUBJECT_FIELDS_UI from .vocabulary import VocabularyCF -__all__ = "VocabularyCF" +__all__ = [ + "VocabularyCF", + "SUBJECT_FIELDS_UI" "SUBJECT_FIELDS", +] diff --git a/invenio_vocabularies/services/custom_fields/subject.py b/invenio_vocabularies/services/custom_fields/subject.py new file mode 100644 index 00000000..d922b4a8 --- /dev/null +++ b/invenio_vocabularies/services/custom_fields/subject.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2024 CERN. +# +# Invenio-RDM-Records is free software; you can redistribute it and/or modify +# it under the terms of the MIT License; see LICENSE file for more details. + + +"""Custom fields.""" +from invenio_i18n import lazy_gettext as _ + +from ...contrib.subjects.api import Subject +from ...contrib.subjects.schema import SubjectRelationSchema +from .vocabulary import VocabularyCF + + +class SubjectCF(VocabularyCF): + """Custom field for subjects.""" + + field_keys = ["id", "subject"] + + def __init__(self, **kwargs): + """Constructor.""" + super().__init__( + vocabulary_id="subjects", + schema=SubjectRelationSchema, + ui_schema=SubjectRelationSchema, + **kwargs + ) + self.pid_field = Subject.pid + + @property + def mapping(self): + """Return the mapping.""" + _mapping = { + "type": "object", + "properties": { + "@v": {"type": "keyword"}, + "id": {"type": "keyword"}, + "subject": {"type": "keyword"}, + }, + } + + return _mapping + + +SUBJECT_FIELDS_UI = [ + { + "section": _("Subjects"), + "fields": [ + dict( + field="subjects", + ui_widget="SubjectAutocompleteDropdown", + isGenericVocabulary=False, + props=dict( + label="Keywords and subjects", + icon="tag", + description="The subjects related to the community", + placeholder="Search for a subject by name e.g. Psychology ...", + autocompleteFrom="api/subjects", + noQueryMessage="Search for subjects...", + autocompleteFromAcceptHeader="application/vnd.inveniordm.v1+json", + required=False, + multiple=True, + clearable=True, + allowAdditions=False, + ), + ) + ], + } +] + + +SUBJECT_FIELDS = { + SubjectCF( + name="subjects", + multiple=True, + dump_options=False, + ) +}