-
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.
refactor: Rewrite some parts of the feedback feature
- Add SMTP mock for local development (in cluster and in dev-mode) - Change to newer Python syntax, e.g. `t.Optional` -> `| None` - Outsource email sending to it's own module, it will be relevant for other parts as well - Import modules and not classes or functions directly. - Remove the anonoymity policy and feedback percentage for simplicity and determinism. - Auto-disable feedback if SMTP is not set, decline enabling with disabled SMTP - Decline enabling feedback with empty list of recipients - Reduce the verbosity of the AnonymizedSession. It did include the whole tool config before. - Rename receivers to recipients. - Add more Stories and pytests. - Add storage limits to all containers (suggested by SonarCloud) - Change some error codes from Server errors to Client errors - Disable footer feedback button if not logged in.
- Loading branch information
1 parent
d13bed2
commit d302c11
Showing
99 changed files
with
1,127 additions
and
836 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
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.