Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions src/django_errors/mail_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,46 @@ def _read_env_file(path: str | Path) -> dict[str, str]:

def identity_tag(
*,
scope: Optional[str] = None,
role: Optional[str] = None,
hostname: Optional[str] = None,
install_tag: Optional[str] = None,
env_file: str | Path = _DEFAULT_ENV_FILE,
environ: Optional[Mapping[str, str]] = None,
scope: str | None = None,
role: str | None = None,
hostname: str | None = None,
install_tag: str | None = None,
env_file: str | Path | None = None,
environ: Mapping[str, str] | None = None,
) -> str:
"""Return the compact identity tag for this host."""
env = environ if environ is not None else os.environ
file_vars = _read_env_file(env_file)
# Resolve default at call time so tests can monkeypatch ``_DEFAULT_ENV_FILE``.
file_vars = _read_env_file(
_DEFAULT_ENV_FILE if env_file is None else env_file
)

cached = (env.get("SH_MAIL_IDENTITY_TAG") or file_vars.get("SH_MAIL_IDENTITY_TAG") or "").strip()
if cached and scope is None and role is None and hostname is None and install_tag is None:
cached = (
env.get("SH_MAIL_IDENTITY_TAG")
or file_vars.get("SH_MAIL_IDENTITY_TAG")
or ""
).strip()
if (
cached
and scope is None
and role is None
and hostname is None
and install_tag is None
):
return cached

scope_v = (scope or env.get("SH_OS_Scope") or file_vars.get("SH_OS_Scope") or "unknown").strip()
role_v = (role or env.get("SH_Host_Role") or file_vars.get("SH_Host_Role") or "unknown").strip()
scope_v = (
scope
or env.get("SH_OS_Scope")
or file_vars.get("SH_OS_Scope")
or "unknown"
).strip()
role_v = (
role
or env.get("SH_Host_Role")
or file_vars.get("SH_Host_Role")
or "unknown"
).strip()
host_v = (
hostname
or env.get("SH_OS_HostName")
Expand All @@ -65,7 +88,11 @@ def identity_tag(
install_v = (
install_tag
if install_tag is not None
else (env.get("SH_Mail_InstallTag") or file_vars.get("SH_Mail_InstallTag") or "")
else (
env.get("SH_Mail_InstallTag")
or file_vars.get("SH_Mail_InstallTag")
or ""
)
).strip()

if scope_v == "PROD" and role_v in _PROD_COMPACT_ROLES:
Expand All @@ -80,16 +107,21 @@ def identity_tag(
def subject_prefix(
kind: str = "app",
*,
environ: Optional[Mapping[str, str]] = None,
env_file: str | Path = _DEFAULT_ENV_FILE,
environ: Mapping[str, str] | None = None,
env_file: str | Path | None = None,
) -> str:
"""Return ``[kind]<IdentityTag>`` with trailing space (Django EMAIL_SUBJECT_PREFIX)."""
env = environ if environ is not None else os.environ
file_vars = _read_env_file(env_file)
explicit = (env.get("EMAIL_SUBJECT_PREFIX") or file_vars.get("EMAIL_SUBJECT_PREFIX") or "").rstrip()
resolved = _DEFAULT_ENV_FILE if env_file is None else env_file
file_vars = _read_env_file(resolved)
explicit = (
env.get("EMAIL_SUBJECT_PREFIX")
or file_vars.get("EMAIL_SUBJECT_PREFIX")
or ""
).rstrip()
if explicit and kind == "app":
return f"{explicit} " if not explicit.endswith(" ") else explicit
return f"[{kind}]{identity_tag(environ=env, env_file=env_file)} "
return f"[{kind}]{identity_tag(environ=env, env_file=resolved)} "


def prefix_subject(subject: str, *, kind: str = "app") -> str:
Expand Down
Loading