diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 2d9797e..fa06590 100755 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -236,6 +236,10 @@ class UnreadMessageManager(models.Manager): class MessageAttachment(models.Model): document = models.FileField(upload_to='mailbox_attachments/%Y/%m/%d/') + def delete(self, *args, **kwargs): + self.document.delete() + return super(MessageAttachment, self).delete(*args, **kwargs) + def __unicode__(self): return self.document.url @@ -351,5 +355,12 @@ class Message(models.Model): def get_email_object(self): return email.message_from_string(self.body) + def delete(self, *args, **kwargs): + for attachment in self.attachments.all(): + if attachment.message_set.count() == 1: + # This attachment is attached only to this message. + attachment.delete() + return super(Message, self).delete(*args, **kwargs) + def __unicode__(self): return self.subject diff --git a/django_mailbox/tests/__init__.py b/django_mailbox/tests/__init__.py index 2dcdfc5..7972100 100644 --- a/django_mailbox/tests/__init__.py +++ b/django_mailbox/tests/__init__.py @@ -5,7 +5,7 @@ import shutil from django.db import models from django.test import TestCase -from django_mailbox.models import Mailbox +from django_mailbox.models import Mailbox, Message class TestProcessMessage(TestCase): def _get_email_object(self, name): @@ -15,10 +15,8 @@ class TestProcessMessage(TestCase): ) def tearDown(self): - try: - shutil.rmtree('mailbox_attachments') - except OSError: - pass + for message in Message.objects.all(): + message.delete() def test_message_without_attachments(self): message = self._get_email_object('generic_message.eml') diff --git a/setup.py b/setup.py index bf3c406..a786adc 100755 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ tests_require=['django'] setup( name='django-mailbox', - version='1.8.2', + version='1.8.3', url='http://bitbucket.org/latestrevision/django-mailbox/', description='Import mail from POP3, IMAP, local mailboxes or directly from Postfix or Exim4 into your Django application automatically.', author='Adam Coddington',