Подправил ридми и проверку на не пустые списки

This commit is contained in:
2024-09-23 13:16:18 +09:00
parent 9f055ef095
commit b5761c5084
2 changed files with 34 additions and 32 deletions

View File

@ -1,9 +1,9 @@
создать виртуальное окружение создать виртуальное окружение \
""python3 -m venv .venv""" ```python3 -m venv .venv``` \
подключиться к виртуальному окружению подключиться к виртуальному окружению \
""".\.venv\Scripts\activate""" ```.\.venv\Scripts\activate``` \
Загрузить библиотеки с файла requirements.txt Загрузить библиотеки с файла `requirements.txt` \
"""pip install -r requirements.txt""" ```pip install -r requirements.txt``` \
в файле .env.template заменить на ваши данные и сохранить в .env в файле `.env.template` заменить на ваши данные и сохранить в `.env` \
Запустить приложение Запустить приложение \
"""main.py""" ```main.py```

48
main.py
View File

@ -1,4 +1,4 @@
from fastapi import FastAPI, APIRouter, Request from fastapi import FastAPI, APIRouter, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import ORJSONResponse, HTMLResponse from fastapi.responses import ORJSONResponse, HTMLResponse
from sqlmodel import create_engine, Field, SQLModel, UUID, DateTime, func, Column, text, bindparam from sqlmodel import create_engine, Field, SQLModel, UUID, DateTime, func, Column, text, bindparam
@ -72,31 +72,33 @@ async def get_ageement(id_account: Optional[UUID] = None) -> Sequence[Agreement]
@router.delete("/") @router.delete("/")
async def delete_pk(acc_list: Sequence[UUID]): async def delete_pk(acc_list: Sequence[UUID]):
with db_engine.begin() as con: if acc_list:
stmt = text("DELETE agreement WHERE id_account IN :acc_list") with db_engine.begin() as con:
params = {"acc_list":acc_list} stmt = text("DELETE agreement WHERE id_account IN :acc_list")
stmt = stmt.bindparams(bindparam('acc_list', expanding=True)) params = {"acc_list":acc_list}
con.execute(stmt, params) stmt = stmt.bindparams(bindparam('acc_list', expanding=True))
return {"msg":"has deleted"} con.execute(stmt, params)
return {"msg":"has deleted"}
raise HTTPException(405,"list is empty")
@router.post("/") @router.post("/")
async def add_agreement(agrees: Sequence[AgreementCreate]): async def add_agreement(agrees: Sequence[AgreementCreate]):
with db_engine.begin() as con: if acc_list:
dict_list=[] with db_engine.begin() as con:
acc_list = [] dict_list=[]
for agree in agrees: acc_list = []
agree_dict = agree.model_dump() for agree in agrees:
dict_list.append(agree_dict) agree_dict = agree.model_dump()
acc_id = agree_dict.get("id_account") dict_list.append(agree_dict)
if acc_id not in acc_list: acc_id = agree_dict.get("id_account")
acc_list.append(acc_id) if acc_id not in acc_list:
await delete_pk(acc_list) acc_list.append(acc_id)
stmt = insert(Agreement).values(dict_list) await delete_pk(acc_list)
con.execute(stmt) stmt = insert(Agreement).values(dict_list)
con.commit() con.execute(stmt)
return {"msg": "added succesfully"} con.commit()
return {"msg": "added succesfully"}
raise HTTPException(405,"list is empty")
app.include_router(router) app.include_router(router)
if __name__ == "__main__": if __name__ == "__main__":