def testBasicReportNoResponses(self):
     result = reports.construct_randr_summary(
         self.district_contact.supply_point)
     self.assertEqual(self.relevant_count, result["total"])
     self.assertEqual(self.relevant_count, result["not_responding"])
     self.assertEqual(0, result["not_submitted"])
     self.assertEqual(0, result["submitted"])
 def testPositiveResponses(self):
     script = """
         %(phone)s > nimetuma
     """
     for i, contact in enumerate(self.facility_contacts):
         self.runScript(script % {"phone": contact.default_connection.identity})
         result = reports.construct_randr_summary(self.district_contact.supply_point)
         self.assertEqual(self.relevant_count, result["total"])
         self.assertEqual(self.relevant_count - 1 - i, result["not_responding"])
         self.assertEqual(0, result["not_submitted"])
         self.assertEqual(1 + i, result["submitted"])
 def testPositiveResponses(self):
     script = """
         %(phone)s > nimetuma
     """
     for i, contact in enumerate(self.facility_contacts):
         self.runScript(script %
                        {"phone": contact.default_connection.identity})
         result = reports.construct_randr_summary(
             self.district_contact.supply_point)
         self.assertEqual(self.relevant_count, result["total"])
         self.assertEqual(self.relevant_count - 1 - i,
                          result["not_responding"])
         self.assertEqual(0, result["not_submitted"])
         self.assertEqual(1 + i, result["submitted"])
 def testOverrides(self):
     self.testPositiveResponses()
     # at this point they should all be positive. But if we send 
     # an additional negative it should override it
     script = """
         %(phone)s > sijatuma
     """
     for i, contact in enumerate(self.facility_contacts):
         self.runScript(script % {"phone": contact.default_connection.identity})
         result = reports.construct_randr_summary(self.district_contact.supply_point)
         self.assertEqual(self.relevant_count, result["total"])
         self.assertEqual(self.relevant_count - 3, result["not_responding"])
         self.assertEqual(1 + i, result["not_submitted"])
         self.assertEqual(self.relevant_count - 1 - i, result["submitted"])
 def testOverrides(self):
     self.testPositiveResponses()
     # at this point they should all be positive. But if we send
     # an additional negative it should override it
     script = """
         %(phone)s > sijatuma
     """
     for i, contact in enumerate(self.facility_contacts):
         self.runScript(script %
                        {"phone": contact.default_connection.identity})
         result = reports.construct_randr_summary(
             self.district_contact.supply_point)
         self.assertEqual(self.relevant_count, result["total"])
         self.assertEqual(self.relevant_count - 3, result["not_responding"])
         self.assertEqual(1 + i, result["not_submitted"])
         self.assertEqual(self.relevant_count - 1 - i, result["submitted"])
    def handle(self, text):
        result = text.lower().split()
        if len(result) < 2:
            return self.help()
        
        command = result[0]
        rest = " ".join(result[1:])
        msd_code = result[1]
        fw_message = " ".join(result[2:])
        try:
            sp_target = SupplyPoint.objects.get(code__iexact=msd_code)
        except SupplyPoint.DoesNotExist:
            # maybe it's a district, get by name
            try:
                sp_target = SupplyPoint.objects.get(name__iexact=rest)
            except SupplyPoint.DoesNotExist:
                # fail
                self.respond_error(_(config.Messages.TEST_HANDLER_BAD_CODE) % {"code":msd_code})
                return True
        
        # target guaranteed to be set by here
        if command in ["la"]:
            for c in sp_target.active_contact_set:
                _send_if_connection(c, _(config.Messages.LOSS_ADJUST_HELP))
            SupplyPointStatus.objects.create(supply_point=sp_target,
                                             status_type=SupplyPointStatusTypes.LOSS_ADJUSTMENT_FACILITY,
                                             status_value=SupplyPointStatusValues.REMINDER_SENT,
                                             status_date=self.msg.timestamp)
            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
        if command in  ["soh", "hmk"]:
            for c in sp_target.active_contact_set:
                _send_if_connection(c, _(config.Messages.SOH_HELP_MESSAGE))
            SupplyPointStatus.objects.create(supply_point=sp_target,
                                             status_type=SupplyPointStatusTypes.SOH_FACILITY,
                                             status_value=SupplyPointStatusValues.REMINDER_SENT,
                                             status_date=self.msg.timestamp)
            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
        if command in  ["fw"]:
            for c in sp_target.active_contact_set:
                _send_if_connection(c, fw_message)
            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
        if command in  ["supervision"]:
            for c in sp_target.active_contact_set:
                _send_if_connection(c, _(config.Messages.SUPERVISION_REMINDER))
            SupplyPointStatus.objects.create(supply_point=sp_target,
                                             status_type=SupplyPointStatusTypes.SUPERVISION_FACILITY,
                                             status_value=SupplyPointStatusValues.REMINDER_SENT,
                                             status_date=self.msg.timestamp)
            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
        if command in  ["si"]:
            pc = result.pop(0)
            try:
                product = Product.objects.get(product_code=pc)
            except Product.DoesNotExist:
                self.respond_error(_(config.Messages.INVALID_PRODUCT_CODE ) % {"product_code": pc})
                return True
            #facility only - SMS usage
            if sp_target.type.code.lower() == config.SupplyPointCodes.FACILITY:
                for c in sp_target.active_contact_set:
                    _send_if_connection(c, _(config.Messages.STOCK_INQUIRY_MESSAGE) % {"product_name":product.name,
                                                                          "msd_code":product.product_code})
                self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
            else:
                self.respond_error(_("Can only initiate product inquiry for a single facility via SMS - %(location_name)s is a %(location_type)s") % {"location_name":"changeme",
                                                                                                                                                "location_type":"changeme"})
        if command in ["randr"]:
            if sp_target.type.code.lower() == config.SupplyPointCodes.DISTRICT:
                for c in sp_target.active_contact_set:
                    _send_if_connection(c, _(config.Messages.SUBMITTED_REMINDER_DISTRICT))
                SupplyPointStatus.objects.create(supply_point=sp_target,
                                                 status_type=SupplyPointStatusTypes.R_AND_R_DISTRICT,
                                                 status_value=SupplyPointStatusValues.REMINDER_SENT,
                                                 status_date=self.msg.timestamp)
            elif sp_target.type.code.lower() == config.SupplyPointCodes.FACILITY:
                for c in sp_target.active_contact_set:
                    _send_if_connection(c, _(config.Messages.SUBMITTED_REMINDER_FACILITY))
                SupplyPointStatus.objects.create(supply_point=sp_target,
                                                 status_type=SupplyPointStatusTypes.R_AND_R_FACILITY,
                                                 status_value=SupplyPointStatusValues.REMINDER_SENT,
                                                 status_date=self.msg.timestamp)

            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
        if command in ["delivery"]:
            if sp_target.type.code.lower() == config.SupplyPointCodes.DISTRICT:
                for c in sp_target.active_contact_set:
                    _send_if_connection(c, _(config.Messages.DELIVERY_REMINDER_DISTRICT))
                SupplyPointStatus.objects.create(supply_point=sp_target,
                                                 status_type=SupplyPointStatusTypes.DELIVERY_DISTRICT,
                                                 status_value=SupplyPointStatusValues.REMINDER_SENT,
                                                 status_date=self.msg.timestamp)
            elif sp_target.type.code.lower() == config.SupplyPointCodes.FACILITY:
                for c in sp_target.active_contact_set:
                    _send_if_connection(c, _(config.Messages.DELIVERY_REMINDER_FACILITY))
                SupplyPointStatus.objects.create(supply_point=sp_target,
                                                 status_type=SupplyPointStatusTypes.DELIVERY_FACILITY,
                                                 status_value=SupplyPointStatusValues.REMINDER_SENT,
                                                 status_date=self.msg.timestamp)

            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
        if command in ["latedelivery"]:
            #TODO: Query out counts
            for c in sp_target.active_contact_set:
                _send_if_connection(c, _(config.Messages.DELIVERY_LATE_DISTRICT) % {"group_name":"changeme",
                                                                       "group_total":1,
                                                                       "not_responded_count":2,
                                                                       "not_received_count":3})
            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))

        if command in ["send_inquiry_message"]:
            #sends to all children under a location - website only
            raise Exception("This handler command hasn't been implemented yet")
            
        # these next three only make sense for districts
        if command in ["randr_report"]:
            assert(sp_target.type.code.lower() == config.SupplyPointCodes.DISTRICT)
            for c in sp_target.active_contact_set:
                _send_if_connection(c, config.Messages.REMINDER_MONTHLY_RANDR_SUMMARY, **reports.construct_randr_summary(sp_target))
            self.respond_success()
        if command in ["soh_report"]:
            assert(sp_target.type.code.lower() == config.SupplyPointCodes.DISTRICT)
            for c in sp_target.active_contact_set:
                _send_if_connection(c, config.Messages.REMINDER_MONTHLY_SOH_SUMMARY, **reports.construct_soh_summary(sp_target))
            self.respond_success()
        if command in ["delivery_report"]:
            assert(sp_target.type.code.lower() == config.SupplyPointCodes.DISTRICT)
            for c in sp_target.active_contact_set:
                _send_if_connection(c, config.Messages.REMINDER_MONTHLY_DELIVERY_SUMMARY, **reports.construct_delivery_summary(sp_target))
            self.respond_success()
        if command in ["soh_thank_you"]:
            # test at the facility level for now
            assert(sp_target.type.code.lower() == config.SupplyPointCodes.FACILITY)
            for c in sp_target.active_contact_set:
                _send_if_connection(c, _(config.Messages.SOH_THANK_YOU))
            self.respond_success()
 def testBasicReportNoResponses(self):
     result = reports.construct_randr_summary(self.district_contact.supply_point)
     self.assertEqual(self.relevant_count, result["total"])
     self.assertEqual(self.relevant_count, result["not_responding"])
     self.assertEqual(0, result["not_submitted"])
     self.assertEqual(0, result["submitted"])
    def handle(self, text):
        result = text.lower().split()
        if len(result) < 2:
            return self.help()

        command = result[0]
        rest = " ".join(result[1:])
        msd_code = result[1]
        fw_message = " ".join(result[2:])
        try:
            sp_target = SupplyPoint.objects.get(code__iexact=msd_code)
        except SupplyPoint.DoesNotExist:
            # maybe it's a district, get by name
            try:
                sp_target = SupplyPoint.objects.get(name__iexact=rest)
            except SupplyPoint.DoesNotExist:
                # fail
                self.respond_error(
                    _(config.Messages.TEST_HANDLER_BAD_CODE) %
                    {"code": msd_code})
                return True

        # target guaranteed to be set by here
        if command in ["la"]:
            for c in sp_target.active_contact_set:
                _send_if_connection(c, _(config.Messages.LOSS_ADJUST_HELP))
            SupplyPointStatus.objects.create(
                supply_point=sp_target,
                status_type=SupplyPointStatusTypes.LOSS_ADJUSTMENT_FACILITY,
                status_value=SupplyPointStatusValues.REMINDER_SENT,
                status_date=self.msg.timestamp)
            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
        if command in ["soh", "hmk"]:
            for c in sp_target.active_contact_set:
                _send_if_connection(c, _(config.Messages.SOH_HELP_MESSAGE))
            SupplyPointStatus.objects.create(
                supply_point=sp_target,
                status_type=SupplyPointStatusTypes.SOH_FACILITY,
                status_value=SupplyPointStatusValues.REMINDER_SENT,
                status_date=self.msg.timestamp)
            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
        if command in ["fw"]:
            for c in sp_target.active_contact_set:
                _send_if_connection(c, fw_message)
            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
        if command in ["supervision"]:
            for c in sp_target.active_contact_set:
                _send_if_connection(c, _(config.Messages.SUPERVISION_REMINDER))
            SupplyPointStatus.objects.create(
                supply_point=sp_target,
                status_type=SupplyPointStatusTypes.SUPERVISION_FACILITY,
                status_value=SupplyPointStatusValues.REMINDER_SENT,
                status_date=self.msg.timestamp)
            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
        if command in ["si"]:
            pc = result.pop(0)
            try:
                product = Product.objects.get(product_code=pc)
            except Product.DoesNotExist:
                self.respond_error(
                    _(config.Messages.INVALID_PRODUCT_CODE) %
                    {"product_code": pc})
                return True
            #facility only - SMS usage
            if sp_target.type.code.lower() == config.SupplyPointCodes.FACILITY:
                for c in sp_target.active_contact_set:
                    _send_if_connection(
                        c,
                        _(config.Messages.STOCK_INQUIRY_MESSAGE) % {
                            "product_name": product.name,
                            "msd_code": product.product_code
                        })
                self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
            else:
                self.respond_error(
                    _("Can only initiate product inquiry for a single facility via SMS - %(location_name)s is a %(location_type)s"
                      ) % {
                          "location_name": "changeme",
                          "location_type": "changeme"
                      })
        if command in ["randr"]:
            if sp_target.type.code.lower() == config.SupplyPointCodes.DISTRICT:
                for c in sp_target.active_contact_set:
                    _send_if_connection(
                        c, _(config.Messages.SUBMITTED_REMINDER_DISTRICT))
                SupplyPointStatus.objects.create(
                    supply_point=sp_target,
                    status_type=SupplyPointStatusTypes.R_AND_R_DISTRICT,
                    status_value=SupplyPointStatusValues.REMINDER_SENT,
                    status_date=self.msg.timestamp)
            elif sp_target.type.code.lower(
            ) == config.SupplyPointCodes.FACILITY:
                for c in sp_target.active_contact_set:
                    _send_if_connection(
                        c, _(config.Messages.SUBMITTED_REMINDER_FACILITY))
                SupplyPointStatus.objects.create(
                    supply_point=sp_target,
                    status_type=SupplyPointStatusTypes.R_AND_R_FACILITY,
                    status_value=SupplyPointStatusValues.REMINDER_SENT,
                    status_date=self.msg.timestamp)

            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
        if command in ["delivery"]:
            if sp_target.type.code.lower() == config.SupplyPointCodes.DISTRICT:
                for c in sp_target.active_contact_set:
                    _send_if_connection(
                        c, _(config.Messages.DELIVERY_REMINDER_DISTRICT))
                SupplyPointStatus.objects.create(
                    supply_point=sp_target,
                    status_type=SupplyPointStatusTypes.DELIVERY_DISTRICT,
                    status_value=SupplyPointStatusValues.REMINDER_SENT,
                    status_date=self.msg.timestamp)
            elif sp_target.type.code.lower(
            ) == config.SupplyPointCodes.FACILITY:
                for c in sp_target.active_contact_set:
                    _send_if_connection(
                        c, _(config.Messages.DELIVERY_REMINDER_FACILITY))
                SupplyPointStatus.objects.create(
                    supply_point=sp_target,
                    status_type=SupplyPointStatusTypes.DELIVERY_FACILITY,
                    status_value=SupplyPointStatusValues.REMINDER_SENT,
                    status_date=self.msg.timestamp)

            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))
        if command in ["latedelivery"]:
            #TODO: Query out counts
            for c in sp_target.active_contact_set:
                _send_if_connection(
                    c,
                    _(config.Messages.DELIVERY_LATE_DISTRICT) % {
                        "group_name": "changeme",
                        "group_total": 1,
                        "not_responded_count": 2,
                        "not_received_count": 3
                    })
            self.respond(_(config.Messages.TEST_HANDLER_CONFIRM))

        if command in ["send_inquiry_message"]:
            #sends to all children under a location - website only
            raise Exception("This handler command hasn't been implemented yet")

        # these next three only make sense for districts
        if command in ["randr_report"]:
            assert (sp_target.type.code.lower() ==
                    config.SupplyPointCodes.DISTRICT)
            for c in sp_target.active_contact_set:
                _send_if_connection(
                    c, config.Messages.REMINDER_MONTHLY_RANDR_SUMMARY,
                    **reports.construct_randr_summary(sp_target))
            self.respond_success()
        if command in ["soh_report"]:
            assert (sp_target.type.code.lower() ==
                    config.SupplyPointCodes.DISTRICT)
            for c in sp_target.active_contact_set:
                _send_if_connection(
                    c, config.Messages.REMINDER_MONTHLY_SOH_SUMMARY,
                    **reports.construct_soh_summary(sp_target))
            self.respond_success()
        if command in ["delivery_report"]:
            assert (sp_target.type.code.lower() ==
                    config.SupplyPointCodes.DISTRICT)
            for c in sp_target.active_contact_set:
                _send_if_connection(
                    c, config.Messages.REMINDER_MONTHLY_DELIVERY_SUMMARY,
                    **reports.construct_delivery_summary(sp_target))
            self.respond_success()
        if command in ["soh_thank_you"]:
            # test at the facility level for now
            assert (sp_target.type.code.lower() ==
                    config.SupplyPointCodes.FACILITY)
            for c in sp_target.active_contact_set:
                _send_if_connection(c, _(config.Messages.SOH_THANK_YOU))
            self.respond_success()