Skip to content

add DJANGO_MAILBOX_MESSAGE_UPLOAD_TO to settings, change utils.get_at… #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions django_mailbox/migrations/0005_auto_20160523_2240.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.db import migrations, models
from functools import partial
import django_mailbox.utils


class Migration(migrations.Migration):

dependencies = [
('django_mailbox', '0004_bytestring_to_unicode'),
]
Expand All @@ -12,6 +12,8 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='messageattachment',
name='document',
field=models.FileField(upload_to=django_mailbox.utils.get_attachment_save_path, verbose_name='Document'),
field=models.FileField(
upload_to=partial(django_mailbox.utils.get_save_path, setting='attachment_upload_to'),
verbose_name='Document'),
),
]
20 changes: 20 additions & 0 deletions django_mailbox/migrations/0009_auto_20200208_0953.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.2.4 on 2020-02-08 09:53

from django.db import migrations, models
import django_mailbox.utils
import functools


class Migration(migrations.Migration):

dependencies = [
('django_mailbox', '0008_auto_20190219_1553'),
]

operations = [
migrations.AlterField(
model_name='message',
name='eml',
field=models.FileField(help_text='Original full content of message', null=True, upload_to=functools.partial(django_mailbox.utils.get_save_path, *(), **{'setting': 'message_upload_to'}), verbose_name='Raw message contents'),
),
]
31 changes: 16 additions & 15 deletions django_mailbox/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from urllib.parse import parse_qs, unquote, urlparse
from quopri import encode as encode_quopri
from io import BytesIO
from functools import partial
import base64
import email
import logging
Expand Down Expand Up @@ -254,25 +255,25 @@ def _get_dehydrated_message(self, msg, record):
self._get_dehydrated_message(part, record)
)
elif (
settings['strip_unallowed_mimetypes']
and not msg.get_content_type() in settings['allowed_mimetypes']
settings['strip_unallowed_mimetypes']
and not msg.get_content_type() in settings['allowed_mimetypes']
):
for header, value in msg.items():
new[header] = value
# Delete header, otherwise when attempting to deserialize the
# payload, it will be expecting a body for this.
del new['Content-Transfer-Encoding']
new[settings['altered_message_header']] = (
'Stripped; Content type %s not allowed' % (
msg.get_content_type()
)
'Stripped; Content type %s not allowed' % (
msg.get_content_type()
)
)
new.set_payload('')
elif (
(
msg.get_content_type() not in settings['text_stored_mimetypes']
) or
('attachment' in msg.get('Content-Disposition', ''))
(
msg.get_content_type() not in settings['text_stored_mimetypes']
) or
('attachment' in msg.get('Content-Disposition', ''))
):
filename = None
raw_filename = msg.get_filename()
Expand Down Expand Up @@ -368,7 +369,7 @@ def _process_message(self, message):
except KeyError as exc:
# email.message.replace_header may raise 'KeyError' if the header
# 'content-transfer-encoding' is missing
logger.warning("Failed to parse message: %s", exc,)
logger.warning("Failed to parse message: %s", exc, )
return None
msg.set_body(body)
if message['in-reply-to']:
Expand Down Expand Up @@ -512,7 +513,7 @@ class Message(models.Model):
eml = models.FileField(
_('Raw message contents'),
null=True,
upload_to="messages",
upload_to=partial(utils.get_save_path, setting='message_upload_to'),
help_text=_('Original full content of message')
)
objects = models.Manager()
Expand Down Expand Up @@ -651,9 +652,9 @@ def _rehydrate(self, msg):
encode_base64(new)
except MessageAttachment.DoesNotExist:
new[settings['altered_message_header']] = (
'Missing; Attachment %s not found' % (
msg[settings['attachment_interpolation_header']]
)
'Missing; Attachment %s not found' % (
msg[settings['attachment_interpolation_header']]
)
)
new.set_payload('')
else:
Expand Down Expand Up @@ -752,7 +753,7 @@ class MessageAttachment(models.Model):

document = models.FileField(
_('Document'),
upload_to=utils.get_attachment_save_path,
upload_to=partial(utils.get_save_path, setting='attachment_upload_to'),
)

def delete(self, *args, **kwargs):
Expand Down
12 changes: 9 additions & 3 deletions django_mailbox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import logging
import os

from django.utils.deconstruct import deconstructible
from django.conf import settings


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -66,7 +66,13 @@ def get_settings():
settings,
'DJANGO_MAILBOX_default_charset',
'iso8859-1',
),
'message_upload_to': getattr(
settings,
'DJANGO_MAILBOX_MESSAGE_UPLOAD_TO',
'messages/'
)

}


Expand Down Expand Up @@ -138,10 +144,10 @@ def get_body_from_message(message, maintype, subtype):
return body


def get_attachment_save_path(instance, filename):
def get_save_path(instance, filename, setting):
settings = get_settings()

path = settings['attachment_upload_to']
path = settings[setting]
if '%' in path:
path = datetime.datetime.utcnow().strftime(path)

Expand Down
6 changes: 6 additions & 0 deletions docs/topics/appendix/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ Settings
* Default: ``False``
* Type: ``boolean``
* Controls whether or not we store original messages in ``eml`` field

* ``DJANGO_MAILBOX_MESSAGE_UPLOAD_TO``

* Default: ``messages/``
* Type: ``string``
* Original message will be saved to this location.