Skip to content

Commit

Permalink
[Fixes #11847] Implement the option to hide resources from search and…
Browse files Browse the repository at this point in the history
… catalogue listing
  • Loading branch information
mattiagiupponi committed Jan 17, 2024
1 parent 1d23058 commit db5f91e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
34 changes: 34 additions & 0 deletions geonode/base/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2567,6 +2567,40 @@ def test_api_should_return_only_the_advertised_false_where_user_is_owner(self):
dataset.delete()
test_user_for_api.delete()

def test_api_should_filter_by_advertised_param(self):
"""
If anonymous user, only the advertised resoruces whould be returned by the API.
"""
dts = create_single_dataset("advertised_false")
dts.advertised = False
dts.save()
# should show the result based on the logic
url = reverse("base-resources-list")
payload = self.client.get(url)
prev_count = payload.json().get("total")
# the user can see only the advertised resources
self.assertEqual(ResourceBase.objects.filter(advertised=True).count(), prev_count)

payload = self.client.get(f"{url}?advertised=True")
# so if advertised is True, we dont see the advertised=False resource
new_count = payload.json().get("total")
# recheck the count
self.assertEqual(new_count, prev_count)

payload = self.client.get(f"{url}?advertised=False")
# so if advertised is False, we see only the resource with advertised==False
new_count = payload.json().get("total")
# recheck the count
self.assertEqual(new_count, 1)

# if all is requested, we will see all the resources
payload = self.client.get(f"{url}?advertised=all")
new_count = payload.json().get("total")
# recheck the count
self.assertEqual(new_count, prev_count + 1)

Dataset.objects.update(advertised=True)


class TestExtraMetadataBaseApi(GeoNodeBaseTestSupport):
def setUp(self):
Expand Down
24 changes: 12 additions & 12 deletions geonode/base/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,22 +383,22 @@ def list(self, request, *args, **kwargs):
# resources is generated
user = request.user
try:
_filter = request.query_params.get("filter{advertised}", "None")
advertised = strtobool(_filter) if _filter!='all' else 'all'
_filter = request.query_params.get("advertised", "None")
advertised = strtobool(_filter) if _filter.lower() != "all" else "all"
except Exception:
advertised = None

if advertised is not None and advertised != 'all':
if advertised is not None and advertised != "all":
queryset = queryset.filter(advertised=advertised)

is_admin = user.is_superuser if user and user.is_authenticated else False

if advertised == 'all':
pass
elif not is_admin and user and not user.is_anonymous:
queryset = (queryset.filter(advertised=True) | queryset.filter(owner=user)).distinct()
elif not user or user.is_anonymous:
queryset = queryset.filter(advertised=True)
else:
is_admin = user.is_superuser if user and user.is_authenticated else False

if advertised == "all":
pass
elif not is_admin and user and not user.is_anonymous:
queryset = (queryset.filter(advertised=True) | queryset.filter(owner=user)).distinct()
elif not user or user.is_anonymous:
queryset = queryset.filter(advertised=True)

page = self.paginate_queryset(queryset)
if page is not None:
Expand Down

0 comments on commit db5f91e

Please sign in to comment.