Skip to content

Commit

Permalink
Merge pull request #73 from edx/bseverino/exclude-status
Browse files Browse the repository at this point in the history
[MST-1275] Add `statuses_to_exclude` arg to `get_verified_name`
  • Loading branch information
bseverino authored Jan 12, 2022
2 parents a641e71 + f7dd961 commit f7fe2bd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Change Log
Unreleased
~~~~~~~~~~

[2.1.0] - 2022-01-11
~~~~~~~~~~~~~~~~~~~~
* Add optional `statuses_to_exclude` argument to `get_verified_name` in order to filter out one or
more statuses from the result.

[2.0.3] - 2021-11-17
~~~~~~~~~~~~~~~~~~~~
* Remove unused celery tasks
Expand Down
2 changes: 1 addition & 1 deletion edx_name_affirmation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
Django app housing name affirmation logic.
"""

__version__ = '2.0.3'
__version__ = '2.1.0'

default_app_config = 'edx_name_affirmation.apps.EdxNameAffirmationConfig' # pylint: disable=invalid-name
7 changes: 6 additions & 1 deletion edx_name_affirmation/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,16 @@ def create_verified_name(
log.info(log_msg)


def get_verified_name(user, is_verified=False):
def get_verified_name(user, is_verified=False, statuses_to_exclude=None):
"""
Get the most recent VerifiedName for a given user.
Arguments:
* `user` (User object)
* `is_verified` (bool): Optional, set to True to ignore entries that are not
verified.
* `statuses_to_exclude` (list): Optional list of statuses to filter out. Only
relevant if `is_verified` is False.
Returns a VerifiedName object.
"""
Expand All @@ -92,6 +94,9 @@ def get_verified_name(user, is_verified=False):
if is_verified:
return verified_name_qs.filter(status=VerifiedNameStatus.APPROVED.value).first()

if statuses_to_exclude:
return verified_name_qs.exclude(status__in=statuses_to_exclude).first()

return verified_name_qs.first()


Expand Down
32 changes: 32 additions & 0 deletions edx_name_affirmation/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,38 @@ def test_get_verified_name_none_exist(self, check_is_verified):

self.assertIsNone(verified_name_obj)

@ddt.data(
([VerifiedNameStatus.PENDING], VerifiedNameStatus.DENIED),
([VerifiedNameStatus.DENIED, VerifiedNameStatus.PENDING], VerifiedNameStatus.APPROVED),
(['invalid status'], VerifiedNameStatus.PENDING)
)
@ddt.unpack
def test_get_verified_name_exclude(self, statuses_to_exclude, expected_status):
"""
Exclude verified names by status when getting the most recent verified name.
"""
self._create_verified_name(status=VerifiedNameStatus.APPROVED)
self._create_verified_name(status=VerifiedNameStatus.DENIED)
self._create_verified_name(status=VerifiedNameStatus.PENDING)

verified_name_obj = get_verified_name(self.user, False, statuses_to_exclude)

self.assertEqual(verified_name_obj.status, expected_status)

@ddt.data(False, True)
def test_get_verified_name_ignore_exlude_parameter(self, is_verified):
"""
If `is_verified` is True, ignore the `statuses_to_exclude` parameter.
"""
self._create_verified_name(status=VerifiedNameStatus.APPROVED)

verified_name_obj = get_verified_name(self.user, is_verified, [VerifiedNameStatus.APPROVED])

if is_verified:
self.assertEqual(verified_name_obj.verified_name, self.VERIFIED_NAME)
else:
self.assertIsNone(verified_name_obj)

def test_get_verified_name_history(self):
"""
Test that get_verified_name_history returns all of the user's VerifiedNames
Expand Down

0 comments on commit f7fe2bd

Please sign in to comment.