chore: Add limit parameter to get_retirement_users view

This commit is contained in:
dnuzz
2023-02-07 11:30:44 -05:00
committed by GitHub
2 changed files with 25 additions and 1 deletions

View File

@@ -776,6 +776,18 @@ class TestAccountRetirementList(RetirementTestCase):
self.assert_status_and_user_list(retirement_values, states_to_request=self._get_non_dead_end_states())
def test_user_limit_works(self):
"""
Verify that request limiting works to limit returned amount.
"""
state = 'PENDING'
for _ in range(5):
create_retirement_status(UserFactory(), state=RetirementState.objects.get(state_name=state))
data = {'cool_off_days': 0, 'states': state, 'limit': '2'}
response = self.client.get(self.url, data, **self.headers)
assert response.status_code == status.HTTP_200_OK
assert len(response.json()) == 2
def test_date_filter(self):
"""
Verifies the functionality of the `cool_off_days` parameter by creating 1 retirement per day for

View File

@@ -917,7 +917,7 @@ class AccountRetirementStatusView(ViewSet):
def retirement_queue(self, request):
"""
GET /api/user/v1/accounts/retirement_queue/
{'cool_off_days': 7, 'states': ['PENDING', 'COMPLETE']}
{'cool_off_days': 7, 'states': ['PENDING', 'COMPLETE'], 'limit': 500}
Returns the list of RetirementStatus users in the given states that were
created in the retirement queue at least `cool_off_days` ago.
@@ -936,6 +936,16 @@ class AccountRetirementStatusView(ViewSet):
found = [s.state_name for s in state_objs]
raise RetirementStateError(f'Unknown state. Requested: {states} Found: {found}')
limit = request.GET.get('limit')
if limit:
try:
limit_count = int(limit)
except ValueError:
return Response(
f'Limit could not be parsed: {limit}, please ensure this is an integer',
status=status.HTTP_400_BAD_REQUEST
)
earliest_datetime = datetime.datetime.now(pytz.UTC) - datetime.timedelta(days=cool_off_days)
retirements = UserRetirementStatus.objects.select_related(
@@ -945,6 +955,8 @@ class AccountRetirementStatusView(ViewSet):
).order_by(
'id'
)
if limit:
retirements = retirements[:limit_count]
serializer = UserRetirementStatusSerializer(retirements, many=True)
return Response(serializer.data)
# This should only occur on the int() conversion of cool_off_days at this point