diff --git a/kibicara/webapi/admin.py b/kibicara/webapi/admin.py index 8098fea..d45eff4 100644 --- a/kibicara/webapi/admin.py +++ b/kibicara/webapi/admin.py @@ -6,11 +6,12 @@ """ REST API endpoints for hood admins. """ -from fastapi import APIRouter, Depends, HTTPException, status +from fastapi import APIRouter, Depends, HTTPException, Response, status from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm from kibicara import email from kibicara.config import config -from kibicara.model import Admin, AdminHoodRelation +from kibicara.model import Admin, AdminHoodRelation, Hood +from kibicara.webapi.utils import delete_hood from logging import getLogger from nacl.encoding import URLSafeBase64Encoder from nacl.exceptions import CryptoError @@ -164,3 +165,25 @@ async def admin_hood_read_all(admin=Depends(get_admin)): return ( await AdminHoodRelation.objects.select_related('hood').filter(admin=admin).all() ) + + +@router.delete( + '/', + status_code=status.HTTP_204_NO_CONTENT, + operation_id='delete_admin', +) +async def admin_delete(admin=Depends(get_admin)): + hood_relations = ( + await AdminHoodRelation.objects.select_related('hood').filter(admin=admin).all() + ) + for hood in hood_relations: + admins = ( + await AdminHoodRelation.objects.select_related('admin') + .filter(hood=hood.id) + .all() + ) + if len(admins) == 1 and admins[0].id == admin.id: + actual_hood = await Hood.objects.filter(id=hood.id).all() + await delete_hood(actual_hood[0]) + await admin.delete() + return Response(status_code=status.HTTP_204_NO_CONTENT) diff --git a/kibicara/webapi/hoods/__init__.py b/kibicara/webapi/hoods/__init__.py index 98c2623..2cbee8d 100644 --- a/kibicara/webapi/hoods/__init__.py +++ b/kibicara/webapi/hoods/__init__.py @@ -6,10 +6,10 @@ """ REST API Endpoints for managing hoods. """ from fastapi import APIRouter, Depends, HTTPException, Response, status -from kibicara.model import AdminHoodRelation, BadWord, Hood, Trigger -from kibicara.platformapi import Spawner +from kibicara.model import AdminHoodRelation, Hood, Trigger from kibicara.platforms.email.bot import spawner from kibicara.webapi.admin import get_admin +from kibicara.webapi.utils import delete_hood from ormantic.exceptions import NoMatch from pydantic import BaseModel from sqlite3 import IntegrityError @@ -117,12 +117,5 @@ async def hood_update(values: BodyHood, hood=Depends(get_hood)): ) async def hood_delete(hood=Depends(get_hood)): """ Deletes hood with id **hood_id**. """ - await Spawner.destroy_hood(hood) - for trigger in await Trigger.objects.filter(hood=hood).all(): - await trigger.delete() - for badword in await BadWord.objects.filter(hood=hood).all(): - await badword.delete() - for relation in await AdminHoodRelation.objects.filter(hood=hood).all(): - await relation.delete() - await hood.delete() + await delete_hood(hood) return Response(status_code=status.HTTP_204_NO_CONTENT) diff --git a/kibicara/webapi/utils.py b/kibicara/webapi/utils.py new file mode 100644 index 0000000..ddd198b --- /dev/null +++ b/kibicara/webapi/utils.py @@ -0,0 +1,17 @@ +# Copyright (C) 2020 by Cathy Hu +# +# SPDX-License-Identifier: 0BSD + +from kibicara.model import AdminHoodRelation, BadWord, Trigger +from kibicara.platformapi import Spawner + + +async def delete_hood(hood): + await Spawner.destroy_hood(hood) + for trigger in await Trigger.objects.filter(hood=hood).all(): + await trigger.delete() + for badword in await BadWord.objects.filter(hood=hood).all(): + await badword.delete() + for relation in await AdminHoodRelation.objects.filter(hood=hood).all(): + await relation.delete() + await hood.delete()