mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: depreciate houses service & route
This commit is contained in:
parent
22464cead4
commit
51aecbb6e3
3 changed files with 1 additions and 208 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from src.routers import activity, blocks, users, auth, houses, orgs, roles
|
from src.routers import activity, blocks, users, auth, orgs, roles
|
||||||
from src.routers.courses import chapters, collections, courses,activities
|
from src.routers.courses import chapters, collections, courses,activities
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -9,7 +9,6 @@ global_router = APIRouter(prefix="/api")
|
||||||
# API Routes
|
# API Routes
|
||||||
global_router.include_router(users.router, prefix="/users", tags=["users"])
|
global_router.include_router(users.router, prefix="/users", tags=["users"])
|
||||||
global_router.include_router(auth.router, prefix="/auth", tags=["auth"])
|
global_router.include_router(auth.router, prefix="/auth", tags=["auth"])
|
||||||
global_router.include_router(houses.router, prefix="/houses", tags=["houses"])
|
|
||||||
global_router.include_router(orgs.router, prefix="/orgs", tags=["orgs"])
|
global_router.include_router(orgs.router, prefix="/orgs", tags=["orgs"])
|
||||||
global_router.include_router(roles.router, prefix="/roles", tags=["roles"])
|
global_router.include_router(roles.router, prefix="/roles", tags=["roles"])
|
||||||
global_router.include_router(blocks.router, prefix="/blocks", tags=["blocks"])
|
global_router.include_router(blocks.router, prefix="/blocks", tags=["blocks"])
|
||||||
|
|
|
||||||
|
|
@ -1,49 +0,0 @@
|
||||||
from fastapi import APIRouter, Depends, Request
|
|
||||||
from src.security.auth import get_current_user
|
|
||||||
|
|
||||||
from src.services.houses import House, HouseInDB, create_house, get_house, get_houses, update_house, delete_house
|
|
||||||
from src.services.users.users import PublicUser, User
|
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter()
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/")
|
|
||||||
async def api_create_house(request: Request,house_object: House, current_user: PublicUser = Depends(get_current_user)):
|
|
||||||
"""
|
|
||||||
Create new house
|
|
||||||
"""
|
|
||||||
return await create_house(request, house_object, current_user)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{house_id}")
|
|
||||||
async def api_get_house(request: Request,house_id: str, current_user: PublicUser = Depends(get_current_user)):
|
|
||||||
"""
|
|
||||||
Get single House by house_id
|
|
||||||
"""
|
|
||||||
return await get_house(request, house_id, current_user=current_user)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/page/{page}/limit/{limit}")
|
|
||||||
async def api_get_house_by(request: Request,page: int, limit: int):
|
|
||||||
"""
|
|
||||||
Get houses by page and limit
|
|
||||||
"""
|
|
||||||
return await get_houses(request, page, limit)
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{house_id}")
|
|
||||||
async def api_update_house(request: Request,house_object: House, house_id: str, current_user: PublicUser = Depends(get_current_user)):
|
|
||||||
"""
|
|
||||||
Update House by house_id
|
|
||||||
"""
|
|
||||||
return await update_house(request, house_object, house_id, current_user)
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{house_id}")
|
|
||||||
async def api_delete_house(request: Request,house_id: str, current_user: PublicUser = Depends(get_current_user)):
|
|
||||||
"""
|
|
||||||
Delete House by ID
|
|
||||||
"""
|
|
||||||
|
|
||||||
return await delete_house(request, house_id, current_user)
|
|
||||||
|
|
@ -1,157 +0,0 @@
|
||||||
import json
|
|
||||||
from typing import List
|
|
||||||
from uuid import uuid4
|
|
||||||
from pydantic import BaseModel
|
|
||||||
from src.services.users.users import PublicUser, User
|
|
||||||
from src.security.security import *
|
|
||||||
from fastapi import FastAPI, HTTPException, status, Request, Response, BackgroundTasks
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
#### Classes ####################################################
|
|
||||||
|
|
||||||
|
|
||||||
class House(BaseModel):
|
|
||||||
name: str
|
|
||||||
photo: str
|
|
||||||
description: str
|
|
||||||
email: str
|
|
||||||
org: str
|
|
||||||
|
|
||||||
|
|
||||||
class HouseInDB(House):
|
|
||||||
house_id: str
|
|
||||||
owners: List[str]
|
|
||||||
admins: List[str]
|
|
||||||
|
|
||||||
#### Classes ####################################################
|
|
||||||
|
|
||||||
# TODO : Add house photo upload and delete
|
|
||||||
|
|
||||||
async def get_house(request: Request, house_id: str, current_user: PublicUser):
|
|
||||||
houses = request.app.db["houses"]
|
|
||||||
|
|
||||||
house = houses.find_one({"house_id": house_id})
|
|
||||||
|
|
||||||
# verify house rights
|
|
||||||
await verify_house_rights(request,house_id, current_user, "read")
|
|
||||||
|
|
||||||
if not house:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_409_CONFLICT, detail="House does not exist")
|
|
||||||
|
|
||||||
house = House(**house)
|
|
||||||
return house
|
|
||||||
|
|
||||||
|
|
||||||
async def create_house(request: Request,house_object: House, current_user: PublicUser):
|
|
||||||
houses = request.app.db["houses"]
|
|
||||||
|
|
||||||
# find if house already exists using name
|
|
||||||
isHouseAvailable = houses.find_one({"name": house_object.name})
|
|
||||||
|
|
||||||
if isHouseAvailable:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_409_CONFLICT, detail="House name already exists")
|
|
||||||
|
|
||||||
# generate house_id with uuid4
|
|
||||||
house_id = str(f"house_{uuid4()}")
|
|
||||||
|
|
||||||
hasRoleRights = await verify_user_rights_with_roles(request, "create", current_user.user_id, house_id)
|
|
||||||
|
|
||||||
if not hasRoleRights:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_409_CONFLICT, detail="Roles : Insufficient rights to perform this action")
|
|
||||||
|
|
||||||
house = HouseInDB(house_id=house_id, owners=[
|
|
||||||
current_user.user_id], admins=[
|
|
||||||
current_user.user_id], **house_object.dict())
|
|
||||||
|
|
||||||
house_in_db = houses.insert_one(house.dict())
|
|
||||||
|
|
||||||
if not house_in_db:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Unavailable database")
|
|
||||||
|
|
||||||
return house.dict()
|
|
||||||
|
|
||||||
|
|
||||||
async def update_house(request: Request,house_object: House, house_id: str, current_user: PublicUser):
|
|
||||||
|
|
||||||
# verify house rights
|
|
||||||
await verify_house_rights(request,house_id, current_user, "update")
|
|
||||||
|
|
||||||
houses = request.app.db["houses"]
|
|
||||||
|
|
||||||
house = houses.find_one({"house_id": house_id})
|
|
||||||
|
|
||||||
if house:
|
|
||||||
# get owner value from house object database
|
|
||||||
owners = house["owners"]
|
|
||||||
admins = house["admins"]
|
|
||||||
|
|
||||||
updated_house = HouseInDB(
|
|
||||||
house_id=house_id, owners=owners, admins=admins, **house_object.dict())
|
|
||||||
|
|
||||||
houses.update_one({"house_id": house_id}, {"$set": updated_house.dict()})
|
|
||||||
|
|
||||||
return HouseInDB(**updated_house.dict())
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_409_CONFLICT, detail="House does not exist")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def delete_house(request: Request,house_id: str, current_user: PublicUser):
|
|
||||||
|
|
||||||
# verify house rights
|
|
||||||
await verify_house_rights(request,house_id, current_user, "delete")
|
|
||||||
|
|
||||||
houses = request.app.db["houses"]
|
|
||||||
|
|
||||||
house = houses.find_one({"house_id": house_id})
|
|
||||||
|
|
||||||
if not house:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_409_CONFLICT, detail="House does not exist")
|
|
||||||
|
|
||||||
isDeleted = houses.delete_one({"house_id": house_id})
|
|
||||||
|
|
||||||
if isDeleted:
|
|
||||||
return {"detail": "House deleted"}
|
|
||||||
else:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Unavailable database")
|
|
||||||
|
|
||||||
|
|
||||||
async def get_houses(request: Request,page: int = 1, limit: int = 10):
|
|
||||||
houses = request.app.db["houses"]
|
|
||||||
# TODO : Get only houses that user is admin/has roles of
|
|
||||||
# get all houses from database
|
|
||||||
all_houses = houses.find().sort("name", 1).skip(10 * (page - 1)).limit(limit)
|
|
||||||
|
|
||||||
return [json.loads(json.dumps(house, default=str)) for house in await all_houses.to_list(length=limit)]
|
|
||||||
|
|
||||||
|
|
||||||
#### Security ####################################################
|
|
||||||
|
|
||||||
async def verify_house_rights(request: Request,house_id: str, current_user: PublicUser, action: str):
|
|
||||||
houses = request.app.db["houses"]
|
|
||||||
|
|
||||||
house = houses.find_one({"house_id": house_id})
|
|
||||||
|
|
||||||
if not house:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_409_CONFLICT, detail="House does not exist")
|
|
||||||
|
|
||||||
hasRoleRights = await verify_user_rights_with_roles(request,action, current_user.user_id, house_id)
|
|
||||||
isOwner = current_user.user_id in house["owners"]
|
|
||||||
|
|
||||||
if not hasRoleRights and not isOwner:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_403_FORBIDDEN, detail="Roles/Ownership : Insufficient rights to perform this action")
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
#### Security ####################################################
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue