From 6121b6882fd396bd0bd5bff6211a1729ad386846 Mon Sep 17 00:00:00 2001 From: Fatimah Zulfiqar Date: Thu, 26 Sep 2024 14:34:34 +0200 Subject: [PATCH 1/4] relation: modified lookup_data for nested fields --- invenio_records/systemfields/relations/results.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/invenio_records/systemfields/relations/results.py b/invenio_records/systemfields/relations/results.py index 7a26f9a4..36fc9903 100644 --- a/invenio_records/systemfields/relations/results.py +++ b/invenio_records/systemfields/relations/results.py @@ -8,6 +8,8 @@ """Relations system field.""" +from itertools import chain + from ...dictutils import dict_lookup, dict_set from .errors import InvalidCheckValue, InvalidRelationValue @@ -154,10 +156,15 @@ def _lookup_id(self, data): def _lookup_data(self): data = dict_lookup(self.record, self.key) if self.relation_field: - filtered_data = [ - e.get(self.relation_field) for e in data if self.relation_field in e - ] - return filtered_data + fields = self.relation_field.split(".") + for field in fields: + if isinstance(data, list): + data = [d.get(field) for d in data if d.get(field)] + else: + data = data.get(field) + if not data: + return [] + return data def validate(self): From 21a7ef154c1a8db0760e375fe9a9d56e37f22d56 Mon Sep 17 00:00:00 2001 From: Fatimah Zulfiqar Date: Thu, 26 Sep 2024 16:00:44 +0200 Subject: [PATCH 2/4] relations: added test for nested field dereferencing --- tests/test_relations_systemfield.py | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/test_relations_systemfield.py b/tests/test_relations_systemfield.py index 5669f333..e90656b1 100644 --- a/tests/test_relations_systemfield.py +++ b/tests/test_relations_systemfield.py @@ -1356,3 +1356,54 @@ class Record4(Record, SystemFieldsMixin): assert res == oe_lang with pytest.raises(StopIteration): # finished first iterator next(res_inner_iter) + + +def test_nested_field_dereferencing(testapp, db, languages): + """Test dereferencing with relation_field containing nested relation.""" + Language, languages = languages + en_lang = languages["en"] + fr_lang = languages["fr"] + + class Record1(Record, SystemFieldsMixin): + # Example for testing a nested field dereferencing + relations = RelationsField( + nested_array_of_objects=PKNestedListRelation( + key="metadata", + keys=["iso", "information", "native_speakers"], + record_cls=Language, + relation_field="desc.languages", + ), + ) + + # Define the record with nested fields + record_data = { + "metadata": [ + { + "desc": { + "id": "1", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit", + "languages": [{"id": str(en_lang.id)}, {"id": str(fr_lang.id)}], + } + }, + ] + } + + # Create the record instance + record = Record1(record_data) + + # Test that dereferencing works for the languages + record.relations.nested_array_of_objects.dereference() + + # Check that the languages have been dereferenced correctly + assert record["metadata"][0]["desc"]["languages"][0] == { + "id": str(en_lang.id), + "iso": en_lang["iso"], + "information": en_lang["information"], + "@v": str(en_lang.id) + "::" + str(en_lang.revision_id), + } + assert record["metadata"][0]["desc"]["languages"][1] == { + "id": str(fr_lang.id), + "iso": fr_lang["iso"], + "information": fr_lang["information"], + "@v": str(fr_lang.id) + "::" + str(fr_lang.revision_id), + } From 4deef8dcf8e1e9bf039f86a05c28825ef9b56d81 Mon Sep 17 00:00:00 2001 From: Fatimah Zulfiqar Date: Wed, 2 Oct 2024 09:35:49 +0200 Subject: [PATCH 3/4] ci: updated test.yaml file --- .github/workflows/tests.yml | 68 +++++------------------------------ invenio_records/validators.py | 2 +- 2 files changed, 10 insertions(+), 60 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f494dfac..d3ea504c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,9 +11,11 @@ name: CI on: push: - branches: master + branches: + - master pull_request: - branches: master + branches: + - master schedule: # * is a special character in YAML so you have to quote this string - cron: "0 3 * * 6" @@ -25,60 +27,8 @@ on: default: "Manual trigger" jobs: - Tests: - runs-on: ubuntu-20.04 - strategy: - matrix: - python-version: [3.8, 3.9] - requirements-level: [pypi] - db-service: [postgresql14, mysql8] - include: - - db-service: postgresql14 - DB: postgresql - POSTGRESQL_VERSION: POSTGRESQL_14_LATEST - SQLALCHEMY_DATABASE_URI: "postgresql+psycopg2://invenio:invenio@localhost:5432/invenio" - EXTRAS: "tests,postgresql" - - - db-service: mysql8 - DB: mysql - MYSQL_VERSION: MYSQL_8_LATEST - SQLALCHEMY_DATABASE_URI: "mysql+pymysql://invenio:invenio@localhost:3306/invenio" - EXTRAS: "tests,mysql" - - env: - SQLALCHEMY_DATABASE_URI: ${{matrix.SQLALCHEMY_DATABASE_URI}} - POSTGRESQL_VERSION: ${{matrix.POSTGRESQL_VERSION}} - MYSQL_VERSION: ${{matrix.MYSQL_VERSION}} - DB: ${{ matrix.DB }} - - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.python-version }} - - - name: Generate dependencies - run: | - pip install wheel requirements-builder - requirements-builder -e ${{ matrix.EXTRAS }} --level=${{ matrix.requirements-level }} setup.py > .${{ matrix.requirements-level }}-${{ matrix.python-version }}-requirements.txt - - - name: Cache pip - uses: actions/cache@v2 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ hashFiles('.${{ matrix.requirements-level }}-${{ matrix.python-version }}-requirements.txt') }} - - - name: Install dependencies - run: | - pip install -r .${{matrix.requirements-level}}-${{ matrix.python-version }}-requirements.txt - pip install .[${{ matrix.EXTRAS }}] - pip freeze - docker --version - docker-compose --version - - - name: Run tests - run: | - ./run-tests.sh + Python: + uses: inveniosoftware/workflows/.github/workflows/tests-python.yml@master + with: + extras: "tests,postgresql,mysql" + search-service: '[""]' diff --git a/invenio_records/validators.py b/invenio_records/validators.py index 5cbc4e42..d38bd296 100644 --- a/invenio_records/validators.py +++ b/invenio_records/validators.py @@ -11,7 +11,7 @@ from jsonschema.validators import Draft4Validator, extend, validator_for -PartialDraft4Validator = extend(Draft4Validator, {"required": None}) +PartialDraft4Validator = extend(Draft4Validator, {"required": lambda *args: None}) """Partial JSON Schema (draft 4) validator. Special validator that contains the same validation rules of Draft4Validator, From 29e1d9e96394a1d4f635b85e13255a97715aa3c6 Mon Sep 17 00:00:00 2001 From: Pablo Tamarit Date: Tue, 8 Oct 2024 14:23:58 +0200 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=A6=20release:=20v2.4.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/tests.yml | 2 +- CHANGES.rst | 5 +++++ invenio_records/__init__.py | 2 +- invenio_records/systemfields/relations/results.py | 2 +- invenio_records/validators.py | 2 +- tests/test_relations_systemfield.py | 2 +- 6 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d3ea504c..bbcb66ff 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2020 CERN. +# Copyright (C) 2020-2024 CERN. # Copyright (C) 2022 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or modify it diff --git a/CHANGES.rst b/CHANGES.rst index 3847df6e..09240b53 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,6 +8,11 @@ Changes ======= +Version v2.4.0 (released 2024-10-08) + +- relation: modified lookup_data for nested fields +- Adds translations + Version 2.3.0 (released 2024-02-19) - tests: add tests for filter_dict_keys diff --git a/invenio_records/__init__.py b/invenio_records/__init__.py index c78818f3..e031a19f 100644 --- a/invenio_records/__init__.py +++ b/invenio_records/__init__.py @@ -331,7 +331,7 @@ from .api import Record from .ext import InvenioRecords -__version__ = "2.3.0" +__version__ = "2.4.0" __all__ = ( "InvenioRecords", diff --git a/invenio_records/systemfields/relations/results.py b/invenio_records/systemfields/relations/results.py index 36fc9903..9d21ff14 100644 --- a/invenio_records/systemfields/relations/results.py +++ b/invenio_records/systemfields/relations/results.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2020-2021 CERN. +# Copyright (C) 2020-2024 CERN. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. diff --git a/invenio_records/validators.py b/invenio_records/validators.py index d38bd296..ef802dec 100644 --- a/invenio_records/validators.py +++ b/invenio_records/validators.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2015-2018 CERN. +# Copyright (C) 2015-2024 CERN. # Copyright (C) 2021 TU Wien. # # Invenio is free software; you can redistribute it and/or modify it diff --git a/tests/test_relations_systemfield.py b/tests/test_relations_systemfield.py index e90656b1..03800ee4 100644 --- a/tests/test_relations_systemfield.py +++ b/tests/test_relations_systemfield.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2020 CERN. +# Copyright (C) 2020-2024 CERN. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details.