Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
WSL0809 committed Jul 9, 2024
1 parent 66f61af commit b069758
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 155 deletions.
6 changes: 0 additions & 6 deletions api/allocate_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
@router.post("/allocate_room_by_client_id")
async def allocate_room_by_client_id(client_id: int, room_number: str, current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)):
if current_user.role != "admin":
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)

client = db.query(Client).filter(Client.id == client_id).first()
client_status = client.status.split("-")[1]
Expand Down
7 changes: 1 addition & 6 deletions api/check_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,7 @@ def update_room_and_baby(db, check_in_recv: CheckInRecv):

@router.post("/check_in")
def check_in_room(check_in_recv: CheckInRecv, current_user: User = Depends(get_current_active_user), db: Session = Depends(get_db)):
if current_user.role != "admin":
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)

try:
update_room_and_baby(db, check_in_recv)
return CheckInResp(status="success", details="入住成功")
Expand Down
2 changes: 0 additions & 2 deletions api/check_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ def client_check_out(db, check_out_recv):


@router.post("/terminate")
@roles_required("admin")
async def terminate_service(check_out_recv: TerminateRecv, current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)):
if current_user.double_check_password != check_out_recv.double_check_password:
Expand All @@ -171,7 +170,6 @@ async def terminate_service(check_out_recv: TerminateRecv, current_user: User =


@router.post("/check_out")
@roles_required("admin")
async def check_out_service(check_out_recv: CheckOutRecv, current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)):
try:
Expand Down
2 changes: 0 additions & 2 deletions api/fault_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ def repair_complete(room_number: str, db: Session):
@router.post("/fault_registration")
def fault_registration(fault_registration: FaultRegistrationRecv, current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)):
if current_user.role != "admin":
return FaultRegistrationResp(status=status.HTTP_401_UNAUTHORIZED, details="权限不足")

if len(fault_registration.fault_list) == 0 or fault_registration.fault_list is None:
repair_complete(fault_registration.room_number, db)
Expand Down
4 changes: 1 addition & 3 deletions api/get_all_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ def get_clients(db: Session, name: Optional[str], page: int, limit: int):


@router.get("/get_clients", response_model=GetAllClientsResp)
def get_clients_by_name(current_user: User = Depends(get_current_active_user), db: Session = Depends(get_db),
def get_clients_by_name(db: Session = Depends(get_db),
name: Optional[str] = Query(None), page: int = 1, limit: int = 10):
if current_user.role != "admin":
raise HTTPException(status_code=401, detail="Unauthorized")

total, clients = get_clients(db, name, page, limit)
if not clients:
Expand Down
1 change: 0 additions & 1 deletion api/get_baby_nurse.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def get_baby_nurses(db: Session, name: Optional[str], page: int, limit: int):


@router.get("/get_baby_nurse", response_model=GetBabyNurseResp)
@roles_required(1)
def get_clients_by_name(db: Session = Depends(get_db),
name: Optional[str] = Query(None),
page: int = 1,
Expand Down
3 changes: 1 addition & 2 deletions api/get_client_by_room_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ def do_get_client_in_room(db: Session, room_number: str):


@router.get("/get_client_in_room", response_model=GetClientByRoomResp)
@roles_required("admin")
async def get_client_in_room(room_number: str, current_user: User = Depends(get_current_active_user),
async def get_client_in_room(room_number: str,
db: Session = Depends(get_db)):
clients = do_get_client_in_room(db, room_number)
if clients is None:
Expand Down
4 changes: 0 additions & 4 deletions api/get_plan_by_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@
@router.get("/get_meal_plan_by_id")
async def get_plan_by_id(meal_plan_id: int, db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)):
if current_user.role != "admin" and not current_user.is_active:
return {"status": 401, "details": "only admin can get plans"}
return db.query(model.MealPlan).filter(model.MealPlan.meal_plan_id == meal_plan_id).first()


@router.get("/get_recovery_plan_by_id")
async def get_plan_by_id(recovery_plan_id: int, db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)):
if current_user.role != "admin" and not current_user.is_active:
return {"status": 401, "details": "only admin can get plans"}
return db.query(model.RecoveryPlan).filter(model.RecoveryPlan.recovery_plan_id == recovery_plan_id).first()
7 changes: 1 addition & 6 deletions api/get_room_by_room_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ class GetRoomByRoomNameRecv(BaseModel):


@router.get("/get_room_by_room_name", response_model=GetRoomByRoomNameResp)
def get_all_room_info(room_number: str, current_user: User = Depends(get_current_active_user),
def get_all_room_info(room_number: str,
db: Session = Depends(get_db)):
if current_user.role != "admin":
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="You must be an admin to access this information."
)

sql = text(
"""
Expand Down
3 changes: 0 additions & 3 deletions api/insert_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ def update_client_and_room(db, insert_client_recv: InsertClientRecv):
@router.post("/insert_client")
async def insert_client(insert_client_recv: InsertClientRecv, current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)):
if current_user.role != "admin":
raise HTTPException(status_code=401, detail="only admin can insert client")

update_client_and_room(db, insert_client_recv)

return {
Expand Down
12 changes: 4 additions & 8 deletions api/manage_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ def get_plans_in_db(db, plan_category):
@router.post("/create_plan")
async def create_plan(plan_create: PlanCreate, current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)):
if current_user.role != "admin":
raise HTTPException(status_code=401, detail="only admin can add new plan")
create_plan_in_db(db, plan_create)

return {
Expand All @@ -147,10 +145,9 @@ async def create_plan(plan_create: PlanCreate, current_user: User = Depends(get_

# delete plan
@router.post("/delete_plan")
async def delete_plan(plan_del: PlanDel, current_user: User = Depends(get_current_active_user),
async def delete_plan(plan_del: PlanDel,
db: Session = Depends(get_db)):
if current_user.role != "admin":
raise HTTPException(status_code=401, detail="only admin can delete plan")

try:
delete_plan_in_db(db, plan_del)
return {
Expand All @@ -162,9 +159,8 @@ async def delete_plan(plan_del: PlanDel, current_user: User = Depends(get_curren


@router.get("/get_plans", response_model=PlanResp)
async def get_plans(current_user: User = Depends(get_current_active_user), db: Session = Depends(get_db)):
if current_user.role != "admin":
raise HTTPException(status_code=401, detail="only admin can get plans")
async def get_plans(db: Session = Depends(get_db)):

meal_plan_res = get_plans_in_db(db, "meal_plan")
recovery_plan_res = get_plans_in_db(db, "recovery_plan")
return PlanResp(meal_plan=meal_plan_res, recovery_plan=recovery_plan_res)
8 changes: 1 addition & 7 deletions api/reminder.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ def pick_babies_by_birthday(db: Session):


@router.get("/get_reminder", response_model=list[ReminderResp])
async def get_reminder(current_user: User = Depends(get_current_active_user), db: Session = Depends(get_db)):
if current_user.role != "admin":
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
async def get_reminder(db: Session = Depends(get_db)):

resp = [ReminderResp(**dict(row), **{"remind_content": "宝妈生日"}) for row in pick_clients_by_birthday(db)]
resp.extend([ReminderResp(**dict(row), **{"remind_content": "宝宝生日"}) for row in pick_babies_by_birthday(db)])
Expand Down
19 changes: 6 additions & 13 deletions api/reserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,14 @@ def update_client_and_room(db, reserve_recv: ReserveRecv):


@router.post("/reserve")
async def reserve_room(reserve_recv: ReserveRecv, current_user: User = Depends(get_current_active_user),
async def reserve_room(reserve_recv: ReserveRecv,
db: Session = Depends(get_db)):
print(dict(reserve_recv))
if current_user.role == "admin":
try:
update_client_and_room(db, reserve_recv)
return ReserveResp(status="success", details="预定成功")
except Exception as e:
return ReserveResp(status="error", details=str(e))
else:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
update_client_and_room(db, reserve_recv)
return ReserveResp(status="success", details="预定成功")
except Exception as e:
return ReserveResp(status="error", details=str(e))


'''
Expand Down
4 changes: 1 addition & 3 deletions api/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class AddRoomResp(BaseModel):


@router.post("/add_room", response_model=AddRoomResp)
@roles_required("admin")
async def add_room(room_info: AddRoomRecv, current_user: User = Depends(get_current_active_user),
async def add_room(room_info: AddRoomRecv,
db: Session = Depends(get_db)):
try:
room = Room(room_number=room_info.room_number, notes=room_info.notes)
Expand All @@ -45,7 +44,6 @@ async def add_room(room_info: AddRoomRecv, current_user: User = Depends(get_curr


@router.post("/delete_room")
@roles_required("admin")
async def delete_room(room: DeleteRoomRecv, db: Session = Depends(get_db)):
try:
db.query(Room).filter(Room.room_number == room.room_number).delete()
Expand Down
10 changes: 2 additions & 8 deletions api/update_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ class UpdateClientStatusRecv(BaseModel):


@router.post("/update_client_status")
async def update_client_status(recv: UpdateClientStatusRecv, db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)):
if current_user.role != "admin":
return {"status": 401, "details": "only admin can update client status"}

async def update_client_status(recv: UpdateClientStatusRecv, db: Session = Depends(get_db)):
client = db.query(Client).filter(Client.id == recv.client_id).first()
if client is None:
return {"status": 404, "details": "client not found"}
Expand All @@ -31,9 +27,7 @@ async def update_client_status(recv: UpdateClientStatusRecv, db: Session = Depen


@router.post("/update_client_meal_plan")
@roles_required("admin")
async def update_client_meal_plan(recv: UpdateClientStatusRecv, db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)):
async def update_client_meal_plan(recv: UpdateClientStatusRecv, db: Session = Depends(get_db)):
client = db.query(Client).filter(Client.id == recv.client_id).first()
if client is None:
return {"status": 404, "details": "client not found"}
Expand Down
1 change: 0 additions & 1 deletion crud/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from .crud import *
Binary file removed crud/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file removed crud/__pycache__/crud.cpython-310.pyc
Binary file not shown.
Loading

0 comments on commit b069758

Please sign in to comment.