From 3d312a773ef5bbbdd2dfa7cf4b0a51ae3b2ff7f4 Mon Sep 17 00:00:00 2001 From: Alex Ioannidis Date: Tue, 6 Feb 2024 14:14:34 +0100 Subject: [PATCH] wip --- .../communities/records/systemfields/pidslug.py | 2 +- .../records/records/systemfields/communities/field.py | 10 ++++++++++ .../records/systemfields/communities/manager.py | 6 ++++++ tests/conftest.py | 3 +-- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/invenio_communities/communities/records/systemfields/pidslug.py b/invenio_communities/communities/records/systemfields/pidslug.py index fd895ca3b..83ef7af58 100644 --- a/invenio_communities/communities/records/systemfields/pidslug.py +++ b/invenio_communities/communities/records/systemfields/pidslug.py @@ -25,7 +25,7 @@ def parse_pid(self, value): return value try: return UUID(value) - except (TypeError, ValueError) as e: + except (TypeError, ValueError): return value def resolve(self, pid_value, registered_only=True): diff --git a/invenio_communities/records/records/systemfields/communities/field.py b/invenio_communities/records/records/systemfields/communities/field.py index 0d75207de..6714aaadd 100644 --- a/invenio_communities/records/records/systemfields/communities/field.py +++ b/invenio_communities/records/records/systemfields/communities/field.py @@ -57,3 +57,13 @@ def __get__(self, record, owner=None): if record is None: return self._context_cls(self, owner) return self.obj(record) + + def post_dump(self, record, data, dumper=None): + """Dump the communities field.""" + comms = getattr(record, self.attr_name) + res = comms.to_dict() + res["entries"] = [ + # TODO: Only keep fields we want to dump + comm.dumps() for comm in comms + ] + data[self.key] = res diff --git a/invenio_communities/records/records/systemfields/communities/manager.py b/invenio_communities/records/records/systemfields/communities/manager.py index 93c45dddd..3aa15dbc1 100644 --- a/invenio_communities/records/records/systemfields/communities/manager.py +++ b/invenio_communities/records/records/systemfields/communities/manager.py @@ -147,6 +147,12 @@ def __iter__(self): # Iterate (sort by identifier to ensure consistent results) return (self._communities_cache[c] for c in sorted(self._communities_ids)) + def __getitem__(self, community_id): + """Get a community by ID.""" + if community_id in self._communities_cache: + return self._communities_cache[community_id] + return self._lookup_community(self._to_id(community_id)) + @property def ids(self): """Get communities ids.""" diff --git a/tests/conftest.py b/tests/conftest.py index 4c3e16e5f..f1eea4899 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -466,9 +466,8 @@ def fake_communities( community_type_record, community_types, ): - """Fake Communities.""" - data = deepcopy(minimal_community) """Multiple community created and posted to test search functionality.""" + data = deepcopy(minimal_community) N = 4 for type_, ind in itertools.product(community_types, list(range(N))):