feat(campaign): enforce attachment delivery policies
This commit is contained in:
@@ -404,11 +404,20 @@ class AttachmentsConfig(StrictModel):
|
||||
base_paths: list[AttachmentBasePathConfig] = Field(default_factory=list)
|
||||
allow_individual: bool = False
|
||||
send_without_attachments: bool = True
|
||||
send_without_attachments_behavior: Behavior | None = None
|
||||
zip: ZipCollectionConfig = Field(default_factory=ZipCollectionConfig)
|
||||
global_: list[AttachmentConfig] = Field(default_factory=list, alias="global")
|
||||
missing_behavior: Behavior = Behavior.ASK
|
||||
ambiguous_behavior: Behavior = Behavior.ASK
|
||||
|
||||
@model_validator(mode="after")
|
||||
def normalize_send_without_attachments_behavior(self) -> "AttachmentsConfig":
|
||||
if self.send_without_attachments_behavior is None:
|
||||
self.send_without_attachments_behavior = Behavior.CONTINUE if self.send_without_attachments else Behavior.BLOCK
|
||||
else:
|
||||
self.send_without_attachments = self.send_without_attachments_behavior != Behavior.BLOCK
|
||||
return self
|
||||
|
||||
@property
|
||||
def individual_base_path_values(self) -> set[str]:
|
||||
return {base_path.path for base_path in self.base_paths if base_path.allow_individual}
|
||||
|
||||
@@ -10,6 +10,7 @@ from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from .field_values import ignored_entry_field_overrides
|
||||
from .models import AttachmentConfig, CampaignConfig, EntryConfig, FieldType, SourceType, ZipArchiveConfig, ZipPasswordMode, ZipPasswordScope, ZipRuleMode
|
||||
from ..attachments.resolver import resolve_campaign_attachments
|
||||
|
||||
|
||||
class Severity(StrEnum):
|
||||
@@ -507,6 +508,7 @@ def _entries_source_file_issues(config: CampaignConfig, campaign_path: Path, map
|
||||
def _file_check_issues(config: CampaignConfig, campaign_path: Path) -> list[SemanticIssue]:
|
||||
issues: list[SemanticIssue] = []
|
||||
issues.extend(_attachment_file_check_issues(config, campaign_path))
|
||||
issues.extend(_attachment_resolution_check_issues(config, campaign_path))
|
||||
issues.extend(_template_source_file_issues(config, campaign_path))
|
||||
return issues
|
||||
|
||||
@@ -536,6 +538,37 @@ def _attachment_file_check_issues(config: CampaignConfig, campaign_path: Path) -
|
||||
]
|
||||
|
||||
|
||||
def _attachment_resolution_check_issues(config: CampaignConfig, campaign_path: Path) -> list[SemanticIssue]:
|
||||
try:
|
||||
report = resolve_campaign_attachments(config, campaign_file=campaign_path)
|
||||
except Exception as exc:
|
||||
return [
|
||||
_issue(
|
||||
Severity.ERROR,
|
||||
"attachment_resolution_failed",
|
||||
f"attachment rules could not be resolved: {exc}",
|
||||
"/attachments",
|
||||
)
|
||||
]
|
||||
|
||||
issues: list[SemanticIssue] = []
|
||||
for entry in report.entries:
|
||||
entry_path = f"/entries/{entry.entry_id or entry.entry_index}"
|
||||
for issue in entry.issues:
|
||||
if any(issue is attachment_issue for attachment in entry.attachments for attachment_issue in attachment.issues):
|
||||
continue
|
||||
issues.append(_issue(Severity(issue.severity.value), issue.code, issue.message, entry_path))
|
||||
for attachment in entry.attachments:
|
||||
attachment_path = (
|
||||
f"/attachments/global/{attachment.index}"
|
||||
if attachment.scope.value == "global"
|
||||
else f"{entry_path}/attachments/{attachment.index}"
|
||||
)
|
||||
for issue in attachment.issues:
|
||||
issues.append(_issue(Severity(issue.severity.value), issue.code, issue.message, attachment_path))
|
||||
return issues
|
||||
|
||||
|
||||
def _template_source_file_issues(config: CampaignConfig, campaign_path: Path) -> list[SemanticIssue]:
|
||||
issues: list[SemanticIssue] = []
|
||||
for schema_path, raw_path in _iter_template_source_paths(config):
|
||||
|
||||
Reference in New Issue
Block a user