From beb68ee65d6e25f379a0c67ce6e3f5c6bdba9683 Mon Sep 17 00:00:00 2001 From: Vlad0n20 Date: Thu, 9 Jan 2025 16:35:11 +0200 Subject: [PATCH] Draft --- api/base/filters.py | 35 +++++++++++++++++-- .../views/test_preprint_list_mixin.py | 3 +- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/api/base/filters.py b/api/base/filters.py index 4d9bc37a442..078fae7620b 100644 --- a/api/base/filters.py +++ b/api/base/filters.py @@ -338,6 +338,35 @@ def parse_query_params(self, query_params): }) elif self.should_parse_special_query_params(field_name): query = self.parse_special_query_params(field_name, key, value, query) + elif not isinstance(value, int) and source_field_name == 'target__guids___id': + base_guid, version = Guid.split_guid(value) + if base_guid is None and version is None: + raise InvalidFilterValue( + value=value, + field_type='guid', + ) + guid_filters = { + field_name: { + 'op': 'in', + 'value': self.bulk_get_values(value, field), + 'source_field_name': source_field_name, + }, + } + if version is not None: + guid_filters = { + field_name: { + 'op': 'eq', + 'value': base_guid, + 'source_field_name': 'target__guids___id', + }, + 'target__versioned_guids__version': { + 'op': 'eq', + 'value': version, + 'source_field_name': 'target__versioned_guids__version', + }, + } + + query.get(key).update(guid_filters) else: query.get(key).update({ field_name: { @@ -482,13 +511,15 @@ def param_queryset(self, query_params, default_queryset): ), ) if not isinstance(queryset, list): - sub_query = functools.reduce(operator.or_, sub_query_parts) + if not list(filter(lambda x: 'versioned_guids' in x.children[0][0], sub_query_parts)): + sub_query = functools.reduce(operator.or_, sub_query_parts) + else: + sub_query = functools.reduce(operator.and_, sub_query_parts) query_parts.append(sub_query) if not isinstance(queryset, list): for query in query_parts: queryset = queryset.filter(query) - return queryset def build_query_from_field(self, field_name, operation): diff --git a/api_tests/preprints/views/test_preprint_list_mixin.py b/api_tests/preprints/views/test_preprint_list_mixin.py index 37ef40942ea..df11d3d0c30 100644 --- a/api_tests/preprints/views/test_preprint_list_mixin.py +++ b/api_tests/preprints/views/test_preprint_list_mixin.py @@ -208,7 +208,8 @@ def test_filter_published_false_admin( res = app.get( f'{url}filter[is_published]=false', auth=user_admin_contrib.auth) - assert len(res.json['data']) == 0 + assert len(res.json['data']) == 1 + assert preprint_unpublished._id in [d['id'] for d in res.json['data']] @pytest.mark.django_db