Ejemplo n.º 1
0
    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())
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
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])
Ejemplo n.º 5
0
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])
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
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])
Ejemplo n.º 8
0
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])
Ejemplo n.º 9
0
    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()
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
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
        )
Ejemplo n.º 12
0
    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')
Ejemplo n.º 13
0
    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)
Ejemplo n.º 14
0
    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)
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
    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)
Ejemplo n.º 17
0
    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)
Ejemplo n.º 18
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
        )
Ejemplo n.º 19
0
    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,
        )
Ejemplo n.º 20
0
    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')
Ejemplo n.º 21
0
    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,
        )
Ejemplo n.º 22
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 = 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
        )
Ejemplo n.º 23
0
    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)
Ejemplo n.º 24
0
    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())
Ejemplo n.º 25
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 = 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)
Ejemplo n.º 26
0
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)
Ejemplo n.º 27
0
def _schedule_next_run(line_item):
    schedule_run_for(line_item)
Ejemplo n.º 28
0
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)
Ejemplo n.º 29
0
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)
Ejemplo n.º 30
0
    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)
Ejemplo n.º 31
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)
Ejemplo n.º 32
0
def on_post_save_line_item(sender, **kwargs):
    node = kwargs['instance']
    if node.track and node.latest_run() is None:
        schedule_run_for(node)
Ejemplo n.º 33
0
Archivo: hook.py Proyecto: yaroing/eums
def _schedule_next_run(runnable):
    schedule_run_for(runnable)
Ejemplo n.º 34
0
def on_post_save_line_item(sender, **kwargs):
    line_item = kwargs['instance']
    schedule_run_for(line_item)
Ejemplo n.º 35
0
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)
Ejemplo n.º 36
0
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)
Ejemplo n.º 37
0
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)
Ejemplo n.º 38
0
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)
Ejemplo n.º 39
0
def _schedule_next_run(node):
    schedule_run_for(node)
Ejemplo n.º 40
0
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)
Ejemplo n.º 41
0
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)
Ejemplo n.º 42
0
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)
Ejemplo n.º 43
0
Archivo: hook.py Proyecto: yaroing/eums
def _schedule_next_run(runnable):
    schedule_run_for(runnable)