Clean up the matching I did more

This commit is contained in:
Trenton Holmes 2023-09-20 20:17:12 -07:00 committed by shamoon
parent b393a0a5cb
commit 62aafe5019
2 changed files with 53 additions and 53 deletions

View File

@ -84,6 +84,9 @@ class DocumentSource(IntEnum):
ApiUpload = 2
MailFetch = 3
def __repr__(self) -> str:
return str(self)
@dataclasses.dataclass
class ConsumableDocument:

View File

@ -244,59 +244,56 @@ def document_matches_template(
Returns True if the incoming document matches all filters and
settings from the template, False otherwise
"""
reason = None
def log_match_failure(reason: str):
logger.info(f"Document did not match template {template.name}")
logger.debug(reason)
# Document source vs template source
match = document.source in [int(x) for x in list(template.sources)]
if document.source not in [int(x) for x in list(template.sources)]:
# TODO: This logs an int vs a string, confusing
log_match_failure(
f"Document source {document.source} not in {template.sources}",
)
return False
# Document mail rule vs template mail rule
if match:
match = (
document.mailrule_id is None
or template.filter_mailrule is None
or document.mailrule_id == template.filter_mailrule.pk
if (
document.mailrule_id is not None
and template.filter_mailrule is not None
and document.mailrule_id != template.filter_mailrule.pk
):
log_match_failure(
f"Document mail rule {document.mailrule_id}"
f" != {template.filter_mailrule.pk}",
)
else:
reason = f"Document source {document.source} not in {template.sources}"
return False
# Document filename vs template filename
if match:
match = (
template.filter_filename is None
or len(template.filter_filename) == 0
or fnmatch(
if (
template.filter_filename is not None
and len(template.filter_filename) > 0
and not fnmatch(
document.original_file.name.lower(),
template.filter_filename.lower(),
)
):
log_match_failure(
f"Document filename {document.original_file.name} does not match"
f" {template.filter_filename.lower()}",
)
else:
reason = (
f"Document mail rule {document.mailrule_id} "
f"!= {template.filter_mailrule.pk}"
)
return False
# Document path vs template path
if match:
match = (
template.filter_path is None
or len(template.filter_path) == 0
or document.original_file.match(template.filter_path)
)
else:
reason = (
f"Document filename {document.original_file.name} "
f"does not match {template.filter_filename.lower()}"
)
if not match:
reason = (
if (
template.filter_path is not None
and len(template.filter_path) > 0
and not document.original_file.match(template.filter_path)
):
log_reason(
f"Document path {document.original_file}"
f"does not match {template.filter_path}"
f"does not match {template.filter_path}",
)
return False
logger.info(
f"Document {'did' if match else 'did not'} match template {template.name}",
)
if not match:
logger.debug(reason)
return match
return True