14 lines
808 B
Python
14 lines
808 B
Python
import requests
|
|
from .models import NotificationPreference
|
|
def send_ntfy(url,message):
|
|
try:
|
|
response=requests.post(url,data=message.encode(),timeout=5);response.raise_for_status();return True
|
|
except requests.RequestException: return False
|
|
def notify_event(event):
|
|
if event.source=='sensor' and event.sensor: users=[event.sensor.owner_id]
|
|
elif event.source=='manual': users=list(NotificationPreference.objects.filter(ntfy_enabled=True,notify_on_manual_submission=True).values_list('user_id',flat=True))
|
|
else: users=[]
|
|
for pref in NotificationPreference.objects.filter(user_id__in=users,ntfy_enabled=True).exclude(ntfy_url=''):
|
|
if event.source=='sensor' and not pref.notify_on_sensor_submission: continue
|
|
send_ntfy(pref.ntfy_url,f'BoomCast: {event.get_source_display()} #{event.pk} awaits review')
|