def test_should_cancel_scheduled_flow_for_a_consignee_before_scheduling_another_one_for_that_node_line_item( self): node = DistributionPlanNodeFactory(consignee=self.consignee) line_item_one = DistributionPlanLineItemFactory( distribution_plan_node=node, planned_distribution_date=datetime.datetime.now()) DistributionPlanLineItemFactory( distribution_plan_node=node, planned_distribution_date=datetime.datetime.now()) when(DistributionPlanNode.objects).get(id=node.id).thenReturn(node) when(DistributionPlanLineItem.objects).get( id=line_item_one.id).thenReturn(line_item_one) when(node.consignee).build_contact().thenReturn(self.contact) schedule_run_for(line_item_one) task_id = line_item_one.current_node_line_item_run( ).scheduled_message_task_id when(celery.app.control).revoke(task_id).thenReturn(None) schedule_run_for(line_item_one) verify(celery.app.control).revoke(task_id) verify(fake_facade, times=2).start_delivery_flow(sender=any(), consignee=any(), item_description=any())
def _reschedule_next_run(phone, run_delay): current_run = Run.objects.filter( Q(phone=phone) & (Q(status='scheduled') | Q(status='not_started'))).order_by('modified').first() if current_run: schedule_run_for(current_run.runnable, run_delay)
def test_should_save_a_node_line_item_run_with_task_id_and_phone_as_cache_after_scheduling_the_flow(self): schedule_run_for(self.line_item) node_line_item_run_set = self.line_item.nodelineitemrun_set.all() self.assertEqual(len(node_line_item_run_set), 1) self.assertEqual(node_line_item_run_set[0].phone, self.contact['phone']) self.assertEqual(node_line_item_run_set[0].scheduled_message_task_id, mock_celery.task_id)
def on_post_save_node(sender, **kwargs): node = kwargs["instance"] _resolve_alert_if_possible(node) if node.track and not node.is_root(): schedule_run_for(node) if settings.CELERY_LIVE and node.get_programme() and node.tree_position != Flow.Label.IMPLEMENTING_PARTNER: update_contact.apply_async(args=[node])
def on_post_save_delivery(sender, **kwargs): delivery = kwargs["instance"] _resolve_alert_if_possible(delivery) if delivery.track and (not delivery.has_existing_run()): schedule_run_for(delivery) if settings.CELERY_LIVE and (delivery.track or delivery.is_auto_track_confirmed): update_contact.apply_async(args=[delivery])
def test_should_save_a_run_with_task_id_and_phone_as_cache_after_scheduling_the_flow(self): schedule_run_for(self.node) run_set = self.node.run_set.all() self.assertEqual(len(run_set), 1) self.assertEqual(run_set[0].phone, self.contact["phone"]) self.assertEqual(run_set[0].scheduled_message_task_id, mock_celery.task_id)
def on_post_save_delivery(sender, **kwargs): delivery = kwargs['instance'] _resolve_alert_if_possible(delivery) if delivery.track and (not delivery.has_existing_run()): schedule_run_for(delivery) if settings.CELERY_LIVE and (delivery.track or delivery.is_auto_track_confirmed): update_contact.apply_async(args=[delivery])
def on_post_save_node(sender, **kwargs): node = kwargs['instance'] _resolve_alert_if_possible(node) if node.track and not node.is_root(): schedule_run_for(node) if settings.CELERY_LIVE and node.get_programme( ) and node.tree_position != Flow.Label.IMPLEMENTING_PARTNER: update_contact.apply_async(args=[node])
def test_should_cancel_scheduled_run_for_consignee_before_scheduling_another_one_for_the_same_node_line_item(self, mock_current_run_for_node): line_item_run = NodeLineItemRunFactory(node_line_item=self.line_item) self.line_item.current_run = MagicMock(return_value=line_item_run) mock_current_run_for_node.return_value = None schedule_run_for(self.line_item) self.assertEqual(line_item_run.status, NodeLineItemRun.STATUS.cancelled) local_celery.app.control.revoke.assert_called() mock_start_delivery_run.assert_called()
def test_should_schedule_flow_to_start_at_specific_time_after_expected_date_of_delivery(self): node = DistributionPlanNodeFactory(consignee=self.consignee) line_item = DistributionPlanLineItemFactory(distribution_plan_node=node, planned_distribution_date=datetime.datetime.now()) when(DistributionPlanNode.objects).get(id=node.id).thenReturn(node) when(DistributionPlanLineItem.objects).get(id=line_item.id).thenReturn(line_item) when(node.consignee).build_contact().thenReturn(self.contact) schedule_run_for(line_item) self.assertEqual(mock_celery.invoked_after, 604800.0)
def test_should_schedule_end_user_flow_if_node_tree_position_is_end_user(self, mock_current_run_for_node): node = NodeFactory(tree_position=Node.END_USER) node.build_contact = MagicMock(return_value=self.contact) mock_current_run_for_node.return_value = None Node.objects.get = MagicMock(return_value=node) schedule_run_for(node) mock_start_delivery_run.assert_called_with( contact_person=self.contact, flow=self.END_USER_FLOW_ID, item_description=ANY, sender=ANY )
def xtest_should_change_status_of_node_run_to_in_progress_when_scheduled_task_is_started(self): node = DistributionPlanNodeFactory(consignee=self.consignee) line_item = DistributionPlanLineItemFactory(distribution_plan_node=node, planned_distribution_date=datetime.datetime.now()) when(DistributionPlanNode.objects).get(id=node.id).thenReturn(node) when(DistributionPlanLineItem.objects).get(id=line_item.id).thenReturn(line_item) when(node.consignee).build_contact().thenReturn(self.contact) schedule_run_for(line_item) self.assertEqual(line_item.current_node_line_item_run().status, 'in_progress')
def test_should_schedule_middleman_flow_if_node_tree_position_is_middleman(self, mock_current_run_for_node): node = NodeFactory(tree_position=Node.MIDDLE_MAN) node.build_contact = MagicMock(return_value=self.contact) line_item = DistributionPlanLineItemFactory(distribution_plan_node=node) mock_current_run_for_node.return_value = None DistributionPlanLineItem.objects.get = MagicMock(return_value=line_item) Node.objects.get = MagicMock(return_value=node) schedule_run_for(line_item) mock_start_delivery_run.assert_called_with(contact_person=self.contact, flow=self.MIDDLEMAN_FLOW_ID, item_description=ANY, sender=ANY)
def test_should_queue_run_for_a_consignee_if_consignee_has_current_run_for_a_different_node_line_item( self, mock_current_run_for_node, mock_run_queue_enqueue ): run = RunFactory(node=self.node) node_two = NodeFactory() self.node.current_run = MagicMock(return_value=None) mock_current_run_for_node.return_value = run mock_run_queue_enqueue.return_value = None schedule_run_for(node_two) mock_run_queue_enqueue.assert_called_with(node_two, ANY)
def test_should_queue_run_for_a_consignee_if_consignee_has_current_run_for_a_different_node_line_item(self, mock_current_run_for_node, mock_run_queue_enqueue): line_item_run = NodeLineItemRunFactory(node_line_item=self.line_item) node_two = NodeFactory() line_item_two = DistributionPlanLineItemFactory(distribution_plan_node=node_two) self.line_item.current_run = MagicMock(return_value=None) mock_current_run_for_node.return_value = line_item_run mock_run_queue_enqueue.return_value = None schedule_run_for(line_item_two) mock_run_queue_enqueue.assert_called_with(line_item_two, ANY)
def test_should_save_a_node_line_item_run_with_task_id_after_and_phone_as_cache_scheduling_the_flow(self): node = DistributionPlanNodeFactory(consignee=self.consignee) line_item = DistributionPlanLineItemFactory(distribution_plan_node=node, planned_distribution_date=datetime.datetime.now()) when(DistributionPlanNode.objects).get(id=node.id).thenReturn(node) when(DistributionPlanLineItem.objects).get(id=line_item.id).thenReturn(line_item) when(node.consignee).build_contact().thenReturn(self.contact) schedule_run_for(line_item) node_line_item_run_set = line_item.nodelineitemrun_set.all() self.assertEqual(len(node_line_item_run_set), 1) self.assertEqual(node_line_item_run_set[0].phone, self.contact['phone']) self.assertEqual(node_line_item_run_set[0].scheduled_message_task_id, mock_celery.task_id)
def test_should_schedule_flow_to_start_at_specific_time_after_expected_date_of_delivery( self): node = DistributionPlanNodeFactory(consignee=self.consignee) line_item = DistributionPlanLineItemFactory( distribution_plan_node=node, planned_distribution_date=datetime.datetime.now()) when(DistributionPlanNode.objects).get(id=node.id).thenReturn(node) when(DistributionPlanLineItem.objects).get( id=line_item.id).thenReturn(line_item) when(node.consignee).build_contact().thenReturn(self.contact) schedule_run_for(line_item) self.assertEqual(mock_celery.invoked_after, 604800.0)
def test_should_schedule_flow_with_sender_as_parent_node_consignee_name_if_node_has_parent(self): sender_org_name = "Dwelling Places" sender_org = ConsigneeFactory(name=sender_org_name) parent_node = NodeFactory(consignee=sender_org) node = NodeFactory(consignee=sender_org, parent=parent_node) node.build_contact = MagicMock(return_value=self.contact) Node.objects.get = MagicMock(return_value=node) node.consignee.build_contact = MagicMock(return_value=self.contact) schedule_run_for(node) mock_start_delivery_run.assert_called_with( contact_person=self.contact, flow=ANY, sender=sender_org_name, item_description=node.item.item.description )
def test_should_schedule_a_flow_with_sender_as_unicef_if_node_has_no_parent(self): node = DistributionPlanNodeFactory(consignee=self.consignee) line_item = DistributionPlanLineItemFactory(distribution_plan_node=node) when(DistributionPlanNode.objects).get(id=node.id).thenReturn(node) when(DistributionPlanLineItem.objects).get(id=line_item.id).thenReturn(line_item) when(node.consignee).build_contact().thenReturn(self.contact) schedule_run_for(line_item) verify(fake_facade).start_delivery_flow( sender='UNICEF', consignee=self.contact, item_description=line_item.item.description, )
def xtest_should_change_status_of_node_run_to_in_progress_when_scheduled_task_is_started( self): node = DistributionPlanNodeFactory(consignee=self.consignee) line_item = DistributionPlanLineItemFactory( distribution_plan_node=node, planned_distribution_date=datetime.datetime.now()) when(DistributionPlanNode.objects).get(id=node.id).thenReturn(node) when(DistributionPlanLineItem.objects).get( id=line_item.id).thenReturn(line_item) when(node.consignee).build_contact().thenReturn(self.contact) schedule_run_for(line_item) self.assertEqual(line_item.current_node_line_item_run().status, 'in_progress')
def test_should_schedule_a_flow_with_sender_as_unicef_if_node_has_no_parent( self): node = DistributionPlanNodeFactory(consignee=self.consignee) line_item = DistributionPlanLineItemFactory( distribution_plan_node=node) when(DistributionPlanNode.objects).get(id=node.id).thenReturn(node) when(DistributionPlanLineItem.objects).get( id=line_item.id).thenReturn(line_item) when(node.consignee).build_contact().thenReturn(self.contact) schedule_run_for(line_item) verify(fake_facade).start_delivery_flow( sender='UNICEF', consignee=self.contact, item_description=line_item.item.description, )
def test_should_schedule_flow_with_sender_as_parent_node_consignee_name_if_node_has_parent(self): sender_org_name = "Dwelling Places" sender_org = ConsigneeFactory(name=sender_org_name) parent_node = DistributionPlanNodeFactory(consignee=sender_org) node = DistributionPlanNodeFactory(consignee=self.consignee, parent=parent_node) line_item = DistributionPlanLineItemFactory(distribution_plan_node=node) when(DistributionPlanNode.objects).get(id=node.id).thenReturn(node) when(DistributionPlanLineItem.objects).get(id=line_item.id).thenReturn(line_item) when(node.consignee).build_contact().thenReturn(self.contact) schedule_run_for(line_item) verify(fake_facade).start_delivery_flow( sender=sender_org_name, consignee=self.contact, item_description=line_item.item.description )
def test_should_save_a_node_line_item_run_with_task_id_after_and_phone_as_cache_scheduling_the_flow( self): node = DistributionPlanNodeFactory(consignee=self.consignee) line_item = DistributionPlanLineItemFactory( distribution_plan_node=node, planned_distribution_date=datetime.datetime.now()) when(DistributionPlanNode.objects).get(id=node.id).thenReturn(node) when(DistributionPlanLineItem.objects).get( id=line_item.id).thenReturn(line_item) when(node.consignee).build_contact().thenReturn(self.contact) schedule_run_for(line_item) node_line_item_run_set = line_item.nodelineitemrun_set.all() self.assertEqual(len(node_line_item_run_set), 1) self.assertEqual(node_line_item_run_set[0].phone, self.contact['phone']) self.assertEqual(node_line_item_run_set[0].scheduled_message_task_id, mock_celery.task_id)
def test_should_cancel_scheduled_flow_for_a_consignee_before_scheduling_another_one_for_that_node_line_item(self): node = DistributionPlanNodeFactory(consignee=self.consignee) line_item_one = DistributionPlanLineItemFactory(distribution_plan_node=node, planned_distribution_date=datetime.datetime.now()) DistributionPlanLineItemFactory(distribution_plan_node=node, planned_distribution_date=datetime.datetime.now()) when(DistributionPlanNode.objects).get(id=node.id).thenReturn(node) when(DistributionPlanLineItem.objects).get(id=line_item_one.id).thenReturn(line_item_one) when(node.consignee).build_contact().thenReturn(self.contact) schedule_run_for(line_item_one) task_id = line_item_one.current_node_line_item_run().scheduled_message_task_id when(celery.app.control).revoke(task_id).thenReturn(None) schedule_run_for(line_item_one) verify(celery.app.control).revoke(task_id) verify(fake_facade, times=2).start_delivery_flow(sender=any(), consignee=any(), item_description=any())
def test_should_schedule_flow_with_sender_as_parent_node_consignee_name_if_node_has_parent( self): sender_org_name = "Dwelling Places" sender_org = ConsigneeFactory(name=sender_org_name) parent_node = DistributionPlanNodeFactory(consignee=sender_org) node = DistributionPlanNodeFactory(consignee=self.consignee, parent=parent_node) line_item = DistributionPlanLineItemFactory( distribution_plan_node=node) when(DistributionPlanNode.objects).get(id=node.id).thenReturn(node) when(DistributionPlanLineItem.objects).get( id=line_item.id).thenReturn(line_item) when(node.consignee).build_contact().thenReturn(self.contact) schedule_run_for(line_item) verify(fake_facade).start_delivery_flow( sender=sender_org_name, consignee=self.contact, item_description=line_item.item.description)
def _reschedule_next_run(phone, run_delay): current_run = Run.objects.filter(Q(phone=phone) & (Q(status='scheduled') | Q(status='not_started'))).order_by( 'modified').first() if current_run: schedule_run_for(current_run.runnable, run_delay)
def _schedule_next_run(line_item): schedule_run_for(line_item)
def _dequeue_next_run(contact_person_id, run_delay): next_run_queue = RunQueue.dequeue(contact_person_id=contact_person_id) if next_run_queue: schedule_run_for(next_run_queue.runnable, run_delay) next_run_queue.update_status(RunQueue.STATUS.started)
def on_post_save_node(sender, **kwargs): node = kwargs['instance'] _resolve_alert_if_possible(node) if node.track and not node.is_root(): schedule_run_for(node)
def test_should_schedule_flow_to_start_at_specific_time_after_expected_date_of_delivery(self): schedule_run_for(self.line_item) self.assertEqual(mock_celery.invoked_after, 604800.0)
def test_should_schedule_a_flow_with_sender_as_unicef_if_node_has_no_parent(self): schedule_run_for(self.line_item) mock_start_delivery_run.assert_called_with(sender='UNICEF', contact_person=self.contact, flow=ANY, item_description=self.line_item.item.description)
def on_post_save_line_item(sender, **kwargs): node = kwargs['instance'] if node.track and node.latest_run() is None: schedule_run_for(node)
def _schedule_next_run(runnable): schedule_run_for(runnable)
def on_post_save_line_item(sender, **kwargs): line_item = kwargs['instance'] schedule_run_for(line_item)
def on_post_save_delivery(sender, **kwargs): delivery = kwargs['instance'] _resolve_alert_if_possible(delivery) if delivery.track and (not delivery.has_existing_run()): schedule_run_for(delivery)
def _dequeue_next_run_for(runnable): next_run = RunQueue.dequeue(contact_person_id=runnable.contact_person_id) logger.info("next run is %s" % next_run) if next_run: schedule_run_for(next_run.runnable) next_run.update_status(RunQueue.STATUS.started)
def _schedule_next_run(node): schedule_run_for(node)
def on_post_save_line_item(sender, **kwargs): line_item = kwargs['instance'] if(line_item.track and (line_item.latest_run() == None)): schedule_run_for(line_item)