2020-07-01 19:34:16 +00:00
|
|
|
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
|
|
|
|
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
2020-07-11 10:54:07 +00:00
|
|
|
""" REST API Endpoints for managing hoods. """
|
|
|
|
|
2020-07-01 19:34:16 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Response, status
|
2020-09-06 17:11:18 +00:00
|
|
|
from kibicara.model import AdminHoodRelation, BadWord, Hood, Trigger
|
|
|
|
from kibicara.platformapi import Spawner
|
2020-07-18 11:53:11 +00:00
|
|
|
from kibicara.platforms.email.bot import spawner
|
2020-07-01 19:34:16 +00:00
|
|
|
from kibicara.webapi.admin import get_admin
|
|
|
|
from ormantic.exceptions import NoMatch
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from sqlite3 import IntegrityError
|
|
|
|
|
|
|
|
|
|
|
|
class BodyHood(BaseModel):
|
|
|
|
name: str
|
|
|
|
landingpage: str = '''
|
|
|
|
Default Landing Page
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
2020-07-09 01:09:48 +00:00
|
|
|
async def get_hood_unauthorized(hood_id: int):
|
2020-07-01 19:34:16 +00:00
|
|
|
try:
|
|
|
|
hood = await Hood.objects.get(id=hood_id)
|
|
|
|
except NoMatch:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
2020-07-09 01:09:48 +00:00
|
|
|
return hood
|
|
|
|
|
|
|
|
|
|
|
|
async def get_hood(hood=Depends(get_hood_unauthorized), admin=Depends(get_admin)):
|
2020-07-01 19:34:16 +00:00
|
|
|
try:
|
|
|
|
await AdminHoodRelation.objects.get(admin=admin, hood=hood)
|
|
|
|
except NoMatch:
|
|
|
|
raise HTTPException(
|
2020-07-02 12:31:53 +00:00
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
headers={'WWW-Authenticate': 'Bearer'},
|
|
|
|
)
|
2020-07-01 19:34:16 +00:00
|
|
|
return hood
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.get(
|
|
|
|
'/',
|
|
|
|
# TODO response_model,
|
2020-07-25 19:46:39 +00:00
|
|
|
operation_id='get_hoods',
|
|
|
|
tags=['hoods'],
|
2020-07-25 11:38:10 +00:00
|
|
|
)
|
2020-07-01 19:34:16 +00:00
|
|
|
async def hood_read_all():
|
2020-07-11 10:54:07 +00:00
|
|
|
""" Get all existing hoods. """
|
2020-07-01 19:34:16 +00:00
|
|
|
return await Hood.objects.all()
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.post(
|
|
|
|
'/',
|
|
|
|
status_code=status.HTTP_201_CREATED,
|
|
|
|
# TODO response_model,
|
|
|
|
operation_id='create_hood',
|
2020-07-25 19:46:39 +00:00
|
|
|
tags=['hoods'],
|
2020-07-25 11:38:10 +00:00
|
|
|
)
|
2020-07-02 12:31:53 +00:00
|
|
|
async def hood_create(values: BodyHood, response: Response, admin=Depends(get_admin)):
|
2020-09-04 23:39:24 +00:00
|
|
|
"""Creates a hood.
|
2020-07-11 10:54:07 +00:00
|
|
|
|
|
|
|
- **name**: Name of the hood
|
|
|
|
- **landingpage**: Markdown formatted description of the hood
|
|
|
|
"""
|
2020-07-01 19:34:16 +00:00
|
|
|
try:
|
|
|
|
hood = await Hood.objects.create(**values.__dict__)
|
|
|
|
await AdminHoodRelation.objects.create(admin=admin.id, hood=hood.id)
|
2020-07-18 11:53:11 +00:00
|
|
|
spawner.start(hood)
|
2020-09-08 11:02:07 +00:00
|
|
|
|
|
|
|
# Initialize Triggers to match all
|
|
|
|
await Trigger.objects.create(hood=hood, pattern='.')
|
|
|
|
|
2020-07-01 19:34:16 +00:00
|
|
|
response.headers['Location'] = '%d' % hood.id
|
|
|
|
return hood
|
|
|
|
except IntegrityError:
|
|
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.get(
|
|
|
|
'/{hood_id}',
|
|
|
|
# TODO response_model,
|
|
|
|
operation_id='get_hood',
|
2020-07-25 19:46:39 +00:00
|
|
|
tags=['hoods'],
|
2020-07-25 11:38:10 +00:00
|
|
|
)
|
2020-08-18 21:44:13 +00:00
|
|
|
async def hood_read(hood=Depends(get_hood_unauthorized)):
|
2020-07-11 10:54:07 +00:00
|
|
|
""" Get hood with id **hood_id**. """
|
2020-07-01 19:34:16 +00:00
|
|
|
return hood
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.put(
|
2020-07-25 19:46:39 +00:00
|
|
|
'/{hood_id}',
|
|
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
|
|
operation_id='update_hood',
|
|
|
|
tags=['hoods'],
|
2020-07-25 11:38:10 +00:00
|
|
|
)
|
2020-07-01 19:34:16 +00:00
|
|
|
async def hood_update(values: BodyHood, hood=Depends(get_hood)):
|
2020-09-04 23:39:24 +00:00
|
|
|
"""Updates hood with id **hood_id**.
|
2020-07-11 10:54:07 +00:00
|
|
|
|
|
|
|
- **name**: New name of the hood
|
|
|
|
- **landingpage**: New Markdown formatted description of the hood
|
|
|
|
"""
|
2020-07-01 19:34:16 +00:00
|
|
|
await hood.update(**values.__dict__)
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.delete(
|
2020-07-25 19:46:39 +00:00
|
|
|
'/{hood_id}',
|
|
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
|
|
operation_id='delete_hood',
|
|
|
|
tags=['hoods'],
|
2020-07-25 11:38:10 +00:00
|
|
|
)
|
2020-07-01 19:34:16 +00:00
|
|
|
async def hood_delete(hood=Depends(get_hood)):
|
2020-07-11 10:54:07 +00:00
|
|
|
""" Deletes hood with id **hood_id**. """
|
2020-09-06 17:11:18 +00:00
|
|
|
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()
|
2020-07-01 19:34:16 +00:00
|
|
|
for relation in await AdminHoodRelation.objects.filter(hood=hood).all():
|
|
|
|
await relation.delete()
|
|
|
|
await hood.delete()
|