-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: send notifications when an alert is received (#272)
* feat: send notifications when an alert is received - send and log a notification message via email (or another type, to be implemented) to the recipients subscribed to a group - add tables and routes: notifications and recipients - allow python >= 3.8, <4 ; update poetry.lock * add subject ; not working * fix: pydantic validators and code quality * fix: reinstate postgresql extra for dependency `databases` * test: add library stubs to mypy test in CI * fix: headers * fix: quack suggestions * telegram instead of email * refactor: change return value of create_event_if_inexistant * refactor: models, send_telegram_msg, tests * fix: header * fix header ; add test ; CI: fix ruff version, add telegram secrets * fix: remove sleep and untested line * refactor send_telegram_msg to remove untested line * doc: improve / fix docstring --------- Co-authored-by: Bruno Lenzi <[email protected]>
- Loading branch information
blenzi
and
Bruno Lenzi
authored
Aug 7, 2023
1 parent
c10ff1c
commit fa2624d
Showing
26 changed files
with
932 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Copyright (C) 2020-2023, Pyronear. | ||
|
||
# This program is licensed under the Apache License 2.0. | ||
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details. | ||
|
||
from typing import List | ||
|
||
from fastapi import APIRouter, HTTPException, Path, Security, status | ||
|
||
from app.api import crud | ||
from app.api.deps import get_current_access | ||
from app.api.endpoints.recipients import get_recipient | ||
from app.db import notifications | ||
from app.models import AccessType, NotificationType | ||
from app.schemas import NotificationIn, NotificationOut, RecipientOut | ||
from app.services import send_telegram_msg | ||
|
||
router = APIRouter(dependencies=[Security(get_current_access, scopes=[AccessType.admin])]) | ||
|
||
|
||
@router.post( | ||
"/", response_model=NotificationOut, status_code=status.HTTP_201_CREATED, summary="Send and log notification" | ||
) | ||
async def send_notification(payload: NotificationIn): | ||
""" | ||
Send a notification to the recipients of the same group as the device that issued the alert; log notification to db | ||
Below, click on "Schema" for more detailed information about arguments | ||
or "Example Value" to get a concrete idea of arguments | ||
""" | ||
recipient = RecipientOut(**(await get_recipient(recipient_id=payload.recipient_id))) | ||
if recipient.notification_type == NotificationType.telegram: | ||
send_telegram_msg(recipient.address, payload.message) | ||
else: | ||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid NotificationType, not treated") | ||
|
||
notification: NotificationOut = NotificationOut(**(await crud.create_entry(notifications, payload))) | ||
return notification | ||
|
||
|
||
@router.get( | ||
"/{notification_id}/", response_model=NotificationOut, summary="Get information about a specific notification" | ||
) | ||
async def get_notification(notification_id: int = Path(..., gt=0)): | ||
""" | ||
Retrieve information about the notification with the given notification_id | ||
""" | ||
return await crud.get_entry(notifications, notification_id) | ||
|
||
|
||
@router.get("/", response_model=List[NotificationOut], summary="Get the list of all notifications") | ||
async def fetch_notifications(): | ||
""" | ||
Retrieves the list of all notifications and their information | ||
""" | ||
return await crud.fetch_all(notifications) | ||
|
||
|
||
@router.delete("/{notification_id}/", response_model=NotificationOut, summary="Delete a specific notification") | ||
async def delete_notification(notification_id: int = Path(..., gt=0)): | ||
""" | ||
Based on a notification_id, deletes the specified notification from the db | ||
""" | ||
return await crud.delete_entry(notifications, notification_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright (C) 2020-2023, Pyronear. | ||
|
||
# This program is licensed under the Apache License 2.0. | ||
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details. | ||
|
||
from typing import List | ||
|
||
from fastapi import APIRouter, Security, status | ||
from pydantic import PositiveInt | ||
|
||
from app.api import crud | ||
from app.api.deps import get_current_access | ||
from app.db import recipients | ||
from app.models import AccessType | ||
from app.schemas import RecipientIn, RecipientOut | ||
|
||
router = APIRouter(dependencies=[Security(get_current_access, scopes=[AccessType.admin])]) | ||
|
||
|
||
@router.post("/", response_model=RecipientOut, status_code=status.HTTP_201_CREATED, summary="Create a new recipient") | ||
async def create_recipient( | ||
payload: RecipientIn, | ||
): | ||
""" | ||
Creates a new recipient based on the given information | ||
Below, click on "Schema" for more detailed information about arguments | ||
or "Example Value" to get a concrete idea of arguments | ||
""" | ||
return await crud.create_entry(recipients, payload) | ||
|
||
|
||
@router.get("/{recipient_id}/", response_model=RecipientOut, summary="Get information about a specific recipient") | ||
async def get_recipient(recipient_id: PositiveInt): | ||
""" | ||
Retrieve information about the recipient with the given recipient_id | ||
""" | ||
return await crud.get_entry(recipients, recipient_id) | ||
|
||
|
||
@router.get("/", response_model=List[RecipientOut], summary="Get the list of all recipients") | ||
async def fetch_recipients(): | ||
""" | ||
Retrieves the list of all recipients and their information | ||
""" | ||
return await crud.fetch_all(recipients) | ||
|
||
|
||
@router.get( | ||
"/group-recipients/{group_id}/", | ||
response_model=List[RecipientOut], | ||
summary="Get the list of all recipients for the given group", | ||
) | ||
async def fetch_recipients_for_group(group_id: PositiveInt): | ||
""" | ||
Retrieves the list of all recipients for the given group and their information | ||
""" | ||
return await crud.fetch_all(recipients, {"group_id": group_id}) | ||
|
||
|
||
@router.put("/{recipient_id}/", response_model=RecipientOut, summary="Update information about a specific recipient") | ||
async def update_recipient(payload: RecipientIn, recipient_id: PositiveInt): | ||
""" | ||
Given its ID, updates information about the specified recipient | ||
""" | ||
return await crud.update_entry(recipients, payload, recipient_id) | ||
|
||
|
||
@router.delete("/{recipient_id}/", response_model=RecipientOut, summary="Delete a specific recipient") | ||
async def delete_recipient(recipient_id: PositiveInt): | ||
""" | ||
Based on a recipient_id, deletes the specified recipient | ||
""" | ||
return await crud.delete_entry(recipients, recipient_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.