Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 312: unrestricted find #522

Merged
merged 13 commits into from
Feb 21, 2024
15 changes: 15 additions & 0 deletions docs/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,21 @@ documents = api.content.find(
%
% self.assertGreater(len(documents), 0)

Find all `Document` content types, and use unrestricted search results:

```python
from plone import api
documents = api.content.find(
context=api.portal.get(),
portal_type="Document",
unrestricted=True,
)
```

% invisible-code-block: python
%
% self.assertGreater(len(documents), 0)

More information about how to use the catalog may be found in the
[Plone Documentation](https://5.docs.plone.org/develop/plone/searching_and_indexing/index.html).

Expand Down
1 change: 1 addition & 0 deletions news/312.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implemented unrestricted find of content types. @gogobd
8 changes: 6 additions & 2 deletions src/plone/api/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,13 @@ def _parse_object_provides_query(query):
return result


def find(context=None, depth=None, **kwargs):
def find(context=None, depth=None, unrestricted=False, **kwargs):
"""Find content in the portal.

:param context: Context for the search
:type obj: Content object
:param depth: How far in the content tree we want to search from context
:param unrestricted: Boolean, use unrestrictedSearchResults if True
:type obj: Content object
:returns: Catalog brains
:rtype: List
Expand Down Expand Up @@ -656,4 +657,7 @@ def find(context=None, depth=None, **kwargs):
if not valid_indexes:
return []

return catalog(**query)
if unrestricted:
return catalog.unrestrictedSearchResults(**query)
else:
return catalog(**query)
16 changes: 16 additions & 0 deletions src/plone/api/tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,22 @@ def test_find(self):
documents = api.content.find(portal_type="Document")
self.assertEqual(len(documents), 2)

def test_untrestricted_find(self):
"""Test the finding of content in with unrestricted search."""

# Search as Anonymous user
from plone.app.testing import logout

logout()

# Find documents (unrestricted)
documents = api.content.find(portal_type="Document", unrestricted=True)
self.assertEqual(len(documents), 2)

# Find documents (restricted)
documents = api.content.find(portal_type="Document")
self.assertEqual(len(documents), 0)

def test_find_empty_query(self):
"""Make sure an empty query yields no results"""

Expand Down
Loading