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 badwords.
|
|
|
|
|
|
|
|
Provides API endpoints for adding, removing and reading regular expressions that block a
|
|
|
|
received message to be dropped by a censor. This provides a message filter customizable
|
|
|
|
by the hood admins.
|
|
|
|
"""
|
|
|
|
|
2020-07-01 19:34:16 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Response, status
|
|
|
|
from kibicara.model import BadWord
|
|
|
|
from kibicara.webapi.hoods import get_hood
|
|
|
|
from ormantic.exceptions import NoMatch
|
|
|
|
from pydantic import BaseModel
|
2020-07-06 08:58:01 +00:00
|
|
|
from re import compile as regex_compile, error as RegexError
|
2020-07-01 19:34:16 +00:00
|
|
|
from sqlite3 import IntegrityError
|
|
|
|
|
|
|
|
|
|
|
|
class BodyBadWord(BaseModel):
|
|
|
|
pattern: str
|
|
|
|
|
|
|
|
|
|
|
|
async def get_badword(badword_id: int, hood=Depends(get_hood)):
|
|
|
|
try:
|
|
|
|
return await BadWord.objects.get(id=badword_id, hood=hood)
|
|
|
|
except NoMatch:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.get(
|
|
|
|
'/',
|
|
|
|
# TODO response_model,
|
|
|
|
operation_id='get_badwords',
|
|
|
|
)
|
2020-07-01 19:34:16 +00:00
|
|
|
async def badword_read_all(hood=Depends(get_hood)):
|
2020-07-11 10:54:07 +00:00
|
|
|
""" Get all badwords of hood with id **hood_id**. """
|
2020-07-01 19:34:16 +00:00
|
|
|
return await BadWord.objects.filter(hood=hood).all()
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.post(
|
|
|
|
'/',
|
|
|
|
status_code=status.HTTP_201_CREATED,
|
|
|
|
# TODO response_model,
|
|
|
|
operation_id='create_badword',
|
|
|
|
)
|
2020-07-01 19:34:16 +00:00
|
|
|
async def badword_create(
|
2020-07-02 12:31:53 +00:00
|
|
|
values: BodyBadWord, response: Response, hood=Depends(get_hood)
|
|
|
|
):
|
2020-07-11 10:54:07 +00:00
|
|
|
""" Creates a new badword for hood with id **hood_id**.
|
|
|
|
|
|
|
|
- **pattern**: Regular expression which is used to match a badword.
|
|
|
|
"""
|
2020-07-01 19:34:16 +00:00
|
|
|
try:
|
2020-07-06 08:58:01 +00:00
|
|
|
regex_compile(values.pattern)
|
2020-07-01 19:34:16 +00:00
|
|
|
badword = await BadWord.objects.create(hood=hood, **values.__dict__)
|
|
|
|
response.headers['Location'] = '%d' % badword.id
|
|
|
|
return badword
|
|
|
|
except IntegrityError:
|
|
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
2020-07-06 08:58:01 +00:00
|
|
|
except RegexError:
|
|
|
|
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
2020-07-01 19:34:16 +00:00
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.get(
|
|
|
|
'/{badword_id}',
|
|
|
|
# TODO response_model,
|
|
|
|
operation_id='get_badword',
|
|
|
|
)
|
2020-07-01 19:34:16 +00:00
|
|
|
async def badword_read(badword=Depends(get_badword)):
|
2020-07-11 10:54:07 +00:00
|
|
|
""" Reads badword with id **badword_id** for hood with id **hood_id**. """
|
2020-07-01 19:34:16 +00:00
|
|
|
return badword
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.put(
|
|
|
|
'/{badword_id}',
|
|
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
|
|
operation_id='update_badword',
|
|
|
|
)
|
2020-07-01 19:34:16 +00:00
|
|
|
async def badword_update(values: BodyBadWord, badword=Depends(get_badword)):
|
2020-07-11 10:54:07 +00:00
|
|
|
""" Updates badword with id **badword_id** for hood with id **hood_id**.
|
|
|
|
|
|
|
|
- **pattern**: Regular expression which is used to match a badword
|
|
|
|
"""
|
2020-07-01 19:34:16 +00:00
|
|
|
await badword.update(**values.__dict__)
|
|
|
|
|
|
|
|
|
2020-07-25 11:38:10 +00:00
|
|
|
@router.delete(
|
|
|
|
'/{badword_id}',
|
|
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
|
|
operation_id='delete_badword',
|
|
|
|
)
|
2020-07-01 19:34:16 +00:00
|
|
|
async def badword_delete(badword=Depends(get_badword)):
|
2020-07-11 10:54:07 +00:00
|
|
|
""" Deletes badword with id **badword_id** for hood with id **hood_id**. """
|
2020-07-01 19:34:16 +00:00
|
|
|
await badword.delete()
|