Files

20 lines
874 B
Python

from django.db.models import F
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils import timezone
from members.models import PendingNotificationBatch, UserSettings
from .models import PendingEvent
@receiver(post_save, sender=PendingEvent)
def count_new_pending_event(sender, instance, created, **kwargs):
if not created:
return
minute = timezone.now().replace(second=0, microsecond=0)
field = "manual_count" if instance.source_type == PendingEvent.SourceType.MANUAL else "sensor_count"
for user_id in UserSettings.objects.filter(ntfy_enabled=True).exclude(ntfy_url="").values_list("user_id", flat=True):
batch, _ = PendingNotificationBatch.objects.get_or_create(user_id=user_id, minute=minute)
PendingNotificationBatch.objects.filter(pk=batch.pk).update(**{field: F(field) + 1})