fixed a couple things.

This commit is contained in:
Jonas Winkler 2020-11-17 01:08:52 +01:00
parent 53cfa5a1c9
commit e1a3f8cd1b
2 changed files with 14 additions and 16 deletions

View File

@ -2,7 +2,6 @@ import os
import tempfile import tempfile
from datetime import timedelta, date from datetime import timedelta, date
from django.conf import settings from django.conf import settings
from django.utils.text import slugify from django.utils.text import slugify
from django_q.tasks import async_task from django_q.tasks import async_task
@ -114,19 +113,22 @@ def handle_message(message, rule):
if rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_NOTHING: if rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_NOTHING:
correspondent = None correspondent = None
elif rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_EMAIL: elif rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_EMAIL:
corerspondent_name = message.from_ correspondent_name = message.from_
correspondent = Correspondent.objects.get_or_create( correspondent = Correspondent.objects.get_or_create(
name=corerspondent_name, defaults={ name=correspondent_name, defaults={
"slug": slugify(corerspondent_name) "slug": slugify(correspondent_name)
})[0] })[0]
elif rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_NAME: elif rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_NAME:
corerspondent_name = message.from_values['name'] \ if message.from_values and \
if (message.from_values and 'name' in message.from_values \
'name' in message.from_values and and message.from_values['name']:
message.from_values['name']) else message.from_ correspondent_name = message.from_values['name']
else:
correspondent_name = message.from_
correspondent = Correspondent.objects.get_or_create( correspondent = Correspondent.objects.get_or_create(
name=corerspondent_name, defaults={ name=correspondent_name, defaults={
"slug": slugify(corerspondent_name) "slug": slugify(correspondent_name)
})[0] })[0]
elif rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_CUSTOM: elif rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_CUSTOM:
correspondent = rule.assign_correspondent correspondent = rule.assign_correspondent
@ -146,16 +148,11 @@ def handle_message(message, rule):
else: else:
raise ValueError("Unknown title selector.") raise ValueError("Unknown title selector.")
# TODO: check with parsers what files types are supported
if att.content_type == 'application/pdf': if att.content_type == 'application/pdf':
print("This is where I would consume the file with name {} and I would "
"give it the title '{}', correspondent '{}', tag '{}', and doc type"
"'{}'."
.format(att.filename, title, correspondent, tag, doc_type))
os.makedirs(settings.SCRATCH_DIR, exist_ok=True) os.makedirs(settings.SCRATCH_DIR, exist_ok=True)
_, temp_filename = tempfile.mkstemp(prefix="paperless-mail-", dir=settings.SCRATCH_DIR) _, temp_filename = tempfile.mkstemp(prefix="paperless-mail-", dir=settings.SCRATCH_DIR)
with open(temp_filename, 'wb') as f: with open(temp_filename, 'wb') as f:
f.write(att.payload) f.write(att.payload)

View File

@ -1,5 +1,6 @@
from django.test import TestCase from django.test import TestCase
class TestMail(TestCase): class TestMail(TestCase):
def testHandleMessage(self): def testHandleMessage(self):