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 ApiUpload = 2
MailFetch = 3 MailFetch = 3
def __repr__(self) -> str:
return str(self)
@dataclasses.dataclass @dataclasses.dataclass
class ConsumableDocument: class ConsumableDocument:

View File

@ -244,59 +244,56 @@ def document_matches_template(
Returns True if the incoming document matches all filters and Returns True if the incoming document matches all filters and
settings from the template, False otherwise 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 # 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 # Document mail rule vs template mail rule
if match: if (
match = ( document.mailrule_id is not None
document.mailrule_id is None and template.filter_mailrule is not None
or template.filter_mailrule is None and document.mailrule_id != template.filter_mailrule.pk
or document.mailrule_id == template.filter_mailrule.pk ):
log_match_failure(
f"Document mail rule {document.mailrule_id}"
f" != {template.filter_mailrule.pk}",
) )
else: return False
reason = f"Document source {document.source} not in {template.sources}"
# Document filename vs template filename # Document filename vs template filename
if match: if (
match = ( template.filter_filename is not None
template.filter_filename is None and len(template.filter_filename) > 0
or len(template.filter_filename) == 0 and not fnmatch(
or fnmatch(
document.original_file.name.lower(), document.original_file.name.lower(),
template.filter_filename.lower(), template.filter_filename.lower(),
) )
):
log_match_failure(
f"Document filename {document.original_file.name} does not match"
f" {template.filter_filename.lower()}",
) )
else: return False
reason = (
f"Document mail rule {document.mailrule_id} "
f"!= {template.filter_mailrule.pk}"
)
# Document path vs template path # Document path vs template path
if match: if (
match = ( template.filter_path is not None
template.filter_path is None and len(template.filter_path) > 0
or len(template.filter_path) == 0 and not document.original_file.match(template.filter_path)
or document.original_file.match(template.filter_path) ):
) log_reason(
else:
reason = (
f"Document filename {document.original_file.name} "
f"does not match {template.filter_filename.lower()}"
)
if not match:
reason = (
f"Document path {document.original_file}" f"Document path {document.original_file}"
f"does not match {template.filter_path}" f"does not match {template.filter_path}",
) )
return False
logger.info( return True
f"Document {'did' if match else 'did not'} match template {template.name}",
)
if not match:
logger.debug(reason)
return match