Skip to content

Commit

Permalink
Merge pull request #2700 from ziv17/2697-nf-api-pagination
Browse files Browse the repository at this point in the history
Adding pagination fo news-flash api
  • Loading branch information
atalyaalon authored Sep 16, 2024
2 parents 83105ba + 58bf0ec commit 67a5c9b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
64 changes: 54 additions & 10 deletions anyway/views/news_flash/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pandas as pd
import os

from typing import List, Optional
from typing import List, Optional, Tuple, Any
from http import HTTPStatus
from collections import OrderedDict

Expand Down Expand Up @@ -37,12 +37,20 @@
DEFAULT_OFFSET_REQ_PARAMETER = 0
DEFAULT_LIMIT_REQ_PARAMETER = 100
DEFAULT_NUMBER_OF_YEARS_AGO = 5
PAGE_NUMBER = "pageNumber"
PAGE_SIZE = "pageSize"
NEWS_FALSH_ID = "newsFlash_Id"
ID = "id"
LIMIT = "limit"
OFFSET = "offset"


class NewsFlashQuery(BaseModel):
id: Optional[int]
road_number: Optional[int]
offset: Optional[int] = DEFAULT_OFFSET_REQ_PARAMETER
pageNumber: Optional[int]
pageSize: Optional[int]
limit: Optional[int] = DEFAULT_LIMIT_REQ_PARAMETER
resolution: Optional[List[str]]
source: Optional[List[BE_CONST.Source]]
Expand Down Expand Up @@ -75,19 +83,54 @@ def news_flash():
except ValidationError as e:
return make_response(jsonify(e.errors()[0]["msg"]), 404)

if "id" in validated_query_params:
return get_news_flash_by_id(validated_query_params["id"])
pagination, validated_query_params = set_pagination_params(validated_query_params)

query = gen_news_flash_query(db.session, validated_query_params)
if ID in validated_query_params:
return get_news_flash_by_id(validated_query_params[ID])

total, query = gen_news_flash_query(db.session, validated_query_params)
news_flashes = query.all()

news_flashes_jsons = [n.serialize() for n in news_flashes]
for news_flash in news_flashes_jsons:
news_flashes_dicts = [n.serialize() for n in news_flashes]
for news_flash in news_flashes_dicts:
set_display_source(news_flash)
return Response(json.dumps(news_flashes_jsons, default=str), mimetype="application/json")


def gen_news_flash_query(session, valid_params: dict):
if pagination:
res = add_pagination_to_result(validated_query_params, news_flashes_dicts, total)
else:
res = news_flashes_dicts
return Response(json.dumps(res, default=str), mimetype="application/json")


def set_pagination_params(validated_params: dict) -> Tuple[bool, dict]:
pagination = False
if NEWS_FALSH_ID in validated_params:
validated_params[ID] = validated_params.pop(NEWS_FALSH_ID)
if PAGE_NUMBER in validated_params:
pagination = True
page_number = validated_params[PAGE_NUMBER]
page_size = validated_params.get(PAGE_SIZE, DEFAULT_LIMIT_REQ_PARAMETER)
validated_params[OFFSET] = (page_number - 1) * page_size
validated_params[LIMIT] = page_size
return pagination, validated_params


def add_pagination_to_result(validated_params: dict, news_flashes: list, num_nf: int) -> dict:
page_size = validated_params[PAGE_SIZE]
page_num = validated_params[PAGE_NUMBER]
total_pages = num_nf // page_size + (1 if num_nf % page_size else 0)
return {
"data": news_flashes,
"pagination": {
"pageNumber": page_num,
"pageSize": page_size,
"totalPages": total_pages,
"totalRecords": num_nf
}
}


def gen_news_flash_query(session, valid_params: dict) -> Tuple[int, Any]:
query = session.query(NewsFlash)
supported_resolutions = set([x.value for x in BE_CONST.SUPPORTED_RESOLUTIONS])
query = query.filter(NewsFlash.resolution.in_(supported_resolutions))
Expand All @@ -113,11 +156,12 @@ def gen_news_flash_query(session, valid_params: dict):
not_(and_(NewsFlash.lat == None, NewsFlash.lon == None)),
)
).order_by(NewsFlash.date.desc())
total = query.count()
if "offset" in valid_params:
query = query.offset(valid_params["offset"])
if "limit" in valid_params:
query = query.limit(valid_params["limit"])
return query
return total, query


def set_display_source(news_flash):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_news_flash_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,21 @@ def _test_update_news_flash_qualifying_not_manual_exists_location_db(self):
def test_gen_news_flash_query(self):
orig_supported_resolutions = BE_CONST.SUPPORTED_RESOLUTIONS
BE_CONST.SUPPORTED_RESOLUTIONS = [BE_CONST.ResolutionCategories.DISTRICT]
actual = gen_news_flash_query(self.session, {"road_number": 12345678})
_, actual = gen_news_flash_query(self.session, {"road_number": 12345678})
news_flashes = actual.all()
self.assertEqual(len(news_flashes), 1, "single news flash")
self.assertEqual(
news_flashes[0].description, self.district_description, "district description"
)

BE_CONST.SUPPORTED_RESOLUTIONS = [BE_CONST.ResolutionCategories.REGION]
actual = gen_news_flash_query(self.session, {"road_number": 12345678})
_, actual = gen_news_flash_query(self.session, {"road_number": 12345678})
news_flashes = actual.all()
self.assertEqual(len(news_flashes), 1, "single news flash")
self.assertEqual(news_flashes[0].description, self.region_description, "region description")

BE_CONST.SUPPORTED_RESOLUTIONS = [BE_CONST.ResolutionCategories.CITY]
actual = gen_news_flash_query(self.session, {"road_number": 12345678})
_, actual = gen_news_flash_query(self.session, {"road_number": 12345678})
news_flashes = actual.all()
self.assertEqual(len(news_flashes), 0, "zero news flash")

Expand Down

0 comments on commit 67a5c9b

Please sign in to comment.