[core] Add endpoint for deleting an account
This commit is contained in:
parent
d66bf759e1
commit
3fddb960d2
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
17
kibicara/webapi/utils.py
Normal file
17
kibicara/webapi/utils.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
|
||||
#
|
||||
# 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()
|
Loading…
Reference in a new issue