create/update app test version

This commit is contained in:
2023-07-13 03:19:41 +00:00
parent 51163749cc
commit 7e312ab768
2 changed files with 150 additions and 13 deletions

View File

@@ -2,21 +2,150 @@ from fastapi import APIRouter, UploadFile,HTTPException,File
from io import BytesIO
import typing as t
import pandas as pd
import json
import httpx
import app.core.config as c
kinton_router = r = APIRouter()
@r.post("/upload",)
async def upload(file:UploadFile = File(...)):
async def upload(files:t.List[UploadFile] = File(...)):
dataframes = []
if file.filename.endswith('.xlsx'):
try:
content = await file.read()
df = pd.read_excel(BytesIO(content))
dataframes.append(df)
except Exception as e:
raise HTTPException(status_code=400, detail=f"Error occurred while parsing file {file.filename}: {str(e)}")
else:
raise HTTPException(status_code=400, detail=f"File {file.filename} is not an Excel file")
return {"file": file.filename}
for file in files:
if file.filename.endswith('.xlsx'):
try:
content = await file.read()
df = pd.read_excel(BytesIO(content))
print(df)
dataframes.append(df)
except Exception as e:
raise HTTPException(status_code=400, detail=f"Error occurred while parsing file {file.filename}: {str(e)}")
else:
raise HTTPException(status_code=400, detail=f"File {file.filename} is not an Excel file")
return {"files": [file.filename for file in files]}
@r.get("/allapps",)
async def allapps():
headers={c.API_V1_AUTH_KEY:c.API_V1_AUTH_VALUE}
url = f"{c.BASE_URL}{c.API_V1_STR}/apps.json"
r = httpx.get(url,headers=headers)
return r.json()
@r.post("/createapp",)
async def createapp(name:str):
headers={c.API_V1_AUTH_KEY:c.API_V1_AUTH_VALUE,"Content-Type": "application/json"}
data = {"name":name}
url = f"{c.BASE_URL}{c.API_V1_STR}/preview/app.json"
r = httpx.post(url,headers=headers,data=json.dumps(data))
result = r.json()
if result.get("app") != None:
url = f"{c.BASE_URL}{c.API_V1_STR}/preview/app/deploy.json"
data = {"apps":[result],"revert": False}
r = httpx.post(url,headers=headers,data=json.dumps(data))
return r.json
property=["label","code","type","required","defaultValue","options"]
@r.post("/createappfromexcel",)
async def createappfromexcel(files:t.List[UploadFile] = File(...)):
for file in files:
if file.filename.endswith('.xlsx'):
try:
content = await file.read()
#アプリ名
df = pd.read_excel(BytesIO(content))
# print(df)
appname = df.iloc[0,2]
col=[]
for row in range(5,len(df)):
if not df.iloc[row,3] in c.KINTONE_FIELD_TYPE:
continue
p=[]
for column in range(1,7):
if(not pd.isna(df.iloc[row,column])):
if(property[column-1]=="options"):
o=[]
for v in df.iloc[row,column].split(','):
o.append(f"\"{v.split('|')[0]}\":{{\"label\":\"{v.split('|')[0]}\",\"index\":\"{v.split('|')[1]}\"}}")
p.append(f"\"{property[column-1]}\":{{{','.join(o)}}}")
elif(property[column-1]=="required"):
p.append(f"\"{property[column-1]}\":{df.iloc[row,column]}")
else:
p.append(f"\"{property[column-1]}\":\"{df.iloc[row,column]}\"")
col.append(f"\"{df.iloc[row,2]}\":{{{','.join(p)}}}")
headers={c.API_V1_AUTH_KEY:c.API_V1_AUTH_VALUE,"Content-Type": "application/json"}
data = {"name":appname}
url = f"{c.BASE_URL}{c.API_V1_STR}/preview/app.json"
r = httpx.post(url,headers=headers,data=json.dumps(data))
result1 = r.json()
if result1.get("app") != None:
url = f"{c.BASE_URL}{c.API_V1_STR}/preview/app/form/fields.json"
form = f"{{\"app\":{result1['app']},\"properties\":{{{','.join(col)}}}}}".replace("False","false").replace("True","true")
print(form)
data = json.loads(form)
r = httpx.post(url,headers=headers,data=json.dumps(data))
result2 = r.json()
if result2.get("revision") != None:
url = f"{c.BASE_URL}{c.API_V1_STR}/preview/app/deploy.json"
data = {"apps":[{"app":result1["app"],"revision":result2["revision"]}],"revert": False}
r = httpx.post(url,headers=headers,data=json.dumps(data))
except Exception as e:
raise HTTPException(status_code=400, detail=f"Error occurred while parsing file {file.filename}: {str(e)}")
else:
raise HTTPException(status_code=400, detail=f"File {file.filename} is not an Excel file")
return {"app":result1["app"],"revision":result2["revision"]}
@r.post("/updateappfromexcel",)
async def updateappfromexcel(app:str,revision:str,files:t.List[UploadFile] = File(...)):
for file in files:
if file.filename.endswith('.xlsx'):
try:
content = await file.read()
#アプリ名
df = pd.read_excel(BytesIO(content))
# print(df)
appname = df.iloc[0,2]
col=[]
for row in range(5,len(df)):
if not df.iloc[row,3] in c.KINTONE_FIELD_TYPE:
continue
p=[]
for column in range(1,7):
if(not pd.isna(df.iloc[row,column])):
if(property[column-1]=="options"):
o=[]
for v in df.iloc[row,column].split(','):
o.append(f"\"{v.split('|')[0]}\":{{\"label\":\"{v.split('|')[0]}\",\"index\":\"{v.split('|')[1]}\"}}")
p.append(f"\"{property[column-1]}\":{{{','.join(o)}}}")
elif(property[column-1]=="required"):
p.append(f"\"{property[column-1]}\":{df.iloc[row,column]}")
else:
p.append(f"\"{property[column-1]}\":\"{df.iloc[row,column]}\"")
col.append(f"\"{df.iloc[row,2]}\":{{{','.join(p)}}}")
headers={c.API_V1_AUTH_KEY:c.API_V1_AUTH_VALUE,"Content-Type": "application/json"}
url = f"{c.BASE_URL}{c.API_V1_STR}/preview/app/form/fields.json"
form = f"{{\"app\":{app},\"revision\": {revision},\"properties\":{{{','.join(col)}}}}}".replace("False","false").replace("True","true")
print(form)
data = json.loads(form)
r = httpx.put(url,headers=headers,data=json.dumps(data))
result = r.json()
if result.get("revision") != None:
url = f"{c.BASE_URL}{c.API_V1_STR}/preview/app/deploy.json"
data = {"apps":[{"app":app,"revision":result["revision"]}],"revert": False}
r = httpx.post(url,headers=headers,data=json.dumps(data))
except Exception as e:
raise HTTPException(status_code=400, detail=f"Error occurred while parsing file {file.filename}: {str(e)}")
else:
raise HTTPException(status_code=400, detail=f"File {file.filename} is not an Excel file")
return r.json()

View File

@@ -4,4 +4,12 @@ PROJECT_NAME = "KintoneAppBuilder"
SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL")
API_V1_STR = "/api/v1"
BASE_URL = "https://mfu07rkgnb7c.cybozu.com"
API_V1_STR = "/k/v1"
API_V1_AUTH_KEY = "X-Cybozu-Authorization"
API_V1_AUTH_VALUE = "TVhaOm1heHoxMjA1"
KINTONE_FIELD_TYPE=["GROUP","GROUP_SELECT","CHECK_BOX","SUBTABLE","RICH_TEXT","RICH_TEXT","LINK","REFERENCE_TABLE","CALC","TIME","NUMBER","ORGANIZATION_SELECT","FILE","DATETIME","DATE","MULTI_SELECT","SINGLE_LINE_TEXT","MULTI_LINE_TEXT"]