def get(self, request: Request) -> Response: """ Accepts only pagination-related query parameters """ models = [ {'name': 'page', 'key': 'page', 'type': 'integer', 'default': 1, 'min': 1}, {'name': 'limit', 'key': 'limit', 'type': 'integer', 'default': 500, 'min': 1, 'max': 500}, ] # Can't use the TinyShield decorator (yet) because this is a GET request only request_dict = request.query_params validated_request_data = TinyShield(models).block(request_dict) limit = validated_request_data["limit"] page = validated_request_data["page"] queryset = Definition.objects.all() queryset, pagination = get_pagination(queryset, int(limit), int(page)) serializer = DefinitionSerializer(queryset, many=True) response = { "page_metadata": pagination, "results": serializer.data } return Response(response)
def post(self, request: Request) -> Response: results = self._business_logic(request.data) paginated_results, page_metadata = get_pagination( results, request.data["limit"], request.data["page"]) response = OrderedDict( (("results", paginated_results), ("page_metadata", page_metadata))) return Response(response)
def post(self, request: Request) -> Response: group_by = SQL("group by ca.awarding_agency_id, taa.agency_id || '-' || taa.main_account_code") order_by = SQL("order by {} {}".format(SORTABLE_COLUMNS[request.data['sort']], request.data['order'])) columns = SQL("""sum(nullif(faba.transaction_obligated_amount, 'NaN')) total_transaction_obligated_amount, taa.agency_id || '-' || taa.main_account_code federal_account,""") results = self._business_logic(request.data, columns, group_by, order_by) paginated_results, page_metadata = get_pagination(results, request.data['limit'], request.data['page']) response = OrderedDict(( ('results', paginated_results), ('page_metadata', page_metadata) )) return Response(response)
def post(self, request: Request) -> Response: order_by = SQL("order by {} {}".format(SORTABLE_COLUMNS[request.data['sort']], request.data['order'])) group_by = SQL("""group by federal_account, fa.account_title, cte.award_id""") columns = SQL("""sum(nullif(faba.transaction_obligated_amount, 'NaN')) total_transaction_obligated_amount, taa.agency_id || '-' || taa.main_account_code federal_account, sum(sum(nullif(faba.transaction_obligated_amount, 'NaN'))) over (partition by cte.award_id) as total, fa.account_title """) results = self._business_logic(request.data, columns, group_by, order_by) paginated_results, page_metadata = get_pagination(results, request.data['limit'], request.data['page']) total = paginated_results[0]['total'] if len(paginated_results) > 0 else 0 for item in paginated_results: del item['total'] response = OrderedDict(( ('results', paginated_results), ('total', total), ('page_metadata', page_metadata) )) return Response(response)
def test_pagination(): # Testing for if anything breaks for the special case of an empty list results = [] empty_page_metadata = {"next": None, "previous": None, "hasNext": False, "hasPrevious": False, "count": 0} assert get_pagination(results, 1, 1) == ([], {**empty_page_metadata, **{"page": 1}}) assert get_pagination(results, 1, 4) == ([], {**empty_page_metadata, **{"page": 4}}) assert get_pagination(results, 3, 1) == ([], {**empty_page_metadata, **{"page": 1}}) assert get_pagination(results, 3, 2) == ([], {**empty_page_metadata, **{"page": 2}}) assert get_pagination(results, 1, 6) == ([], {**empty_page_metadata, **{"page": 6}}) assert get_pagination(results, 5, 2) == ([], {**empty_page_metadata, **{"page": 2}}) assert get_pagination(results, 1000, 1) == ([], {**empty_page_metadata, **{"page": 1}}) assert get_pagination(results, 1000, 2) == ([], {**empty_page_metadata, **{"page": 2}}) assert get_pagination(results, 0, 1) == ([], {**empty_page_metadata, **{"page": 1}}) assert get_pagination(results, 10, 0) == ([], {**empty_page_metadata, **{"page": 0}}) # Normal tests results = ["A", "B", "C", "D", "E"] populated_page_metadata = {"next": 2, "hasNext": True, "count": 5, "page": 1} assert get_pagination(results, 1, 1) == (["A"], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"next": 5, "hasNext": True, "previous": 3, "hasPrevious": True, "count": 5, "page": 4} assert get_pagination(results, 1, 4) == (["D"], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"next": 2, "hasNext": True, "count": 5, "page": 1} assert get_pagination(results, 3, 1) == (["A", "B", "C"], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"previous": 1, "hasPrevious": True, "count": 5, "page": 2} assert get_pagination(results, 3, 2) == (["D", "E"], {**empty_page_metadata, **populated_page_metadata}) # Testing special cases populated_page_metadata = {"previous": 5, "hasPrevious": True, "count": 5, "page": 6} assert get_pagination(results, 1, 6) == ([], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"previous": 1, "hasPrevious": True, "count": 5, "page": 2} assert get_pagination(results, 5, 2) == ([], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"page": 1, "count": 5} assert get_pagination(results, 1000, 1) == (["A", "B", "C", "D", "E"], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"previous": 1, "hasPrevious": True, "page": 2, "count": 5} assert get_pagination(results, 1000, 2) == ([], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"page": 1, "count": 5} assert get_pagination(results, 0, 1) == ([], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"page": 0, "count": 5} assert get_pagination(results, 10, 0) == ([], {**empty_page_metadata, **populated_page_metadata})
def test_pagination(): # Testing for if anything breaks for the special case of an empty list results = [] empty_page_metadata = {"next": None, "previous": None, "hasNext": False, "hasPrevious": False, "count": 0} assert get_pagination(results, 1, 1) == ([], {**empty_page_metadata, **{"page": 1}}) assert get_pagination(results, 1, 4) == ([], {**empty_page_metadata, **{"page": 4}}) assert get_pagination(results, 3, 1) == ([], {**empty_page_metadata, **{"page": 1}}) assert get_pagination(results, 3, 2) == ([], {**empty_page_metadata, **{"page": 2}}) assert get_pagination(results, 1, 6) == ([], {**empty_page_metadata, **{"page": 6}}) assert get_pagination(results, 5, 2) == ([], {**empty_page_metadata, **{"page": 2}}) assert get_pagination(results, 1000, 1) == ([], {**empty_page_metadata, **{"page": 1}}) assert get_pagination(results, 1000, 2) == ([], {**empty_page_metadata, **{"page": 2}}) assert get_pagination(results, 0, 1) == ([], {**empty_page_metadata, **{"page": 1}}) assert get_pagination(results, 10, 0) == ([], {**empty_page_metadata, **{"page": 0}}) # Normal tests results = ["A", "B", "C", "D", "E"] populated_page_metadata = {"next": 2, "hasNext": True, "count": 5, "page": 1} assert get_pagination(results, 1, 1) == (["A"], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"next": 5, "hasNext": True, "previous": 3, "hasPrevious": True, "count": 5, "page": 4} assert get_pagination(results, 1, 4) == (["D"], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"next": 2, "hasNext": True, "count": 5, "page": 1} assert get_pagination(results, 3, 1) == (["A", "B", "C"], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"previous": 1, "hasPrevious": True, "count": 5, "page": 2} assert get_pagination(results, 3, 2) == (["D", "E"], {**empty_page_metadata, **populated_page_metadata}) # Testing special cases populated_page_metadata = {"previous": 5, "hasPrevious": True, "count": 5, "page": 6} assert get_pagination(results, 1, 6) == ([], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"previous": 1, "hasPrevious": True, "count": 5, "page": 2} assert get_pagination(results, 5, 2) == ([], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"page": 1, "count": 5} assert get_pagination(results, 1000, 1) == (["A", "B", "C", "D", "E"], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"previous": 1, "hasPrevious": True, "page": 2, "count": 5} assert get_pagination(results, 1000, 2) == ([], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"page": 1, "count": 5} assert get_pagination(results, 0, 1) == ([], {**empty_page_metadata, **populated_page_metadata}) populated_page_metadata = {"page": 0, "count": 5} assert get_pagination(results, 10, 0) == ([], {**empty_page_metadata, **populated_page_metadata})