-
Notifications
You must be signed in to change notification settings - Fork 9
[OSDEV-1468] Limit page parameter for GET /api/facilities. #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vladsha-dev
merged 3 commits into
main
from
OSDEV-1468-limit-page-parameter-for-get-api-facilities
Dec 9, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/django/api/serializers/facility/facility_list_page_parameter_serializer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from rest_framework.serializers import ( | ||
| IntegerField, | ||
| Serializer | ||
| ) | ||
|
|
||
| from api.constants import FacilitiesListSettings | ||
|
|
||
|
|
||
| class FacilityListPageParameterSerializer(Serializer): | ||
| ''' | ||
| The serializer validates the page query parameter for the list action of | ||
| the FacilitiesViewSet. | ||
| ''' | ||
| page = IntegerField( | ||
| required=False, | ||
| max_value=FacilitiesListSettings.DEFAULT_PAGE_LIMIT, | ||
| min_value=1, | ||
| error_messages={ | ||
| 'max_value': ( | ||
| 'This value must be less or equal to ' | ||
| f'{FacilitiesListSettings.DEFAULT_PAGE_LIMIT}. If you need ' | ||
| 'access to more data, please contact ' | ||
| '[email protected].'), | ||
| } | ||
| ) |
36 changes: 36 additions & 0 deletions
36
src/django/api/tests/test_page_limit_in_facilities_viewset_list_action.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import json | ||
|
|
||
| from rest_framework.test import APITestCase | ||
| from django.urls import reverse | ||
|
|
||
|
|
||
| class TestPageLimitInFacilitiesViewsetListAction(APITestCase): | ||
| def setUp(self): | ||
| self.url = reverse('facility-list') | ||
|
|
||
| def test_page_limit_blocks_request_when_exceeding_limit(self): | ||
| expected_response = { | ||
| 'page': [ | ||
| ('This value must be less or equal to 100. If you need access ' | ||
| 'to more data, please contact [email protected].') | ||
| ] | ||
| } | ||
|
|
||
| page = {'page': 101} | ||
|
|
||
| response = self.client.get(self.url, page) | ||
| self.assertEqual(response.status_code, 400) | ||
|
|
||
| response_body_dict = json.loads(response.content) | ||
| self.assertEqual(response_body_dict, expected_response) | ||
|
|
||
| def test_page_limit_does_not_block_request_within_limit(self): | ||
| page = {'page': 1} | ||
|
|
||
| response = self.client.get(self.url, page) | ||
| response_body_dict = json.loads(response.content) | ||
|
|
||
| # Made sure that the successful response with an empty facility list | ||
| # is returned even if the test database is empty. | ||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual(response_body_dict['count'], 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ | |
| TYPE_INTEGER | ||
| ) | ||
|
|
||
| from api.constants import FacilitiesListSettings | ||
|
|
||
| facility_parameters = [ | ||
| Parameter( | ||
| 'created_at_of_data_points', | ||
|
|
@@ -31,6 +33,17 @@ | |
| ] | ||
|
|
||
| facilities_list_parameters = [ | ||
| Parameter( | ||
| 'page', | ||
| IN_QUERY, | ||
| type=TYPE_INTEGER, | ||
| required=False, | ||
| description=( | ||
| 'A page number within the paginated result set. As of now, the ' | ||
| 'last page that can be accessed is ' | ||
| f'{FacilitiesListSettings.DEFAULT_PAGE_LIMIT}. To access more ' | ||
| 'data, please contact [email protected].'), | ||
| ), | ||
| Parameter( | ||
| 'q', | ||
| IN_QUERY, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.