def test_assign_exclusion(self):
     self._make_tree()
     assign_case(self.primary, self.primary_user._id, include_subcases=True, include_parent_cases=True,
                 exclude=(self.grandfather._id, self.primary._id, self.grandson._id))
     self._check_state(new_owner_id=self.primary_user._id, expected_changed=[
         self.grandmother, self.parent, self.son, self.daughter,self.granddaughter, self.grandson2
     ])
Esempio n. 2
0
 def test_assign_subcases(self):
     self._make_tree()
     assign_case(self.primary.id, self.primary_user._id, include_subcases=True, include_parent_cases=False)
     self._check_state(
         new_owner_id=self.primary_user._id,
         expected_changed=[self.primary, self.son, self.daughter, self.grandson, self.granddaughter, self.grandson2],
     )
Esempio n. 3
0
def bihar_reassignment(sender, xform, cases, **kwargs):
    if (
        hasattr(xform, "domain")
        and xform.domain in BIHAR_DOMAINS
        and xform.metadata
        and xform.metadata.userID != SYSTEM_USERID
    ):
        owner_ids = set(c.owner_id for c in cases)
        if len(owner_ids) != 1:
            logging.warning("form {form} had mismatched case owner ids".format(form=xform._id))
        else:
            [owner_id] = owner_ids
            if owner_id not in DEMO_OWNER_IDS:
                form_cases = set(c._id for c in cases)

                def bihar_exclude(case):
                    return case._id in form_cases or case.type == "anm_review"

                for case in cases:
                    if case.type in ("cc_bihar_pregnancy", "cc_bihar_newborn"):

                        assign_case(
                            case,
                            owner_id,
                            BiharMockUser(),
                            include_subcases=True,
                            include_parent_cases=True,
                            exclude_function=bihar_exclude,
                            update={"reassignment_form_id": xform._id},
                        )
Esempio n. 4
0
 def test_assign_to_unknown_user(self):
     case = self._new_case()
     try:
         assign_case(case.case_id, 'noonehere')
         self.fail('reassigning to nonexistent user should fail')
     except CaseAssignmentError:
         pass
Esempio n. 5
0
 def test_assign_parent_cases(self):
     self._make_tree()
     assign_case(self.primary.id, self.primary_user._id, include_subcases=False, include_parent_cases=True)
     self._check_state(
         new_owner_id=self.primary_user._id,
         expected_changed=[self.grandfather, self.grandmother, self.parent, self.primary],
     )
Esempio n. 6
0
 def test_assign_add_property(self):
     self._make_tree()
     update = {'reassigned': 'yes'}
     assign_case(self.primary.case_id, self.primary_user._id, include_subcases=True, include_parent_cases=True,
                 update=update)
     self._check_state(new_owner_id=self.primary_user._id,
                       expected_changed=self.all, update=update)
 def test_assign_exclusion(self):
     self._make_tree()
     exclude_fn = lambda case: case._id in (self.grandfather._id, self.primary._id, self.grandson._id)
     assign_case(self.primary, self.primary_user._id, include_subcases=True, include_parent_cases=True,
                 exclude_function=exclude_fn)
     self._check_state(new_owner_id=self.primary_user._id, expected_changed=[
         self.grandmother, self.parent, self.son, self.daughter,self.granddaughter, self.grandson2
     ])
Esempio n. 8
0
 def test_assign_to_user_in_different_domain(self):
     other_user = CommCareUser.create('bad-domain', format_username('bad-domain-user', self.domain), "****")
     case = self._new_case()
     try:
         assign_case(case.case_id, other_user._id)
         self.fail('reassigning to user in wrong domain should fail')
     except CaseAssignmentError:
         pass
Esempio n. 9
0
 def test_assign_no_related(self):
     self._make_tree()
     assign_case(self.primary,
                 self.primary_user._id,
                 include_subcases=False,
                 include_parent_cases=False)
     self._check_state(new_owner_id=self.primary_user._id,
                       expected_changed=[self.primary])
Esempio n. 10
0
 def test_assign_subcases(self):
     self._make_tree()
     assign_case(self.primary.case_id,
                 self.primary_user._id,
                 include_subcases=True,
                 include_parent_cases=False)
     self._check_state(new_owner_id=self.primary_user._id,
                       expected_changed=[
                           self.primary, self.son, self.daughter,
                           self.grandson, self.granddaughter, self.grandson2
                       ])
Esempio n. 11
0
 def test_assign_parent_cases(self):
     self._make_tree()
     assign_case(self.primary.case_id,
                 self.primary_user._id,
                 include_subcases=False,
                 include_parent_cases=True)
     self._check_state(new_owner_id=self.primary_user._id,
                       expected_changed=[
                           self.grandfather, self.grandmother, self.parent,
                           self.primary
                       ])
Esempio n. 12
0
 def test_assign_add_property(self):
     self._make_tree()
     update = {'reassigned': 'yes'}
     assign_case(self.primary.case_id,
                 self.primary_user._id,
                 include_subcases=True,
                 include_parent_cases=True,
                 update=update)
     self._check_state(new_owner_id=self.primary_user._id,
                       expected_changed=self.all,
                       update=update)
Esempio n. 13
0
 def test_assign_to_group(self):
     group = Group(users=[],
                   name='case-assignment-group',
                   domain=self.domain)
     group.save()
     self._make_tree()
     assign_case(self.primary.case_id,
                 group._id,
                 include_subcases=True,
                 include_parent_cases=True)
     self._check_state(new_owner_id=group._id, expected_changed=self.all)
Esempio n. 14
0
def bihar_reassignment(sender, xform, cases, **kwargs):
    if hasattr(xform, 'domain') and xform.domain in BIHAR_DOMAINS and xform.metadata and xform.metadata.userID != SYSTEM_USERID:
        owner_ids = set(c.owner_id for c in cases)
        if len(owner_ids) != 1:
            logging.error('form {form} had mismatched case owner ids'.format(form=xform._id))
        else:
            [owner_id] = owner_ids
            if owner_id not in DEMO_OWNER_IDS:
                form_cases = set(c._id for c in cases)
                for case in cases:
                    if case.type in ('cc_bihar_pregnancy', 'cc_bihar_newborn'):
                        assign_case(case, owner_id, BiharMockUser(),
                                    include_subcases=True, include_parent_cases=True,
                                    exclude=form_cases)
 def test_assign_noop(self):
     self._make_tree()
     num_forms = XFormInstance.get_db().view('hqadmin/forms_over_time').all()[0]['value']
     res = assign_case(self.primary, self.original_owner._id, include_subcases=True, include_parent_cases=True)
     self.assertEqual(0, len(res))
     new_num_forms = XFormInstance.get_db().view('hqadmin/forms_over_time').all()[0]['value']
     self.assertEqual(new_num_forms, num_forms)
 def test_assign_noop(self):
     self._make_tree()
     num_forms = XFormInstance.get_db().view('hqadmin/forms_over_time').all()[0]['value']
     res = assign_case(self.primary, self.original_owner._id, include_subcases=True, include_parent_cases=True)
     self.assertEqual(0, len(res))
     new_num_forms = XFormInstance.get_db().view('hqadmin/forms_over_time').all()[0]['value']
     self.assertEqual(new_num_forms, num_forms)
Esempio n. 17
0
 def test_assign_noop(self):
     self._make_tree()
     num_forms = get_number_of_forms_in_all_domains()
     res = assign_case(self.primary.case_id, self.original_owner._id, include_subcases=True, include_parent_cases=True)
     self.assertEqual(0, len(res))
     new_num_forms = get_number_of_forms_in_all_domains()
     self.assertEqual(new_num_forms, num_forms)
Esempio n. 18
0
 def test_assign_noop(self):
     self._make_tree()
     num_forms = len(FormAccessors(self.domain).get_forms_by_type('XFormInstance', limit=1000))
     self.assertTrue(num_forms < 1000)
     res = assign_case(self.primary.case_id, self.original_owner._id, include_subcases=True, include_parent_cases=True)
     self.assertEqual(0, len(res))
     new_num_forms = len(FormAccessors(self.domain).get_forms_by_type('XFormInstance', limit=1000))
     self.assertEqual(new_num_forms, num_forms)
 def test_assign_bad_index_ref(self):
     # the case has to exist to create the index, but if we delete it later the assignment
     # shouldn't fail
     case = self._new_case()
     case_with_bad_ref = self._new_case(index={'parent': ('person', case._id)})
     case.delete()
     # this call previously failed
     res = assign_case(case_with_bad_ref, self.primary_user._id, include_subcases=True, include_parent_cases=True)
     self.assertEqual(1, len(res))
     self.assertEqual(case_with_bad_ref._id, res[0])
Esempio n. 20
0
def bihar_reassignment(sender, xform, cases, **kwargs):
    if hasattr(xform, 'domain') and xform.domain in BIHAR_DOMAINS and xform.metadata and xform.metadata.userID != SYSTEM_USERID:
        owner_ids = set(c.owner_id for c in cases)
        if len(owner_ids) != 1:
            logging.warning('form {form} had mismatched case owner ids'.format(form=xform._id))
        else:
            [owner_id] = owner_ids
            if owner_id not in DEMO_OWNER_IDS:
                form_cases = set(c._id for c in cases)

                def bihar_exclude(case):
                    return case._id in form_cases or case.type in REASSIGN_BLACKLIST

                for case in cases:
                    if case.type in ('cc_bihar_pregnancy', 'cc_bihar_newborn'):

                        assign_case(case, owner_id, BiharMockUser(),
                                    include_subcases=True, include_parent_cases=True,
                                    exclude_function=bihar_exclude, update={'reassignment_form_id': xform._id})
Esempio n. 21
0
 def test_assign_noop(self):
     self._make_tree()
     num_forms = get_number_of_forms_in_all_domains()
     res = assign_case(self.primary,
                       self.original_owner._id,
                       include_subcases=True,
                       include_parent_cases=True)
     self.assertEqual(0, len(res))
     new_num_forms = get_number_of_forms_in_all_domains()
     self.assertEqual(new_num_forms, num_forms)
Esempio n. 22
0
 def test_assign_bad_index_ref(self):
     # the case has to exist to create the index, but if we delete it later the assignment
     # shouldn't fail
     case = self._new_case()
     case_with_bad_ref = self._new_case(index={"parent": ("person", case._id)})
     FormProcessorInterface.soft_delete_case(case._id)
     # this call previously failed
     res = assign_case(case_with_bad_ref.id, self.primary_user._id, include_subcases=True, include_parent_cases=True)
     self.assertEqual(2, len(res))
     self.assertIn(case_with_bad_ref._id, res)
     self.assertIn(case._id, res)
Esempio n. 23
0
 def test_assign_exclusion(self):
     self._make_tree()
     exclude_fn = lambda case: case._id in (self.grandfather._id, self.primary._id, self.grandson._id)
     assign_case(
         self.primary.id,
         self.primary_user._id,
         include_subcases=True,
         include_parent_cases=True,
         exclude_function=exclude_fn,
     )
     self._check_state(
         new_owner_id=self.primary_user._id,
         expected_changed=[
             self.grandmother,
             self.parent,
             self.son,
             self.daughter,
             self.granddaughter,
             self.grandson2,
         ],
     )
Esempio n. 24
0
 def test_assign_noop(self):
     self._make_tree()
     num_forms = len(
         FormAccessors(self.domain).get_forms_by_type('XFormInstance',
                                                      limit=1000))
     self.assertTrue(num_forms < 1000)
     res = assign_case(self.primary.case_id,
                       self.original_owner._id,
                       include_subcases=True,
                       include_parent_cases=True)
     self.assertEqual(0, len(res))
     new_num_forms = len(
         FormAccessors(self.domain).get_forms_by_type('XFormInstance',
                                                      limit=1000))
     self.assertEqual(new_num_forms, num_forms)
Esempio n. 25
0
def bihar_reassignment(sender, xform, cases, **kwargs):
    if hasattr(xform, 'domain') and xform.domain in BIHAR_DOMAINS and xform.metadata and xform.metadata.userID != SYSTEM_USERID:
        owner_ids = set(c.owner_id for c in cases)
        if len(owner_ids) != 1:
            logging.warning('form {form} had mismatched case owner ids'.format(form=xform._id))
        else:
            [owner_id] = owner_ids
            if owner_id not in DEMO_OWNER_IDS:
                # don't attempt to reassign the cases included in this form
                cases_not_to_touch = set(c._id for c in cases)
                def bihar_exclude(case):
                    return case._id in cases_not_to_touch or case.type in REASSIGN_BLACKLIST

                for case in cases:
                    if case.type in ('cc_bihar_pregnancy', 'cc_bihar_newborn'):
                        reassigned = assign_case(
                            case, owner_id, BiharMockUser(),
                            include_subcases=True, include_parent_cases=True,
                            exclude_function=bihar_exclude, update={'reassignment_form_id': xform._id}
                        )
                        # update the list of cases not to touch so we don't reassign the same
                        # cases multiple times in the same form
                        cases_not_to_touch = cases_not_to_touch | set(reassigned or [])
 def test_assign_no_related(self):
     self._make_tree()
     assign_case(self.primary, self.primary_user._id, include_subcases=False, include_parent_cases=False)
     self._check_state(new_owner_id=self.primary_user._id, expected_changed=[self.primary])
Esempio n. 27
0
 def test_assign_all_related(self):
     self._make_tree()
     assign_case(self.primary.case_id, self.primary_user._id, include_subcases=True, include_parent_cases=True)
     self._check_state(new_owner_id=self.primary_user._id,
                       expected_changed=self.all)
Esempio n. 28
0
        self._check_state(new_owner_id=self.primary_user._id,
                          expected_changed=[
                              self.grandmother, self.parent, self.son,
                              self.daughter, self.granddaughter, self.grandson2
                          ])

    def test_assign_bad_index_ref(self):
        # the case has to exist to create the index, but if we delete it later the assignment
        # shouldn't fail
        case = self._new_case()
        case_with_bad_ref = self._new_case(
            index={'parent': ('person', case._id)})
        case.soft_delete()
        # this call previously failed
        res = assign_case(case_with_bad_ref.case_id,
                          self.primary_user._id,
                          include_subcases=True,
                          include_parent_cases=True)
        self.assertEqual(2, len(res))
        self.assertIn(case_with_bad_ref.case_id, res)
        self.assertIn(case._id, res)

    def _make_tree(self):
        # create a tree that looks like this:
        #      grandmother    grandfather
        #                parent
        #                primary
        #         son               daughter
        # grandson  granddaughter   grandson2
        self.grandmother = self._new_case()
        self.grandfather = self._new_case()
        self.parent = self._new_case(
Esempio n. 29
0
 def test_assign_to_group(self):
     group = Group(users=[], name='case-assignment-group', domain=self.domain)
     group.save()
     self._make_tree()
     assign_case(self.primary.case_id, group._id, include_subcases=True, include_parent_cases=True)
     self._check_state(new_owner_id=group._id, expected_changed=self.all)