From 075d2c8c0c0d455cee8e44e56b75fa21c62977c8 Mon Sep 17 00:00:00 2001 From: wayangalihpratama Date: Tue, 8 Aug 2023 19:23:50 +0800 Subject: [PATCH] [#157] Create init map & update data/maps endpoint --- backend/models/data.py | 33 ++++++++++++-- backend/routes/data.py | 72 +++++++++++++++++++++++++++-- backend/tests/test_100_data.py | 83 ++++++++++++++-------------------- 3 files changed, 132 insertions(+), 56 deletions(-) diff --git a/backend/models/data.py b/backend/models/data.py index 3a7d4aa5..072c985c 100644 --- a/backend/models/data.py +++ b/backend/models/data.py @@ -63,9 +63,10 @@ class DataResponse(BaseModel): class MapsData(BaseModel): id: int - school_information: dict - year_conducted: int - geo: List[float] + # TODO:: DELETE commented code + # school_information: dict + # year_conducted: int + # geo: List[float] answer: Union[AnswerDict, dict] @@ -76,6 +77,20 @@ class MapDataResponse(BaseModel): total_page: int +class InitMapsData(BaseModel): + id: int + school_information: dict + year_conducted: int + geo: List[float] + + +class InitMapDataResponse(BaseModel): + current: int + data: List[InitMapsData] + total: int + total_page: int + + class RegistrationDict(TypedDict): question: str answer: Union[str, int, List[str]] @@ -207,10 +222,20 @@ def to_maps(self): return { "id": self.id, "identifier": self.identifier, + # TODO:: DELETE commented code + # "school_information": self.school_information, + # "year_conducted": self.year_conducted, + # "geo": self.geo, + "answer": {} + } + + @property + def init_maps(self): + return { + "id": self.id, "school_information": self.school_information, "year_conducted": self.year_conducted, "geo": self.geo, - "answer": {} } # only used in test case diff --git a/backend/routes/data.py b/backend/routes/data.py index 566007b9..8c74013d 100644 --- a/backend/routes/data.py +++ b/backend/routes/data.py @@ -30,7 +30,8 @@ ) from models.data import ( MapDataResponse, ChartDataDetail, - DataDetailPopup, DataResponse + DataDetailPopup, DataResponse, + InitMapDataResponse ) from models.answer import Answer from models.question import QuestionType @@ -109,6 +110,70 @@ def get_paginated_data( } +@data_route.get( + "/data/maps-init", + response_model=InitMapDataResponse, + name="data:init_maps_data", + summary="get maps data", + tags=["Data"], +) +def get_maps_init( + req: Request, + page: int = 1, + perpage: int = 100, + page_only: Optional[bool] = False, + session: Session = Depends(get_session), +): + # get the data + page_data = get_all_data( + session=session, + current=True, + skip=(perpage * (page - 1)), + perpage=perpage + ) + # handle pagination + count = page_data.get("count") + if not count: + return { + "current": page, + "data": [], + "total": count, + "total_page": 0, + } + total_page = ceil(count / perpage) if count > 0 else 0 + if total_page < page: + return { + "current": page, + "data": [], + "total": count, + "total_page": total_page, + } + if page_only: + return { + "current": page, + "data": [], + "total": count, + "total_page": total_page, + } + # data + data = page_data.get("data") or [] + data = [d.init_maps for d in data] + # transform data + for d in data: + d["school_information"] = extract_school_information( + school_information=d["school_information"], to_object=True) + res = { + "current": page, + "data": data, + "total": count, + "total_page": total_page, + } + return Response( + content=orjson.dumps(res), + media_type="application/json" + ) + + @data_route.get( "/data/maps", response_model=MapDataResponse, @@ -214,8 +279,9 @@ def get_maps( )) # transform data for d in data: - d["school_information"] = extract_school_information( - school_information=d["school_information"], to_object=True) + # TODO:: DELETE commented code + # d["school_information"] = extract_school_information( + # school_information=d["school_information"], to_object=True) d["jmp_filter"] = None data_id = str(d.get('identifier')) if "jmp" not in str(indicator): diff --git a/backend/tests/test_100_data.py b/backend/tests/test_100_data.py index 5dfd28b8..72c155ea 100644 --- a/backend/tests/test_100_data.py +++ b/backend/tests/test_100_data.py @@ -148,6 +148,30 @@ async def test_get_answers_of_data( assert "type" in c assert "value" in c + @pytest.mark.asyncio + async def test_get_init_maps_data( + self, app: FastAPI, session: Session, client: AsyncClient + ) -> None: + res = await client.get( + app.url_path_for("data:init_maps_data"), + params={"page_only": True} + ) + assert res.status_code == 200 + res = res.json() + assert list(res) == [ + 'current', 'data', 'total', 'total_page' + ] + # load all data + res = await client.get(app.url_path_for("data:init_maps_data")) + assert res.status_code == 200 + res = res.json() + assert list(res) == [ + 'current', 'data', 'total', 'total_page' + ] + assert list(res["data"][0]) == [ + 'id', 'school_information', 'year_conducted', 'geo' + ] + @pytest.mark.asyncio async def test_get_maps_data( self, app: FastAPI, session: Session, client: AsyncClient @@ -168,10 +192,7 @@ async def test_get_maps_data( assert list(res) == [ 'current', 'data', 'total', 'total_page' ] - assert list(res["data"][0]) == [ - 'id', 'school_information', - 'year_conducted', 'geo', 'answer' - ] + assert list(res["data"][0]) == ['id', 'answer'] # TODO:: Delete # assert res["data"][0] == { # 'id': 649130936, @@ -195,10 +216,7 @@ async def test_get_maps_data( assert list(res) == [ 'current', 'data', 'total', 'total_page' ] - assert list(res["data"][0]) == [ - 'id', 'school_information', - 'year_conducted', 'geo', 'answer' - ] + assert list(res["data"][0]) == ['id', 'answer'] assert list(res["data"][0]["answer"]) == ['question', 'value'] assert res["data"][0]["answer"]["question"] == indicator_id # TODO:: delete @@ -234,10 +252,7 @@ async def test_get_maps_data( assert list(res) == [ 'current', 'data', 'total', 'total_page' ] - assert list(res["data"][0]) == [ - 'id', 'school_information', - 'year_conducted', 'geo', 'answer' - ] + assert list(res["data"][0]) == ['id', 'answer'] assert list(res["data"][0]["answer"]) == ['question', 'value'] # TODO:: Delete # assert res["data"] == [{ @@ -271,10 +286,7 @@ async def test_get_maps_data( assert list(res) == [ 'current', 'data', 'total', 'total_page' ] - assert list(res["data"][0]) == [ - 'id', 'school_information', - 'year_conducted', 'geo', 'answer' - ] + assert list(res["data"][0]) == ['id', 'answer'] assert list(res["data"][0]["answer"]) == ['question', 'value'] assert res["data"][0]["answer"]["question"] == indicator_id # TODO: Delete @@ -311,10 +323,7 @@ async def test_get_maps_data( assert list(res) == [ 'current', 'data', 'total', 'total_page' ] - assert list(res["data"][0]) == [ - 'id', 'school_information', - 'year_conducted', 'geo', 'answer' - ] + assert list(res["data"][0]) == ['id', 'answer'] assert list(res["data"][0]["answer"]) == ['question', 'value'] assert res["data"][0]["answer"]["question"] == indicator_id # TODO: Delete @@ -349,12 +358,7 @@ async def test_get_maps_data( assert list(res) == [ 'current', 'data', 'total', 'total_page' ] - assert list(res["data"][0]) == [ - 'id', 'school_information', - 'year_conducted', 'geo', 'answer' - ] - for d in res["data"]: - assert d["school_information"]["province"] == "Guadalcanal" + assert list(res["data"][0]) == ['id', 'answer'] # TODO:: delete # assert res["data"][0] == { # 'id': 649130936, @@ -393,13 +397,7 @@ async def test_get_maps_data( assert list(res) == [ 'current', 'data', 'total', 'total_page' ] - assert list(res["data"][0]) == [ - 'id', 'school_information', - 'year_conducted', 'geo', 'answer' - ] - for d in res["data"]: - sctype = d["school_information"]["school_type"] - assert sctype == "Community High School" + assert list(res["data"][0]) == ['id', 'answer'] # TODO:: delete # assert res["data"][0] == { # 'id': 649130936, @@ -439,14 +437,7 @@ async def test_get_maps_data( assert list(res) == [ 'current', 'data', 'total', 'total_page' ] - assert list(res["data"][0]) == [ - 'id', 'school_information', - 'year_conducted', 'geo', 'answer' - ] - for d in res["data"]: - sctype = d["school_information"]["school_type"] - assert sctype == "Community High School" - assert d["school_information"]["province"] == "Guadalcanal" + assert list(res["data"][0]) == ['id', 'answer'] # TODO:: delete # assert res["data"][0] == { # 'id': 649130936, @@ -473,10 +464,7 @@ async def test_get_maps_data( assert list(res) == [ 'current', 'data', 'total', 'total_page' ] - assert list(res["data"][0]) == [ - 'id', 'school_information', - 'year_conducted', 'geo', 'answer' - ] + assert list(res["data"][0]) == ['id', 'answer'] assert list(res["data"][0]["answer"]) == ['question', 'value'] assert indicator_id in res["data"][0]["answer"]["question"] # TODO: Delete @@ -513,10 +501,7 @@ async def test_get_maps_data( assert list(res) == [ 'current', 'data', 'total', 'total_page' ] - assert list(res["data"][0]) == [ - 'id', 'school_information', - 'year_conducted', 'geo', 'answer' - ] + assert list(res["data"][0]) == ['id', 'answer'] assert list(res["data"][0]["answer"]) == ['question', 'value'] assert indicator_id in res["data"][0]["answer"]["question"] # TODO: Delete