Module kibicara.webapi.hoods.triggers

REST API endpoints for managing triggers.

Provides API endpoints for adding, removing and reading regular expressions that allow a message to be passed through by a censor. A published message must match one of these regular expressions otherwise it gets dropped by the censor. This provides a message filter customizable by the hood admins.

Expand source code
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
#
# SPDX-License-Identifier: 0BSD

""" REST API endpoints for managing triggers.

Provides API endpoints for adding, removing and reading regular expressions that allow a
message to be passed through by a censor. A published message must match one of these
regular expressions otherwise it gets dropped by the censor. This provides a message
filter customizable by the hood admins.
"""

from fastapi import APIRouter, Depends, HTTPException, Response, status
from kibicara.model import Trigger
from kibicara.webapi.hoods import get_hood
from ormantic.exceptions import NoMatch
from pydantic import BaseModel
from re import compile as regex_compile, error as RegexError
from sqlite3 import IntegrityError


class BodyTrigger(BaseModel):
    pattern: str


async def get_trigger(trigger_id: int, hood=Depends(get_hood)):
    try:
        return await Trigger.objects.get(id=trigger_id, hood=hood)
    except NoMatch:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)


router = APIRouter()


@router.get(
    '/',
    # TODO response_model,
    operation_id='get_triggers',
)
async def trigger_read_all(hood=Depends(get_hood)):
    """ Get all triggers of hood with id **hood_id**. """
    return await Trigger.objects.filter(hood=hood).all()


@router.post(
    '/',
    status_code=status.HTTP_201_CREATED,
    # TODO response_model,
    operation_id='create_trigger',
)
async def trigger_create(
    values: BodyTrigger, response: Response, hood=Depends(get_hood)
):
    """Creates a new trigger for hood with id **hood_id**.

    - **pattern**: Regular expression which is used to match a trigger.
    """
    try:
        regex_compile(values.pattern)
        trigger = await Trigger.objects.create(hood=hood, **values.__dict__)
        response.headers['Location'] = '%d' % trigger.id
        return trigger
    except IntegrityError:
        raise HTTPException(status_code=status.HTTP_409_CONFLICT)
    except RegexError:
        raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)


@router.get(
    '/{trigger_id}',
    # TODO response_model,
    operation_id='get_trigger',
)
async def trigger_read(trigger=Depends(get_trigger)):
    """ Reads trigger with id **trigger_id** for hood with id **hood_id**. """
    return trigger


@router.put(
    '/{trigger_id}',
    status_code=status.HTTP_204_NO_CONTENT,
    operation_id='update_trigger',
)
async def trigger_update(values: BodyTrigger, trigger=Depends(get_trigger)):
    """Updates trigger with id **trigger_id** for hood with id **hood_id**.

    - **pattern**: Regular expression which is used to match a trigger
    """
    await trigger.update(**values.__dict__)
    return Response(status_code=status.HTTP_204_NO_CONTENT)


@router.delete(
    '/{trigger_id}',
    status_code=status.HTTP_204_NO_CONTENT,
    operation_id='delete_trigger',
)
async def trigger_delete(trigger=Depends(get_trigger)):
    """ Deletes trigger with id **trigger_id** for hood with id **hood_id**. """
    await trigger.delete()
    return Response(status_code=status.HTTP_204_NO_CONTENT)

Functions

async def get_trigger(trigger_id: int, hood=Depends(get_hood))
Expand source code
async def get_trigger(trigger_id: int, hood=Depends(get_hood)):
    try:
        return await Trigger.objects.get(id=trigger_id, hood=hood)
    except NoMatch:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
async def trigger_create(values: BodyTrigger, response: starlette.responses.Response, hood=Depends(get_hood))

Creates a new trigger for hood with id hood_id.

  • pattern: Regular expression which is used to match a trigger.
Expand source code
@router.post(
    '/',
    status_code=status.HTTP_201_CREATED,
    # TODO response_model,
    operation_id='create_trigger',
)
async def trigger_create(
    values: BodyTrigger, response: Response, hood=Depends(get_hood)
):
    """Creates a new trigger for hood with id **hood_id**.

    - **pattern**: Regular expression which is used to match a trigger.
    """
    try:
        regex_compile(values.pattern)
        trigger = await Trigger.objects.create(hood=hood, **values.__dict__)
        response.headers['Location'] = '%d' % trigger.id
        return trigger
    except IntegrityError:
        raise HTTPException(status_code=status.HTTP_409_CONFLICT)
    except RegexError:
        raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
async def trigger_delete(trigger=Depends(get_trigger))

Deletes trigger with id trigger_id for hood with id hood_id.

Expand source code
@router.delete(
    '/{trigger_id}',
    status_code=status.HTTP_204_NO_CONTENT,
    operation_id='delete_trigger',
)
async def trigger_delete(trigger=Depends(get_trigger)):
    """ Deletes trigger with id **trigger_id** for hood with id **hood_id**. """
    await trigger.delete()
    return Response(status_code=status.HTTP_204_NO_CONTENT)
async def trigger_read(trigger=Depends(get_trigger))

Reads trigger with id trigger_id for hood with id hood_id.

Expand source code
@router.get(
    '/{trigger_id}',
    # TODO response_model,
    operation_id='get_trigger',
)
async def trigger_read(trigger=Depends(get_trigger)):
    """ Reads trigger with id **trigger_id** for hood with id **hood_id**. """
    return trigger
async def trigger_read_all(hood=Depends(get_hood))

Get all triggers of hood with id hood_id.

Expand source code
@router.get(
    '/',
    # TODO response_model,
    operation_id='get_triggers',
)
async def trigger_read_all(hood=Depends(get_hood)):
    """ Get all triggers of hood with id **hood_id**. """
    return await Trigger.objects.filter(hood=hood).all()
async def trigger_update(values: BodyTrigger, trigger=Depends(get_trigger))

Updates trigger with id trigger_id for hood with id hood_id.

  • pattern: Regular expression which is used to match a trigger
Expand source code
@router.put(
    '/{trigger_id}',
    status_code=status.HTTP_204_NO_CONTENT,
    operation_id='update_trigger',
)
async def trigger_update(values: BodyTrigger, trigger=Depends(get_trigger)):
    """Updates trigger with id **trigger_id** for hood with id **hood_id**.

    - **pattern**: Regular expression which is used to match a trigger
    """
    await trigger.update(**values.__dict__)
    return Response(status_code=status.HTTP_204_NO_CONTENT)

Classes

class BodyTrigger (**data: Any)

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class BodyTrigger(BaseModel):
    pattern: str

Ancestors

  • pydantic.main.BaseModel
  • pydantic.utils.Representation

Class variables

var pattern : str