Skip to content

Commit

Permalink
Merge pull request #522 from plone/issue_312
Browse files Browse the repository at this point in the history
Issue 312: unrestricted find
  • Loading branch information
mauritsvanrees committed Feb 21, 2024
2 parents 44c9006 + 0b0984d commit 7d36c5a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
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

0 comments on commit 7d36c5a

Please sign in to comment.