Skip to content

Commit

Permalink
Added parameter only_active=False to utils.get_persons_from_userid an…
Browse files Browse the repository at this point in the history
…d utils.get_person_from_userid that will only consider "person" that is "active"
  • Loading branch information
gbastien committed Feb 16, 2024
1 parent 2b8fa8b commit 375b5ad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/collective/contact/plonegroup/tests/test_utils.py
Expand Up @@ -82,6 +82,11 @@ def test_get_persons_from_userid(self):
self.diry.debra.reindexObject(['userid'])
self.assertListEqual(get_persons_from_userid(TEST_USER_ID), [self.diry.dexter, self.diry.debra])
self.assertEqual(get_person_from_userid(TEST_USER_ID), self.diry.dexter)
api.content.transition(self.diry.dexter, 'deactivate')
self.assertListEqual(get_persons_from_userid(TEST_USER_ID), [self.diry.dexter, self.diry.debra])
self.assertListEqual(get_persons_from_userid(TEST_USER_ID, only_active=True), [self.diry.debra])
self.assertEqual(get_person_from_userid(TEST_USER_ID), self.diry.dexter)
self.assertEqual(get_person_from_userid(TEST_USER_ID, only_active=True), self.diry.debra)

def test_organizations_with_suffixes(self):
class Dum(object):
Expand Down
29 changes: 25 additions & 4 deletions src/collective/contact/plonegroup/utils.py
Expand Up @@ -25,17 +25,26 @@
from zope.schema.vocabulary import SimpleVocabulary


def get_persons_from_userid(userid, context=None, depth=None, unrestricted=False, objects=True):
def get_persons_from_userid(userid,
context=None,
depth=None,
unrestricted=False,
objects=True,
only_active=False):
"""Return persons from userid.
:param userid: mandatory string
:param context: context to search (default None)
:param depth: depth to search (default None)
:param unrestricted: search unrestrictedly (default False)
:param objects: return objects list (default True) or brains (False)
:param only_active: only consider "person" that is "active" (default to False)
:return: object or brains list
"""
res = find(context=context, depth=depth, unrestricted=unrestricted, userid=userid)
query = {'userid': userid}
if only_active:
query['review_state'] = 'active'
res = find(context=context, depth=depth, unrestricted=unrestricted, **query)
if objects:
if unrestricted:
res = [br._unrestrictedGetObject() for br in res]
Expand All @@ -44,17 +53,29 @@ def get_persons_from_userid(userid, context=None, depth=None, unrestricted=False
return res


def get_person_from_userid(userid, context=None, depth=None, unrestricted=False, objects=True):
def get_person_from_userid(userid,
context=None,
depth=None,
unrestricted=False,
objects=True,
only_active=False):
"""Return one person from userid.
:param userid: mandatory string
:param context: context to search (default None)
:param depth: depth to search (default None)
:param unrestricted: search unrestrictedly (default False)
:param objects: return objects list (default True) or brains (False)
:param only_active: only consider "person" that is "active" (default to False)
:return: object or brain
"""
res = get_persons_from_userid(userid, context=context, depth=depth, unrestricted=unrestricted, objects=objects)
res = get_persons_from_userid(
userid,
context=context,
depth=depth,
unrestricted=unrestricted,
objects=objects,
only_active=only_active)
if not res:
return None
return res[0]
Expand Down

0 comments on commit 375b5ad

Please sign in to comment.