From 50820172b11e5ecb5a351a2ab733012e01ad7432 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sun, 18 Dec 2022 17:49:42 +0000 Subject: [PATCH] Send notification on new user creations --- core/lib/notify.py | 10 ++++------ core/views/base.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/core/lib/notify.py b/core/lib/notify.py index def28b2..8546b51 100644 --- a/core/lib/notify.py +++ b/core/lib/notify.py @@ -8,7 +8,9 @@ log = logs.get_logger(__name__) # Actual function to send a message to a topic -def _sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None): +def raw_sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None): + if url is None: + url = NTFY_URL headers = {"Title": "Fisk"} if title: headers["Title"] = title @@ -26,10 +28,6 @@ def _sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None): # Sendmsg helper to send a message to a user's notification settings def sendmsg(user, *args, **kwargs): notification_settings = user.get_notification_settings() - if notification_settings.ntfy_url is None: - url = NTFY_URL - else: - url = notification_settings.ntfy_url if notification_settings.ntfy_topic is None: # No topic set, so don't send @@ -37,4 +35,4 @@ def sendmsg(user, *args, **kwargs): else: topic = notification_settings.ntfy_topic - _sendmsg(*args, **kwargs, url=url, topic=topic) + raw_sendmsg(*args, **kwargs, url=notification_settings.ntfy_url, topic=topic) diff --git a/core/views/base.py b/core/views/base.py index 3751a31..87e800f 100644 --- a/core/views/base.py +++ b/core/views/base.py @@ -11,6 +11,7 @@ from django.views import View from django.views.generic.edit import CreateView from core.forms import NewUserForm +from core.lib.notify import raw_sendmsg from core.lib.products import assemble_plan_map from core.models import Plan, Session @@ -91,6 +92,16 @@ class Signup(CreateView): success_url = reverse_lazy("two_factor:login") template_name = "registration/signup.html" + def form_valid(self, form): + """If the form is valid, save the associated model.""" + self.object = form.save() + raw_sendmsg( + f"New user signup: {self.object.username} - {self.object.email}", + title="New user", + topic=settings.NOTIFY_TOPIC, + ) + return super().form_valid(form) + def get(self, request, *args, **kwargs): if not settings.REGISTRATION_OPEN: return render(request, "registration/registration_closed.html")