Premium: [sheet-service] update reconstruct table name, API export sheet data

This commit is contained in:
Ittipat Lusuk 2026-05-08 13:07:23 +07:00
parent f24002010e
commit 2612d33eb2
3 changed files with 146 additions and 27 deletions

153
main.py
View file

@ -929,7 +929,7 @@ def update_sheets(country: str, catalog: str, content: list):
def sync_nv2_to_nl(country: str, catalog: str, nv2_updates: list, headers: dict):
"""Sync updates จาก new-layout-v2 ไปอัปเดต new-layout สำหรับ THA"""
"""Sync updates from new-layout-v2 to new-layout (If is tha)"""
config = COUNTRY_MAPPING.get(country)
grist_docs = config.get("grist_doc_id", {})
@ -1539,6 +1539,74 @@ def grist_push_data_sheet_api(country: str):
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(e))
@app.get("/export/{country}")
def export_country(country: str):
try:
data = get_country_sheet_data(country)
return {
"country": country,
"sheets": data
}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
"""
Get data sheet from grist
"""
def get_country_sheet_data(country_key):
if country_key not in COUNTRY_MAPPING:
raise Exception(f"Country '{country_key}' not found")
config = COUNTRY_MAPPING[country_key]
sheets = config["sheets"]
grist_doc_ids = config.get("grist_doc_id", {})
result = {}
for sheet_key, sheet_name in sheets.items():
if not sheet_name:
continue
doc_id = grist_doc_ids.get(sheet_key)
if not doc_id:
continue
try:
if sheet_key == "name-desc-v2":
rows = fetch_grist_table_data(doc_id, "Name_desc_v2")
result[sheet_key] = [r["fields"] for r in rows]
elif sheet_key in ["new-layout", "new-layout-v2"]:
all_tables = get_all_grist_tables(doc_id)
all_rows = []
for t_id in all_tables:
if t_id.lower() == "name_desc_v2":
continue
rows = fetch_grist_table_data(doc_id, t_id)
if rows:
original_name = reconstruct_table_name(t_id)
all_rows.append([original_name])
all_rows.extend([r["fields"] for r in rows])
result[sheet_key] = all_rows
except Exception as e:
result[sheet_key] = {
"error": str(e)
}
return result
"""
@ -1697,13 +1765,84 @@ def col_to_index(col_str):
def reconstruct_table_name(t_id):
name = t_id
if name.startswith("Name_"):
name = name.replace("Name_", "Name=", 1)
if "_file_" in name:
name = name.replace("_file_", ",file=", 1)
if name.endswith("_skt"):
name = name[:-4] + ".skt"
return name
name = name[5:]
if "_file_" in name:
parts = name.split("_file_", 1)
name_part = parts[0]
rest = parts[1]
name_value = name_part.replace("_", "")
result = f"Name={name_value},file="
if "_skt_" in rest:
file_part, params_part = rest.split("_skt_", 1)
result += reconstruct_file_part(file_part) + ".skt"
params = reconstruct_params(params_part)
if params:
result += "," + params
elif rest.endswith("_skt"):
file_part = rest[:-4]
result += reconstruct_file_part(file_part) + ".skt"
else:
result += reconstruct_file_part(rest)
return result
return t_id
def reconstruct_file_part(file_id_part):
return file_id_part
def reconstruct_params(params_part):
result_parts = []
remaining = params_part
if "_parameter_" in remaining:
parts = remaining.rsplit("_parameter_", 1)
if len(parts) == 2:
before, param_value = parts
if "_file_" in before or "_xml_" in before:
if "_xml_file_" in before:
file_parts = before.split("_xml_file_", 1)
key_name = file_parts[0] # "topping"
full_key = key_name + "_xml_file" if not key_name.endswith("_xml") else key_name + "_file"
value_part = file_parts[1]
result_parts.append(f"{full_key}={reconstruct_file_with_ext(value_part)}")
elif "_file_" in before:
file_parts = before.rsplit("_file_", 1)
key_name = file_parts[0]
value_part = file_parts[1]
result_parts.append(f"{key_name}_file={reconstruct_file_with_ext(value_part)}")
result_parts.append(f"parameter={param_value.replace('_', '-')}")
return ",".join(result_parts)
def reconstruct_file_with_ext(file_id_part):
extensions = {
"_lxml": ".lxml",
"_xml": ".xml",
"_skt": ".skt"
}
for ext_id, ext_real in extensions.items():
if file_id_part.endswith(ext_id):
return file_id_part[:-len(ext_id)] + ext_real
return file_id_part
def get_all_grist_tables(doc_id):
""" Pull all table name in Document """