allow enabling/disabling of notifications

This commit is contained in:
Mark
2025-04-09 11:14:11 +02:00
parent d0889d26ca
commit d5fdb39a60
3 changed files with 70 additions and 39 deletions

View File

@@ -57,6 +57,8 @@ async def send_all_notifications(
select(Notification).where(Notification.event == event_type)
).all()
for notification in notifications:
if not notification.enabled:
continue
await send_notification(
session=session,
notification=notification,

View File

@@ -461,6 +461,24 @@ def read_notifications(
)
def _list_notifications(request: Request, session: Session, admin_user: DetailedUser):
notifications = session.exec(select(Notification)).all()
event_types = [e.value for e in EventEnum]
notifications = session.exec(select(Notification)).all()
event_types = [e.value for e in EventEnum]
return template_response(
"settings_page/notifications.html",
request,
admin_user,
{
"page": "notifications",
"notifications": notifications,
"event_types": event_types,
},
block_name="notfications_block",
)
def _upsert_notification(
request: Request,
name: str,
@@ -514,19 +532,7 @@ def _upsert_notification(
session.add(notification)
session.commit()
notifications = session.exec(select(Notification)).all()
event_types = [e.value for e in EventEnum]
return template_response(
"settings_page/notifications.html",
request,
admin_user,
{
"page": "notifications",
"notifications": notifications,
"event_types": event_types,
},
block_name="notfications_block",
)
return _list_notifications(request, session, admin_user)
@router.post("/notification")
@@ -585,6 +591,25 @@ def update_notification(
)
@router.patch("/notification/{notification_id}/enable")
def toggle_notification(
request: Request,
notification_id: uuid.UUID,
admin_user: Annotated[
DetailedUser, Depends(get_authenticated_user(GroupEnum.admin))
],
session: Annotated[Session, Depends(get_session)],
):
notification = session.get_one(Notification, notification_id)
if not notification:
raise ToastException("Notification not found", "error")
notification.enabled = not notification.enabled
session.add(notification)
session.commit()
return _list_notifications(request, session, admin_user)
@router.delete("/notification/{notification_id}")
def delete_notification(
request: Request,
@@ -594,25 +619,17 @@ def delete_notification(
],
session: Annotated[Session, Depends(get_session)],
):
notifications = session.exec(select(Notification)).all()
for notif in notifications:
if notif.id == notification_id:
session.delete(notif)
session.commit()
break
notifications = session.exec(select(Notification)).all()
notification = session.get_one(Notification, notification_id)
if not notification:
raise ToastException("Notification not found", "error")
session.delete(notification)
session.commit()
return template_response(
"settings_page/notifications.html",
request,
admin_user,
{"page": "notifications", "notifications": notifications},
block_name="notfications_block",
)
return _list_notifications(request, session, admin_user)
@router.post("/notification/{notification_id}")
async def execute_notification(
async def test_notification(
notification_id: uuid.UUID,
admin_user: Annotated[
DetailedUser, Depends(get_authenticated_user(GroupEnum.admin))

View File

@@ -124,17 +124,7 @@
<td>{{ n.name }}</td>
<td>{{ n.event.value }}</td>
<td>{{ n.apprise_url }}</td>
<td class="flex gap-1">
<button
title="Delete"
class="btn btn-error btn-square"
hx-delete="/settings/notification/{{n.id}}"
hx-target="#notification-list"
hx-swap="outerHTML"
hx-confirm="Are you sure you want to delete this notification?"
>
{% include 'icons/trash.html' %}
</button>
<td class="grid grid-cols-2 min-w-[8rem] gap-1">
<button
title="Test"
class="btn btn-square"
@@ -150,6 +140,28 @@
>
{% include 'icons/pencil.html' %}
</button>
<button
title="{{ 'Enabled' if n.enabled else 'Disabled' }}"
class="btn btn-square {{ 'btn-success' if n.enabled else 'btn-error' }}"
hx-patch="/settings/notification/{{n.id}}/enable"
hx-disabled-elt="this"
hx-target="#notification-list"
hx-swap="outerHTML"
>
{% if n.enabled %}
<span>{% include 'icons/checkmark.html' %}</span> {% else %}
<span>{% include 'icons/xmark.html' %}</span> {% endif %}
</button>
<button
title="Delete"
class="btn btn-error btn-square"
hx-delete="/settings/notification/{{n.id}}"
hx-target="#notification-list"
hx-swap="outerHTML"
hx-confirm="Are you sure you want to delete this notification? ({{n.name}})"
>
{% include 'icons/trash.html' %}
</button>
</td>
</tr>
{% endfor %}