diff --git a/aleksis/core/models.py b/aleksis/core/models.py index 0e372436cba183cfe6ebb67e8e1f8e436d95215e..9d6e45d935394eb00dcdedf9213966203f1d981f 100644 --- a/aleksis/core/models.py +++ b/aleksis/core/models.py @@ -14,8 +14,8 @@ from phonenumber_field.modelfields import PhoneNumberField from polymorphic.models import PolymorphicModel from .mixins import ExtensibleModel, PureDjangoModel +from .tasks import send_notification from .util.core_helpers import now_tomorrow -from .util.notifications import send_notification from .util.model_helpers import ICONS from constance import config @@ -276,7 +276,8 @@ class Notification(ExtensibleModel): return str(self.title) def save(self, **kwargs): - send_notification(self) + if not self.sent: + send_notification(self.pk, resend=True) self.sent = True super().save(**kwargs) diff --git a/aleksis/core/settings.py b/aleksis/core/settings.py index 70cbc0fbf78ae902f871eeef861b02bb4a39d6f4..aaac65562d98c9aa707a75a3b316e7ede9a1d5c0 100644 --- a/aleksis/core/settings.py +++ b/aleksis/core/settings.py @@ -436,7 +436,7 @@ if _settings.get("twilio.sid", None): if _settings.get("celery.enabled", False): INSTALLED_APPS += ("django_celery_beat", "django_celery_results") - CELERY_BROKER_URL = "redis://localhost" + CELERY_BROKER_URL = _settings.get("celery.broker", "redis://localhost") CELERY_RESULT_BACKEND = "django-db" CELERY_CACHE_BACKEND = "django-cache" CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler" diff --git a/aleksis/core/tasks.py b/aleksis/core/tasks.py new file mode 100644 index 0000000000000000000000000000000000000000..a59f829e817cfbd036fca3afc215eef2c9a5281d --- /dev/null +++ b/aleksis/core/tasks.py @@ -0,0 +1,7 @@ +from .util.core_helpers import celery_optional +from .util.notifications import send_notification as _send_notification + + +@celery_optional +def send_notification(notification: int, resend: bool = False) -> None: + _send_notification(notification, resend) diff --git a/aleksis/core/util/core_helpers.py b/aleksis/core/util/core_helpers.py index 67e9fe71ba5d038b9da729430a3e37bc837bc4c2..9f91154c30913ae14eff1332b5f6bea3207bfa86 100644 --- a/aleksis/core/util/core_helpers.py +++ b/aleksis/core/util/core_helpers.py @@ -116,11 +116,12 @@ def celery_optional(orig: Callable) -> Callable: and it is executed synchronously. """ + if hasattr(settings, "CELERY_RESULT_BACKEND"): + from ..celery import app # noqa + task = app.task(orig) + def wrapped(*args, **kwargs): if hasattr(settings, "CELERY_RESULT_BACKEND"): - from ..celery import app # noqa - task = app.task(orig) - task.delay(*args, **kwargs) else: orig(*args, **kwargs) diff --git a/aleksis/core/util/notifications.py b/aleksis/core/util/notifications.py index 5f6f8fded9a00323b2695e27268d4d4a744c0525..7f309d1cc2e0a0a046334683f5e2100f28fdae2c 100644 --- a/aleksis/core/util/notifications.py +++ b/aleksis/core/util/notifications.py @@ -68,7 +68,6 @@ _CHANNELS_MAP = { } -@celery_optional def send_notification(notification: Union[int, "Notification"], resend: bool = False) -> None: """ Send a notification through enabled channels.