def update_contact_info(request, id): """ Handles PUT /contacts/<id>/ Invoked when the contact type is changed by the app. """ id = id.split('.')[0] contact = models.Contact.objects.get(id = id) contact_payload = json.loads(request.raw_post_data) contact.type = int(contact_payload['type']) contact.name = contact_payload['name'] contact.save() if contact.type == models.Contact.BUSINESS: for address in contact.addresses.all(): rule, created = models.MessageRule.objects.get_or_create( type = models.MessageRule.NEWSLETTER, owner = request.user, sender_address = address) apply_message_rule(rule) elif contact.type == models.Contact.PERSON: for address in contact.addresses.all(): try: rule = models.MessageRule.objects.get( type = models.MessageRule.NEWSLETTER, owner = request.user, sender_address = address) rule.delete() except models.MessageRule.DoesNotExist, e: pass
def test_newsletter(self): """ - receive a message (new conversation is created) - flag it as newsletter (conversation is replaced with a newsletter) - receive a message of the same type (they are all put in the newsletter) - forward one newsletter (that message ends up in a new conversation and still sits in the newsletter aggregate) - receive another newsletter """ MessageAggregate.objects.all().delete() Message.objects.all().delete() MessageRule.objects.all().delete() original_message_count = Message.objects.all().count() original_conversation_count = MessageAggregate.objects.filter(status = MessageAggregate.STATUS_READY, creator__type = MessageRule.CONVERSATION).count() original_newsletter_count = MessageAggregate.objects.filter(status = MessageAggregate.STATUS_READY, creator__type = MessageRule.NEWSLETTER).count() self._enqueue_message(os.path.join(self.message_dir, 'newsletter_message1.eml'), '*****@*****.**', 'label3', 'S,F') self._process_queue(); # new conversation is created new_conversation = MessageAggregate.objects.get(owner = self.user, tags__label = 'label3') self.assertEqual(new_conversation.creator.type, MessageRule.CONVERSATION) self.assertEqual(MessageAggregate.objects.filter(status = MessageAggregate.STATUS_READY, creator__type = MessageRule.CONVERSATION).count(), original_conversation_count + 1) # another message arrives, another conversation created self._enqueue_message(os.path.join(self.message_dir, 'newsletter_message2.eml'), '*****@*****.**', 'label3', 'S,F') self._process_queue(); # got two conversations self.assertEqual(MessageAggregate.objects.filter(status = MessageAggregate.STATUS_READY, creator__type = MessageRule.CONVERSATION).count(), original_conversation_count + 2) # add a newsletter rule for the sender rule = MessageRule(type = MessageRule.NEWSLETTER, owner = self.user, sender_address = new_conversation.messages.all()[0].sender_address) rule.save() operations.apply_message_rule(rule) # conversations got replaced by a newsletter self.assertEqual(MessageAggregate.objects.filter(status = MessageAggregate.STATUS_READY, creator__type = MessageRule.CONVERSATION).count(), original_conversation_count) self.assertEqual(MessageAggregate.objects.filter(status = MessageAggregate.STATUS_READY, creator__type = MessageRule.NEWSLETTER).count(), 1 + original_newsletter_count) # a newsletter was forwarded self._enqueue_message(os.path.join(self.message_dir, 'newsletter_message3.eml'), '*****@*****.**', 'label3', 'S,F') self._process_queue(); # a new conversation is created self.assertEqual(MessageAggregate.objects.filter(status = MessageAggregate.STATUS_READY, creator__type = MessageRule.CONVERSATION).count(), 1 + original_conversation_count) # a new newsletter was received self._enqueue_message(os.path.join(self.message_dir, 'newsletter_message4.eml'), '*****@*****.**', 'label3', 'S,F') self._process_queue(); # the new newsletter message goes into the existing aggregate # the conversation is still present, with 2 messages self.assertEqual(MessageAggregate.objects.filter(status = MessageAggregate.STATUS_READY, creator__type = MessageRule.NEWSLETTER).count(), 1 + original_newsletter_count) newsletter = MessageAggregate.objects.get(status = MessageAggregate.STATUS_READY, owner = self.user, creator__type = MessageRule.NEWSLETTER, tags__label = 'label3') self.assertEqual(newsletter.messages.all().count(), 3) conversation = MessageAggregate.objects.get(status = MessageAggregate.STATUS_READY, owner = self.user, creator__type = MessageRule.CONVERSATION, tags__label = 'label3') self.assertEqual(conversation.messages.all().count(), 2)
def test_get_newsletter(self): """ Checks that modified_date is set on all messages that are moved into a newsletter aggregate """ MessageAggregate.objects.all().delete() Message.objects.all().delete() MessageRule.objects.all().delete() self._enqueue_message( os.path.join(self.message_dir, 'newsletter_message1.eml'), '*****@*****.**', 'label4', 'S,F') self._enqueue_message( os.path.join(self.message_dir, 'newsletter_message2.eml'), '*****@*****.**', 'label4', 'S,F') self._enqueue_message( os.path.join(self.message_dir, 'newsletter_message3.eml'), '*****@*****.**', 'label4', 'S,F') self._enqueue_message( os.path.join(self.message_dir, 'newsletter_message4.eml'), '*****@*****.**', 'label4', 'S,F') self._process_queue() # now there's 4 messages, but one of them isn't part of the newsletter aggregate self.assertEqual(Message.objects.all().count(), 4) time_after_processing_before_newsletter = datetime.datetime.now() # take the time now = datetime.datetime.utcnow() ms_time_before_rule = time.mktime( now.timetuple()) * 1000000 + now.microsecond # add a newsletter rule for the sender new_conversation = MessageAggregate.objects.filter( status=MessageAggregate.STATUS_READY, owner=self.user, tags__label='label4')[0] rule = MessageRule( type=MessageRule.NEWSLETTER, owner=self.user, sender_address=new_conversation.messages.all()[0].sender_address) rule.save() operations.apply_message_rule(rule) response = self.client.get( '/api/1.0/sync/events/?body=0&modified_since=%.0f' % ms_time_before_rule) self.assertEqual(response.status_code, 200) body = json.loads(response.content) # The request returns nothing since there are no fresher messages self.assertEqual(len(body['messages']), 3)
def test_newsletter_delayed_rule_application(self): """ - receive several messages from the same source (newsletter) and one message with a forward - apply a newsletter rule - check there's a single newsletter aggreate and a single conversation (for the forward) - check the names of conversation and newsletter """ MessageAggregate.objects.all().delete() Message.objects.all().delete() MessageRule.objects.all().delete() original_message_count = Message.objects.all().count() original_conversation_count = MessageAggregate.objects.filter(status = MessageAggregate.STATUS_READY, creator__type = MessageRule.CONVERSATION).count() original_newsletter_count = MessageAggregate.objects.filter(status = MessageAggregate.STATUS_READY, creator__type = MessageRule.NEWSLETTER).count() self._enqueue_message(os.path.join(self.message_dir, 'newsletter_message1.eml'), '*****@*****.**', 'label4', 'S,F') self._enqueue_message(os.path.join(self.message_dir, 'newsletter_message2.eml'), '*****@*****.**', 'label4', 'S,F') self._enqueue_message(os.path.join(self.message_dir, 'newsletter_message3.eml'), '*****@*****.**', 'label4', 'S,F') self._enqueue_message(os.path.join(self.message_dir, 'newsletter_message4.eml'), '*****@*****.**', 'label4', 'S,F') self._process_queue(); new_conversation = MessageAggregate.objects.filter(status = MessageAggregate.STATUS_READY, owner = self.user, tags__label = 'label4')[0] # add a newsletter rule for the sender rule = MessageRule(type = MessageRule.NEWSLETTER, owner = self.user, sender_address = new_conversation.messages.all()[0].sender_address) rule.save() time.sleep(2) operations.apply_message_rule(rule) time.sleep(2) # the new newsletter message goes into the existing aggregate # the conversation is still present, with 2 messages self.assertEqual(MessageAggregate.objects.filter(status = MessageAggregate.STATUS_READY, owner = self.user, creator__type = MessageRule.NEWSLETTER).count(), 1 + original_newsletter_count) newsletter = MessageAggregate.objects.get(status = MessageAggregate.STATUS_READY, owner = self.user, creator__type = MessageRule.NEWSLETTER, tags__label = 'label4') self.assertEqual(newsletter.messages.all().count(), 3) conversation = MessageAggregate.objects.get(status = MessageAggregate.STATUS_READY, owner = self.user, creator__type = MessageRule.CONVERSATION, tags__label = 'label4') self.assertEqual(conversation.messages.all().count(), 2) # Aggregate's name is the subject name of the first message self.assertEqual(conversation.name, 'New on The Economist online - 22nd October 2010') self.assertEqual(newsletter.name, 'New on The Economist online - 18th October 2010')
def test_get_newsletter(self): """ Checks that modified_date is set on all messages that are moved into a newsletter aggregate """ MessageAggregate.objects.all().delete() Message.objects.all().delete() MessageRule.objects.all().delete() self._enqueue_message(os.path.join(self.message_dir, 'newsletter_message1.eml'), '*****@*****.**', 'label4', 'S,F') self._enqueue_message(os.path.join(self.message_dir, 'newsletter_message2.eml'), '*****@*****.**', 'label4', 'S,F') self._enqueue_message(os.path.join(self.message_dir, 'newsletter_message3.eml'), '*****@*****.**', 'label4', 'S,F') self._enqueue_message(os.path.join(self.message_dir, 'newsletter_message4.eml'), '*****@*****.**', 'label4', 'S,F') self._process_queue(); # now there's 4 messages, but one of them isn't part of the newsletter aggregate self.assertEqual(Message.objects.all().count(), 4) time_after_processing_before_newsletter = datetime.datetime.now() # take the time now = datetime.datetime.utcnow() ms_time_before_rule = time.mktime(now.timetuple())*1000000+now.microsecond # add a newsletter rule for the sender new_conversation = MessageAggregate.objects.filter(status = MessageAggregate.STATUS_READY, owner = self.user, tags__label = 'label4')[0] rule = MessageRule(type = MessageRule.NEWSLETTER, owner = self.user, sender_address = new_conversation.messages.all()[0].sender_address) rule.save() operations.apply_message_rule(rule) response = self.client.get('/api/1.0/sync/events/?body=0&modified_since=%.0f' % ms_time_before_rule) self.assertEqual(response.status_code, 200) body = json.loads(response.content) # The request returns nothing since there are no fresher messages self.assertEqual(len(body['messages']), 3)