Skip to content

Commit

Permalink
custom_fields: added subject field
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatimah committed Feb 29, 2024
1 parent 20f414b commit 1c6ed84
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
18 changes: 18 additions & 0 deletions invenio_rdm_records/contrib/subject/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-

Check failure on line 1 in invenio_rdm_records/contrib/subject/__init__.py

View workflow job for this annotation

GitHub Actions / Tests (3.9, pypi, postgresql14, opensearch2, 16.x)

isort-check """Subjects module.""" -from .custom_fields import ( - SUBJECT_FIELDS_UI, - SUBJECT_FIELDS -) +from .custom_fields import SUBJECT_FIELDS, SUBJECT_FIELDS_UI __all__ = ( SUBJECT_FIELDS_UI,

Check failure on line 1 in invenio_rdm_records/contrib/subject/__init__.py

View workflow job for this annotation

GitHub Actions / Tests (3.9, pypi, postgresql14, opensearch2, 16.x)

Black format check --- /home/runner/work/invenio-rdm-records/invenio-rdm-records/invenio_rdm_records/contrib/subject/__init__.py 2024-02-29 16:13:37.435122+00:00 +++ /home/runner/work/invenio-rdm-records/invenio-rdm-records/invenio_rdm_records/contrib/subject/__init__.py 2024-02-29 16:28:56.794568+00:00 @@ -5,14 +5,8 @@ # 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. """Subjects module.""" -from .custom_fields import ( - SUBJECT_FIELDS_UI, - SUBJECT_FIELDS -) +from .custom_fields import SUBJECT_FIELDS_UI, SUBJECT_FIELDS -__all__ = ( - SUBJECT_FIELDS_UI, - SUBJECT_FIELDS -) +__all__ = (SUBJECT_FIELDS_UI, SUBJECT_FIELDS)
#
# 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.

"""Subjects module."""

from .custom_fields import (
SUBJECT_FIELDS_UI,
SUBJECT_FIELDS
)

__all__ = (
SUBJECT_FIELDS_UI,
SUBJECT_FIELDS
)
79 changes: 79 additions & 0 deletions invenio_rdm_records/contrib/subject/custom_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# -*- coding: utf-8 -*-

Check failure on line 1 in invenio_rdm_records/contrib/subject/custom_fields.py

View workflow job for this annotation

GitHub Actions / Tests (3.9, pypi, postgresql14, opensearch2, 16.x)

Black format check --- /home/runner/work/invenio-rdm-records/invenio-rdm-records/invenio_rdm_records/contrib/subject/custom_fields.py 2024-02-29 16:13:37.435122+00:00 +++ /home/runner/work/invenio-rdm-records/invenio-rdm-records/invenio_rdm_records/contrib/subject/custom_fields.py 2024-02-29 16:28:57.101067+00:00 @@ -74,6 +74,6 @@ SubjectCF( name="subjects", multiple=True, dump_options=False, ) -} \ No newline at end of file +}
#
# 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 invenio_vocabularies.contrib.subjects.api import Subject
from invenio_vocabularies.contrib.subjects.schema import SubjectRelationSchema
from invenio_vocabularies.services.custom_fields 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",
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,
isCustomVocabulary=True,
),
)
],
}
]


SUBJECT_FIELDS = {
SubjectCF(
name="subjects",
multiple=True,
dump_options=False,
)
}

0 comments on commit 1c6ed84

Please sign in to comment.