From 2824cde5e9ababb58d0d13669abdeb8729e6703a Mon Sep 17 00:00:00 2001 From: Ilya Tleukenov Date: Fri, 31 Jan 2020 13:31:04 +0700 Subject: [PATCH 1/3] add DJANGO_MAILBOX_MESSAGE_UPLOAD_TO to settings, change utils.get_attachment_save_path to utils.get_save_path, change eml upload_to in model --- django_mailbox/models.py | 4 ++-- django_mailbox/utils.py | 12 +++++++++--- docs/topics/appendix/settings.rst | 6 ++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 4e5b1110..379b779f 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -512,7 +512,7 @@ class Message(models.Model): eml = models.FileField( _('Raw message contents'), null=True, - upload_to="messages", + upload_to=utils.get_save_path(setting='message_upload_to'), help_text=_('Original full content of message') ) objects = models.Manager() @@ -752,7 +752,7 @@ class MessageAttachment(models.Model): document = models.FileField( _('Document'), - upload_to=utils.get_attachment_save_path, + upload_to=utils.get_save_path(setting='attachment_upload_to'), ) def delete(self, *args, **kwargs): diff --git a/django_mailbox/utils.py b/django_mailbox/utils.py index 6603612f..82a52d2a 100644 --- a/django_mailbox/utils.py +++ b/django_mailbox/utils.py @@ -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/' ) + } @@ -138,14 +144,14 @@ 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) return os.path.join( path, filename, - ) + ) \ No newline at end of file diff --git a/docs/topics/appendix/settings.rst b/docs/topics/appendix/settings.rst index 41464828..c6950e87 100644 --- a/docs/topics/appendix/settings.rst +++ b/docs/topics/appendix/settings.rst @@ -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. From 02acef8edef70654f04fd7d7d947626043a4ce61 Mon Sep 17 00:00:00 2001 From: Ilya Tleukenov Date: Fri, 31 Jan 2020 15:44:51 +0700 Subject: [PATCH 2/3] fix utils.get_save_path fix get_save_path in Migration --- .../migrations/0005_auto_20160523_2240.py | 2 +- django_mailbox/utils.py | 21 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/django_mailbox/migrations/0005_auto_20160523_2240.py b/django_mailbox/migrations/0005_auto_20160523_2240.py index f4bdae65..525d2c0e 100644 --- a/django_mailbox/migrations/0005_auto_20160523_2240.py +++ b/django_mailbox/migrations/0005_auto_20160523_2240.py @@ -12,6 +12,6 @@ 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=django_mailbox.utils.get_save_path(setting='attachment_upload_to'), verbose_name='Document'), ), ] diff --git a/django_mailbox/utils.py b/django_mailbox/utils.py index 82a52d2a..51204ed5 100644 --- a/django_mailbox/utils.py +++ b/django_mailbox/utils.py @@ -5,7 +5,6 @@ from django.conf import settings - logger = logging.getLogger(__name__) @@ -144,14 +143,16 @@ def get_body_from_message(message, maintype, subtype): return body -def get_save_path(instance, filename, setting): - settings = get_settings() +def get_save_path(setting): + def _tmp(instance, filename): + settings = get_settings() - path = settings[setting] - if '%' in path: - path = datetime.datetime.utcnow().strftime(path) + path = settings[setting] + if '%' in path: + path = datetime.datetime.utcnow().strftime(path) - return os.path.join( - path, - filename, - ) \ No newline at end of file + return os.path.join( + path, + filename, + ) + return _tmp From 804d91f2ad4791d564fca270bdd28d0a5ed8cbea Mon Sep 17 00:00:00 2001 From: rokfordchez Date: Sat, 8 Feb 2020 22:57:45 +0700 Subject: [PATCH 3/3] fix valuerror of wrapper in get_save_path --- .../migrations/0005_auto_20160523_2240.py | 6 ++-- .../migrations/0009_auto_20200208_0953.py | 20 ++++++++++++ django_mailbox/models.py | 31 ++++++++++--------- django_mailbox/utils.py | 21 ++++++------- 4 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 django_mailbox/migrations/0009_auto_20200208_0953.py diff --git a/django_mailbox/migrations/0005_auto_20160523_2240.py b/django_mailbox/migrations/0005_auto_20160523_2240.py index 525d2c0e..b2a77741 100644 --- a/django_mailbox/migrations/0005_auto_20160523_2240.py +++ b/django_mailbox/migrations/0005_auto_20160523_2240.py @@ -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'), ] @@ -12,6 +12,8 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='messageattachment', name='document', - field=models.FileField(upload_to=django_mailbox.utils.get_save_path(setting='attachment_upload_to'), verbose_name='Document'), + field=models.FileField( + upload_to=partial(django_mailbox.utils.get_save_path, setting='attachment_upload_to'), + verbose_name='Document'), ), ] diff --git a/django_mailbox/migrations/0009_auto_20200208_0953.py b/django_mailbox/migrations/0009_auto_20200208_0953.py new file mode 100644 index 00000000..ae04b745 --- /dev/null +++ b/django_mailbox/migrations/0009_auto_20200208_0953.py @@ -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'), + ), + ] diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 379b779f..27933367 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -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 @@ -254,8 +255,8 @@ 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 @@ -263,16 +264,16 @@ def _get_dehydrated_message(self, msg, record): # 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() @@ -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']: @@ -512,7 +513,7 @@ class Message(models.Model): eml = models.FileField( _('Raw message contents'), null=True, - upload_to=utils.get_save_path(setting='message_upload_to'), + upload_to=partial(utils.get_save_path, setting='message_upload_to'), help_text=_('Original full content of message') ) objects = models.Manager() @@ -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: @@ -752,7 +753,7 @@ class MessageAttachment(models.Model): document = models.FileField( _('Document'), - upload_to=utils.get_save_path(setting='attachment_upload_to'), + upload_to=partial(utils.get_save_path, setting='attachment_upload_to'), ) def delete(self, *args, **kwargs): diff --git a/django_mailbox/utils.py b/django_mailbox/utils.py index 51204ed5..df1f648e 100644 --- a/django_mailbox/utils.py +++ b/django_mailbox/utils.py @@ -3,6 +3,7 @@ import logging import os +from django.utils.deconstruct import deconstructible from django.conf import settings logger = logging.getLogger(__name__) @@ -143,16 +144,14 @@ def get_body_from_message(message, maintype, subtype): return body -def get_save_path(setting): - def _tmp(instance, filename): - settings = get_settings() +def get_save_path(instance, filename, setting): + settings = get_settings() - path = settings[setting] - if '%' in path: - path = datetime.datetime.utcnow().strftime(path) + path = settings[setting] + if '%' in path: + path = datetime.datetime.utcnow().strftime(path) - return os.path.join( - path, - filename, - ) - return _tmp + return os.path.join( + path, + filename, + )