Exemple #1
0
    def test_partner_export(self):
        url = reverse('statistics.dailycountexport_create')

        tz = pytz.timezone("Africa/Kampala")
        d1 = date(2016, 1, 1)
        d2 = date(2016, 1, 15)

        # Jan 1st
        with patch.object(timezone,
                          'now',
                          return_value=self.anytime_on_day(d1, tz)):
            [msg] = self.new_messages(d1, 1)
            Case.get_or_open(self.unicef, self.user1, msg, 'summary', self.moh)

        # Jan 15th
        with patch.object(timezone,
                          'now',
                          return_value=self.anytime_on_day(d2, tz)):
            [msg] = self.new_messages(d2, 1)
            Case.get_or_open(self.unicef, self.user1, msg, 'summary', self.moh)

        self.new_outgoing(self.user1, d1, 1)  # Jan 1st
        self.new_outgoing(self.user3, d2, 1)  # Jan 15th

        self.login(self.admin)

        response = self.url_post_json('unicef', url, {
            'type': 'P',
            'after': "2016-01-01",
            'before': "2016-01-31"
        })
        self.assertEqual(response.status_code, 200)

        export = DailyCountExport.objects.get()
        workbook = self.openWorkbook(export.filename)
        (replies_sheet, ave_reply_sheet, ave_closed_sheet, cases_opened_sheet,
         cases_closed_sheet) = workbook.sheets()

        self.assertEqual(replies_sheet.nrows, 32)
        self.assertExcelRow(replies_sheet, 0, ["Date", "MOH", "WHO"])
        self.assertExcelRow(replies_sheet, 1, [d1, 1, 0], tz=tz)
        self.assertExcelRow(replies_sheet, 15, [d2, 0, 1], tz=tz)

        self.assertExcelRow(ave_reply_sheet, 0, ["Date", "MOH", "WHO"])
        self.assertExcelRow(ave_reply_sheet, 1, [d1, 0, 0], tz=tz)
        self.assertExcelRow(ave_reply_sheet, 15, [d2, 0, 0], tz=tz)

        self.assertExcelRow(ave_closed_sheet, 0, ["Date", "MOH", "WHO"])
        self.assertExcelRow(ave_closed_sheet, 1, [d1, 0, 0], tz=tz)
        self.assertExcelRow(ave_closed_sheet, 15, [d2, 0, 0], tz=tz)

        self.assertExcelRow(cases_opened_sheet, 0, ["Date", "MOH", "WHO"])
        self.assertExcelRow(cases_opened_sheet, 1, [d1, 1, 0], tz=tz)
        self.assertExcelRow(cases_opened_sheet, 15, [d2, 1, 0], tz=tz)

        self.assertExcelRow(cases_closed_sheet, 0, ["Date", "MOH", "WHO"])
        self.assertExcelRow(cases_closed_sheet, 1, [d1, 0, 0], tz=tz)
        self.assertExcelRow(cases_closed_sheet, 15, [d2, 0, 0], tz=tz)
Exemple #2
0
    def test_case_counts_opened(self):
        d1 = self.anytime_on_day(date(2015, 1, 1),
                                 pytz.timezone("Africa/Kampala"))
        msg2 = self.create_message(self.unicef,
                                   234,
                                   self.ann,
                                   "Hello again", [self.aids],
                                   created_on=d1)

        with patch.object(timezone, 'now', return_value=d1):
            case = Case.get_or_open(self.unicef, self.user1, msg2, "Summary",
                                    self.moh)
Exemple #3
0
    def test_user_export(self):
        url = reverse("statistics.dailycountexport_create")

        tz = pytz.timezone("Africa/Kampala")
        d1 = date(2016, 1, 1)
        d2 = date(2016, 1, 15)

        # Jan 1st
        with patch.object(timezone,
                          "now",
                          return_value=self.anytime_on_day(d1, tz)):
            [msg] = self.new_messages(d1, 1)
            Case.get_or_open(self.unicef, self.user1, msg, "summary", self.moh)

        # Jan 15th
        with patch.object(timezone,
                          "now",
                          return_value=self.anytime_on_day(d2, tz)):
            [msg] = self.new_messages(d2, 1)
            Case.get_or_open(self.unicef, self.user1, msg, "summary", self.moh)

        self.new_outgoing(self.user1, d1, 1)  # Jan 1st
        self.new_outgoing(self.user3, d2, 1)  # Jan 15th

        self.login(self.admin)

        response = self.url_post_json("unicef", url, {
            "type": "U",
            "after": "2016-01-01",
            "before": "2016-01-31"
        })
        self.assertEqual(response.status_code, 200)

        export = DailyCountExport.objects.get()
        workbook = self.openWorkbook(export.filename)
        (replies_sheet, cases_opened_sheet,
         cases_closed_sheet) = workbook.sheets()

        self.assertEqual(replies_sheet.nrows, 32)
        self.assertExcelRow(replies_sheet, 0,
                            ["Date", "Carol", "Evan", "Kidus", "Rick"])
        self.assertExcelRow(replies_sheet, 1, [d1, 0, 1, 0, 0], tz=tz)
        self.assertExcelRow(replies_sheet, 15, [d2, 1, 0, 0, 0], tz=tz)

        self.assertExcelRow(cases_opened_sheet, 0,
                            ["Date", "Carol", "Evan", "Kidus", "Rick"])
        self.assertExcelRow(cases_opened_sheet, 1, [d1, 0, 1, 0, 0], tz=tz)
        self.assertExcelRow(cases_opened_sheet, 15, [d2, 0, 1, 0, 0], tz=tz)

        self.assertExcelRow(cases_closed_sheet, 0,
                            ["Date", "Carol", "Evan", "Kidus", "Rick"])
        self.assertExcelRow(cases_closed_sheet, 1, [d1, 0, 0, 0, 0], tz=tz)
        self.assertExcelRow(cases_closed_sheet, 15, [d2, 0, 0, 0, 0], tz=tz)
Exemple #4
0
    def test_first_reply_counts(self):
        msg1 = self.create_message(self.unicef, 123, self.ann, "Hello 1", [self.aids])
        msg2 = self.create_message(self.unicef, 234, self.ned, "Hello 2", [self.aids, self.pregnancy])
        msg3 = self.create_message(self.unicef, 345, self.ann, "Hello 3", [self.pregnancy])
        msg4 = self.create_message(self.nyaruka, 456, self.ned, "Hello 4", [self.code])
        msg5 = self.create_message(self.unicef, 789, self.ann, "Hello 5", [self.code])

        case1 = self.create_case(self.unicef, self.ann, self.moh, msg1, [self.aids])
        case2 = self.create_case(self.unicef, self.ned, self.moh, msg2, [self.aids, self.pregnancy])
        case3 = self.create_case(self.unicef, self.ann, self.who, msg3, [self.pregnancy])
        case4 = self.create_case(self.unicef, self.ned, self.who, msg4, [self.code])

        # create a case by "WHO" user and assign it to "WHO" partner
        case5 = Case.get_or_open(self.unicef, self.user3, msg5, "Hello", self.who)

        # create a case by "MOH" user and assign it to "WHO" partner
        msg6 = self.create_message(self.unicef, 678, self.ann, "Hello 6", [self.pregnancy])
        case6 = Case.get_or_open(self.unicef, self.user1, msg6, "Hello", self.who)

        self.create_outgoing(self.unicef, self.user1, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case1)
        self.create_outgoing(self.unicef, self.user1, 201, Outgoing.CASE_REPLY, "Good question", self.ned, case=case2)
        self.create_outgoing(self.unicef, self.user3, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case3)
        self.create_outgoing(self.unicef, self.user3, 201, Outgoing.CASE_REPLY, "Good question", self.ned, case=case4)
        self.create_outgoing(self.unicef, self.user3, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case6)

        self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], "A").total(), 5)
        self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], "A").seconds(), 5)

        # First reply only counted for re-assignments, immediate replies NOT counted
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], "A").total(), 0)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], "A").seconds(), 0)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], "A").average(), 0)

        # First replies should be counted for cases not created by self
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.who], "A").total(), 1)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.who], "A").seconds(), 1)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.who], "A").average(), 1)

        # check a reassigned case response
        case5.reassign(self.user3, self.moh)
        self.create_outgoing(self.unicef, self.user1, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case5)
        self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], "A").total(), 6)
        self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], "A").seconds(), 6)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], "A").total(), 1)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], "A").seconds(), 1)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], "A").average(), 1)

        # check empty partner metrics
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.klab], "A").average(), 0)

        self.assertEqual(DailySecondTotalCount.objects.count(), 8)
        squash_counts()
        self.assertEqual(DailySecondTotalCount.objects.count(), 3)
Exemple #5
0
    def test_case_counts_closed(self):
        d1 = self.anytime_on_day(date(2015, 1, 1), pytz.timezone("Africa/Kampala"))
        msg2 = self.create_message(self.unicef, 234, self.ann, "Hello again", [self.aids], created_on=d1)

        with patch.object(timezone, "now", return_value=d1):
            case = Case.get_or_open(self.unicef, self.user1, msg2, "Summary", self.moh)
            case.close(self.user1, note="closing case")

            self.assertEqual(
                DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_OPENED).day_totals(),
                [(date(2015, 1, 1), 1)],
            )
            self.assertEqual(
                DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_CLOSED).day_totals(),
                [(date(2015, 1, 1), 1)],
            )

            self.assertEqual(
                DailyCount.get_by_org([self.unicef], DailyCount.TYPE_CASE_OPENED).day_totals(), [(date(2015, 1, 1), 1)]
            )
            self.assertEqual(
                DailyCount.get_by_org([self.unicef], DailyCount.TYPE_CASE_CLOSED).day_totals(), [(date(2015, 1, 1), 1)]
            )

            self.assertEqual(
                DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_OPENED).day_totals(),
                [(date(2015, 1, 1), 1)],
            )
            self.assertEqual(
                DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_CLOSED).day_totals(),
                [(date(2015, 1, 1), 1)],
            )

            case.reopen(self.user1, "Re-opened")
            case.close(self.user1, "Re-closed")

            self.assertEqual(
                DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_OPENED).day_totals(),
                [(date(2015, 1, 1), 1)],
            )
            self.assertEqual(
                DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_CLOSED).day_totals(),
                [(date(2015, 1, 1), 1)],
            )

            self.assertEqual(
                DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_OPENED).day_totals(),
                [(date(2015, 1, 1), 1)],
            )
            self.assertEqual(
                DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_CLOSED).day_totals(),
                [(date(2015, 1, 1), 1)],
            )
Exemple #6
0
    def test_first_reply_counts(self):
        msg1 = self.create_message(self.unicef, 123, self.ann, "Hello 1", [self.aids])
        msg2 = self.create_message(self.unicef, 234, self.ned, "Hello 2", [self.aids, self.pregnancy])
        msg3 = self.create_message(self.unicef, 345, self.ann, "Hello 3", [self.pregnancy])
        msg4 = self.create_message(self.nyaruka, 456, self.ned, "Hello 4", [self.code])
        msg5 = self.create_message(self.unicef, 789, self.ann, "Hello 5", [self.code])
        msg6 = self.create_message(self.unicef, 678, self.ann, "Hello 6", [self.pregnancy])

        case1 = self.create_case(self.unicef, self.ann, self.moh, msg1, [self.aids])
        case2 = self.create_case(self.unicef, self.ned, self.moh, msg2, [self.aids, self.pregnancy])
        case3 = self.create_case(self.unicef, self.ann, self.who, msg3, [self.pregnancy])
        case4 = self.create_case(self.unicef, self.ned, self.who, msg4, [self.code])

        # create a case by "WHO" user and assign it to "WHO" partner
        case5 = Case.get_or_open(self.unicef, self.user3, msg5, 'Hello', self.who)
        # create a case by "MOH" user and assign it to "WHO" partner
        case6 = Case.get_or_open(self.unicef, self.user1, msg6, 'Hello', self.who)

        self.create_outgoing(self.unicef, self.user1, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case1)
        self.create_outgoing(self.unicef, self.user1, 201, Outgoing.CASE_REPLY, "Good question", self.ned, case=case2)
        self.create_outgoing(self.unicef, self.user3, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case3)
        self.create_outgoing(self.unicef, self.user3, 201, Outgoing.CASE_REPLY, "Good question", self.ned, case=case4)
        self.create_outgoing(self.unicef, self.user3, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case6)

        self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], 'A').total(), 5)
        self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], 'A').seconds(), 5)

        # First reply only counted for re-assignments, immediate replies NOT counted
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], 'A').total(), 0)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], 'A').seconds(), 0)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], 'A').average(), 0)

        # First replies should be counted for cases not created by self
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.who], 'A').total(), 1)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.who], 'A').seconds(), 1)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.who], 'A').average(), 1)

        # check a reassigned case response
        case5.reassign(self.user3, self.moh)
        self.create_outgoing(self.unicef, self.user1, 201, Outgoing.CASE_REPLY, "Good question", self.ann, case=case5)
        self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], 'A').total(), 6)
        self.assertEqual(DailySecondTotalCount.get_by_org([self.unicef], 'A').seconds(), 6)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], 'A').total(), 1)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], 'A').seconds(), 1)
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.moh], 'A').average(), 1)

        # check empty partner metrics
        self.assertEqual(DailySecondTotalCount.get_by_partner([self.klab], 'A').average(), 0)

        self.assertEqual(DailySecondTotalCount.objects.count(), 8)
        squash_counts()
        self.assertEqual(DailySecondTotalCount.objects.count(), 3)
Exemple #7
0
    def test_partner_export(self):
        url = reverse('statistics.dailycountexport_create')

        tz = pytz.timezone("Africa/Kampala")
        d1 = date(2016, 1, 1)
        d2 = date(2016, 1, 15)

        # Jan 1st
        with patch.object(timezone, 'now', return_value=self.anytime_on_day(d1, tz)):
            [msg] = self.new_messages(d1, 1)
            Case.get_or_open(self.unicef, self.user1, msg, 'summary', self.moh)

        # Jan 15th
        with patch.object(timezone, 'now', return_value=self.anytime_on_day(d2, tz)):
            [msg] = self.new_messages(d2, 1)
            Case.get_or_open(self.unicef, self.user1, msg, 'summary', self.moh)

        self.new_outgoing(self.user1, d1, 1)  # Jan 1st
        self.new_outgoing(self.user3, d2, 1)  # Jan 15th

        self.login(self.admin)

        response = self.url_post_json('unicef', url, {'type': 'P', 'after': "2016-01-01", 'before': "2016-01-31"})
        self.assertEqual(response.status_code, 200)

        export = DailyCountExport.objects.get()
        workbook = self.openWorkbook(export.filename)
        (replies_sheet, ave_reply_sheet, ave_closed_sheet, cases_opened_sheet, cases_closed_sheet) = workbook.sheets()

        self.assertEqual(replies_sheet.nrows, 32)
        self.assertExcelRow(replies_sheet, 0, ["Date", "MOH", "WHO"])
        self.assertExcelRow(replies_sheet, 1, [d1, 1, 0], tz=tz)
        self.assertExcelRow(replies_sheet, 15, [d2, 0, 1], tz=tz)

        self.assertExcelRow(ave_reply_sheet, 0, ["Date", "MOH", "WHO"])
        self.assertExcelRow(ave_reply_sheet, 1, [d1, 0, 0], tz=tz)
        self.assertExcelRow(ave_reply_sheet, 15, [d2, 0, 0], tz=tz)

        self.assertExcelRow(ave_closed_sheet, 0, ["Date", "MOH", "WHO"])
        self.assertExcelRow(ave_closed_sheet, 1, [d1, 0, 0], tz=tz)
        self.assertExcelRow(ave_closed_sheet, 15, [d2, 0, 0], tz=tz)

        self.assertExcelRow(cases_opened_sheet, 0, ["Date", "MOH", "WHO"])
        self.assertExcelRow(cases_opened_sheet, 1, [d1, 1, 0], tz=tz)
        self.assertExcelRow(cases_opened_sheet, 15, [d2, 1, 0], tz=tz)

        self.assertExcelRow(cases_closed_sheet, 0, ["Date", "MOH", "WHO"])
        self.assertExcelRow(cases_closed_sheet, 1, [d1, 0, 0], tz=tz)
        self.assertExcelRow(cases_closed_sheet, 15, [d2, 0, 0], tz=tz)
    def setUp(self):
        super(SubscriptionPodTest, self).setUp()
        self.contact = self.create_contact(self.unicef, 'test_id', "Tester")
        self.msg1 = self.create_message(
            self.unicef, 123, self.contact, "Test message")
        self.case = Case.get_or_open(
            self.unicef, self.user1, self.msg1, "Summary", self.moh)
        self.base_url = 'http://example.com/'

        self.pod = SubscriptionPod(
            apps.get_app_config('family_connect_subscription_pod'),
            SubscriptionPodConfig({
                'index': 23,
                'title': "My subscription Pod",
                'url': "http://example.com/",
                'token': "test_token",
            }))

        self.subscription_data = {
            "url": "http://example.com/api/v1/subscriptions/sub_id/",
            "id": "sub_id",
            "version": 1,
            "identity": "C-002",
            "messageset": 1,
            "next_sequence_number": 1,
            "lang": "eng",
            "active": True,
            "completed": False,
            "schedule": 1,
            "process_status": 0,
            "metadata": None,
            "created_at": "2016-07-22T15:53:42.282902Z",
            "updated_at": "2016-09-06T17:17:54.746390Z"
        }
Exemple #9
0
    def test_case_counts_opened(self):
        d1 = self.anytime_on_day(date(2015, 1, 1), pytz.timezone("Africa/Kampala"))
        msg2 = self.create_message(
            self.unicef, 234, self.ann, "Hello again", [self.aids],
            created_on=d1)

        with patch.object(timezone, 'now', return_value=d1):
            case = Case.get_or_open(
                self.unicef, self.user1, msg2, "Summary", self.moh)

        self.assertEqual(
            DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_OPENED).day_totals(),
            [(date(2015, 1, 1), 1)])

        self.assertEqual(
            DailyCount.get_by_org([self.unicef], DailyCount.TYPE_CASE_OPENED).day_totals(),
            [(date(2015, 1, 1), 1)])

        self.assertEqual(
            DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_CLOSED).day_totals(),
            [])
        self.assertEqual(
            DailyCount.get_by_org([self.unicef], DailyCount.TYPE_CASE_CLOSED).day_totals(),
            [])

        self.assertEqual(
            DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_OPENED).day_totals(),
            [(date(2015, 1, 1), 1)])
        self.assertEqual(
            DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_CLOSED).day_totals(),
            [])
Exemple #10
0
        def get_context_data(self, **kwargs):
            context = super(ContactCRUDL.Cases, self).get_context_data(**kwargs)

            cases = Case.get_all(self.request.org, self.request.user).filter(contact=self.object).order_by('-opened_on')
            cases = cases.prefetch_related('labels').select_related('contact', 'assignee')

            context['object_list'] = cases
            return context
Exemple #11
0
        def get_context_data(self, **kwargs):
            context = super(ContactCRUDL.Cases,
                            self).get_context_data(**kwargs)

            cases = (Case.get_all(self.request.org, self.request.user).filter(
                contact=self.object).order_by("-opened_on"))
            cases = cases.prefetch_related("labels").select_related(
                "contact", "assignee")

            context["object_list"] = cases
            return context
Exemple #12
0
        def get_context_data(self, **kwargs):
            context = super(ContactCRUDL.Cases,
                            self).get_context_data(**kwargs)

            cases = Case.get_all(self.request.org, self.request.user).filter(
                contact=self.object).order_by('-opened_on')
            cases = cases.prefetch_related('labels').select_related(
                'contact', 'assignee')

            context['object_list'] = cases
            return context
Exemple #13
0
def handle_messages(org, since, until):
    from casepro.backend import get_backend
    from casepro.cases.models import Case, Label
    from .models import Message
    backend = get_backend()

    labelled, unlabelled, case_replies = [], [], []

    # fetch all unhandled messages who now have full contacts
    unhandled = list(Message.get_unhandled(org).filter(contact__is_stub=False).select_related('contact'))
    if unhandled:
        labels_by_keyword = Label.get_keyword_map(org)
        label_matches = defaultdict(list)  # messages that match each label

        for msg in unhandled:
            open_case = Case.get_open_for_contact_on(org, msg.contact, msg.created_on)

            if open_case:
                msg.case = open_case
                msg.is_archived = True
                msg.save(update_fields=('case', 'is_archived'))

                case_replies.append(msg)
            else:
                # only apply labels if there isn't a currently open case for this contact
                matched_labels = msg.auto_label(labels_by_keyword)
                if matched_labels:
                    labelled.append(msg)
                    for label in matched_labels:
                        label_matches[label].append(msg)
                else:
                    unlabelled.append(msg)

        # add labels to matching messages
        for label, matched_msgs in six.iteritems(label_matches):
            if matched_msgs:
                # TODO check for pointless re-labelling

                for msg in matched_msgs:
                    msg.labels.add(label)

                backend.label_messages(org, matched_msgs, label)

        # archive messages which are case replies on the backend
        if case_replies:
            backend.archive_messages(org, case_replies)

        # mark all of these messages as handled
        Message.objects.filter(pk__in=[m.pk for m in unhandled]).update(is_handled=True)

    return {'messages': len(unhandled), 'labelled': len(labelled), 'case_replies': len(case_replies)}
Exemple #14
0
def handle_messages(org):
    from casepro.cases.models import Case
    from casepro.rules.models import Rule

    from .models import Message

    backend = org.get_backend()

    case_replies = []
    num_rules_matched = 0

    # fetch all unhandled messages who now have full contacts
    unhandled = Message.get_unhandled(org).filter(contact__is_stub=False)
    unhandled = list(
        unhandled.select_related("contact").prefetch_related(
            "contact__groups"))

    if unhandled:
        rules = Rule.get_all(org)
        rule_processor = Rule.BatchProcessor(org, rules)

        for msg in unhandled:
            open_case = Case.get_open_for_contact_on(org, msg.contact,
                                                     msg.created_on)

            # only apply rules if there isn't a currently open case for this contact
            if open_case:
                open_case.add_reply(msg)

                case_replies.append(msg)
            else:
                rules_matched, actions_deferred = rule_processor.include_messages(
                    msg)
                num_rules_matched += rules_matched

        # archive messages which are case replies on the backend
        if case_replies:
            backend.archive_messages(org, case_replies)

        rule_processor.apply_actions()

        # mark all of these messages as handled
        Message.objects.filter(pk__in=[m.pk for m in unhandled]).update(
            is_handled=True, modified_on=timezone.now())

    return {
        "handled": len(unhandled),
        "rules_matched": num_rules_matched,
        "case_replies": len(case_replies)
    }
    def setUp(self):
        super(RegistrationPodTest, self).setUp()
        self.contact = self.create_contact(self.unicef, 'test_id', "Mother")
        self.msg1 = self.create_message(
            self.unicef, 123, self.contact, "Hello")
        self.case = Case.get_or_open(
            self.unicef, self.user1, self.msg1, "Summary", self.moh)
        self.url = 'http://example.com/registration/?mother_id=' + \
            self.contact.uuid

        self.pod = RegistrationPod(
            apps.get_app_config('family_connect_registration_pod'),
            RegistrationPodConfig({
                'index': 23,
                'title': "My registration Pod",
                'url': "http://example.com/registration/",
                'token': "test_token",
                'contact_id_fieldname': "mother_id",
                'field_mapping': [
                    {"field": "mama_name", "field_name": "Mother Name"},
                    {"field": "mama_surname",
                        "field_name": "Mother Surname"},
                    {"field": "last_period_date",
                        "field_name": "Date of last period"},
                    {"field": "language",
                        "field_name": "Language Preference"},
                    {"field": "mama_id_type", "field_name": "ID Type"},
                    {"field": "mama_id_no", "field_name": "ID Number"},
                    {"field": "msg_receiver",
                        "field_name": "Message Receiver"},
                    {"field": "receiver_id", "field_name": "Receiver ID"},
                    {"field": "hoh_name",
                        "field_name": "Head of Household Name"},
                    {"field": "hoh_surname",
                        "field_name": "Head of Household Surname"},
                    {"field": "hoh_id",
                        "field_name": "Head of Household ID"},
                    {"field": "operator_id", "field_name": "Operator ID"},
                    {"field": "msg_type",
                        "field_name": "Receives Messages As"},
                ]
            }))
Exemple #16
0
def handle_messages(org):
    from casepro.backend import get_backend
    from casepro.cases.models import Case
    from casepro.rules.models import Rule
    from .models import Message
    backend = get_backend()

    case_replies = []
    num_rules_matched = 0

    # fetch all unhandled messages who now have full contacts
    unhandled = Message.get_unhandled(org).filter(contact__is_stub=False)
    unhandled = list(unhandled.select_related('contact').prefetch_related('contact__groups'))

    if unhandled:
        rules = Rule.get_all(org)
        rule_processor = Rule.BatchProcessor(org, rules)

        for msg in unhandled:
            open_case = Case.get_open_for_contact_on(org, msg.contact, msg.created_on)

            # only apply rules if there isn't a currently open case for this contact
            if open_case:
                open_case.add_reply(msg)

                case_replies.append(msg)
            else:
                rules_matched, actions_deferred = rule_processor.include_messages(msg)
                num_rules_matched += rules_matched

        # archive messages which are case replies on the backend
        if case_replies:
            backend.archive_messages(org, case_replies)

        rule_processor.apply_actions()

        # mark all of these messages as handled
        Message.objects.filter(pk__in=[m.pk for m in unhandled]).update(is_handled=True)

    return {'handled': len(unhandled), 'rules_matched': num_rules_matched, 'case_replies': len(case_replies)}
Exemple #17
0
    def test_cases_charts(self):
        opened_url = reverse("statistics.cases_opened_chart")
        closed_url = reverse("statistics.cases_closed_chart")

        self.assertLoginRedirect(self.url_get("unicef", opened_url), opened_url)
        self.assertLoginRedirect(self.url_get("unicef", closed_url), closed_url)

        tz = pytz.timezone("Africa/Kampala")
        d1 = date(2020, 1, 1)
        d2 = date(2020, 3, 15)

        # Jan 1st
        with patch.object(timezone, "now", return_value=self.anytime_on_day(d1, tz)):
            [msg] = self.new_messages(d1, 1)
            c = Case.get_or_open(self.unicef, self.user1, msg, "summary", self.moh)
            c.close(self.user1)

        # Mar 15th
        with patch.object(timezone, "now", return_value=self.anytime_on_day(d2, tz)):
            [msg] = self.new_messages(d2, 1)
            Case.get_or_open(self.unicef, self.user1, msg, "summary", self.moh)

        self.login(self.user3)

        # simulate making requests in April
        with patch.object(timezone, "now", return_value=datetime(2016, 4, 20, 9, 0, tzinfo=pytz.UTC)):
            response = self.url_get("unicef", opened_url)

            self.assertEqual(
                response.json,
                {
                    "categories": [
                        "May",
                        "June",
                        "July",
                        "August",
                        "September",
                        "October",
                        "November",
                        "December",
                        "January",
                        "February",
                        "March",
                        "April",
                    ],
                    "series": [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
                },
            )

            response = self.url_get("unicef", closed_url)

            self.assertEqual(
                response.json,
                {
                    "categories": [
                        "May",
                        "June",
                        "July",
                        "August",
                        "September",
                        "October",
                        "November",
                        "December",
                        "January",
                        "February",
                        "March",
                        "April",
                    ],
                    "series": [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
                },
            )