Added object deleter.
This commit is contained in:
@@ -347,3 +347,37 @@ async def update_object(
|
|||||||
created_at=obj.created_at,
|
created_at=obj.created_at,
|
||||||
modified_at=obj.modified_at
|
modified_at=obj.modified_at
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@router.delete("/objects/{uuid}", status_code=204)
|
||||||
|
async def delete_object(
|
||||||
|
uuid: UUID,
|
||||||
|
db: DbDependency,
|
||||||
|
current_user: HttpUser = Depends(get_current_http_user)
|
||||||
|
):
|
||||||
|
username = current_user.username
|
||||||
|
|
||||||
|
try:
|
||||||
|
with db.transaction() as conn:
|
||||||
|
root = conn.root()
|
||||||
|
|
||||||
|
obj = Object.get_object_by_uuid(uuid, root)
|
||||||
|
if not obj:
|
||||||
|
raise HTTPException(status_code=404, detail="Object not found")
|
||||||
|
|
||||||
|
user = User.get_user_by_username(username, root)
|
||||||
|
if not user or user.uuid != obj.owner:
|
||||||
|
raise HTTPException(status_code=403, detail="Not authorized to delete this object")
|
||||||
|
|
||||||
|
# Remove references
|
||||||
|
user.remove_obj_uuid(uuid) # from user's object_uuids set
|
||||||
|
del conn.root.objects[uuid] # from global objects mapping
|
||||||
|
|
||||||
|
logging.info(f"User {username} deleted object {uuid}")
|
||||||
|
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Object delete failed for {username} on {uuid}: {e}\n{traceback.format_exc()}")
|
||||||
|
raise HTTPException(status_code=500, detail="Failed to delete object")
|
||||||
|
|
||||||
|
return None
|
||||||
Reference in New Issue
Block a user