Skip to content

Commit

Permalink
use system email where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
KlemenSpruk committed Oct 16, 2023
1 parent ee06707 commit b54b685
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 44 deletions.
7 changes: 2 additions & 5 deletions django_project_base/account/rest/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']}. "
Expand Down
26 changes: 13 additions & 13 deletions django_project_base/base/event.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import copy
import datetime
from abc import ABC, abstractmethod
from gettext import gettext

import swapper
from django.conf import settings
from django.shortcuts import get_object_or_404
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:
Expand Down Expand Up @@ -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):
Expand Down
28 changes: 15 additions & 13 deletions django_project_base/management/commands/confirm_setting.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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"
32 changes: 19 additions & 13 deletions django_project_base/management/commands/list_pending_settings.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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))
20 changes: 20 additions & 0 deletions django_project_base/notifications/email_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
kwargs.get("project"),
kwargs.get("user"),
level=kwargs.get("level"),
locale=kwargs.get("locale"),
type=kwargs.get("type"),
is_system_notification=True,
**kwargs,
)

0 comments on commit b54b685

Please sign in to comment.