diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e3b9a7e..5f8a8e0 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 diff --git a/edx_name_affirmation/__init__.py b/edx_name_affirmation/__init__.py index 1cf9f02..5ae1f29 100644 --- a/edx_name_affirmation/__init__.py +++ b/edx_name_affirmation/__init__.py @@ -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 diff --git a/edx_name_affirmation/api.py b/edx_name_affirmation/api.py index 9590da8..a143b3d 100644 --- a/edx_name_affirmation/api.py +++ b/edx_name_affirmation/api.py @@ -76,7 +76,7 @@ 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. @@ -84,6 +84,8 @@ def get_verified_name(user, is_verified=False): * `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. """ @@ -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() diff --git a/edx_name_affirmation/tests/test_api.py b/edx_name_affirmation/tests/test_api.py index 4a0437c..2b4094b 100644 --- a/edx_name_affirmation/tests/test_api.py +++ b/edx_name_affirmation/tests/test_api.py @@ -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