-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
refactor[wip]: Implement review comments for #1751
1 parent
d13bed2
commit 4d3e8de
Showing
66 changed files
with
604 additions
and
673 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -330,25 +330,30 @@ class PrometheusConfig(BaseConfig): | |
) | ||
|
||
|
||
class SmtpConfig(BaseConfig): | ||
class SMTPConfig(BaseConfig): | ||
enabled: bool = pydantic.Field( | ||
default=False, | ||
default=True, | ||
description="Whether to enable SMTP. Necessary for feedback.", | ||
examples=[True, False], | ||
) | ||
host: str = pydantic.Field( | ||
description="The SMTP server host.", | ||
default="localhost:587", | ||
examples=["smtp.example.com:587"], | ||
pattern=r"^(.*):(\d+)$", | ||
) | ||
user: str = pydantic.Field( | ||
description="The SMTP server user.", examples=["username"] | ||
default="username", | ||
description="The SMTP server user.", | ||
examples=["username"], | ||
) | ||
password: str = pydantic.Field( | ||
default="password", | ||
description="The SMTP server password.", | ||
examples=["password"], | ||
) | ||
sender: str = pydantic.Field( | ||
default="[email protected]", | ||
description="The sender email address.", | ||
examples=["[email protected]"], | ||
) | ||
|
@@ -366,4 +371,4 @@ class AppConfig(BaseConfig): | |
logging: LoggingConfig = LoggingConfig() | ||
requests: RequestsConfig = RequestsConfig() | ||
pipelines: PipelineConfig = PipelineConfig() | ||
smtp: t.Optional[SmtpConfig] = None | ||
smtp: SMTPConfig | None = SMTPConfig() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from fastapi import status | ||
|
||
from capellacollab.core import exceptions as core_exceptions | ||
|
||
|
||
class SMTPNotConfiguredError(core_exceptions.BaseError): | ||
def __init__(self): | ||
super().__init__( | ||
status_code=status.HTTP_403_FORBIDDEN, | ||
title="SMTP is not configured", | ||
reason="SMTP must be configured in the application configuration before sending emails and activating related features.", | ||
err_code="SMTP_NOT_CONFIGURED", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from capellacollab.core import pydantic as core_pydantic | ||
|
||
|
||
class EMailContent(core_pydantic.BaseModelStrict): | ||
subject: str | ||
message: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import logging | ||
import smtplib | ||
from email.mime import multipart, text | ||
|
||
import pydantic | ||
|
||
from capellacollab.config import config | ||
|
||
from . import exceptions, models | ||
|
||
|
||
def send_email( | ||
recipients: list[pydantic.EmailStr], | ||
email: models.EMailContent, | ||
logger: logging.LoggerAdapter, | ||
): | ||
if not (config.smtp and config.smtp.enabled): | ||
raise exceptions.SMTPNotConfiguredError() | ||
|
||
try: | ||
with smtplib.SMTP( | ||
config.smtp.host.split(":")[0], int(config.smtp.host.split(":")[1]) | ||
) as smtp: | ||
smtp.ehlo() | ||
smtp.starttls() | ||
smtp.ehlo() | ||
smtp.login(config.smtp.user, config.smtp.password) | ||
|
||
logger.info( | ||
"Sending emails to recipients %s", ", ".join(recipients) | ||
) | ||
|
||
for recipient in recipients: | ||
msg = multipart.MIMEMultipart() | ||
msg["From"] = config.smtp.sender | ||
msg["To"] = recipient | ||
msg["Subject"] = email.subject | ||
msg.attach(text.MIMEText(email.message, "plain")) | ||
|
||
logger.info( | ||
"Sending email to '%s' with subject '%s'", | ||
recipient, | ||
email.subject, | ||
) | ||
|
||
smtp.sendmail(config.smtp.sender, recipient, msg.as_string()) | ||
except Exception: | ||
logger.exception("Error while sending email(s).") | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.