-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from factly/feat/update-tags-from-gsheets
feat: Update dictionary values from sheets
- Loading branch information
Showing
7 changed files
with
116 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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,68 @@ | ||
import pandas as pd | ||
from fastapi import APIRouter, HTTPException, status | ||
from fastapi.encoders import jsonable_encoder | ||
from fastapi.responses import JSONResponse | ||
|
||
from app.core.config import CORE_FOLDER, Settings | ||
from app.models.gsheets import GsheetSaveRequest | ||
from app.utils.gsheets import get_records_from_gsheets | ||
|
||
settings = Settings() | ||
|
||
dictionary_router = router = APIRouter() | ||
|
||
|
||
@router.get("/", summary="Get all Saved Entities csv file name") | ||
async def get_entity_names(): | ||
# List down all the csv files present in the config folder | ||
return [ | ||
csv_file.name.replace(".csv", "") | ||
for csv_file in CORE_FOLDER.glob("**/*.csv") | ||
] | ||
|
||
|
||
@router.get( | ||
"/{entity}", | ||
summary="Get data about Saved Entity csv file", | ||
response_class=JSONResponse, | ||
) | ||
async def get_entity_data(entity: str): | ||
entity_df = pd.read_csv(CORE_FOLDER / f"{entity}.csv") | ||
# to avoid json conversion error | ||
entity_df = entity_df.fillna("") | ||
|
||
# convert to json | ||
json_compatible_item_data = jsonable_encoder( | ||
entity_df.to_dict(orient="records") | ||
) | ||
return JSONResponse(content=json_compatible_item_data) | ||
|
||
|
||
@router.put( | ||
"/", | ||
summary="Update an Entity csv file and save it to config", | ||
) | ||
async def update_entity(request: GsheetSaveRequest): | ||
# looking for destination that needs to be updated | ||
destination_file = CORE_FOLDER / f"{request.entity}.csv" | ||
|
||
# only proceed if file is existing as we don not want to create new files | ||
if not destination_file.is_file(): | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail="Provided entity does not exist", | ||
) | ||
|
||
# get dataset/tags from the data dictionary google sheet | ||
dataset_meta_data = get_records_from_gsheets( | ||
sheet_id=request.sheet_id, | ||
worksheet=request.worksheet, | ||
) | ||
|
||
# read the file in pandas and replace it to config | ||
entity_df = pd.DataFrame(dataset_meta_data) | ||
|
||
# Save the dataset inside config folder | ||
entity_df.to_csv(CORE_FOLDER / f"{request.entity}.csv", index=False) | ||
|
||
return {"entity": request.entity, "action": "UPDATE", "status": "SUCCEED"} |
This file contains 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 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 |
---|---|---|
@@ -1,53 +1,53 @@ | ||
sector | ||
Administration & Governance | ||
Administration and Governance | ||
Agriculture and Allied | ||
Art and Culture | ||
Automobile | ||
Banking | ||
Aviation | ||
Biotechnology | ||
Census and Surveys | ||
Aviation | ||
Chemicals and Fertilizers | ||
Commerce | ||
Corporate | ||
Defence | ||
Demographics | ||
Education | ||
Entertainment | ||
Electoral Statistics | ||
Energy | ||
Environment and Forest | ||
Finance | ||
Food | ||
Foreign Affairs | ||
Governance and Administration | ||
Health and Family Welfare | ||
Home Affairs and Enforcement | ||
Horticulture | ||
Hospitality | ||
Housing | ||
Industries | ||
Urban Development | ||
Industries and Factories | ||
Information and Broadcasting | ||
Information Technology | ||
Infrastructure | ||
Insurance | ||
International | ||
Irrigation | ||
Judiciary | ||
Labour and Employment | ||
Livestock | ||
Law and Justice | ||
Media | ||
Mining | ||
Parliament of India | ||
Ports and Shipping | ||
Postal | ||
Power and Energy | ||
Prices | ||
Railway | ||
Railways | ||
Rural Development | ||
Science and Technology | ||
Shipping | ||
Social Development | ||
State Functions | ||
Telecommunications | ||
Trade | ||
Tourism and Hospitality | ||
Transport | ||
Tourism | ||
Urban Development | ||
Water and Sanitation | ||
Youth and Sports | ||
Banking | ||
Trade | ||
Water Resources | ||
Youth and Sports | ||
Youth and Sports |
This file contains 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 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,10 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class GsheetRequest(BaseModel): | ||
sheet_id: str | ||
worksheet: str | ||
|
||
|
||
class GsheetSaveRequest(GsheetRequest): | ||
entity: str |
This file contains 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 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