mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-10 06:48:19 +02:00
Delete attachments after they are no longer referenced by an e-mail message. Fixes #1. Bumping version number to 1.8.3
This commit is contained in:
parent
83584af2e0
commit
ee6b4b3e1d
3 changed files with 15 additions and 6 deletions
|
|
@ -236,6 +236,10 @@ class UnreadMessageManager(models.Manager):
|
||||||
class MessageAttachment(models.Model):
|
class MessageAttachment(models.Model):
|
||||||
document = models.FileField(upload_to='mailbox_attachments/%Y/%m/%d/')
|
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):
|
def __unicode__(self):
|
||||||
return self.document.url
|
return self.document.url
|
||||||
|
|
||||||
|
|
@ -351,5 +355,12 @@ class Message(models.Model):
|
||||||
def get_email_object(self):
|
def get_email_object(self):
|
||||||
return email.message_from_string(self.body)
|
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):
|
def __unicode__(self):
|
||||||
return self.subject
|
return self.subject
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import shutil
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from django_mailbox.models import Mailbox
|
from django_mailbox.models import Mailbox, Message
|
||||||
|
|
||||||
class TestProcessMessage(TestCase):
|
class TestProcessMessage(TestCase):
|
||||||
def _get_email_object(self, name):
|
def _get_email_object(self, name):
|
||||||
|
|
@ -15,10 +15,8 @@ class TestProcessMessage(TestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
try:
|
for message in Message.objects.all():
|
||||||
shutil.rmtree('mailbox_attachments')
|
message.delete()
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def test_message_without_attachments(self):
|
def test_message_without_attachments(self):
|
||||||
message = self._get_email_object('generic_message.eml')
|
message = self._get_email_object('generic_message.eml')
|
||||||
|
|
|
||||||
2
setup.py
2
setup.py
|
|
@ -4,7 +4,7 @@ tests_require=['django']
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='django-mailbox',
|
name='django-mailbox',
|
||||||
version='1.8.2',
|
version='1.8.3',
|
||||||
url='http://bitbucket.org/latestrevision/django-mailbox/',
|
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.',
|
description='Import mail from POP3, IMAP, local mailboxes or directly from Postfix or Exim4 into your Django application automatically.',
|
||||||
author='Adam Coddington',
|
author='Adam Coddington',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue