diff --git a/django_project_base/account/rest/profile.py b/django_project_base/account/rest/profile.py index c0912807..ba3af1ad 100644 --- a/django_project_base/account/rest/profile.py +++ b/django_project_base/account/rest/profile.py @@ -39,8 +39,8 @@ from django_project_base.base.event import UserRegisteredEvent from django_project_base.constants import NOTIFY_NEW_USER_SETTING_NAME from django_project_base.notifications.email_notification import ( - EMailNotificationWithListOfEmails, SystemEMailNotification, + SystemEMailNotificationWithListOfEmails, ) from django_project_base.notifications.models import DjangoProjectBaseMessage from django_project_base.permissions import BasePermissions @@ -444,10 +444,7 @@ def update_current_profile(self, request: Request, **kwargs) -> Response: code = randrange(100001, 999999) response.set_cookie("verify-email", user.pk, samesite="Lax") request.session[f"email-changed-{code}-{user.pk}"] = new_email - # TODO: Use system email - # TODO: SEND THIS AS SYSTEM MSG WHEN PR IS MERGED - # TODO: https://taiga.velis.si/project/velis-django-project-admin/issue/728 - EMailNotificationWithListOfEmails( + SystemEMailNotificationWithListOfEmails( message=DjangoProjectBaseMessage( subject=f"{_('Email change for account on')} {request.META['HTTP_HOST']}", body=f"{_('You requested an email change for your account at')} {request.META['HTTP_HOST']}. " diff --git a/django_project_base/base/event.py b/django_project_base/base/event.py index ad0b5809..7f4bfd9d 100644 --- a/django_project_base/base/event.py +++ b/django_project_base/base/event.py @@ -1,6 +1,7 @@ import copy import datetime from abc import ABC, abstractmethod +from gettext import gettext import swapper from django.conf import settings @@ -8,6 +9,8 @@ from rest_registration.settings import registration_settings from django_project_base.constants import EMAIL_SENDER_ID_SETTING_NAME, SMS_SENDER_ID_SETTING_NAME +from django_project_base.notifications.email_notification import SystemEMailNotificationWithListOfEmails +from django_project_base.notifications.models import DjangoProjectBaseMessage class UserModel: @@ -181,19 +184,16 @@ def trigger(self, payload=None, **kwargs): if not payload: return - # if to := getattr(settings, "ADMINS", getattr(settings, "MANAGERS", [])): - # # TODO: SEND THIS AS SYSTEM MSG WHEN PR IS MERGED - # EMailNotificationWithListOfEmails( - # message=DjangoProjectBaseMessage( - # subject=gettext"Project settings action required"), - # body=f"{gettext('Action required for setting')} {payload.name} in project {payload.project.name}", - # footer="", - # content_type=DjangoProjectBaseMessage.HTML, - # ), - # recipients=to, - # project=None, - # user=None, - # ).send() + if to := getattr(settings, "ADMINS", getattr(settings, "MANAGERS", [])): + SystemEMailNotificationWithListOfEmails( + message=DjangoProjectBaseMessage( + subject=gettext("Project settings action required"), + body=f"{gettext('Action required for setting')} {payload.name} in project {payload.project.name}", + footer="", + content_type=DjangoProjectBaseMessage.HTML, + ), + recipients=to, + ).send() class ProjectSettingPendingResetEvent(BaseEvent): diff --git a/django_project_base/management/commands/confirm_setting.py b/django_project_base/management/commands/confirm_setting.py index 39683da7..653201d6 100644 --- a/django_project_base/management/commands/confirm_setting.py +++ b/django_project_base/management/commands/confirm_setting.py @@ -1,8 +1,12 @@ +from gettext import gettext + import swapper from django.core.management.base import BaseCommand from django.shortcuts import get_object_or_404 from django_project_base.base.event import ProjectSettingConfirmedEvent +from django_project_base.notifications.email_notification import SystemEMailNotification +from django_project_base.notifications.models import DjangoProjectBaseMessage class Command(BaseCommand): @@ -20,17 +24,15 @@ def handle(self, *args, **options): name=str(options["setting-name"]), ) ProjectSettingConfirmedEvent(user=None).trigger(payload=setting) - # TODO: send email when owner is known - # # TODO: SEND THIS AS SYSTEM MSG WHEN PR IS MERGED - # SystemEMailNotification( - # message=DjangoProjectBaseMessage( - # subject=f"{__('Project setting confirmed')}", - # body=f"{__('Setting')} {setting.name} {__('in project')} " - # f"{project.name} {__('has been confirmed and is now active.')}", - # footer="", - # content_type=DjangoProjectBaseMessage.PLAIN_TEXT, - # ), - # recipients=[], # TODO: find project owner - # user=None, # TODO: find project owner - # ).send() + SystemEMailNotification( + message=DjangoProjectBaseMessage( + subject=f"{gettext('Project setting confirmed')}", + body=f"{gettext('Setting')} {setting.name} {gettext('in project')} " + f"{project.name} {gettext('has been confirmed and is now active.')}", + footer="", + content_type=DjangoProjectBaseMessage.PLAIN_TEXT, + ), + recipients=[], # TODO: find project owner and send + user=None, # TODO: find project owner and send -> add .send() + ) return "ok" diff --git a/django_project_base/management/commands/list_pending_settings.py b/django_project_base/management/commands/list_pending_settings.py index 0325d700..7bb062bb 100644 --- a/django_project_base/management/commands/list_pending_settings.py +++ b/django_project_base/management/commands/list_pending_settings.py @@ -1,6 +1,13 @@ +import json +from gettext import gettext + import swapper +from django.conf import settings from django.core.management.base import BaseCommand +from django_project_base.notifications.email_notification import SystemEMailNotificationWithListOfEmails +from django_project_base.notifications.models import DjangoProjectBaseMessage + class Command(BaseCommand): help = "Lists pending project settings. Example: python manage.py list_pending_settings" @@ -19,17 +26,16 @@ def handle(self, *args, **options): "value": setting.python_value, "pending_value": setting.python_pending_value, } - # if to := getattr(settings, "ADMINS", getattr(settings, "MANAGERS", [])): - # # TODO: SEND THIS AS SYSTEM MSG WHEN PR IS MERGED - # EMailNotificationWithListOfEmails( - # message=DjangoProjectBaseMessage( - # subject=_("Pending settings report"), - # body=json.dumps(result), - # footer="", - # content_type=DjangoProjectBaseMessage.HTML, - # ), - # recipients=to, - # project=None, - # user=None, - # ).send() + + if to := getattr(settings, "ADMINS", getattr(settings, "MANAGERS", [])) and result: + SystemEMailNotificationWithListOfEmails( + message=DjangoProjectBaseMessage( + subject=gettext("Pending settings report"), + body=json.dumps(result), + footer="", + content_type=DjangoProjectBaseMessage.HTML, + ), + recipients=to, + ).send() + self.stdout.write(self.style.WARNING(result)) diff --git a/django_project_base/notifications/email_notification.py b/django_project_base/notifications/email_notification.py index 6f5364e5..85c37777 100644 --- a/django_project_base/notifications/email_notification.py +++ b/django_project_base/notifications/email_notification.py @@ -144,3 +144,23 @@ def send(self) -> DjangoProjectBaseNotification: self._check_request_limit() self._register_system_email() return super().send() + + +class SystemEMailNotificationWithListOfEmails(EMailNotificationWithListOfEmails): + def __init__( + self, + message: DjangoProjectBaseMessage, + recipients, + **kwargs, + ) -> None: + super().__init__( + message, + recipients, + project=kwargs.pop("project", None), + user=kwargs.pop("user", None), + level=kwargs.pop("level", None), + locale=kwargs.pop("locale", None), + type=kwargs.pop("type", None), + is_system_notification=True, + **kwargs, + )