diff options
author | Patrick Maia <pmaia@thoughtworks.com> | 2014-08-28 15:06:21 +0000 |
---|---|---|
committer | Patrick Maia <pmaia@thoughtworks.com> | 2014-08-28 15:06:21 +0000 |
commit | dc3f7d1ae9a56192676ae4efb8286b0968ec9726 (patch) | |
tree | c33b7801f57716a30a5f2829749b4cff0895f78f /service | |
parent | de9c26359ed448d4ee4937fa0d241469a7d76cf1 (diff) |
#53 - adds ability to remove tags from email
Diffstat (limited to 'service')
-rw-r--r-- | service/pixelated/adapter/mail_service.py | 31 | ||||
-rw-r--r-- | service/pixelated/adapter/pixelated_mail.py | 6 | ||||
-rw-r--r-- | service/test/adapter/mail_service_test.py | 8 | ||||
-rw-r--r-- | service/test/adapter/pixelated_mail_test.py | 6 |
4 files changed, 35 insertions, 16 deletions
diff --git a/service/pixelated/adapter/mail_service.py b/service/pixelated/adapter/mail_service.py index d9c30e51..3d87971e 100644 --- a/service/pixelated/adapter/mail_service.py +++ b/service/pixelated/adapter/mail_service.py @@ -54,23 +54,32 @@ class MailService: def update_tags(self, mail_id, new_tags): mail = self.mail(mail_id) - new_tags = mail.update_tags(new_tags) - self._update_mail_flags(new_tags, mail_id) - self._update_mailbox_tags(new_tags) - return new_tags + tags = set(Tag(str_tag) for str_tag in new_tags) + current_tags, removed_tags = mail.update_tags(tags) + self._update_mail_flags(current_tags, removed_tags, mail_id) + self._update_mailbox_tags(tags) + return current_tags - def _update_mailbox_tags(self, str_tags): - tags = [Tag(str_tag) for str_tag in str_tags] + def _update_mailbox_tags(self, tags): self.mailbox.update_tags(tags) - def _update_mail_flags(self, new_tags, mail_id): - new_tags_flag_name = ['tag_' + tag.name for tag in new_tags if tag.name not in PixelatedMailbox.SPECIAL_TAGS] - self._set_mail_flags(mail_id, new_tags_flag_name) + def _update_mail_flags(self, current_tags, removed_tags, mail_id): + new_flags = ['tag_' + tag.name for tag in current_tags if tag.name not in PixelatedMailbox.SPECIAL_TAGS] + self._append_mail_flags(mail_id, new_flags) - def _set_mail_flags(self, mail_id, new_tags_flag_name): + removed_flags = ['tag_' + tag.name for tag in removed_tags if tag.name not in PixelatedMailbox.SPECIAL_TAGS] + self._remove_mail_flags(mail_id, removed_flags) + + def _append_mail_flags(self, mail_id, flags): + self._set_mail_flags(mail_id, flags, 1) + + def _remove_mail_flags(self, mail_id, flags): + self._set_mail_flags(mail_id, flags, -1) + + def _set_mail_flags(self, mail_id, flags, operation): observer = defer.Deferred() leap_mailbox = self.account.getMailbox(self.mailbox_name) - self.mailbox.messages.set_flags(leap_mailbox, [mail_id], tuple(new_tags_flag_name), 1, observer) + self.mailbox.messages.set_flags(leap_mailbox, [mail_id], tuple(flags), operation, observer) def mail(self, mail_id): return self.mailbox.mail(mail_id) diff --git a/service/pixelated/adapter/pixelated_mail.py b/service/pixelated/adapter/pixelated_mail.py index c3f43a70..f9bce3cf 100644 --- a/service/pixelated/adapter/pixelated_mail.py +++ b/service/pixelated/adapter/pixelated_mail.py @@ -51,8 +51,10 @@ class PixelatedMail: return Tag.from_flags(self.leap_mail.getFlags()) def update_tags(self, tags): - self.tags = [Tag(tag) for tag in tags] - return self.tags + old_tags = self.tags + self.tags = tags + removed_tags = old_tags.difference(self.tags) + return self.tags, removed_tags def has_tag(self, tag): return Tag(tag) in self.tags diff --git a/service/test/adapter/mail_service_test.py b/service/test/adapter/mail_service_test.py index 6972f6f6..7b3f4e96 100644 --- a/service/test/adapter/mail_service_test.py +++ b/service/test/adapter/mail_service_test.py @@ -24,8 +24,9 @@ from pixelated.adapter.pixelated_mailbox import PixelatedMailbox class TestMailService(unittest.TestCase): - @patch.object(MailService, '_set_mail_flags', return_value=None) - def test_custom_tags_get_created_if_not_exists(self, mockSetFlags): + @patch.object(MailService, '_append_mail_flags', return_value=None) + @patch.object(MailService, '_remove_mail_flags', return_value=None) + def test_custom_tags_get_created_if_not_exists(self, mockRemoveFlags, mockAppendFlags): MailService._open_leap_session = lambda self: None MailService.mailbox = PixelatedMailbox(test_helper.leap_mailbox(leap_flags=['\\Recent'])) MailService.account = Mock(return_value=MagicMock()) @@ -37,4 +38,5 @@ class TestMailService(unittest.TestCase): self.assertEquals(set([Tag('test'), Tag('inbox')]), set(updated_tags)) # make sure that special tags are skipped when setting leap flags (eg.: tag_inbox) - mockSetFlags.assert_called_with(6, ['tag_test']) + mockAppendFlags.assert_called_with(6, ['tag_test']) + mockRemoveFlags.assert_called_with(6, []) diff --git a/service/test/adapter/pixelated_mail_test.py b/service/test/adapter/pixelated_mail_test.py index 8d1b70df..81e9f340 100644 --- a/service/test/adapter/pixelated_mail_test.py +++ b/service/test/adapter/pixelated_mail_test.py @@ -69,3 +69,9 @@ class TestPixelatedMail(unittest.TestCase): self.assertEqual(mail.ident, '') self.assertEqual(mail.tags, ['sent']) self.assertEqual(mail.body, 'Este \xe9 o corpo') + + def test_update_tags_return_a_set_for_current_tags_and_a_set_for_removed(self): + pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(leap_flags=[], extra_flags=['tag_custom_1', 'tag_custom_2'])) + current_tags, removed_tags = pixelated_mail.update_tags(set([Tag('custom_1'), Tag('custom_3')])) + self.assertEquals(set([Tag('custom_3'), Tag('custom_1')]), current_tags) + self.assertEquals(set([Tag('custom_2')]), removed_tags) |