Skip to content

Commit

Permalink
[#547] Move and fix filter by submission id in data cleaning page
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Dec 17, 2024
1 parent 3da4e60 commit 928978a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
31 changes: 17 additions & 14 deletions backend/db/crud_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,17 @@ def delete_bulk(session: Session, ids: List[int]) -> None:

def get_data(
session: Session,
form: int,
skip: int,
perpage: int,
form: Optional[Union[int, str]] = None,
submitted: Optional[bool] = False,
org_ids: Optional[List[int]] = None,
monitoring_round: Optional[int] = None,
data_id: Optional[int] = None,
) -> PaginatedData:
data = session.query(Data).filter(Data.form == form)
data = session.query(Data)
if form and form != "all":
data = data.filter(Data.form == form)
if data_id:
data = data.filter(Data.id == data_id)
if submitted:
Expand Down Expand Up @@ -246,19 +248,20 @@ def get_data_by_form(session: Session, form: int):


def get_history_datapoint(
session: Session, form: int, organisation_id: int, last_year: int
session: Session,
organisation_id: int,
last_year: int,
form: Optional[Union[int, str]] = None,
):
"""Query the history data for submitted datapoint"""
data = (
session.query(Data)
.filter(
and_(
Data.form == form,
Data.organisation == organisation_id,
Data.submitted != null(),
extract("year", Data.submitted) != last_year,
)
data = session.query(Data)
if form and form != "all":
data = data.filter(Data.form == form)
data = data.filter(
and_(
Data.organisation == organisation_id,
Data.submitted != null(),
extract("year", Data.submitted) != last_year,
)
.all()
)
).all()
return data
23 changes: 18 additions & 5 deletions backend/routes/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from fastapi import Depends, Request, Response, APIRouter, HTTPException, Query
from fastapi.security import HTTPBearer
from fastapi.security import HTTPBasicCredentials as credentials
from typing import List, Optional
from typing import List, Optional, Union
from sqlalchemy import func, and_, extract, null
from sqlalchemy.orm import Session
import db.crud_data as crud
Expand Down Expand Up @@ -224,9 +224,9 @@ def notify_secretariat_admin(session: Session, user, form_name: str):
)
def get(
req: Request,
form_id: int,
page: int = 1,
perpage: int = 10,
form_id: Optional[Union[int, str]] = None,
submitted: Optional[bool] = False,
filter_same_isco: Optional[bool] = False,
monitoring_round: Optional[int] = Query(None),
Expand Down Expand Up @@ -261,11 +261,16 @@ def get(

# transform cascade answer value
result = [d.serializeWithQuestionName for d in data["data"]]

question_forms = [form_id]
if form_id == "all":
question_forms = [int(d.form) for d in data["data"]]

questions = (
session.query(Question)
.filter(
and_(
Question.form == form_id,
Question.form.in_(question_forms),
Question.type == QuestionType.cascade.value,
)
)
Expand Down Expand Up @@ -308,7 +313,11 @@ def get(
value = a["value"]
if qid in cascade_qids and value:
new_value = [cascades.get(int(float(x))) for x in value]
a["value"] = "|".join(new_value) if new_value else value
a["value"] = (
"|".join(str(v) for v in new_value if v is not None)
if new_value
else value
)
history_data[f"{d.organisation}-{d.created.year}"] = history_result

# transform cascade answer value by cascade list
Expand All @@ -318,7 +327,11 @@ def get(
value = a["value"]
if qid in cascade_qids and value:
new_value = [cascades.get(int(float(x))) for x in value]
a["value"] = "|".join(new_value) if new_value else value
a["value"] = (
"|".join(str(v) for v in new_value if v is not None)
if new_value
else value
)
# Add history answer
res["history"] = history_data.get(
f"{res['organisation']}-{res['year']}", []
Expand Down
20 changes: 11 additions & 9 deletions frontend/src/pages/data-cleaning/DataCleaning.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ const DataCleaning = () => {
}, [forms]);

const fetchData = useCallback(() => {
if (formSelected) {
if (formSelected || dataID) {
setIsLoading(true);
let url = `/data/form/${formSelected}?page=${page}&perpage=${pageSize}`;
let url = `/data/form/${
formSelected || "all"
}?page=${page}&perpage=${pageSize}`;
url = `${url}&submitted=1&filter_same_isco=1`;
if (selectedMonitoringRound) {
url = `${url}&monitoring_round=${selectedMonitoringRound}`;
Expand Down Expand Up @@ -342,6 +344,13 @@ const DataCleaning = () => {
<Col flex={1}>
{" "}
<Space wrap>
<InputNumber
placeholder="Data ID"
controls={false}
onChange={handleDataIDFilter}
value={dataID}
// disabled={!formSelected}
/>
<Select
showArrow
showSearch
Expand Down Expand Up @@ -382,13 +391,6 @@ const DataCleaning = () => {
onChange={setSelectedMonitoringRound}
disabled={!formSelected}
/>
<InputNumber
placeholder="Data ID"
controls={false}
onChange={handleDataIDFilter}
value={dataID}
disabled={!formSelected}
/>
</Space>
</Col>
</Row>
Expand Down

0 comments on commit 928978a

Please # to comment.